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