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