blob: eb8ffa984d0e122a60d4b6f74372c446ce24a3e5 [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
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -080019import com.android.contacts.model.AccountTypeManager;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080020import com.android.contacts.test.InjectedServices;
Makoto Onuki49627cc2011-08-28 13:56:48 -070021import com.android.contacts.util.Constants;
Flavio Lerda37a26842011-06-27 11:36:52 +010022import com.google.common.annotations.VisibleForTesting;
Dmitri Plotnikov169ff2a2010-11-29 16:58:31 -080023
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080024import android.app.Application;
Daisuke Miyakawab5c9f632011-09-21 16:05:18 -070025import android.app.FragmentManager;
Dmitri Plotnikov53a04d62011-01-25 12:04:59 -080026import android.app.LoaderManager;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080027import android.content.ContentResolver;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080028import android.content.Context;
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080029import android.content.SharedPreferences;
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080030import android.os.StrictMode;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080031import android.preference.PreferenceManager;
Makoto Onuki49627cc2011-08-28 13:56:48 -070032import android.util.Log;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080033
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080034public final class ContactsApplication extends Application {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080035 private static InjectedServices sInjectedServices;
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080036 private AccountTypeManager mAccountTypeManager;
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080037 private ContactPhotoManager mContactPhotoManager;
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
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080098 return super.getSystemService(name);
99 }
100
101 @Override
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800102 public void onCreate() {
103 super.onCreate();
104
Makoto Onuki49627cc2011-08-28 13:56:48 -0700105 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
106 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start");
107 }
108
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800109 // Priming caches to placate the StrictMode police
110 Context context = getApplicationContext();
111 PreferenceManager.getDefaultSharedPreferences(context);
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -0800112 AccountTypeManager.getInstance(context);
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800113
Daisuke Miyakawab5c9f632011-09-21 16:05:18 -0700114 if (Log.isLoggable(Constants.STRICT_MODE_TAG, Log.DEBUG)) {
115 StrictMode.setThreadPolicy(
116 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
117 }
Makoto Onuki49627cc2011-08-28 13:56:48 -0700118
119 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
120 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish");
121 }
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800122 }
123}