Home | History | Annotate | Download | only in common
      1 /*
      2  * Copyright (C) 2013 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.binary.common;
     18 
     19 import android.app.Application;
     20 import android.os.StrictMode;
     21 import android.os.Trace;
     22 import android.support.annotation.NonNull;
     23 import android.support.v4.os.BuildCompat;
     24 import com.android.dialer.blocking.BlockedNumbersAutoMigrator;
     25 import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
     26 import com.android.dialer.buildtype.BuildType;
     27 import com.android.dialer.calllog.CallLogComponent;
     28 import com.android.dialer.common.concurrent.DefaultDialerExecutorFactory;
     29 import com.android.dialer.inject.HasRootComponent;
     30 import com.android.dialer.notification.NotificationChannelManager;
     31 import com.android.dialer.persistentlog.PersistentLogger;
     32 
     33 /** A common application subclass for all Dialer build variants. */
     34 public abstract class DialerApplication extends Application implements HasRootComponent {
     35 
     36   private volatile Object rootComponent;
     37 
     38   @Override
     39   public void onCreate() {
     40     Trace.beginSection("DialerApplication.onCreate");
     41     if (BuildType.get() == BuildType.BUGFOOD) {
     42       enableStrictMode();
     43     }
     44     super.onCreate();
     45     new BlockedNumbersAutoMigrator(
     46             this.getApplicationContext(),
     47             new FilteredNumberAsyncQueryHandler(this),
     48             new DefaultDialerExecutorFactory())
     49         .asyncAutoMigrate();
     50     CallLogComponent.get(this).callLogFramework().registerContentObservers(getApplicationContext());
     51     PersistentLogger.initialize(this);
     52 
     53     if (BuildCompat.isAtLeastO()) {
     54       NotificationChannelManager.initChannels(this);
     55     }
     56     Trace.endSection();
     57   }
     58 
     59   private void enableStrictMode() {
     60     StrictMode.setThreadPolicy(
     61         new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().penaltyDeath().build());
     62     StrictMode.setVmPolicy(
     63         new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().penaltyDeath().build());
     64   }
     65 
     66   /**
     67    * Returns a new instance of the root component for the application. Sub classes should define a
     68    * root component that extends all the sub components "HasComponent" intefaces. The component
     69    * should specify all modules that the application supports and provide stubs for the remainder.
     70    */
     71   @NonNull
     72   protected abstract Object buildRootComponent();
     73 
     74   /** Returns a cached instance of application's root component. */
     75   @Override
     76   @NonNull
     77   public final Object component() {
     78     Object result = rootComponent;
     79     if (result == null) {
     80       synchronized (this) {
     81         result = rootComponent;
     82         if (result == null) {
     83           rootComponent = result = buildRootComponent();
     84         }
     85       }
     86     }
     87     return result;
     88   }
     89 }
     90