• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.dialer.strictmode.impl;
18 
19 import android.app.Application;
20 import android.os.Build;
21 import android.os.Handler;
22 import android.os.Looper;
23 import android.os.StrictMode;
24 import android.os.StrictMode.ThreadPolicy;
25 import android.os.StrictMode.VmPolicy;
26 import android.support.annotation.MainThread;
27 import android.support.annotation.Nullable;
28 import com.android.dialer.common.Assert;
29 import com.android.dialer.strictmode.DialerStrictMode;
30 import com.android.dialer.strictmode.StrictModeUtils;
31 import com.google.auto.value.AutoValue;
32 import java.util.Map;
33 import javax.inject.Inject;
34 
35 final class SystemDialerStrictMode implements DialerStrictMode {
36   private static final VmPolicy VM_DEATH_PENALTY =
37       new StrictMode.VmPolicy.Builder().penaltyLog().penaltyDeath().build();
38 
39   private static final ThreadPolicy THREAD_DEATH_PENALTY =
40       new StrictMode.ThreadPolicy.Builder().penaltyLog().penaltyDeath().build();
41 
42   @Inject
SystemDialerStrictMode()43   public SystemDialerStrictMode() {}
44 
45   @MainThread
46   @Override
onApplicationCreate(Application application)47   public void onApplicationCreate(Application application) {
48     if (StrictModeUtils.isStrictModeAllowed()) {
49       StrictModeUtils.warmupSharedPrefs(application);
50       setRecommendedMainThreadPolicy(THREAD_DEATH_PENALTY);
51       setRecommendedVMPolicy(VM_DEATH_PENALTY);
52 
53       // Because Android resets StrictMode policies after Application.onCreate is done, we set it
54       // again right after.
55       // See cl/105932355 for the discussion.
56       // See a bug for the public bug.
57       Handler handler = new Handler(Looper.myLooper());
58       handler.postAtFrontOfQueue(() -> setRecommendedMainThreadPolicy(THREAD_DEATH_PENALTY));
59     }
60   }
61 
62   /**
63    * Set the recommended policy for the app.
64    *
65    * @param threadPenalties policy with preferred penalties. Detection bits will be ignored.
66    */
setRecommendedMainThreadPolicy(StrictMode.ThreadPolicy threadPenalties)67   private static void setRecommendedMainThreadPolicy(StrictMode.ThreadPolicy threadPenalties) {
68     StrictMode.ThreadPolicy threadPolicy =
69         new StrictMode.ThreadPolicy.Builder(threadPenalties).detectAll().build();
70     StrictMode.setThreadPolicy(threadPolicy);
71   }
72 
73   /**
74    * Set the recommended policy for the app.
75    *
76    * @param vmPenalties policy with preferred penalties. Detection bits should be unset.
77    */
setRecommendedVMPolicy(StrictMode.VmPolicy vmPenalties)78   private static void setRecommendedVMPolicy(StrictMode.VmPolicy vmPenalties) {
79     setRecommendedVMPolicy(vmPenalties, StrictModeVmConfig.empty());
80   }
81 
82   /**
83    * Set the recommended policy for the app.
84    *
85    * @param vmPenalties policy with preferred penalties. Detection bits should be unset.
86    */
setRecommendedVMPolicy( StrictMode.VmPolicy vmPenalties, StrictModeVmConfig config)87   private static void setRecommendedVMPolicy(
88       StrictMode.VmPolicy vmPenalties, StrictModeVmConfig config) {
89     Assert.isNotNull(config);
90     StrictMode.VmPolicy.Builder vmPolicyBuilder =
91         new StrictMode.VmPolicy.Builder(vmPenalties)
92             .detectLeakedClosableObjects()
93             .detectLeakedSqlLiteObjects();
94     if (Build.VERSION.SDK_INT >= 26) {
95       vmPolicyBuilder.detectContentUriWithoutPermission();
96       // TODO(azlatin): Enable detecting untagged sockets once: a bug is fixed.
97       // vmPolicyBuilder.detectUntaggedSockets();
98     }
99     StrictMode.setVmPolicy(vmPolicyBuilder.build());
100   }
101 
102   /** VmPolicy configuration. */
103   @AutoValue
104   abstract static class StrictModeVmConfig {
105     /** A map of a class to the maximum number of allowed instances of that class. */
106     @Nullable
maxInstanceLimits()107     abstract Map<Class<?>, Integer> maxInstanceLimits();
108 
empty()109     public static StrictModeVmConfig empty() {
110       return builder().build();
111     }
112 
builder()113     public static Builder builder() {
114       return new AutoValue_SystemDialerStrictMode_StrictModeVmConfig.Builder();
115     }
116 
117     /** VmPolicy configuration builder. */
118     @AutoValue.Builder
119     public abstract static class Builder {
setMaxInstanceLimits(Map<Class<?>, Integer> limits)120       public abstract Builder setMaxInstanceLimits(Map<Class<?>, Integer> limits);
121 
build()122       public abstract StrictModeVmConfig build();
123 
Builder()124       Builder() {}
125     }
126 
StrictModeVmConfig()127     StrictModeVmConfig() {}
128   }
129 }
130