blob: c925ec01eaecb6ed166ac211762b61abacd65252 [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;
Dmitri Plotnikov169ff2a2010-11-29 16:58:31 -080021
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080022import android.app.Application;
Dmitri Plotnikov53a04d62011-01-25 12:04:59 -080023import android.app.LoaderManager;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080024import android.content.ContentResolver;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080025import android.content.Context;
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080026import android.content.SharedPreferences;
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080027import android.os.StrictMode;
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -080028import android.preference.PreferenceManager;
29
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080030public final class ContactsApplication extends Application {
31
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080032 private static InjectedServices sInjectedServices;
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080033 private AccountTypeManager mAccountTypeManager;
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080034 private ContactPhotoManager mContactPhotoManager;
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080035
36 /**
37 * Overrides the system services with mocks for testing.
38 */
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080039 public static void injectServices(InjectedServices services) {
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080040 sInjectedServices = services;
41 }
42
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080043 public static InjectedServices getInjectedServices() {
44 return sInjectedServices;
45 }
46
Dmitri Plotnikov43fd1e82011-01-09 11:29:39 -080047 @Override
48 public ContentResolver getContentResolver() {
49 if (sInjectedServices != null) {
50 ContentResolver resolver = sInjectedServices.getContentResolver();
51 if (resolver != null) {
52 return resolver;
53 }
54 }
55 return super.getContentResolver();
56 }
57
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080058 @Override
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080059 public SharedPreferences getSharedPreferences(String name, int mode) {
60 if (sInjectedServices != null) {
61 SharedPreferences prefs = sInjectedServices.getSharedPreferences();
62 if (prefs != null) {
63 return prefs;
64 }
65 }
66
67 return super.getSharedPreferences(name, mode);
68 }
69
70 @Override
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080071 public Object getSystemService(String name) {
72 if (sInjectedServices != null) {
73 Object service = sInjectedServices.getSystemService(name);
74 if (service != null) {
75 return service;
76 }
77 }
78
79 if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) {
80 if (mAccountTypeManager == null) {
81 mAccountTypeManager = AccountTypeManager.createAccountTypeManager(this);
82 }
83 return mAccountTypeManager;
84 }
85
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080086 if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) {
87 if (mContactPhotoManager == null) {
88 mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this);
Dmitri Plotnikov7edf2382011-01-31 16:15:48 -080089 mContactPhotoManager.preloadPhotosInBackground();
Dmitri Plotnikov34b24ef2011-01-28 13:41:07 -080090 }
91 return mContactPhotoManager;
92 }
93
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080094 return super.getSystemService(name);
95 }
96
97 @Override
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -080098 public void onCreate() {
99 super.onCreate();
100
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800101 // Priming caches to placate the StrictMode police
102 Context context = getApplicationContext();
103 PreferenceManager.getDefaultSharedPreferences(context);
Dmitri Plotnikova07fa5f2011-01-09 11:56:50 -0800104 AccountTypeManager.getInstance(context);
Dmitri Plotnikov53a04d62011-01-25 12:04:59 -0800105 LoaderManager.enableDebugLogging(true);
Dmitri Plotnikov3b7dedd2010-11-29 14:46:10 -0800106
Dmitri Plotnikovf049ff02010-11-29 10:15:24 -0800107 StrictMode.setThreadPolicy(
108 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
109 }
110}