Java源码示例:com.tencent.tinker.loader.app.ApplicationLike

示例1
/**
 * 自定义安装Tinker(使用你自定义的reporter类:SampleLoadReporter、SamplePatchReporter、SamplePatchListener、SampleResultService)
 * you can specify all class you want.
 * sometimes, you can only install tinker in some process you want!
 */
public static void installTinker(ApplicationLike appLike) {
    if (isInstalled) {
        TinkerLog.w(TAG, "install tinker, but has installed, ignore");
        return;
    }
    //or you can just use DefaultLoadReporter
    LoadReporter loadReporter = new SampleLoadReporter(appLike.getApplication());
    //or you can just use DefaultPatchReporter
    PatchReporter patchReporter = new SamplePatchReporter(appLike.getApplication());
    //or you can just use DefaultPatchListener
    PatchListener patchListener = new SamplePatchListener(appLike.getApplication());
    //you can set your own upgrade patch if you need
    AbstractPatch upgradePatchProcessor = new UpgradePatch();

    TinkerInstaller.install(appLike,
            loadReporter, patchReporter, patchListener,
            SampleResultService.class, upgradePatchProcessor);

    isInstalled = true;
}
 
示例2
/**
     * you can specify all class you want.
     * sometimes, you can only install com.dx168.patchsdk.sample in some process you want!
     *
     * @param appLike
     */
    public static void installTinker(ApplicationLike appLike) {
        if (isInstalled) {
            TinkerLog.w(TAG, "install com.dx168.patchsdk.sample, but has installed, ignore");
            return;
        }
        //or you can just use DefaultLoadReporter
        LoadReporter loadReporter = new SampleLoadReporter(appLike.getApplication());
        //or you can just use DefaultPatchReporter
        PatchReporter patchReporter = new SamplePatchReporter(appLike.getApplication());
        //or you can just use DefaultPatchListener
        PatchListener patchListener = new SamplePatchListener(appLike.getApplication());
        //you can set your own upgrade patch if you need
//        AbstractPatch upgradePatchProcessor = new SampleUpgradePatch();
        AbstractPatch upgradePatchProcessor = new UpgradePatch();

        TinkerInstaller.install(appLike, loadReporter, patchReporter, patchListener, SampleResultService.class,
                upgradePatchProcessor);

        isInstalled = true;
    }
 
示例3
/**
 * you can specify all class you want.
 * sometimes, you can only install tinker in some process you want!
 *
 * @param appLike ApplicationLike
 */
public static void installTinker(ApplicationLike appLike) {
    if (isInstalled) {
        TinkerLog.w(TAG, "install tinker, but has installed, ignore");
        return;
    }
    //or you can just use DefaultLoadReporter
    LoadReporter loadReporter = new TinkerServerLoadReporter(appLike.getApplication());
    //or you can just use DefaultPatchReporter
    PatchReporter patchReporter = new DefaultPatchReporter(appLike.getApplication());
    //or you can just use DefaultPatchListener
    PatchListener patchListener = new TinkerServerPatchListener(appLike.getApplication());
    //you can set your own upgrade patch if you need
    AbstractPatch upgradePatchProcessor = new UpgradePatch();
    TinkerInstaller.install(appLike,
        loadReporter, patchReporter, patchListener,
        TinkerServerResultService.class, upgradePatchProcessor
    );

    isInstalled = true;
}
 
示例4
/**
 * 默认安装Tinker(使用默认的reporter类:DefaultLoadReporter、DefaultPatchReporter、DefaultPatchListener、DefaultTinkerResultService)
 * 如果你不需要对监听app打补丁的情况(如:当打补丁失败时上传失败信息),则使用该方法
 */
public static void sampleInstallTinker(ApplicationLike appLike) {
    if (isInstalled) {
        TinkerLog.w(TAG, "install tinker, but has installed, ignore");
        return;
    }
    TinkerInstaller.install(appLike);
    isInstalled = true;

}
 
示例5
/**
 * if tinker is load, and it crash more than MAX_CRASH_COUNT, then we just clean patch.
 */
private boolean tinkerFastCrashProtect() {
    ApplicationLike applicationLike = TinkerManager.getTinkerApplicationLike();

    if (applicationLike == null || applicationLike.getApplication() == null) {
        return false;
    }
    if (!TinkerApplicationHelper.isTinkerLoadSuccess(applicationLike)) {
        return false;
    }

    final long elapsedTime = SystemClock.elapsedRealtime() - applicationLike.getApplicationStartElapsedTime();
    //this process may not install tinker, so we use TinkerApplicationHelper api
    if (elapsedTime < QUICK_CRASH_ELAPSE) {
        String currentVersion = TinkerApplicationHelper.getCurrentVersion(applicationLike);
        if (ShareTinkerInternals.isNullOrNil(currentVersion)) {
            return false;
        }

        SharedPreferences sp = applicationLike.getApplication().getSharedPreferences(ShareConstants.TINKER_SHARE_PREFERENCE_CONFIG, Context.MODE_MULTI_PROCESS);
        int fastCrashCount = sp.getInt(currentVersion, 0) + 1;
        if (fastCrashCount >= MAX_CRASH_COUNT) {
            SampleTinkerReport.onFastCrashProtect();
            TinkerApplicationHelper.cleanPatch(applicationLike);
            TinkerLog.e(TAG, "tinker has fast crash more than %d, we just clean patch!", fastCrashCount);
            return true;
        } else {
            sp.edit().putInt(currentVersion, fastCrashCount).commit();
            TinkerLog.e(TAG, "tinker has fast crash %d times", fastCrashCount);
        }
    }

    return false;
}
 
示例6
/**
 * if com.dx168.patchsdk.sample is load, and it crash more than MAX_CRASH_COUNT, then we just clean patch.
 */
private boolean tinkerFastCrashProtect() {
    ApplicationLike applicationLike = SampleTinkerManager.getTinkerApplicationLike();

    if (applicationLike == null || applicationLike.getApplication() == null) {
        return false;
    }
    if (!TinkerApplicationHelper.isTinkerLoadSuccess(applicationLike)) {
        return false;
    }

    final long elapsedTime = SystemClock.elapsedRealtime() - applicationLike.getApplicationStartElapsedTime();
    //this process may not install com.dx168.patchsdk.sample, so we use TinkerApplicationHelper api
    if (elapsedTime < QUICK_CRASH_ELAPSE) {
        String currentVersion = TinkerApplicationHelper.getCurrentVersion(applicationLike);
        if (ShareTinkerInternals.isNullOrNil(currentVersion)) {
            return false;
        }

        SharedPreferences sp = applicationLike.getApplication().getSharedPreferences(ShareConstants.TINKER_SHARE_PREFERENCE_CONFIG, Context.MODE_MULTI_PROCESS);
        int fastCrashCount = sp.getInt(currentVersion, 0);
        if (fastCrashCount >= MAX_CRASH_COUNT) {
            SampleTinkerReport.onFastCrashProtect();
            TinkerApplicationHelper.cleanPatch(applicationLike);
            TinkerLog.e(TAG, "com.dx168.patchsdk.sample has fast crash more than %d, we just clean patch!", fastCrashCount);
            return true;
        } else {
            sp.edit().putInt(currentVersion, ++fastCrashCount).commit();
            TinkerLog.e(TAG, "com.dx168.patchsdk.sample has fast crash %d times", fastCrashCount);
        }
    }

    return false;
}
 
示例7
/**
 * all use default class, simply Tinker install method
 */
public static void sampleInstallTinker(ApplicationLike appLike) {
    if (isInstalled) {
        TinkerLog.w(TAG, "install com.dx168.patchsdk.sample, but has installed, ignore");
        return;
    }
    TinkerInstaller.install(appLike);
    isInstalled = true;

}
 
示例8
public static void setTinkerApplicationLike(ApplicationLike appLike) {
    applicationLike = appLike;
}
 
示例9
public static ApplicationLike getTinkerApplicationLike() {
    return applicationLike;
}
 
示例10
/**
 * Such as Xposed, if it try to load some class before we load from patch files.
 * With dalvik, it will crash with "Class ref in pre-verified class resolved to unexpected implementation".
 * With art, it may crash at some times. But we can't know the actual crash type.
 * If it use Xposed, we can just clean patch or mention user to uninstall it.
 */
private void tinkerPreVerifiedCrashHandler(Throwable ex) {
    ApplicationLike applicationLike = TinkerManager.getTinkerApplicationLike();
    if (applicationLike == null || applicationLike.getApplication() == null) {
        TinkerLog.w(TAG, "applicationlike is null");
        return;
    }

    if (!TinkerApplicationHelper.isTinkerLoadSuccess(applicationLike)) {
        TinkerLog.w(TAG, "tinker is not loaded");
        return;
    }

    Throwable throwable = ex;
    boolean isXposed = false;
    while (throwable != null) {
        if (!isXposed) {
            isXposed = TinkerUtils.isXposedExists(throwable);
        }

        // xposed?
        if (isXposed) {
            boolean isCausedByXposed = false;
            //for art, we can't know the actually crash type
            //just ignore art
            if (throwable instanceof IllegalAccessError && throwable.getMessage().contains(DALVIK_XPOSED_CRASH)) {
                //for dalvik, we know the actual crash type
                isCausedByXposed = true;
            }

            if (isCausedByXposed) {
                SampleTinkerReport.onXposedCrash();
                TinkerLog.e(TAG, "have xposed: just clean tinker");
                //kill all other process to ensure that all process's code is the same.
                ShareTinkerInternals.killAllOtherProcess(applicationLike.getApplication());

                TinkerApplicationHelper.cleanPatch(applicationLike);
                ShareTinkerInternals.setTinkerDisableWithSharedPreferences(applicationLike.getApplication());
                return;
            }
        }
        throwable = throwable.getCause();
    }
}
 
示例11
/**
 * Such as Xposed, if it try to load some class before we load from patch files.
 * With dalvik, it will crash with "Class ref in pre-verified class resolved to unexpected implementation".
 * With art, it may crash at some times. But we can't know the actual crash type.
 * If it use Xposed, we can just clean patch or mention user to uninstall it.
 */
private void tinkerPreVerifiedCrashHandler(Throwable ex) {
    ApplicationLike applicationLike = SampleTinkerManager.getTinkerApplicationLike();
    if (applicationLike == null || applicationLike.getApplication() == null) {
        TinkerLog.w(TAG, "applicationlike is null");
        return;
    }

    if (!TinkerApplicationHelper.isTinkerLoadSuccess(applicationLike)) {
        TinkerLog.w(TAG, "tinker is not loaded");
        return;
    }

    Throwable throwable = ex;
    boolean isXposed = false;
    while (throwable != null) {
        if (!isXposed) {
            isXposed = SampleUtils.isXposedExists(throwable);
        }

        // xposed?
        if (isXposed) {
            boolean isCausedByXposed = false;
            //for art, we can't know the actually crash type
            //just ignore art
            if (throwable instanceof IllegalAccessError && throwable.getMessage().contains(DALVIK_XPOSED_CRASH)) {
                //for dalvik, we know the actual crash type
                isCausedByXposed = true;
            }

            if (isCausedByXposed) {
                SampleTinkerReport.onXposedCrash();
                TinkerLog.e(TAG, "have xposed: just clean tinker");
                //kill all other process to ensure that all process's code is the same.
                ShareTinkerInternals.killAllOtherProcess(applicationLike.getApplication());

                TinkerApplicationHelper.cleanPatch(applicationLike);
                ShareTinkerInternals.setTinkerDisableWithSharedPreferences(applicationLike.getApplication());
                return;
            }
        }
        throwable = throwable.getCause();
    }
}
 
示例12
public static void setTinkerApplicationLike(ApplicationLike appLike) {
    applicationLike = appLike;
}
 
示例13
public static ApplicationLike getTinkerApplicationLike() {
    return applicationLike;
}
 
示例14
/**
 * 用默认的构造参数初始化TinkerPatch的SDK
 * @param applicationLike
 * @return
 */
public static TinkerPatch init(ApplicationLike applicationLike) {
    return null;
}
 
示例15
/**
 * 获得ApplicationLike的实例
 * @return
 */
public abstract ApplicationLike getApplcationLike();