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.tv.common; 18 19 import android.annotation.TargetApi; 20 import android.content.Context; 21 import android.os.Build; 22 import android.os.StrictMode; 23 import android.support.annotation.VisibleForTesting; 24 25 import com.android.tv.common.dev.DeveloperPreferences; 26 import com.android.tv.common.feature.CommonFeatures; 27 import com.android.tv.common.recording.RecordingStorageStatusManager; 28 import com.android.tv.common.util.Clock; 29 import com.android.tv.common.util.CommonUtils; 30 import com.android.tv.common.util.Debug; 31 32 import dagger.Lazy; 33 import dagger.android.DaggerApplication; 34 35 import javax.inject.Inject; 36 37 /** The base application class for TV applications. */ 38 public abstract class BaseApplication extends DaggerApplication implements BaseSingletons { 39 @Inject Lazy<RecordingStorageStatusManager> mRecordingStorageStatusManager; 40 41 /** 42 * An instance of {@link BaseSingletons}. Note that this can be set directly only for the test 43 * purpose. 44 */ 45 @VisibleForTesting public static BaseSingletons sSingletons; 46 47 /** 48 * Returns the {@link BaseSingletons} using the application context. 49 * 50 * @deprecated use {@link com.android.tv.common.singletons.HasSingletons#get(Class, Context)} 51 * instead 52 */ 53 @Deprecated getSingletons(Context context)54 public static BaseSingletons getSingletons(Context context) { 55 // STOP-SHIP: changing the method to protected once the Tuner application is created. 56 // No need to be "synchronized" because this doesn't create any instance. 57 if (sSingletons == null) { 58 sSingletons = (BaseSingletons) context.getApplicationContext(); 59 } 60 return sSingletons; 61 } 62 63 @Override onCreate()64 public void onCreate() { 65 super.onCreate(); 66 Debug.getTimer(Debug.TAG_START_UP_TIMER).start(); 67 Debug.getTimer(Debug.TAG_START_UP_TIMER) 68 .log("Start " + this.getClass().getSimpleName() + ".onCreate"); 69 CommonPreferences.initialize(this); 70 71 // Only set StrictMode for ENG builds because the build server only produces userdebug 72 // builds. 73 if (BuildConfig.ENG && DeveloperPreferences.ALLOW_STRICT_MODE.get(this)) { 74 StrictMode.ThreadPolicy.Builder threadPolicyBuilder = 75 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog(); 76 // TODO(b/69565157): Turn penaltyDeath on for VMPolicy when tests are fixed. 77 // TODO(b/120840665): Restore detecting untagged network sockets 78 StrictMode.VmPolicy.Builder vmPolicyBuilder = 79 new StrictMode.VmPolicy.Builder() 80 .detectActivityLeaks() 81 .detectLeakedClosableObjects() 82 .detectLeakedRegistrationObjects() 83 .detectFileUriExposure() 84 .detectContentUriWithoutPermission() 85 .penaltyLog(); 86 87 if (!CommonUtils.isRunningInTest()) { 88 threadPolicyBuilder.penaltyDialog(); 89 } 90 StrictMode.setThreadPolicy(threadPolicyBuilder.build()); 91 StrictMode.setVmPolicy(vmPolicyBuilder.build()); 92 } 93 if (CommonFeatures.DVR.isEnabled(this)) { 94 getRecordingStorageStatusManager(); 95 } 96 } 97 98 @Override getClock()99 public Clock getClock() { 100 return Clock.SYSTEM; 101 } 102 103 /** Returns {@link RecordingStorageStatusManager}. */ 104 @Override 105 @TargetApi(Build.VERSION_CODES.N) getRecordingStorageStatusManager()106 public RecordingStorageStatusManager getRecordingStorageStatusManager() { 107 return mRecordingStorageStatusManager.get(); 108 } 109 } 110