blob: b23dde1389f21beccd294140ffe9a3af0eee0d99 [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;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080029import android.content.Context;
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080030import android.content.SharedPreferences;
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080031import android.os.StrictMode;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080032import android.preference.PreferenceManager;
Makoto Onuki49627cc2011-08-28 13:56:48 -070033import android.util.Log;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080034
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080035public final class ContactsApplication extends Application {
Makoto Onuki7c91de12011-11-02 15:43:20 -070036 private static final boolean ENABLE_LOADER_LOG = false; // Don't submit with true
37 private static final boolean ENABLE_FRAGMENT_LOG = false; // Don't submit with true
38
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080039 private static InjectedServices sInjectedServices;
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080040 private AccountTypeManager mAccountTypeManager;
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080041 private ContactPhotoManager mContactPhotoManager;
Daisuke Miyakawaa012aec2011-10-16 10:24:43 -070042 private ContactListFilterController mContactListFilterController;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080043
44 /**
45 * Overrides the system services with mocks for testing.
46 */
Flavio Lerda37a26842011-06-27 11:36:52 +010047 @VisibleForTesting
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080048 public static void injectServices(InjectedServices services) {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080049 sInjectedServices = services;
50 }
51
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080052 public static InjectedServices getInjectedServices() {
53 return sInjectedServices;
54 }
55
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080056 @Override
57 public ContentResolver getContentResolver() {
58 if (sInjectedServices != null) {
59 ContentResolver resolver = sInjectedServices.getContentResolver();
60 if (resolver != null) {
61 return resolver;
62 }
63 }
64 return super.getContentResolver();
65 }
66
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080067 @Override
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080068 public SharedPreferences getSharedPreferences(String name, int mode) {
69 if (sInjectedServices != null) {
70 SharedPreferences prefs = sInjectedServices.getSharedPreferences();
71 if (prefs != null) {
72 return prefs;
73 }
74 }
75
76 return super.getSharedPreferences(name, mode);
77 }
78
79 @Override
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080080 public Object getSystemService(String name) {
81 if (sInjectedServices != null) {
82 Object service = sInjectedServices.getSystemService(name);
83 if (service != null) {
84 return service;
85 }
86 }
87
88 if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) {
89 if (mAccountTypeManager == null) {
90 mAccountTypeManager = AccountTypeManager.createAccountTypeManager(this);
91 }
92 return mAccountTypeManager;
93 }
94
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080095 if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) {
96 if (mContactPhotoManager == null) {
97 mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this);
Makoto Onuki8f8bd6d2011-11-08 14:09:01 -080098 registerComponentCallbacks(mContactPhotoManager);
Dmitri Plotnikov7edf2382011-01-31 16:15:48 -080099 mContactPhotoManager.preloadPhotosInBackground();
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -0800100 }
101 return mContactPhotoManager;
102 }
103
Daisuke Miyakawaa012aec2011-10-16 10:24:43 -0700104 if (ContactListFilterController.CONTACT_LIST_FILTER_SERVICE.equals(name)) {
105 if (mContactListFilterController == null) {
106 mContactListFilterController =
107 ContactListFilterController.createContactListFilterController(this);
108 }
109 return mContactListFilterController;
110 }
111
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -0800112 return super.getSystemService(name);
113 }
114
115 @Override
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800116 public void onCreate() {
117 super.onCreate();
118
Makoto Onuki49627cc2011-08-28 13:56:48 -0700119 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
120 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start");
121 }
122
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800123 // Priming caches to placate the StrictMode police
124 Context context = getApplicationContext();
125 PreferenceManager.getDefaultSharedPreferences(context);
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -0800126 AccountTypeManager.getInstance(context);
Makoto Onuki7c91de12011-11-02 15:43:20 -0700127 if (ENABLE_FRAGMENT_LOG) FragmentManager.enableDebugLogging(true);
128 if (ENABLE_LOADER_LOG) LoaderManager.enableDebugLogging(true);
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800129
Daisuke Miyakawab5c9f632011-09-21 16:05:18 -0700130 if (Log.isLoggable(Constants.STRICT_MODE_TAG, Log.DEBUG)) {
131 StrictMode.setThreadPolicy(
132 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
133 }
Makoto Onuki49627cc2011-08-28 13:56:48 -0700134
135 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
136 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish");
137 }
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800138 }
139}