ContactsApplication.java revision b5c9f63a8e32e0eab77daf98661d318f6248eb7d
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.android.contacts.util.Constants; 22import com.google.common.annotations.VisibleForTesting; 23 24import android.app.Application; 25import android.app.FragmentManager; 26import android.app.LoaderManager; 27import android.content.ContentResolver; 28import android.content.Context; 29import android.content.SharedPreferences; 30import android.os.StrictMode; 31import android.preference.PreferenceManager; 32import android.util.Log; 33 34public final class ContactsApplication extends Application { 35 private static InjectedServices sInjectedServices; 36 private AccountTypeManager mAccountTypeManager; 37 private ContactPhotoManager mContactPhotoManager; 38 39 /** 40 * Overrides the system services with mocks for testing. 41 */ 42 @VisibleForTesting 43 public static void injectServices(InjectedServices services) { 44 sInjectedServices = services; 45 } 46 47 public static InjectedServices getInjectedServices() { 48 return sInjectedServices; 49 } 50 51 @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 62 @Override 63 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 75 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 90 if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) { 91 if (mContactPhotoManager == null) { 92 mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this); 93 mContactPhotoManager.preloadPhotosInBackground(); 94 } 95 return mContactPhotoManager; 96 } 97 98 return super.getSystemService(name); 99 } 100 101 @Override 102 public void onCreate() { 103 super.onCreate(); 104 105 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) { 106 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start"); 107 } 108 109 // Priming caches to placate the StrictMode police 110 Context context = getApplicationContext(); 111 PreferenceManager.getDefaultSharedPreferences(context); 112 AccountTypeManager.getInstance(context); 113 114 LoaderManager.enableDebugLogging(Log.isLoggable(Constants.LOADER_MANAGER_TAG, Log.DEBUG)); 115 FragmentManager.enableDebugLogging( 116 Log.isLoggable(Constants.FRAGMENT_MANAGER_TAG, Log.DEBUG)); 117 if (Log.isLoggable(Constants.STRICT_MODE_TAG, Log.DEBUG)) { 118 StrictMode.setThreadPolicy( 119 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); 120 } 121 122 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) { 123 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish"); 124 } 125 } 126} 127