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 17 package com.android.contacts; 18 19 import android.app.Application; 20 import android.app.FragmentManager; 21 import android.app.LoaderManager; 22 import android.content.ContentResolver; 23 import android.content.ContentUris; 24 import android.content.Context; 25 import android.content.SharedPreferences; 26 import android.os.AsyncTask; 27 import android.os.StrictMode; 28 import android.preference.PreferenceManager; 29 import android.provider.ContactsContract.Contacts; 30 import android.util.Log; 31 32 import com.android.contacts.common.testing.InjectedServices; 33 import com.android.contacts.common.util.Constants; 34 import com.android.contacts.commonbind.analytics.AnalyticsUtil; 35 36 import com.android.contacts.common.testing.NeededForTesting; 37 import com.google.common.annotations.VisibleForTesting; 38 39 @NeededForTesting 40 public class ContactsApplication extends Application { 41 private static final boolean ENABLE_LOADER_LOG = false; // Don't submit with true 42 private static final boolean ENABLE_FRAGMENT_LOG = false; // Don't submit with true 43 44 private static InjectedServices sInjectedServices; 45 /** 46 * Log tag for enabling/disabling StrictMode violation log. 47 * To enable: adb shell setprop log.tag.ContactsStrictMode DEBUG 48 */ 49 public static final String STRICT_MODE_TAG = "ContactsStrictMode"; 50 51 /** 52 * Overrides the system services with mocks for testing. 53 */ 54 @VisibleForTesting 55 public static void injectServices(InjectedServices services) { 56 sInjectedServices = services; 57 } 58 59 public static InjectedServices getInjectedServices() { 60 return sInjectedServices; 61 } 62 63 @Override 64 public ContentResolver getContentResolver() { 65 if (sInjectedServices != null) { 66 ContentResolver resolver = sInjectedServices.getContentResolver(); 67 if (resolver != null) { 68 return resolver; 69 } 70 } 71 return super.getContentResolver(); 72 } 73 74 @Override 75 public SharedPreferences getSharedPreferences(String name, int mode) { 76 if (sInjectedServices != null) { 77 SharedPreferences prefs = sInjectedServices.getSharedPreferences(); 78 if (prefs != null) { 79 return prefs; 80 } 81 } 82 83 return super.getSharedPreferences(name, mode); 84 } 85 86 @Override 87 public Object getSystemService(String name) { 88 if (sInjectedServices != null) { 89 Object service = sInjectedServices.getSystemService(name); 90 if (service != null) { 91 return service; 92 } 93 } 94 95 return super.getSystemService(name); 96 } 97 98 @Override 99 public void onCreate() { 100 super.onCreate(); 101 102 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) { 103 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start"); 104 } 105 106 if (ENABLE_FRAGMENT_LOG) FragmentManager.enableDebugLogging(true); 107 if (ENABLE_LOADER_LOG) LoaderManager.enableDebugLogging(true); 108 109 if (Log.isLoggable(STRICT_MODE_TAG, Log.DEBUG)) { 110 StrictMode.setThreadPolicy( 111 new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); 112 } 113 114 // Perform the initialization that doesn't have to finish immediately. 115 // We use an async task here just to avoid creating a new thread. 116 (new DelayedInitializer()).execute(); 117 118 if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) { 119 Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish"); 120 } 121 122 AnalyticsUtil.initialize(this); 123 } 124 125 private class DelayedInitializer extends AsyncTask<Void, Void, Void> { 126 @Override 127 protected Void doInBackground(Void... params) { 128 final Context context = ContactsApplication.this; 129 130 // Warm up the preferences and the contacts provider. We delay initialization 131 // of the account type manager because we may not have the contacts group permission 132 // (and thus not have the get accounts permission). 133 PreferenceManager.getDefaultSharedPreferences(context); 134 getContentResolver().getType(ContentUris.withAppendedId(Contacts.CONTENT_URI, 1)); 135 136 return null; 137 } 138 139 public void execute() { 140 executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, 141 (Void[]) null); 142 } 143 } 144 } 145