File tree 3 files changed +19
-2
lines changed
pandora-core/src/main/java/tech/linjiang/pandora
3 files changed +19
-2
lines changed Original file line number Diff line number Diff line change 3
3
import android .app .Activity ;
4
4
import android .app .Application ;
5
5
import androidx .core .content .FileProvider ;
6
+ import android .content .Context ;
6
7
7
8
import tech .linjiang .pandora .crash .CrashHandler ;
8
9
import tech .linjiang .pandora .database .Databases ;
@@ -30,7 +31,8 @@ public Pandora() {
30
31
@ Override
31
32
public boolean onCreate () {
32
33
INSTANCE = this ;
33
- init (((Application ) getContext ()));
34
+ Context context = Utils .makeContextSafe (getContext ());
35
+ init (((Application ) context ));
34
36
return super .onCreate ();
35
37
}
36
38
Original file line number Diff line number Diff line change @@ -62,7 +62,7 @@ public Response intercept(Chain chain) throws IOException {
62
62
Response response ;
63
63
try {
64
64
response = chain .proceed (request );
65
- } catch (IOException e ) {
65
+ } catch (Throwable e ) {
66
66
if (Config .isNetLogEnable () && id >= 0 ) {
67
67
markFailed (id , Utils .collectThrow (e ));
68
68
notifyEnd (id );
Original file line number Diff line number Diff line change 21
21
import java .io .PrintWriter ;
22
22
import java .io .StringWriter ;
23
23
import java .io .Writer ;
24
+ import java .lang .reflect .Method ;
24
25
import java .math .BigDecimal ;
25
26
import java .text .DateFormat ;
26
27
import java .text .SimpleDateFormat ;
@@ -229,4 +230,18 @@ public static String collectThrow(Throwable ex) {
229
230
printWriter .close ();
230
231
return writer .toString ();
231
232
}
233
+
234
+ public static Context makeContextSafe (Context context ) {
235
+ if (context != null ) {
236
+ return context ;
237
+ }
238
+ try {
239
+ Class actThreadClass = Reflect28Util .forName ("android.app.ActivityThread" );
240
+ Method method = Reflect28Util .getDeclaredMethod (actThreadClass , "currentApplication" );
241
+ return (Context ) method .invoke (null );
242
+ } catch (Exception e ) {
243
+ e .printStackTrace ();
244
+ return null ;
245
+ }
246
+ }
232
247
}
You can’t perform that action at this time.
0 commit comments