blob: 1c8c080f93c17a7b9cfe8e12889b78ff98c3a72b [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;
Flavio Lerda37a26842011-06-27 11:36:52 +010021import com.google.common.annotations.VisibleForTesting;
Dmitri Plotnikov169ff2a2010-11-29 16:58:31 -080022
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080023import android.app.Application;
Dmitri Plotnikov53a04d62011-01-25 12:04:59 -080024import android.app.LoaderManager;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080025import android.content.ContentResolver;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080026import android.content.Context;
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080027import android.content.SharedPreferences;
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080028import android.os.StrictMode;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080029import android.preference.PreferenceManager;
30
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080031public final class ContactsApplication extends Application {
32
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080033 private static InjectedServices sInjectedServices;
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080034 private AccountTypeManager mAccountTypeManager;
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080035 private ContactPhotoManager mContactPhotoManager;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080036
37 /**
38 * Overrides the system services with mocks for testing.
39 */
Flavio Lerda37a26842011-06-27 11:36:52 +010040 @VisibleForTesting
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080041 public static void injectServices(InjectedServices services) {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080042 sInjectedServices = services;
43 }
44
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080045 public static InjectedServices getInjectedServices() {
46 return sInjectedServices;
47 }
48
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080049 @Override
50 public ContentResolver getContentResolver() {
51 if (sInjectedServices != null) {
52 ContentResolver resolver = sInjectedServices.getContentResolver();
53 if (resolver != null) {
54 return resolver;
55 }
56 }
57 return super.getContentResolver();
58 }
59
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080060 @Override
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080061 public SharedPreferences getSharedPreferences(String name, int mode) {
62 if (sInjectedServices != null) {
63 SharedPreferences prefs = sInjectedServices.getSharedPreferences();
64 if (prefs != null) {
65 return prefs;
66 }
67 }
68
69 return super.getSharedPreferences(name, mode);
70 }
71
72 @Override
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080073 public Object getSystemService(String name) {
74 if (sInjectedServices != null) {
75 Object service = sInjectedServices.getSystemService(name);
76 if (service != null) {
77 return service;
78 }
79 }
80
81 if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) {
82 if (mAccountTypeManager == null) {
83 mAccountTypeManager = AccountTypeManager.createAccountTypeManager(this);
84 }
85 return mAccountTypeManager;
86 }
87
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080088 if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) {
89 if (mContactPhotoManager == null) {
90 mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this);
Dmitri Plotnikov7edf2382011-01-31 16:15:48 -080091 mContactPhotoManager.preloadPhotosInBackground();
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080092 }
93 return mContactPhotoManager;
94 }
95
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080096 return super.getSystemService(name);
97 }
98
99 @Override
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800100 public void onCreate() {
101 super.onCreate();
102
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800103 // Priming caches to placate the StrictMode police
104 Context context = getApplicationContext();
105 PreferenceManager.getDefaultSharedPreferences(context);
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -0800106 AccountTypeManager.getInstance(context);
Dmitri Plotnikov53a04d62011-01-25 12:04:59 -0800107 LoaderManager.enableDebugLogging(true);
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800108
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800109 StrictMode.setThreadPolicy(
110 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
111 }
112}