blob: ff6a1aed3362907e38ac1ba2740549b099bb43fd [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
Chiao Cheng79a6b5d2012-10-16 13:17:28 -070032import com.android.contacts.common.ContactPhotoManager;
Chiao Cheng0d5588d2012-11-26 15:34:14 -080033import com.android.contacts.common.list.ContactListFilterController;
34import com.android.contacts.common.model.AccountTypeManager;
Yorke Lee70396332014-05-28 12:16:35 -070035import com.android.contacts.common.testing.InjectedServices;
Chiao Chengd9eab4d2012-11-14 18:24:25 -080036import com.android.contacts.common.util.Constants;
Brian Attwella3e498a2014-10-27 10:46:24 -070037import com.android.contacts.commonbind.analytics.AnalyticsUtil;
38
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070039import com.google.common.annotations.VisibleForTesting;
40
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080041public final class ContactsApplication extends Application {
Makoto Onuki7c91de12011-11-02 15:43:20 -070042 private static final boolean ENABLE_LOADER_LOG = false; // Don't submit with true
43 private static final boolean ENABLE_FRAGMENT_LOG = false; // Don't submit with true
44
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080045 private static InjectedServices sInjectedServices;
Chiao Chengd9eab4d2012-11-14 18:24:25 -080046 /**
47 * Log tag for enabling/disabling StrictMode violation log.
48 * To enable: adb shell setprop log.tag.ContactsStrictMode DEBUG
49 */
50 public static final String STRICT_MODE_TAG = "ContactsStrictMode";
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080051 private ContactPhotoManager mContactPhotoManager;
Daisuke Miyakawaa012aec2011-10-16 10:24:43 -070052 private ContactListFilterController mContactListFilterController;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080053
54 /**
55 * Overrides the system services with mocks for testing.
56 */
Flavio Lerda37a26842011-06-27 11:36:52 +010057 @VisibleForTesting
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080058 public static void injectServices(InjectedServices services) {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080059 sInjectedServices = services;
60 }
61
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080062 public static InjectedServices getInjectedServices() {
63 return sInjectedServices;
64 }
65
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080066 @Override
67 public ContentResolver getContentResolver() {
68 if (sInjectedServices != null) {
69 ContentResolver resolver = sInjectedServices.getContentResolver();
70 if (resolver != null) {
71 return resolver;
72 }
73 }
74 return super.getContentResolver();
75 }
76
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080077 @Override
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080078 public SharedPreferences getSharedPreferences(String name, int mode) {
79 if (sInjectedServices != null) {
80 SharedPreferences prefs = sInjectedServices.getSharedPreferences();
81 if (prefs != null) {
82 return prefs;
83 }
84 }
85
86 return super.getSharedPreferences(name, mode);
87 }
88
89 @Override
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080090 public Object getSystemService(String name) {
91 if (sInjectedServices != null) {
92 Object service = sInjectedServices.getSystemService(name);
93 if (service != null) {
94 return service;
95 }
96 }
97
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080098 if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) {
99 if (mContactPhotoManager == null) {
100 mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this);
Makoto Onuki8f8bd6d2011-11-08 14:09:01 -0800101 registerComponentCallbacks(mContactPhotoManager);
Dmitri Plotnikov7edf2382011-01-31 16:15:48 -0800102 mContactPhotoManager.preloadPhotosInBackground();
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -0800103 }
104 return mContactPhotoManager;
105 }
106
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -0800107 return super.getSystemService(name);
108 }
109
110 @Override
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800111 public void onCreate() {
112 super.onCreate();
113
Makoto Onuki49627cc2011-08-28 13:56:48 -0700114 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
115 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start");
116 }
117
Makoto Onuki7c91de12011-11-02 15:43:20 -0700118 if (ENABLE_FRAGMENT_LOG) FragmentManager.enableDebugLogging(true);
119 if (ENABLE_LOADER_LOG) LoaderManager.enableDebugLogging(true);
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800120
Chiao Chengd9eab4d2012-11-14 18:24:25 -0800121 if (Log.isLoggable(STRICT_MODE_TAG, Log.DEBUG)) {
Daisuke Miyakawab5c9f632011-09-21 16:05:18 -0700122 StrictMode.setThreadPolicy(
123 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
124 }
Makoto Onuki49627cc2011-08-28 13:56:48 -0700125
Makoto Onukifc0a89f2012-05-14 17:37:34 -0700126 // Perform the initialization that doesn't have to finish immediately.
127 // We use an async task here just to avoid creating a new thread.
128 (new DelayedInitializer()).execute();
129
Makoto Onuki49627cc2011-08-28 13:56:48 -0700130 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
131 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish");
132 }
Brian Attwella3e498a2014-10-27 10:46:24 -0700133
134 AnalyticsUtil.initialize(this);
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800135 }
Makoto Onukifc0a89f2012-05-14 17:37:34 -0700136
137 private class DelayedInitializer extends AsyncTask<Void, Void, Void> {
138 @Override
139 protected Void doInBackground(Void... params) {
140 final Context context = ContactsApplication.this;
141
142 // Warm up the preferences, the account type manager and the contacts provider.
143 PreferenceManager.getDefaultSharedPreferences(context);
144 AccountTypeManager.getInstance(context);
145 getContentResolver().getType(ContentUris.withAppendedId(Contacts.CONTENT_URI, 1));
146 return null;
147 }
148
149 public void execute() {
150 executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
151 (Void[]) null);
152 }
153 }
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800154}