blob: 956ce65404510ab4a451761c637fab5994687ff2 [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
Daisuke Miyakawaa012aec2011-10-16 10:24:43 -070019import com.android.contacts.list.ContactListFilterController;
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -080020import com.android.contacts.model.AccountTypeManager;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080021import com.android.contacts.test.InjectedServices;
Makoto Onuki49627cc2011-08-28 13:56:48 -070022import com.android.contacts.util.Constants;
Flavio Lerda37a26842011-06-27 11:36:52 +010023import com.google.common.annotations.VisibleForTesting;
Dmitri Plotnikov169ff2a2010-11-29 16:58:31 -080024
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080025import android.app.Application;
Makoto Onuki7c91de12011-11-02 15:43:20 -070026import android.app.FragmentManager;
27import android.app.LoaderManager;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080028import android.content.ContentResolver;
Makoto Onukifc0a89f2012-05-14 17:37:34 -070029import android.content.ContentUris;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080030import android.content.Context;
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080031import android.content.SharedPreferences;
Makoto Onukifc0a89f2012-05-14 17:37:34 -070032import android.os.AsyncTask;
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080033import android.os.StrictMode;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080034import android.preference.PreferenceManager;
Makoto Onukifc0a89f2012-05-14 17:37:34 -070035import android.provider.ContactsContract.Contacts;
Makoto Onuki49627cc2011-08-28 13:56:48 -070036import android.util.Log;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080037
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080038public final class ContactsApplication extends Application {
Makoto Onuki7c91de12011-11-02 15:43:20 -070039 private static final boolean ENABLE_LOADER_LOG = false; // Don't submit with true
40 private static final boolean ENABLE_FRAGMENT_LOG = false; // Don't submit with true
41
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080042 private static InjectedServices sInjectedServices;
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080043 private AccountTypeManager mAccountTypeManager;
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080044 private ContactPhotoManager mContactPhotoManager;
Daisuke Miyakawaa012aec2011-10-16 10:24:43 -070045 private ContactListFilterController mContactListFilterController;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080046
47 /**
48 * Overrides the system services with mocks for testing.
49 */
Flavio Lerda37a26842011-06-27 11:36:52 +010050 @VisibleForTesting
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080051 public static void injectServices(InjectedServices services) {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080052 sInjectedServices = services;
53 }
54
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080055 public static InjectedServices getInjectedServices() {
56 return sInjectedServices;
57 }
58
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080059 @Override
60 public ContentResolver getContentResolver() {
61 if (sInjectedServices != null) {
62 ContentResolver resolver = sInjectedServices.getContentResolver();
63 if (resolver != null) {
64 return resolver;
65 }
66 }
67 return super.getContentResolver();
68 }
69
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080070 @Override
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080071 public SharedPreferences getSharedPreferences(String name, int mode) {
72 if (sInjectedServices != null) {
73 SharedPreferences prefs = sInjectedServices.getSharedPreferences();
74 if (prefs != null) {
75 return prefs;
76 }
77 }
78
79 return super.getSharedPreferences(name, mode);
80 }
81
82 @Override
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080083 public Object getSystemService(String name) {
84 if (sInjectedServices != null) {
85 Object service = sInjectedServices.getSystemService(name);
86 if (service != null) {
87 return service;
88 }
89 }
90
91 if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) {
92 if (mAccountTypeManager == null) {
93 mAccountTypeManager = AccountTypeManager.createAccountTypeManager(this);
94 }
95 return mAccountTypeManager;
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
Daisuke Miyakawaa012aec2011-10-16 10:24:43 -0700107 if (ContactListFilterController.CONTACT_LIST_FILTER_SERVICE.equals(name)) {
108 if (mContactListFilterController == null) {
109 mContactListFilterController =
110 ContactListFilterController.createContactListFilterController(this);
111 }
112 return mContactListFilterController;
113 }
114
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -0800115 return super.getSystemService(name);
116 }
117
118 @Override
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800119 public void onCreate() {
120 super.onCreate();
121
Makoto Onuki49627cc2011-08-28 13:56:48 -0700122 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
123 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start");
124 }
125
Makoto Onuki7c91de12011-11-02 15:43:20 -0700126 if (ENABLE_FRAGMENT_LOG) FragmentManager.enableDebugLogging(true);
127 if (ENABLE_LOADER_LOG) LoaderManager.enableDebugLogging(true);
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800128
Daisuke Miyakawab5c9f632011-09-21 16:05:18 -0700129 if (Log.isLoggable(Constants.STRICT_MODE_TAG, Log.DEBUG)) {
130 StrictMode.setThreadPolicy(
131 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
132 }
Makoto Onuki49627cc2011-08-28 13:56:48 -0700133
Makoto Onukifc0a89f2012-05-14 17:37:34 -0700134 // Perform the initialization that doesn't have to finish immediately.
135 // We use an async task here just to avoid creating a new thread.
136 (new DelayedInitializer()).execute();
137
Makoto Onuki49627cc2011-08-28 13:56:48 -0700138 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
139 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish");
140 }
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800141 }
Makoto Onukifc0a89f2012-05-14 17:37:34 -0700142
143 private class DelayedInitializer extends AsyncTask<Void, Void, Void> {
144 @Override
145 protected Void doInBackground(Void... params) {
146 final Context context = ContactsApplication.this;
147
148 // Warm up the preferences, the account type manager and the contacts provider.
149 PreferenceManager.getDefaultSharedPreferences(context);
150 AccountTypeManager.getInstance(context);
151 getContentResolver().getType(ContentUris.withAppendedId(Contacts.CONTENT_URI, 1));
152 return null;
153 }
154
155 public void execute() {
156 executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
157 (Void[]) null);
158 }
159 }
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800160}