Skip to content

Commit dcca181

Browse files
committed
logic optimize
1 parent 6db88d7 commit dcca181

File tree

8 files changed

+120
-3
lines changed

8 files changed

+120
-3
lines changed

app/src/main/java/com/zxy/recovery/test/App.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.zxy.recovery.test;
22

33
import android.app.Application;
4+
import android.content.Context;
45
import android.util.Log;
56

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

1419
@Override
1520
public void onCreate() {
@@ -43,5 +48,10 @@ public void exception(String exceptionType, String throwClassName, String throwM
4348
Log.e("zxy", "throwMethodName:" + throwMethodName);
4449
Log.e("zxy", "throwLineNumber:" + throwLineNumber);
4550
}
51+
52+
@Override
53+
public void throwable(Throwable throwable) {
54+
55+
}
4656
}
4757
}

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ buildscript {
66
mavenCentral()
77
}
88
dependencies {
9-
classpath 'com.android.tools.build:gradle:2.2.0-rc1'
9+
classpath 'com.android.tools.build:gradle:2.2.0'
1010
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
1111
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
1212
// NOTE: Do not place your application dependencies here; they belong

recovery/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dependencies {
2727

2828
ext {
2929
upload_group_id = 'com.zxy.android'
30-
upload_version = '0.0.5'
30+
upload_version = '0.0.6'
3131

3232
site_url = 'https://github.com/Sunzxyong/Recovery'
3333
git_url = 'https://github.com/Sunzxyong/Recovery.git'

recovery/src/main/java/com/zxy/recovery/callback/RecoveryCallback.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,6 @@ public interface RecoveryCallback {
1010
void cause(String cause);
1111

1212
void exception(String throwExceptionType, String throwClassName, String throwMethodName, int throwLineNumber);
13+
14+
void throwable(Throwable throwable);
1315
}

recovery/src/main/java/com/zxy/recovery/core/Recovery.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public void init(Context context) {
6161
throw new RecoveryException("Context can not be null!");
6262
if (!(context instanceof Application))
6363
context = context.getApplicationContext();
64+
// if (!RecoveryUtil.isMainProcess(context)) return;
6465
mContext = context;
6566
registerRecoveryHandler();
6667
registerRecoveryLifecycleCallback();

recovery/src/main/java/com/zxy/recovery/core/RecoveryHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.zxy.recovery.callback.RecoveryCallback;
77
import com.zxy.recovery.tools.DefaultHandlerUtil;
88
import com.zxy.recovery.tools.RecoverySharedPrefsUtil;
9+
import com.zxy.recovery.tools.RecoverySilentSharedPrefsUtil;
910
import com.zxy.recovery.tools.RecoveryUtil;
1011

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

40-
RecoverySharedPrefsUtil.recordCrashData();
41+
if (Recovery.getInstance().isSilentEnabled()) {
42+
RecoverySilentSharedPrefsUtil.recordCrashData();
43+
} else {
44+
RecoverySharedPrefsUtil.recordCrashData();
45+
}
4146

4247
StringWriter sw = new StringWriter();
4348
PrintWriter pw = new PrintWriter(sw);
@@ -86,6 +91,7 @@ public synchronized void uncaughtException(Thread t, Throwable e) {
8691
mCallback.stackTrace(stackTrace);
8792
mCallback.cause(cause);
8893
mCallback.exception(exceptionType, throwClassName, throwMethodName, throwLineNumber);
94+
mCallback.throwable(e);
8995
}
9096

9197
if (!DefaultHandlerUtil.isSystemDefaultUncaughtExceptionHandler(mDefaultUncaughtExceptionHandler)) {

recovery/src/main/java/com/zxy/recovery/core/RecoveryService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import android.content.Intent;
66
import android.os.IBinder;
77

8+
import com.zxy.recovery.tools.RecoverySilentSharedPrefsUtil;
89
import com.zxy.recovery.tools.RecoveryUtil;
910

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

34+
if (RecoverySilentSharedPrefsUtil.shouldClearAppNotRestart()) {
35+
//If restore failed twice within 30 seconds, will only delete data and not restored.
36+
RecoverySilentSharedPrefsUtil.clear();
37+
stopSelf();
38+
killProcess();
39+
}
40+
3341
Recovery.SilentMode mode = getRecoverySilentMode(intent);
3442

3543
if (mode == Recovery.SilentMode.RESTART) {
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package com.zxy.recovery.tools;
2+
3+
import com.zxy.recovery.core.CrashData;
4+
import com.zxy.recovery.core.Recovery;
5+
import com.zxy.recovery.exception.RecoveryException;
6+
7+
/**
8+
* Created by zhengxiaoyong on 2016/9/29.
9+
*/
10+
public class RecoverySilentSharedPrefsUtil {
11+
12+
private static final long DEFAULT_TIME_INTERVAL = 30 * 1000;
13+
14+
private static final int DEFAULT_MAX_COUNT = 2;
15+
16+
private static final String SHARED_PREFS_NAME = "recovery_silent_info";
17+
18+
private static final String CRASH_COUNT = "crash_count";
19+
20+
private static final String CRASH_TIME = "crash_time";
21+
22+
private static final String SHOULD_CLEAR_APP_AND_NOT_RESTART = "should_clear_app_and_not_restart";
23+
24+
private RecoverySilentSharedPrefsUtil() {
25+
throw new RecoveryException("Stub!");
26+
}
27+
28+
public static void recordCrashData() {
29+
int count = 0;
30+
long time = 0L;
31+
boolean shouldRestart = false;
32+
try {
33+
count = Integer.parseInt(get(CRASH_COUNT, String.valueOf(0)));
34+
time = Long.parseLong(get(CRASH_TIME, String.valueOf(0L)));
35+
} catch (Exception e) {
36+
count = 0;
37+
time = 0L;
38+
RecoveryLog.e(e.getMessage());
39+
}
40+
count = Math.max(count, 0);
41+
time = Math.max(time, 0L);
42+
43+
count += 1;
44+
time = time == 0L ? System.currentTimeMillis() : time;
45+
46+
long interval = System.currentTimeMillis() - time;
47+
if (count >= DEFAULT_MAX_COUNT && interval <= DEFAULT_TIME_INTERVAL) {
48+
shouldRestart = true;
49+
count = 0;
50+
time = 0L;
51+
} else {
52+
if (count >= DEFAULT_MAX_COUNT || interval > DEFAULT_TIME_INTERVAL) {
53+
count = 1;
54+
time = System.currentTimeMillis();
55+
}
56+
}
57+
CrashData data = CrashData.newInstance()
58+
.count(count)
59+
.time(time)
60+
.restart(shouldRestart);
61+
RecoveryLog.e(data.toString());
62+
saveCrashData(data);
63+
}
64+
65+
private static void saveCrashData(CrashData data) {
66+
if (data == null)
67+
return;
68+
SharedPreferencesCompat.newBuilder(Recovery.getInstance().getContext(), SHARED_PREFS_NAME)
69+
.put(CRASH_COUNT, String.valueOf(data.crashCount))
70+
.put(CRASH_TIME, String.valueOf(data.crashTime))
71+
.put(SHOULD_CLEAR_APP_AND_NOT_RESTART, String.valueOf(data.shouldRestart))
72+
.apply();
73+
}
74+
75+
public static boolean shouldClearAppNotRestart() {
76+
return Boolean.parseBoolean(get(SHOULD_CLEAR_APP_AND_NOT_RESTART, String.valueOf(false)));
77+
}
78+
79+
public static void clear() {
80+
SharedPreferencesCompat.clear(Recovery.getInstance().getContext(), SHARED_PREFS_NAME);
81+
}
82+
83+
private static void put(String key, String value) {
84+
SharedPreferencesCompat.put(Recovery.getInstance().getContext(), SHARED_PREFS_NAME, key, value);
85+
}
86+
87+
private static String get(String key, String defValue) {
88+
return SharedPreferencesCompat.get(Recovery.getInstance().getContext(), SHARED_PREFS_NAME, key, defValue);
89+
}
90+
}

0 commit comments

Comments
 (0)