blob: f806c1d7a2da3e6a2c29131d0d6b7ac652e85529 [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;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080026import android.content.ContentResolver;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080027import android.content.Context;
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080028import android.content.SharedPreferences;
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080029import android.os.StrictMode;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080030import android.preference.PreferenceManager;
Makoto Onuki49627cc2011-08-28 13:56:48 -070031import android.util.Log;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080032
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080033public final class ContactsApplication extends Application {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080034 private static InjectedServices sInjectedServices;
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080035 private AccountTypeManager mAccountTypeManager;
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080036 private ContactPhotoManager mContactPhotoManager;
Daisuke Miyakawaa012aec2011-10-16 10:24:43 -070037 private ContactListFilterController mContactListFilterController;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080038
39 /**
40 * Overrides the system services with mocks for testing.
41 */
Flavio Lerda37a26842011-06-27 11:36:52 +010042 @VisibleForTesting
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080043 public static void injectServices(InjectedServices services) {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080044 sInjectedServices = services;
45 }
46
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080047 public static InjectedServices getInjectedServices() {
48 return sInjectedServices;
49 }
50
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080051 @Override
52 public ContentResolver getContentResolver() {
53 if (sInjectedServices != null) {
54 ContentResolver resolver = sInjectedServices.getContentResolver();
55 if (resolver != null) {
56 return resolver;
57 }
58 }
59 return super.getContentResolver();
60 }
61
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080062 @Override
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080063 public SharedPreferences getSharedPreferences(String name, int mode) {
64 if (sInjectedServices != null) {
65 SharedPreferences prefs = sInjectedServices.getSharedPreferences();
66 if (prefs != null) {
67 return prefs;
68 }
69 }
70
71 return super.getSharedPreferences(name, mode);
72 }
73
74 @Override
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080075 public Object getSystemService(String name) {
76 if (sInjectedServices != null) {
77 Object service = sInjectedServices.getSystemService(name);
78 if (service != null) {
79 return service;
80 }
81 }
82
83 if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) {
84 if (mAccountTypeManager == null) {
85 mAccountTypeManager = AccountTypeManager.createAccountTypeManager(this);
86 }
87 return mAccountTypeManager;
88 }
89
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080090 if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) {
91 if (mContactPhotoManager == null) {
92 mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this);
Dmitri Plotnikov7edf2382011-01-31 16:15:48 -080093 mContactPhotoManager.preloadPhotosInBackground();
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080094 }
95 return mContactPhotoManager;
96 }
97
Daisuke Miyakawaa012aec2011-10-16 10:24:43 -070098 if (ContactListFilterController.CONTACT_LIST_FILTER_SERVICE.equals(name)) {
99 if (mContactListFilterController == null) {
100 mContactListFilterController =
101 ContactListFilterController.createContactListFilterController(this);
102 }
103 return mContactListFilterController;
104 }
105
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -0800106 return super.getSystemService(name);
107 }
108
109 @Override
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800110 public void onCreate() {
111 super.onCreate();
112
Makoto Onuki49627cc2011-08-28 13:56:48 -0700113 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
114 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start");
115 }
116
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800117 // Priming caches to placate the StrictMode police
118 Context context = getApplicationContext();
119 PreferenceManager.getDefaultSharedPreferences(context);
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -0800120 AccountTypeManager.getInstance(context);
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800121
Daisuke Miyakawab5c9f632011-09-21 16:05:18 -0700122 if (Log.isLoggable(Constants.STRICT_MODE_TAG, Log.DEBUG)) {
123 StrictMode.setThreadPolicy(
124 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
125 }
Makoto Onuki49627cc2011-08-28 13:56:48 -0700126
127 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
128 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish");
129 }
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800130 }
131}