1 /*
2  * Copyright 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.pump.app;
18 
19 import android.os.Handler;
20 import android.os.StrictMode;
21 
22 import androidx.annotation.MainThread;
23 import androidx.annotation.NonNull;
24 
25 import com.android.pump.BuildConfig;
26 import com.android.pump.util.Clog;
27 
28 @MainThread
29 public class PumpApplication extends GlobalsApplication implements Thread.UncaughtExceptionHandler {
30     private static final String TAG = Clog.tag(PumpApplication.class);
31 
32     private final Thread.UncaughtExceptionHandler mSystemUncaughtExceptionHandler =
33             Thread.getDefaultUncaughtExceptionHandler();
34 
PumpApplication()35     public PumpApplication() {
36         Thread.setDefaultUncaughtExceptionHandler(this);
37     }
38 
39     @Override
onCreate()40     public void onCreate() {
41         super.onCreate();
42 
43         if (BuildConfig.DEBUG) {
44             StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
45                     .detectAll()
46                     .penaltyLog()
47                     .penaltyFlashScreen()
48                     .penaltyDialog()
49                     .penaltyDeath()
50                     .build());
51             StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
52                     .detectAll()
53                     .penaltyLog()
54                     .penaltyDeath()
55                     .build());
56         }
57     }
58 
59     @Override
uncaughtException(@onNull Thread t, @NonNull Throwable e)60     public void uncaughtException(@NonNull Thread t, @NonNull Throwable e) {
61         if (getMainLooper().getThread() != t) {
62             Clog.e(TAG, "Uncaught exception in background thread " + t, e);
63             new Handler(getMainLooper()).post(() ->
64                     mSystemUncaughtExceptionHandler.uncaughtException(t, e));
65         } else {
66             mSystemUncaughtExceptionHandler.uncaughtException(t, e);
67         }
68     }
69 }
70