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