Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
osama-raddad committed Dec 6, 2017
1 parent 66102d4 commit d67c639
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ android {
applicationId "com.osama.firecrasherdemo"
minSdkVersion 14
targetSdkVersion 27
versionCode 2
versionName "1.4"
versionCode 3
versionName "1.5"
}
buildTypes {
release {
Expand All @@ -20,6 +20,11 @@ android {
testCoverageEnabled true
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

dependencies {
Expand Down
9 changes: 7 additions & 2 deletions firecrasher/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 27
versionCode 2
versionName "1.4"
versionCode 3
versionName "1.5"
}
buildTypes {
release {
Expand All @@ -52,6 +52,11 @@ android {
'proguard-rules-debug.pro'
}
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}

apply from: 'https://raw.githubusercontent.com/nuuneoi/JCenter/master/installv1.gradle'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@
import android.os.Bundle;
import android.util.Log;

/**
* Created by Osama Raddad.
*/
public final class CrashHandler implements Thread.UncaughtExceptionHandler {
private Activity activity;
private Application.ActivityLifecycleCallbacks lifecycleCallbacks;
private CrashListener crashListener;
private CrashInterface crashInterface;

public CrashHandler() {
CrashHandler() {
lifecycleCallbacks = new Application.ActivityLifecycleCallbacks() {
@Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
CrashHandler.this.activity = activity;
Expand Down Expand Up @@ -48,11 +45,11 @@ public CrashHandler() {
};
}

public void setCrashListener(CrashListener crashListener) {
void setCrashListener(CrashListener crashListener) {
this.crashListener = crashListener;
}

public void setCrashInterface(CrashInterface crashListener) {
void setCrashInterface(CrashInterface crashListener) {
this.crashInterface = crashListener;
}

Expand Down Expand Up @@ -81,7 +78,7 @@ public void onClick(DialogInterface dialog, int which) {
Log.e("FireCrasher.err", thread.getName(), throwable);
}

public Application.ActivityLifecycleCallbacks getLifecycleCallbacks() {
Application.ActivityLifecycleCallbacks getLifecycleCallbacks() {
return lifecycleCallbacks;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

import android.app.Activity;

/**
* Created by menilv on 2/17/17.
*/

public interface CrashInterface {
public void onCrash(Throwable throwable, Activity activity);

public void recover(Activity activity);

void onCrash(Throwable throwable, Activity activity);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public abstract class CrashListener {
public abstract void onCrash(Throwable throwable, Activity activity);

public void recover(Activity activity) {
protected void recover(Activity activity) {
FireCrasher.recover(activity);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import android.content.pm.PackageManager;
import android.util.Log;

/**
* Created by Osama Raddad.
*/

public class FireCrasher {
public static void install(Application application) {
if (!FireLooper.isSafe()) {
Expand Down Expand Up @@ -43,7 +41,7 @@ public static void install(Application application, CrashInterface crashListener
}
}

protected static void recover(Activity activity) {
static void recover(Activity activity) {
if (activity != null) {
try {
ActivityInfo[] list = activity.getPackageManager()
Expand Down
100 changes: 43 additions & 57 deletions firecrasher/src/main/java/com/osama/firecrasher/FireLooper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.osama.firecrasher;

import android.annotation.SuppressLint;
import android.os.Binder;
import android.os.Handler;
import android.os.Looper;
Expand All @@ -10,103 +11,88 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* Created by osama.
*/


public class FireLooper implements Runnable {
private static final Object EXIT = new Object();
private static final ThreadLocal<FireLooper> RUNNINGS = new ThreadLocal<>();
private static Object EXIT;
private static ThreadLocal<FireLooper> FIRE_LOOPER_THREAD_LOCAL;
private static Thread.UncaughtExceptionHandler uncaughtExceptionHandler;
private static Handler handler = new Handler(Looper.getMainLooper());
private static Handler handler;

public static void install() {
handler.removeMessages(0, EXIT);
handler.post(new FireLooper());
static {
EXIT = new Object();
FIRE_LOOPER_THREAD_LOCAL = new ThreadLocal<>();
handler = new Handler(Looper.getMainLooper());
}

public static void uninstallDelay(long millis) {
static void install() {
handler.removeMessages(0, EXIT);
handler.sendMessageDelayed(handler.obtainMessage(0, EXIT), millis);
}

public static void uninstall() {
uninstallDelay(0);
handler.post(new FireLooper());
}

public static boolean isSafe() {
return RUNNINGS.get() != null;
static boolean isSafe() {
return FIRE_LOOPER_THREAD_LOCAL.get() != null;
}

public static void setUncaughtExceptionHandler(
static void setUncaughtExceptionHandler(
Thread.UncaughtExceptionHandler h) {
uncaughtExceptionHandler = h;
}

@Override
public void run() {
if (RUNNINGS.get() != null)
if (FIRE_LOOPER_THREAD_LOCAL.get() != null)
return;

Method next;
Field target;
try {
Method m = MessageQueue.class.getDeclaredMethod("next");
m.setAccessible(true);
next = m;
Field f = Message.class.getDeclaredField("target");
f.setAccessible(true);
target = f;
@SuppressLint("PrivateApi") Method method = MessageQueue.class.getDeclaredMethod("next");
method.setAccessible(true);
next = method;
Field field = Message.class.getDeclaredField("target");
field.setAccessible(true);
target = field;
} catch (Exception e) {
return;
}

RUNNINGS.set(this);
FIRE_LOOPER_THREAD_LOCAL.set(this);
MessageQueue queue = Looper.myQueue();
Binder.clearCallingIdentity();

while (true) {
try {
Message msg = (Message) next.invoke(queue);
if (msg == null || msg.obj == EXIT)
Message message = (Message) next.invoke(queue);
if (message == null || message.obj == EXIT)
break;

Handler h = (Handler) target.get(msg);
h.dispatchMessage(msg);

Handler handler = (Handler) target.get(message);
handler.dispatchMessage(message);

Binder.clearCallingIdentity();

int currentapiVersion = android.os.Build.VERSION.SDK_INT;
if (currentapiVersion < android.os.Build.VERSION_CODES.LOLLIPOP){
msg.recycle();
}
} catch (InvocationTargetException e) {
Thread.UncaughtExceptionHandler h = uncaughtExceptionHandler;
Throwable ex = e;
ex = e.getCause();
if (ex == null) {
ex = e;
}

if (h != null) {
h.uncaughtException(Thread.currentThread(), ex);
}
int currentVersion = android.os.Build.VERSION.SDK_INT;
if (currentVersion < android.os.Build.VERSION_CODES.LOLLIPOP)
message.recycle();
} catch (InvocationTargetException exception) {
Thread.UncaughtExceptionHandler exceptionHandler = uncaughtExceptionHandler;
Throwable throwable;
throwable = exception.getCause();
if (throwable == null) throwable = exception;

if (exceptionHandler != null) exceptionHandler
.uncaughtException(Thread.currentThread(), throwable);

new Handler().post(this);
break;
}catch (Exception e) {
Thread.UncaughtExceptionHandler h = uncaughtExceptionHandler;
Throwable ex = e;

if (h != null) {
h.uncaughtException(Thread.currentThread(), ex);
}
} catch (Exception e) {
Thread.UncaughtExceptionHandler exceptionHandler = uncaughtExceptionHandler;
if (exceptionHandler != null)
exceptionHandler.uncaughtException(Thread.currentThread(), e);
new Handler().post(this);
break;
}
}

RUNNINGS.set(null);
FIRE_LOOPER_THREAD_LOCAL.set(null);
}
}

0 comments on commit d67c639

Please sign in to comment.