-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
recovery/src/main/java/com/zxy/recovery/tools/RecoverySilentSharedPrefsUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |