ContactsApplication.java revision 37a2684d1e47f6d78d757c437a187548f242ee13
1/* 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 19import com.android.contacts.model.AccountTypeManager; 20import com.android.contacts.test.InjectedServices; 21import com.google.common.annotations.VisibleForTesting; 22 23import android.app.Application; 24import android.app.LoaderManager; 25import android.content.ContentResolver; 26import android.content.Context; 27import android.content.SharedPreferences; 28import android.os.StrictMode; 29import android.preference.PreferenceManager; 30 31public final class ContactsApplication extends Application { 32 33 private static InjectedServices sInjectedServices; 34 private AccountTypeManager mAccountTypeManager; 35 private ContactPhotoManager mContactPhotoManager; 36 37 /** 38 * Overrides the system services with mocks for testing. 39 */ 40 @VisibleForTesting 41 public static void injectServices(InjectedServices services) { 42 sInjectedServices = services; 43 } 44 45 public static InjectedServices getInjectedServices() { 46 return sInjectedServices; 47 } 48 49 @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 60 @Override 61 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 73 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 88 if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) { 89 if (mContactPhotoManager == null) { 90 mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this); 91 mContactPhotoManager.preloadPhotosInBackground(); 92 } 93 return mContactPhotoManager; 94 } 95 96 return super.getSystemService(name); 97 } 98 99 @Override 100 public void onCreate() { 101 super.onCreate(); 102 103 // Priming caches to placate the StrictMode police 104 Context context = getApplicationContext(); 105 PreferenceManager.getDefaultSharedPreferences(context); 106 AccountTypeManager.getInstance(context); 107 LoaderManager.enableDebugLogging(true); 108 109 StrictMode.setThreadPolicy( 110 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); 111 } 112} 113