Java Code Examples for com.tencent.tinker.lib.util.TinkerLog#e()

The following examples show how to use com.tencent.tinker.lib.util.TinkerLog#e() . 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: DefaultPatchRequestCallback.java    From tinkerpatch-sdk with MIT License 6 votes vote down vote up
@Override
public void onPatchDownloadFail(Exception e, Integer newVersion, Integer currentVersion) {
    TinkerLog.w(TAG, "onPatchDownloadFail e:" + e);
    //check network
    TinkerServerClient client = TinkerServerClient.get();
    //due to network, just return
    if (!NetStatusUtil.isConnected(client.getContext())) {
        TinkerLog.e(TAG, "onPatchDownloadFail, not connect to internet just return");
        return;
    }
    Context context = client.getContext();
    if (increaseDownloadError(context)) {
        client.reportPatchFail(newVersion, ERROR_DOWNLOAD_FAIL);
    }

}
 
Example 3
Source File: SampleTinkerReport.java    From HotFixDemo with MIT License 6 votes vote down vote up
public static void onLoaded(long cost) {
    if (reporter == null) {
        return;
    }
    reporter.onReport(KEY_LOADED);

    if (cost < 0L) {
        TinkerLog.e(TAG, "hp_report report load cost failed, invalid cost");
        return;
    }

    if (cost <= 500) {
        reporter.onReport(KEY_LOADED_SUCC_COST_500_LESS);
    } else if (cost <= 1000) {
        reporter.onReport(KEY_LOADED_SUCC_COST_1000_LESS);
    } else if (cost <= 3000) {
        reporter.onReport(KEY_LOADED_SUCC_COST_3000_LESS);
    } else if (cost <= 5000) {
        reporter.onReport(KEY_LOADED_SUCC_COST_5000_LESS);
    } else {
        reporter.onReport(KEY_LOADED_SUCC_COST_OTHER);
    }
}
 
Example 4
Source File: TinkerServerManager.java    From tinkerpatch-sdk with MIT License 6 votes vote down vote up
/**
 * 向服务器请求在线参数信息
 * @param configRequestCallback
 * @param immediately            是否立刻请求,忽略时间间隔限制
 */
public static void getDynamicConfig(final ConfigRequestCallback configRequestCallback, final boolean immediately) {
    if (sTinkerServerClient == null) {
        TinkerLog.e(TAG, "checkTinkerUpdate, sTinkerServerClient == null");
        return;
    }
    Tinker tinker = sTinkerServerClient.getTinker();
    //only check at the main process
    if (tinker.isMainProcess()) {
        Looper.getMainLooper().myQueue().addIdleHandler(new MessageQueue.IdleHandler() {
            @Override public boolean queueIdle() {
                sTinkerServerClient.getDynamicConfig(configRequestCallback, immediately);
                return false;
            }
        });
    }
}
 
Example 5
Source File: BaseReport.java    From tinkerpatch-sdk with MIT License 6 votes vote down vote up
private String getPostDataString(HashMap<String, String> params) {
    StringBuilder result = new StringBuilder();

    try {
        boolean first = true;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (first) {
                first = false;
            } else {
                result.append('&');
            }
            result.append(URLEncoder.encode(entry.getKey(), ServerUtils.CHARSET));
            result.append('=');
            result.append(URLEncoder.encode(entry.getValue(), ServerUtils.CHARSET));
        }
    } catch (Exception e) {
        TinkerLog.e(TAG, "getPostDataString fail" + e.getMessage());
    }

    return result.toString();
}
 
Example 6
Source File: TinkerServerClient.java    From tinkerpatch-sdk with MIT License 5 votes vote down vote up
/**
 * 上报补丁下载成功
 * @param patchVersion 补丁包版本号
 */
public void reportPatchDownloadSuccess(Integer patchVersion) {
    if (!checkParameter()) {
        TinkerLog.e(TAG, "check parameter fail, appKey or appVersion is null, "
            + "reportPatchDownloadSuccess just return");
        return;
    }
    TinkerLog.i(TAG, "tinker server report patch download success, patchVersion:%d", patchVersion);
    clientAPI.reportDownloadSuccess(patchVersion);
}
 
Example 7
Source File: TinkerServerPatchRequestCallback.java    From tinkerpatch-sdk with MIT License 5 votes vote down vote up
@Override
public boolean beforePatchRequest() {
    boolean result = super.beforePatchRequest();
    if (result) {
        TinkerServerClient client = TinkerServerClient.get();
        Tinker tinker = client.getTinker();
        Context context = client.getContext();

        if (!tinker.isMainProcess()) {
            TinkerLog.e(TAG, "beforePatchRequest, only request on the main process");
            return false;
        }
        if (TinkerServerManager.isGooglePlayChannel()) {
            TinkerLog.e(TAG, "beforePatchRequest, google play channel, return false");
            return false;
        }
        // main process must be the newly version
        // check whether it is pending work
        String currentPatchMd5 = client.getCurrentPatchMd5();
        TinkerLoadResult tinkerLoadResult = tinker.getTinkerLoadResultIfPresent();

        if (tinkerLoadResult.currentVersion == null || !currentPatchMd5.equals(tinkerLoadResult.currentVersion)) {
            Integer version = client.getCurrentPatchVersion();
            if (version > 0) {
                File patchFile = ServerUtils.getServerFile(
                    context, client.getAppVersion(), String.valueOf(version)
                );
                if (patchFile.exists() && patchFile.isFile() && handlePatchFile(context, version, patchFile)) {
                    return false;
                }
            }
        }
    }
    return result;
}
 
Example 8
Source File: SampleDexDiffPatchInternal.java    From tinker-manager with Apache License 2.0 5 votes vote down vote up
public static boolean waitDexOptFile() {
        if (optFiles.isEmpty()) {
            return true;
        }

        int size = optFiles.size() * 6;
        if (size > MAX_WAIT_COUNT) {
            size = MAX_WAIT_COUNT;
        }
        TinkerLog.i(TAG, "dex count: %d, final wait time: %d", optFiles.size(), size);

        for (int i = 0; i < size; i++) {
            if (!checkAllDexOptFile(optFiles, i + 1)) {
                try {
                    Thread.sleep(WAIT_ASYN_OAT_TIME);
                } catch (InterruptedException e) {
                    TinkerLog.e(TAG, "thread sleep InterruptedException e:" + e);
                }
            }
        }

        // check again, if still can be found, just return
        for (File file : optFiles) {
            TinkerLog.i(TAG, "check dex optimizer file %s, size %d", file.getName(), file.length());

            if (!SharePatchFileUtil.isLegalFile(file)) {
                TinkerLog.e(TAG, "final parallel dex optimizer file %s is not exist, return false", file.getName());
                // don't report fail
//                manager.getPatchReporter()
//                    .onPatchDexOptFail(patchFile, file, file.getParentFile().getPath(),
//                        file.getName(), new TinkerRuntimeException("dexOpt file:" + file.getName() + " is not exist"));
                return false;

            }
        }
        return true;
    }
 
Example 9
Source File: TinkerServerManager.java    From tinkerpatch-sdk with MIT License 5 votes vote down vote up
/**
 * 设置在线参数的时间间隔
 * @param hours 大于等于0的整数
 */
public static void setGetConfigIntervalByHours(int hours) {
    if (sTinkerServerClient == null) {
        TinkerLog.e(TAG, "setGetConfigIntervalByHours, sTinkerServerClient == null");
        return;
    }
    sTinkerServerClient.setGetConfigIntervalByHours(hours);
}
 
Example 10
Source File: SampleTinkerReport.java    From tinker-manager with Apache License 2.0 5 votes vote down vote up
public static void onLoadException(Throwable throwable, int errorCode) {
    if (reporter == null) {
        return;
    }
    boolean isCheckFail = false;
    switch (errorCode) {
        case ShareConstants.ERROR_LOAD_EXCEPTION_DEX:
            if (throwable.getMessage().contains(ShareConstants.CHECK_DEX_INSTALL_FAIL)) {
                reporter.onReport(KEY_LOADED_EXCEPTION_DEX_CHECK);
                isCheckFail = true;
                TinkerLog.e(TAG, "tinker dex check fail:" + throwable.getMessage());
            } else {
                reporter.onReport(KEY_LOADED_EXCEPTION_DEX);
                TinkerLog.e(TAG, "tinker dex reflect fail:" + throwable.getMessage());
            }
            break;
        case ShareConstants.ERROR_LOAD_EXCEPTION_RESOURCE:
            if (throwable.getMessage().contains(ShareConstants.CHECK_RES_INSTALL_FAIL)) {
                reporter.onReport(KEY_LOADED_EXCEPTION_RESOURCE_CHECK);
                isCheckFail = true;
                TinkerLog.e(TAG, "tinker res check fail:" + throwable.getMessage());
            } else {
                reporter.onReport(KEY_LOADED_EXCEPTION_RESOURCE);
                TinkerLog.e(TAG, "tinker res reflect fail:" + throwable.getMessage());
            }
            break;
        case ShareConstants.ERROR_LOAD_EXCEPTION_UNCAUGHT:
            reporter.onReport(KEY_LOADED_UNCAUGHT_EXCEPTION);
            break;
        case ShareConstants.ERROR_LOAD_EXCEPTION_UNKNOWN:
            reporter.onReport(KEY_LOADED_UNKNOWN_EXCEPTION);
            break;
    }
    //reporter exception, for dex check fail, we don't need to report stacktrace
    if (!isCheckFail) {
        reporter.onReport("Tinker Exception:load tinker occur exception " + SampleUtils.getExceptionCauseString(throwable));
    }
}
 
Example 11
Source File: Debugger.java    From tinkerpatch-sdk with MIT License 5 votes vote down vote up
private Debugger(final Context context) {
    final ContentResolver cr = context.getContentResolver();
    Cursor cu;
    try {
        cu = cr.query(CONTENT_URI, columns, null, null, null);
    } catch (Exception e) {
        TinkerLog.e(TAG, "Get contentProvider error", e);
        cu = null;
    }

    if (cu == null || cu.getCount() <= 0) {
        TinkerLog.w(TAG, "debugger not attached cu == null");
        if (cu != null) {
            cu.close();
        }
        return;
    }

    TinkerLog.i(TAG, "debugger attached");

    final int keyIdx = cu.getColumnIndex("key");
    final int typeIdx = cu.getColumnIndex("type");
    final int valueIdx = cu.getColumnIndex("value");

    while (cu.moveToNext()) {
        final Object obj = Resolver.resolveObj(cu.getInt(typeIdx), cu.getString(valueIdx));
        values.put(cu.getString(keyIdx), obj);
    }
    cu.close();
}
 
Example 12
Source File: SamplePatchService.java    From tinker-manager with Apache License 2.0 5 votes vote down vote up
public static void runPatchService(Context context, String path) {
    try {
        Intent intent = new Intent(context, SamplePatchService.class);
        intent.putExtra(PATCH_PATH_EXTRA, path);
        intent.putExtra(RESULT_CLASS_EXTRA, SampleResultService.class.getName());
        context.startService(intent);
    } catch (Throwable throwable) {
        TinkerLog.e(TAG, "start patch service fail, exception:" + throwable);
    }
}
 
Example 13
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 14
Source File: SampleUncaughtExceptionHandler.java    From tinker-manager with Apache License 2.0 5 votes vote down vote up
@Override
public void uncaughtException(Thread thread, Throwable ex) {
    TinkerLog.e(TAG, "uncaughtException:" + ex.getMessage());
    tinkerFastCrashProtect();
    tinkerPreVerifiedCrashHandler(ex);
    ueh.uncaughtException(thread, ex);
}
 
Example 15
Source File: DefaultPatchRequestCallback.java    From tinkerpatch-sdk with MIT License 5 votes vote down vote up
@Override
public boolean beforePatchRequest() {
    TinkerServerClient client = TinkerServerClient.get();

    // check network
    if (!NetStatusUtil.isConnected(client.getContext())) {
        TinkerLog.e(TAG, "not connect to internet");
        return false;
    }
    if (TinkerServiceInternals.isTinkerPatchServiceRunning(client.getContext())) {
        TinkerLog.e(TAG, "tinker service is running");
        return false;
    }
    return true;
}
 
Example 16
Source File: SampleUncaughtExceptionHandler.java    From HotFixDemo with MIT License 5 votes vote down vote up
@Override
public void uncaughtException(Thread thread, Throwable ex) {
    TinkerLog.e(TAG, "uncaughtException:" + ex.getMessage());
    tinkerFastCrashProtect();
    tinkerPreVerifiedCrashHandler(ex);
    ueh.uncaughtException(thread, ex);
}
 
Example 17
Source File: TinkerServerClient.java    From tinkerpatch-sdk with MIT License 5 votes vote down vote up
/**
 * 上报补丁应用成功
 * @param patchVersion 补丁包版本号
 */
public void reportPatchApplySuccess(Integer patchVersion) {
    if (!checkParameter()) {
        TinkerLog.e(TAG, "check parameter fail, appKey or appVersion is null, "
            + "reportPatchApplySuccess just return");
        return;
    }
    TinkerLog.i(TAG, "tinker server report patch apply success, patchVersion:%d", patchVersion);
    clientAPI.reportApplySuccess(patchVersion);
}
 
Example 18
Source File: SampleResultService.java    From tinker-manager with Apache License 2.0 4 votes vote down vote up
@Override
public void onPatchResult(final PatchResult result) {
    if (result == null) {
        TinkerLog.e(TAG, "SampleResultService received null result!!!!");
        return;
    }
    TinkerLog.i(TAG, "SampleResultService receive result: %s", result.toString());

    //first, we want to kill the recover process
    TinkerServiceInternals.killTinkerPatchServiceProcess(getApplicationContext());

    Handler handler = new Handler(Looper.getMainLooper());
    handler.post(new Runnable() {
        @Override
        public void run() {
            if (result.isSuccess) {
                TinkerLog.i(TAG, "patch success, please restart process");
            } else {
                TinkerLog.e(TAG, "patch fail, please check reason");
            }
        }
    });
    // is success and newPatch, it is nice to delete the raw file, and restart at once
    // for old patch, you can't delete the patch file
    if (!result.isSuccess) {
        //补丁合成异常
        PatchManager.getInstance().onPatchFailure(result.rawPatchFilePath, PatchManager.ERROR_CODE_PATCH_RESULT + PatchManager.ERROR_PATCH_FAIL);
        return;
    }
    //deleteRawPatchFile(new File(result.rawPatchFilePath));

    PatchManager.getInstance().onPatchSuccess(result.rawPatchFilePath);

    //not like TinkerResultService, I want to restart just when I am at background!
    //if you have not install tinker this moment, you can use TinkerApplicationHelper api
    if (checkIfNeedKill(result)) {
        if (SampleUtils.isBackground()) {
            TinkerLog.i(TAG, "it is in background, just restart process");
            restartProcess();
        } else {
            //we can wait process at background, such as onAppBackground
            //or we can restart when the screen off
            TinkerLog.i(TAG, "tinker wait screen to restart process");
            new SampleUtils.ScreenState(getApplicationContext(), new SampleUtils.ScreenState.IOnScreenOff() {
                @Override
                public void onScreenOff() {
                    restartProcess();
                }
            });
        }
    } else {
        TinkerLog.i(TAG, "I have already install the newly patch version!");
    }
}
 
Example 19
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();
    }
}
 
Example 20
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();
    }
}