Skip to content

Commit

Permalink
Fixing bugs (#52, #53)
Browse files Browse the repository at this point in the history
  • Loading branch information
whataa committed May 12, 2019
1 parent d2bd926 commit 6b2f3f4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
Expand Up @@ -2,6 +2,7 @@

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.support.v4.content.FileProvider;

import tech.linjiang.pandora.crash.CrashHandler;
Expand Down Expand Up @@ -30,7 +31,8 @@ public Pandora() {
@Override
public boolean onCreate() {
INSTANCE = this;
init(((Application) getContext()));
Context context = Utils.makeContextSafe(getContext());
init(((Application) context));
return super.onCreate();
}

Expand Down
Expand Up @@ -62,7 +62,7 @@ public Response intercept(Chain chain) throws IOException {
Response response;
try {
response = chain.proceed(request);
} catch (IOException e) {
} catch (Throwable e) {
if (Config.isNetLogEnable() && id >= 0) {
markFailed(id, Utils.collectThrow(e));
notifyEnd(id);
Expand Down
15 changes: 15 additions & 0 deletions pandora-core/src/main/java/tech/linjiang/pandora/util/Utils.java
Expand Up @@ -21,6 +21,7 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -229,4 +230,18 @@ public static String collectThrow(Throwable ex) {
printWriter.close();
return writer.toString();
}

public static Context makeContextSafe(Context context) {
if (context != null) {
return context;
}
try {
Class actThreadClass = Reflect28Util.forName("android.app.ActivityThread");
Method method = Reflect28Util.getDeclaredMethod(actThreadClass, "currentApplication");
return (Context) method.invoke(null);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

0 comments on commit 6b2f3f4

Please sign in to comment.