Skip to content

Commit

Permalink
logic optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
Sunzxyong committed Sep 29, 2016
1 parent 6db88d7 commit dcca181
Show file tree
Hide file tree
Showing 8 changed files with 120 additions and 3 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/com/zxy/recovery/test/App.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zxy.recovery.test;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import com.zxy.recovery.callback.RecoveryCallback;
Expand All @@ -10,6 +11,10 @@
* Created by zhengxiaoyong on 16/8/26.
*/
public class App extends Application {
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
}

@Override
public void onCreate() {
Expand Down Expand Up @@ -43,5 +48,10 @@ public void exception(String exceptionType, String throwClassName, String throwM
Log.e("zxy", "throwMethodName:" + throwMethodName);
Log.e("zxy", "throwLineNumber:" + throwLineNumber);
}

@Override
public void throwable(Throwable throwable) {

}
}
}
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.2.0-rc1'
classpath 'com.android.tools.build:gradle:2.2.0'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
// NOTE: Do not place your application dependencies here; they belong
Expand Down
2 changes: 1 addition & 1 deletion recovery/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ dependencies {

ext {
upload_group_id = 'com.zxy.android'
upload_version = '0.0.5'
upload_version = '0.0.6'

site_url = 'https://github.com/Sunzxyong/Recovery'
git_url = 'https://github.com/Sunzxyong/Recovery.git'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface RecoveryCallback {
void cause(String cause);

void exception(String throwExceptionType, String throwClassName, String throwMethodName, int throwLineNumber);

void throwable(Throwable throwable);
}
1 change: 1 addition & 0 deletions recovery/src/main/java/com/zxy/recovery/core/Recovery.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void init(Context context) {
throw new RecoveryException("Context can not be null!");
if (!(context instanceof Application))
context = context.getApplicationContext();
// if (!RecoveryUtil.isMainProcess(context)) return;
mContext = context;
registerRecoveryHandler();
registerRecoveryLifecycleCallback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.zxy.recovery.callback.RecoveryCallback;
import com.zxy.recovery.tools.DefaultHandlerUtil;
import com.zxy.recovery.tools.RecoverySharedPrefsUtil;
import com.zxy.recovery.tools.RecoverySilentSharedPrefsUtil;
import com.zxy.recovery.tools.RecoveryUtil;

import java.io.PrintWriter;
Expand Down Expand Up @@ -37,7 +38,11 @@ static RecoveryHandler newInstance(Thread.UncaughtExceptionHandler defHandler) {
@Override
public synchronized void uncaughtException(Thread t, Throwable e) {

RecoverySharedPrefsUtil.recordCrashData();
if (Recovery.getInstance().isSilentEnabled()) {
RecoverySilentSharedPrefsUtil.recordCrashData();
} else {
RecoverySharedPrefsUtil.recordCrashData();
}

StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
Expand Down Expand Up @@ -86,6 +91,7 @@ public synchronized void uncaughtException(Thread t, Throwable e) {
mCallback.stackTrace(stackTrace);
mCallback.cause(cause);
mCallback.exception(exceptionType, throwClassName, throwMethodName, throwLineNumber);
mCallback.throwable(e);
}

if (!DefaultHandlerUtil.isSystemDefaultUncaughtExceptionHandler(mDefaultUncaughtExceptionHandler)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.Intent;
import android.os.IBinder;

import com.zxy.recovery.tools.RecoverySilentSharedPrefsUtil;
import com.zxy.recovery.tools.RecoveryUtil;

import java.util.ArrayList;
Expand All @@ -30,6 +31,13 @@ public IBinder onBind(Intent intent) {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {

if (RecoverySilentSharedPrefsUtil.shouldClearAppNotRestart()) {
//If restore failed twice within 30 seconds, will only delete data and not restored.
RecoverySilentSharedPrefsUtil.clear();
stopSelf();
killProcess();
}

Recovery.SilentMode mode = getRecoverySilentMode(intent);

if (mode == Recovery.SilentMode.RESTART) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.zxy.recovery.tools;

import com.zxy.recovery.core.CrashData;
import com.zxy.recovery.core.Recovery;
import com.zxy.recovery.exception.RecoveryException;

/**
* Created by zhengxiaoyong on 2016/9/29.
*/
public class RecoverySilentSharedPrefsUtil {

private static final long DEFAULT_TIME_INTERVAL = 30 * 1000;

private static final int DEFAULT_MAX_COUNT = 2;

private static final String SHARED_PREFS_NAME = "recovery_silent_info";

private static final String CRASH_COUNT = "crash_count";

private static final String CRASH_TIME = "crash_time";

private static final String SHOULD_CLEAR_APP_AND_NOT_RESTART = "should_clear_app_and_not_restart";

private RecoverySilentSharedPrefsUtil() {
throw new RecoveryException("Stub!");
}

public static void recordCrashData() {
int count = 0;
long time = 0L;
boolean shouldRestart = false;
try {
count = Integer.parseInt(get(CRASH_COUNT, String.valueOf(0)));
time = Long.parseLong(get(CRASH_TIME, String.valueOf(0L)));
} catch (Exception e) {
count = 0;
time = 0L;
RecoveryLog.e(e.getMessage());
}
count = Math.max(count, 0);
time = Math.max(time, 0L);

count += 1;
time = time == 0L ? System.currentTimeMillis() : time;

long interval = System.currentTimeMillis() - time;
if (count >= DEFAULT_MAX_COUNT && interval <= DEFAULT_TIME_INTERVAL) {
shouldRestart = true;
count = 0;
time = 0L;
} else {
if (count >= DEFAULT_MAX_COUNT || interval > DEFAULT_TIME_INTERVAL) {
count = 1;
time = System.currentTimeMillis();
}
}
CrashData data = CrashData.newInstance()
.count(count)
.time(time)
.restart(shouldRestart);
RecoveryLog.e(data.toString());
saveCrashData(data);
}

private static void saveCrashData(CrashData data) {
if (data == null)
return;
SharedPreferencesCompat.newBuilder(Recovery.getInstance().getContext(), SHARED_PREFS_NAME)
.put(CRASH_COUNT, String.valueOf(data.crashCount))
.put(CRASH_TIME, String.valueOf(data.crashTime))
.put(SHOULD_CLEAR_APP_AND_NOT_RESTART, String.valueOf(data.shouldRestart))
.apply();
}

public static boolean shouldClearAppNotRestart() {
return Boolean.parseBoolean(get(SHOULD_CLEAR_APP_AND_NOT_RESTART, String.valueOf(false)));
}

public static void clear() {
SharedPreferencesCompat.clear(Recovery.getInstance().getContext(), SHARED_PREFS_NAME);
}

private static void put(String key, String value) {
SharedPreferencesCompat.put(Recovery.getInstance().getContext(), SHARED_PREFS_NAME, key, value);
}

private static String get(String key, String defValue) {
return SharedPreferencesCompat.get(Recovery.getInstance().getContext(), SHARED_PREFS_NAME, key, defValue);
}
}

0 comments on commit dcca181

Please sign in to comment.