blob: f8a1f20bb795f12ca1e4fe61779af845bbc7718d [file] [log] [blame]
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -08001/*
2 * Copyright (C) 2010 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.contacts;
18
19import android.app.Application;
Makoto Onuki7c91de12011-11-02 15:43:20 -070020import android.app.FragmentManager;
21import android.app.LoaderManager;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080022import android.content.ContentResolver;
Makoto Onukifc0a89f2012-05-14 17:37:34 -070023import android.content.ContentUris;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080024import android.content.Context;
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080025import android.content.SharedPreferences;
Makoto Onukifc0a89f2012-05-14 17:37:34 -070026import android.os.AsyncTask;
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080027import android.os.StrictMode;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080028import android.preference.PreferenceManager;
Makoto Onukifc0a89f2012-05-14 17:37:34 -070029import android.provider.ContactsContract.Contacts;
Makoto Onuki49627cc2011-08-28 13:56:48 -070030import android.util.Log;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080031
Yorke Lee70396332014-05-28 12:16:35 -070032import com.android.contacts.common.testing.InjectedServices;
Chiao Chengd9eab4d2012-11-14 18:24:25 -080033import com.android.contacts.common.util.Constants;
Walter Jange837ae32016-08-15 12:50:37 -070034import com.android.contactsbind.analytics.AnalyticsUtil;
Brian Attwella3e498a2014-10-27 10:46:24 -070035
Tingting Wang9deead62016-03-28 10:08:08 -070036import com.android.contacts.common.testing.NeededForTesting;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070037import com.google.common.annotations.VisibleForTesting;
38
Tingting Wang9deead62016-03-28 10:08:08 -070039@NeededForTesting
Walter Jang457f8652016-01-08 09:47:24 -080040public class ContactsApplication extends Application {
Makoto Onuki7c91de12011-11-02 15:43:20 -070041 private static final boolean ENABLE_LOADER_LOG = false; // Don't submit with true
42 private static final boolean ENABLE_FRAGMENT_LOG = false; // Don't submit with true
43
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080044 private static InjectedServices sInjectedServices;
Chiao Chengd9eab4d2012-11-14 18:24:25 -080045 /**
46 * Log tag for enabling/disabling StrictMode violation log.
47 * To enable: adb shell setprop log.tag.ContactsStrictMode DEBUG
48 */
49 public static final String STRICT_MODE_TAG = "ContactsStrictMode";
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080050
51 /**
52 * Overrides the system services with mocks for testing.
53 */
Flavio Lerda37a26842011-06-27 11:36:52 +010054 @VisibleForTesting
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080055 public static void injectServices(InjectedServices services) {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080056 sInjectedServices = services;
57 }
58
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080059 public static InjectedServices getInjectedServices() {
60 return sInjectedServices;
61 }
62
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080063 @Override
64 public ContentResolver getContentResolver() {
65 if (sInjectedServices != null) {
66 ContentResolver resolver = sInjectedServices.getContentResolver();
67 if (resolver != null) {
68 return resolver;
69 }
70 }
71 return super.getContentResolver();
72 }
73
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080074 @Override
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080075 public SharedPreferences getSharedPreferences(String name, int mode) {
76 if (sInjectedServices != null) {
77 SharedPreferences prefs = sInjectedServices.getSharedPreferences();
78 if (prefs != null) {
79 return prefs;
80 }
81 }
82
83 return super.getSharedPreferences(name, mode);
84 }
85
86 @Override
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080087 public Object getSystemService(String name) {
88 if (sInjectedServices != null) {
89 Object service = sInjectedServices.getSystemService(name);
90 if (service != null) {
91 return service;
92 }
93 }
94
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080095 return super.getSystemService(name);
96 }
97
98 @Override
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080099 public void onCreate() {
100 super.onCreate();
101
Makoto Onuki49627cc2011-08-28 13:56:48 -0700102 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
103 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start");
104 }
105
Makoto Onuki7c91de12011-11-02 15:43:20 -0700106 if (ENABLE_FRAGMENT_LOG) FragmentManager.enableDebugLogging(true);
107 if (ENABLE_LOADER_LOG) LoaderManager.enableDebugLogging(true);
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800108
Chiao Chengd9eab4d2012-11-14 18:24:25 -0800109 if (Log.isLoggable(STRICT_MODE_TAG, Log.DEBUG)) {
Daisuke Miyakawab5c9f632011-09-21 16:05:18 -0700110 StrictMode.setThreadPolicy(
111 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
112 }
Makoto Onuki49627cc2011-08-28 13:56:48 -0700113
Makoto Onukifc0a89f2012-05-14 17:37:34 -0700114 // Perform the initialization that doesn't have to finish immediately.
115 // We use an async task here just to avoid creating a new thread.
116 (new DelayedInitializer()).execute();
117
Makoto Onuki49627cc2011-08-28 13:56:48 -0700118 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
119 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish");
120 }
Brian Attwella3e498a2014-10-27 10:46:24 -0700121
122 AnalyticsUtil.initialize(this);
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800123 }
Makoto Onukifc0a89f2012-05-14 17:37:34 -0700124
125 private class DelayedInitializer extends AsyncTask<Void, Void, Void> {
126 @Override
127 protected Void doInBackground(Void... params) {
128 final Context context = ContactsApplication.this;
129
Walter Jangdccfa942015-07-23 15:08:00 -0700130 // Warm up the preferences and the contacts provider. We delay initialization
131 // of the account type manager because we may not have the contacts group permission
132 // (and thus not have the get accounts permission).
Makoto Onukifc0a89f2012-05-14 17:37:34 -0700133 PreferenceManager.getDefaultSharedPreferences(context);
Makoto Onukifc0a89f2012-05-14 17:37:34 -0700134 getContentResolver().getType(ContentUris.withAppendedId(Contacts.CONTENT_URI, 1));
Walter Jangdccfa942015-07-23 15:08:00 -0700135
Makoto Onukifc0a89f2012-05-14 17:37:34 -0700136 return null;
137 }
138
139 public void execute() {
140 executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
141 (Void[]) null);
142 }
143 }
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800144}