blob: 16f9e575c743c0d5cfb4580d10c366e86b53d8ae [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);
Dmitri Plotnikov7edf2382011-01-31 16:15:48 -080098 mContactPhotoManager.preloadPhotosInBackground();
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080099 }
100 return mContactPhotoManager;
101 }
102
Daisuke Miyakawaa012aec2011-10-16 10:24:43 -0700103 if (ContactListFilterController.CONTACT_LIST_FILTER_SERVICE.equals(name)) {
104 if (mContactListFilterController == null) {
105 mContactListFilterController =
106 ContactListFilterController.createContactListFilterController(this);
107 }
108 return mContactListFilterController;
109 }
110
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -0800111 return super.getSystemService(name);
112 }
113
114 @Override
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800115 public void onCreate() {
116 super.onCreate();
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 start");
120 }
121
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800122 // Priming caches to placate the StrictMode police
123 Context context = getApplicationContext();
124 PreferenceManager.getDefaultSharedPreferences(context);
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -0800125 AccountTypeManager.getInstance(context);
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
134 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
135 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish");
136 }
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800137 }
138}