com.tencent.tinker.loader.shareutil.ShareTinkerInternals Java Examples

The following examples show how to use com.tencent.tinker.loader.shareutil.ShareTinkerInternals. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TinkerServerClient.java    From tinkerpatch-sdk with MIT License 6 votes vote down vote up
/**
 * 检查服务器是否存在补丁更新
 * @param immediately 是否忽略时间间隔
 */
public void checkTinkerUpdate(boolean immediately) {
    if (!tinker.isTinkerEnabled() || !ShareTinkerInternals.isTinkerEnableWithSharedPreferences(context)) {
        TinkerLog.e(TAG, "tinker is disable, just return");
        return;
    }

    SharedPreferences sp = context.getSharedPreferences(
        SHARE_SERVER_PREFERENCE_CONFIG, Context.MODE_PRIVATE
    );
    long last = sp.getLong(TINKER_LAST_CHECK, 0);
    if (last == NEVER_CHECK_UPDATE) {
        TinkerLog.i(TAG, "tinker update is disabled, with never check flag!");
        return;
    }
    long interval = System.currentTimeMillis() - last;
    if (immediately || clientAPI.isDebug() || interval >= checkInterval) {
        sp.edit().putLong(TINKER_LAST_CHECK, System.currentTimeMillis()).commit();
        clientAPI.update(context, patchRequestCallback);
    } else {
        TinkerLog.i(TAG, "tinker sync should wait interval %ss", (checkInterval - interval) / 1000);
    }
}
 
Example #2
Source File: SampleTinkerReport.java    From HotFixDemo with MIT License 5 votes vote down vote up
public static void onXposedCrash() {
    if (reporter == null) {
        return;
    }
    if (ShareTinkerInternals.isVmArt()) {
        reporter.onReport(KEY_CRASH_CAUSE_XPOSED_ART);
    } else {
        reporter.onReport(KEY_CRASH_CAUSE_XPOSED_DALVIK);
    }
}
 
Example #3
Source File: SamplePatchListener.java    From HotFixDemo with MIT License 5 votes vote down vote up
/**
 * 若检查成功,我们会调用TinkerPatchService.runPatchService唤起:patch进程,去尝试完成补丁合成操作。反之,会回调检验失败的接口。
 * 若检查失败,会在LoadReporter的onLoadPatchListenerReceiveFail中回调。
 * <p>
 * because we use the defaultCheckPatchReceived method
 * the error code define by myself should after {@code ShareConstants.ERROR_RECOVER_INSERVICE
 *
 * @param path
 * @param newPatch
 * @return
 */
@Override
public int patchCheck(String path, String patchMd5) {
    File patchFile = new File(path);
    TinkerLog.i(TAG, "receive a patch file: %s, file size:%d", path, SharePatchFileUtil.getFileOrDirectorySize(patchFile));
    int returnCode = super.patchCheck(path, patchMd5);

    if (returnCode == ShareConstants.ERROR_PATCH_OK) {
        returnCode = TinkerUtils.checkForPatchRecover(NEW_PATCH_RESTRICTION_SPACE_SIZE_MIN, maxMemory);
    }

    if (returnCode == ShareConstants.ERROR_PATCH_OK) {
        SharedPreferences sp = context.getSharedPreferences(ShareConstants.TINKER_SHARE_PREFERENCE_CONFIG, Context.MODE_MULTI_PROCESS);
        //optional, only disable this patch file with md5
        int fastCrashCount = sp.getInt(patchMd5, 0);
        if (fastCrashCount >= SampleUncaughtExceptionHandler.MAX_CRASH_COUNT) {
            returnCode = TinkerUtils.ERROR_PATCH_CRASH_LIMIT;
        }
    }
    // Warning, it is just a sample case, you don't need to copy all of these
    // Interception some of the request
    if (returnCode == ShareConstants.ERROR_PATCH_OK) {
        Properties properties = ShareTinkerInternals.fastGetPatchPackageMeta(patchFile);
        if (properties == null) {
            returnCode = TinkerUtils.ERROR_PATCH_CONDITION_NOT_SATISFIED;
        } else {
            String platform = properties.getProperty(TinkerUtils.PLATFORM);
            TinkerLog.i(TAG, "get platform:" + platform);
            // check patch platform require
            if (platform == null || !platform.equals("all")) {
                returnCode = TinkerUtils.ERROR_PATCH_CONDITION_NOT_SATISFIED;
            }
        }
    }

    SampleTinkerReport.onTryApply(returnCode == ShareConstants.ERROR_PATCH_OK);
    return returnCode;
}
 
Example #4
Source File: SampleUncaughtExceptionHandler.java    From HotFixDemo with MIT License 5 votes vote down vote up
/**
 * 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;
}
 
Example #5
Source File: MainActivity.java    From HotFixDemo with MIT License 5 votes vote down vote up
public void show_info(View view) {
    // add more Build Info
    final StringBuilder sb = new StringBuilder();
    Tinker tinker = Tinker.with(getApplicationContext());
    if (tinker.isTinkerLoaded()) {
        sb.append(String.format("[patch is loaded] \n"));
        sb.append(String.format("[buildConfig TINKER_ID] %s \n", BuildInfo.TINKER_ID));
        sb.append(String.format("[buildConfig BASE_TINKER_ID] %s \n", BaseBuildInfo.BASE_TINKER_ID));

        sb.append(String.format("[buildConfig MESSSAGE] %s \n", BuildInfo.MESSAGE));
        sb.append(String.format("[TINKER_ID] %s \n", tinker.getTinkerLoadResultIfPresent().getPackageConfigByName(ShareConstants.TINKER_ID)));
        sb.append(String.format("[packageConfig patchMessage] %s \n", tinker.getTinkerLoadResultIfPresent().getPackageConfigByName("patchMessage")));
        sb.append(String.format("[TINKER_ID Rom Space] %d k \n", tinker.getTinkerRomSpace()));

    } else {
        sb.append(String.format("[patch is not loaded] \n"));
        sb.append(String.format("[buildConfig TINKER_ID] %s \n", BuildInfo.TINKER_ID));
        sb.append(String.format("[buildConfig BASE_TINKER_ID] %s \n", BaseBuildInfo.BASE_TINKER_ID));

        sb.append(String.format("[buildConfig MESSSAGE] %s \n", BuildInfo.MESSAGE));
        sb.append(String.format("[TINKER_ID] %s \n", ShareTinkerInternals.getManifestTinkerID(getApplicationContext())));
    }
    sb.append(String.format("[BaseBuildInfo Message] %s \n", BaseBuildInfo.TEST_MESSAGE));

    final TextView v = new TextView(this);
    v.setText(sb);
    v.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
    v.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
    v.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    v.setTextColor(0xFF000000);
    v.setTypeface(Typeface.MONOSPACE);
    final int padding = 16;
    v.setPadding(padding, padding, padding, padding);

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setCancelable(true);
    builder.setView(v);
    final AlertDialog alert = builder.create();
    alert.show();
}
 
Example #6
Source File: SampleUncaughtExceptionHandler.java    From tinker-manager with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #7
Source File: SampleTinkerReport.java    From tinker-manager with Apache License 2.0 5 votes vote down vote up
public static void onXposedCrash() {
    if (reporter == null) {
        return;
    }
    if (ShareTinkerInternals.isVmArt()) {
        reporter.onReport(KEY_CRASH_CAUSE_XPOSED_ART);
    } else {
        reporter.onReport(KEY_CRASH_CAUSE_XPOSED_DALVIK);
    }
}
 
Example #8
Source File: SamplePatchListener.java    From tinker-manager with Apache License 2.0 5 votes vote down vote up
/**
 * because we use the defaultCheckPatchReceived method
 * the error code define by myself should after {@code ShareConstants.ERROR_RECOVER_INSERVICE
 *
 * @param path
 * @param newPatch
 * @return
 */
@Override
public int patchCheck(String path, String patchMd5) {
    File patchFile = new File(path);
    TinkerLog.i(TAG, "receive a patch file: %s, file size:%d", path, SharePatchFileUtil.getFileOrDirectorySize(patchFile));
    int returnCode = super.patchCheck(path, patchMd5);

    if (returnCode == ShareConstants.ERROR_PATCH_OK) {
        returnCode = SampleUtils.checkForPatchRecover(NEW_PATCH_RESTRICTION_SPACE_SIZE_MIN, maxMemory);
    }

    if (returnCode == ShareConstants.ERROR_PATCH_OK) {
        SharedPreferences sp = context.getSharedPreferences(ShareConstants.TINKER_SHARE_PREFERENCE_CONFIG, Context.MODE_MULTI_PROCESS);
        //optional, only disable this patch file with md5
        int fastCrashCount = sp.getInt(patchMd5, 0);
        if (fastCrashCount >= SampleUncaughtExceptionHandler.MAX_CRASH_COUNT) {
            returnCode = SampleUtils.ERROR_PATCH_CRASH_LIMIT;
        }
    }
    // Warning, it is just a sample case, you don't need to copy all of these
    // Interception some of the request
    if (returnCode == ShareConstants.ERROR_PATCH_OK) {
        Properties properties = ShareTinkerInternals.fastGetPatchPackageMeta(patchFile);
        if (properties == null) {
            returnCode = SampleUtils.ERROR_PATCH_CONDITION_NOT_SATISFIED;
        } else {
            String platform = properties.getProperty(SampleUtils.PLATFORM);
            TinkerLog.i(TAG, "get platform:" + platform);
            // check patch platform require
            if (platform == null) {
                returnCode = SampleUtils.ERROR_PATCH_CONDITION_NOT_SATISFIED;
            }
        }
    }

    SampleTinkerReport.onTryApply(returnCode == ShareConstants.ERROR_PATCH_OK);
    return returnCode;
}
 
Example #9
Source File: SampleDexDiffPatchInternal.java    From tinker-manager with Apache License 2.0 5 votes vote down vote up
private static boolean extractDexFile(ZipFile zipFile, ZipEntry entryFile, File extractTo, ShareDexDiffPatchInfo dexInfo) throws IOException {
    final String fileMd5 = ShareTinkerInternals.isVmArt() ? dexInfo.destMd5InArt : dexInfo.destMd5InDvm;
    final String rawName = dexInfo.rawName;
    final boolean isJarMode = dexInfo.isJarMode;
    //it is raw dex and we use jar mode, so we need to zip it!
    if (SharePatchFileUtil.isRawDexFile(rawName) && isJarMode) {
        return extractDexToJar(zipFile, entryFile, extractTo, fileMd5);
    }
    return extract(zipFile, entryFile, extractTo, fileMd5, true);
}
 
Example #10
Source File: DefaultPatchRequestCallback.java    From tinkerpatch-sdk with MIT License 5 votes vote down vote up
public void rollbackPatchDirectly() {
    TinkerServerClient client = TinkerServerClient.get();
    final Context context = client.getContext();
    final Tinker tinker = client.getTinker();
    //restart now
    tinker.cleanPatch();
    ShareTinkerInternals.killAllOtherProcess(context);
    android.os.Process.killProcess(android.os.Process.myPid());
}
 
Example #11
Source File: SampleUncaughtExceptionHandler.java    From HotFixDemo with MIT License 4 votes vote down vote up
/**
 * 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();
    }
}
 
Example #12
Source File: MainActivity.java    From HotFixDemo with MIT License 4 votes vote down vote up
public void uninstall_patch(View view) {
    ShareTinkerInternals.killAllOtherProcess(getApplicationContext());
    Tinker.with(getApplicationContext()).cleanPatch();
}
 
Example #13
Source File: MainActivity.java    From HotFixDemo with MIT License 4 votes vote down vote up
public void kill_myself(View view) {
    ShareTinkerInternals.killAllOtherProcess(getApplicationContext());
    android.os.Process.killProcess(android.os.Process.myPid());
}
 
Example #14
Source File: SampleUncaughtExceptionHandler.java    From tinker-manager with Apache License 2.0 4 votes vote down vote up
/**
 * 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();
    }
}