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.Trace;
     21 import android.support.annotation.NonNull;
     22 import android.support.v4.os.BuildCompat;
     23 import com.android.dialer.blocking.BlockedNumbersAutoMigrator;
     24 import com.android.dialer.blocking.FilteredNumberAsyncQueryHandler;
     25 import com.android.dialer.calllog.CallLogComponent;
     26 import com.android.dialer.common.concurrent.DefaultFutureCallback;
     27 import com.android.dialer.common.concurrent.DialerExecutorComponent;
     28 import com.android.dialer.inject.HasRootComponent;
     29 import com.android.dialer.notification.NotificationChannelManager;
     30 import com.android.dialer.persistentlog.PersistentLogger;
     31 import com.android.dialer.strictmode.StrictModeComponent;
     32 import com.google.common.util.concurrent.Futures;
     33 import com.google.common.util.concurrent.MoreExecutors;
     34 
     35 /** A common application subclass for all Dialer build variants. */
     36 public abstract class DialerApplication extends Application implements HasRootComponent {
     37 
     38   private volatile Object rootComponent;
     39 
     40   @Override
     41   public void onCreate() {
     42     Trace.beginSection("DialerApplication.onCreate");
     43     StrictModeComponent.get(this).getDialerStrictMode().onApplicationCreate(this);
     44 
     45     super.onCreate();
     46     new BlockedNumbersAutoMigrator(
     47             this.getApplicationContext(),
     48             new FilteredNumberAsyncQueryHandler(this),
     49             DialerExecutorComponent.get(this).dialerExecutorFactory())
     50         .asyncAutoMigrate();
     51     CallLogComponent.get(this).callLogFramework().registerContentObservers(getApplicationContext());
     52     Futures.addCallback(
     53         CallLogComponent.get(this).getAnnotatedCallLogMigrator().migrate(),
     54         new DefaultFutureCallback<>(),
     55         MoreExecutors.directExecutor());
     56     PersistentLogger.initialize(this);
     57 
     58     if (BuildCompat.isAtLeastO()) {
     59       NotificationChannelManager.initChannels(this);
     60     }
     61     Trace.endSection();
     62   }
     63 
     64   /**
     65    * Returns a new instance of the root component for the application. Sub classes should define a
     66    * root component that extends all the sub components "HasComponent" intefaces. The component
     67    * should specify all modules that the application supports and provide stubs for the remainder.
     68    */
     69   @NonNull
     70   protected abstract Object buildRootComponent();
     71 
     72   /** Returns a cached instance of application's root component. */
     73   @Override
     74   @NonNull
     75   public final Object component() {
     76     Object result = rootComponent;
     77     if (result == null) {
     78       synchronized (this) {
     79         result = rootComponent;
     80         if (result == null) {
     81           rootComponent = result = buildRootComponent();
     82         }
     83       }
     84     }
     85     return result;
     86   }
     87 }
     88