Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 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; |
Makoto Onuki | 7c91de1 | 2011-11-02 15:43:20 -0700 | [diff] [blame] | 20 | import android.app.FragmentManager; |
| 21 | import android.app.LoaderManager; |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 22 | import android.content.ContentResolver; |
Makoto Onuki | fc0a89f | 2012-05-14 17:37:34 -0700 | [diff] [blame] | 23 | import android.content.ContentUris; |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 24 | import android.content.Context; |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame] | 25 | import android.content.SharedPreferences; |
Makoto Onuki | fc0a89f | 2012-05-14 17:37:34 -0700 | [diff] [blame] | 26 | import android.os.AsyncTask; |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 27 | import android.os.StrictMode; |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 28 | import android.preference.PreferenceManager; |
Makoto Onuki | fc0a89f | 2012-05-14 17:37:34 -0700 | [diff] [blame] | 29 | import android.provider.ContactsContract.Contacts; |
Makoto Onuki | 49627cc | 2011-08-28 13:56:48 -0700 | [diff] [blame] | 30 | import android.util.Log; |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 31 | |
Chiao Cheng | 79a6b5d | 2012-10-16 13:17:28 -0700 | [diff] [blame] | 32 | import com.android.contacts.common.ContactPhotoManager; |
Chiao Cheng | 0d5588d | 2012-11-26 15:34:14 -0800 | [diff] [blame] | 33 | import com.android.contacts.common.list.ContactListFilterController; |
| 34 | import com.android.contacts.common.model.AccountTypeManager; |
Yorke Lee | 7039633 | 2014-05-28 12:16:35 -0700 | [diff] [blame] | 35 | import com.android.contacts.common.testing.InjectedServices; |
Chiao Cheng | d9eab4d | 2012-11-14 18:24:25 -0800 | [diff] [blame] | 36 | import com.android.contacts.common.util.Constants; |
Brian Attwell | a3e498a | 2014-10-27 10:46:24 -0700 | [diff] [blame] | 37 | import com.android.contacts.commonbind.analytics.AnalyticsUtil; |
| 38 | |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 39 | import com.google.common.annotations.VisibleForTesting; |
| 40 | |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 41 | public final class ContactsApplication extends Application { |
Makoto Onuki | 7c91de1 | 2011-11-02 15:43:20 -0700 | [diff] [blame] | 42 | private static final boolean ENABLE_LOADER_LOG = false; // Don't submit with true |
| 43 | private static final boolean ENABLE_FRAGMENT_LOG = false; // Don't submit with true |
| 44 | |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 45 | private static InjectedServices sInjectedServices; |
Chiao Cheng | d9eab4d | 2012-11-14 18:24:25 -0800 | [diff] [blame] | 46 | /** |
| 47 | * Log tag for enabling/disabling StrictMode violation log. |
| 48 | * To enable: adb shell setprop log.tag.ContactsStrictMode DEBUG |
| 49 | */ |
| 50 | public static final String STRICT_MODE_TAG = "ContactsStrictMode"; |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 51 | private ContactPhotoManager mContactPhotoManager; |
Daisuke Miyakawa | a012aec | 2011-10-16 10:24:43 -0700 | [diff] [blame] | 52 | private ContactListFilterController mContactListFilterController; |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * Overrides the system services with mocks for testing. |
| 56 | */ |
Flavio Lerda | 37a2684 | 2011-06-27 11:36:52 +0100 | [diff] [blame] | 57 | @VisibleForTesting |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 58 | public static void injectServices(InjectedServices services) { |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 59 | sInjectedServices = services; |
| 60 | } |
| 61 | |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 62 | public static InjectedServices getInjectedServices() { |
| 63 | return sInjectedServices; |
| 64 | } |
| 65 | |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 66 | @Override |
| 67 | public ContentResolver getContentResolver() { |
| 68 | if (sInjectedServices != null) { |
| 69 | ContentResolver resolver = sInjectedServices.getContentResolver(); |
| 70 | if (resolver != null) { |
| 71 | return resolver; |
| 72 | } |
| 73 | } |
| 74 | return super.getContentResolver(); |
| 75 | } |
| 76 | |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 77 | @Override |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame] | 78 | public SharedPreferences getSharedPreferences(String name, int mode) { |
| 79 | if (sInjectedServices != null) { |
| 80 | SharedPreferences prefs = sInjectedServices.getSharedPreferences(); |
| 81 | if (prefs != null) { |
| 82 | return prefs; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | return super.getSharedPreferences(name, mode); |
| 87 | } |
| 88 | |
| 89 | @Override |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 90 | public Object getSystemService(String name) { |
| 91 | if (sInjectedServices != null) { |
| 92 | Object service = sInjectedServices.getSystemService(name); |
| 93 | if (service != null) { |
| 94 | return service; |
| 95 | } |
| 96 | } |
| 97 | |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 98 | return super.getSystemService(name); |
| 99 | } |
| 100 | |
| 101 | @Override |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 102 | public void onCreate() { |
| 103 | super.onCreate(); |
| 104 | |
Makoto Onuki | 49627cc | 2011-08-28 13:56:48 -0700 | [diff] [blame] | 105 | if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) { |
| 106 | Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start"); |
| 107 | } |
| 108 | |
Makoto Onuki | 7c91de1 | 2011-11-02 15:43:20 -0700 | [diff] [blame] | 109 | if (ENABLE_FRAGMENT_LOG) FragmentManager.enableDebugLogging(true); |
| 110 | if (ENABLE_LOADER_LOG) LoaderManager.enableDebugLogging(true); |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 111 | |
Chiao Cheng | d9eab4d | 2012-11-14 18:24:25 -0800 | [diff] [blame] | 112 | if (Log.isLoggable(STRICT_MODE_TAG, Log.DEBUG)) { |
Daisuke Miyakawa | b5c9f63 | 2011-09-21 16:05:18 -0700 | [diff] [blame] | 113 | StrictMode.setThreadPolicy( |
| 114 | new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); |
| 115 | } |
Makoto Onuki | 49627cc | 2011-08-28 13:56:48 -0700 | [diff] [blame] | 116 | |
Makoto Onuki | fc0a89f | 2012-05-14 17:37:34 -0700 | [diff] [blame] | 117 | // Perform the initialization that doesn't have to finish immediately. |
| 118 | // We use an async task here just to avoid creating a new thread. |
| 119 | (new DelayedInitializer()).execute(); |
| 120 | |
Makoto Onuki | 49627cc | 2011-08-28 13:56:48 -0700 | [diff] [blame] | 121 | if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) { |
| 122 | Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish"); |
| 123 | } |
Brian Attwell | a3e498a | 2014-10-27 10:46:24 -0700 | [diff] [blame] | 124 | |
| 125 | AnalyticsUtil.initialize(this); |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 126 | } |
Makoto Onuki | fc0a89f | 2012-05-14 17:37:34 -0700 | [diff] [blame] | 127 | |
| 128 | private class DelayedInitializer extends AsyncTask<Void, Void, Void> { |
| 129 | @Override |
| 130 | protected Void doInBackground(Void... params) { |
| 131 | final Context context = ContactsApplication.this; |
| 132 | |
Walter Jang | dccfa94 | 2015-07-23 15:08:00 -0700 | [diff] [blame^] | 133 | // Warm up the preferences and the contacts provider. We delay initialization |
| 134 | // of the account type manager because we may not have the contacts group permission |
| 135 | // (and thus not have the get accounts permission). |
Makoto Onuki | fc0a89f | 2012-05-14 17:37:34 -0700 | [diff] [blame] | 136 | PreferenceManager.getDefaultSharedPreferences(context); |
Makoto Onuki | fc0a89f | 2012-05-14 17:37:34 -0700 | [diff] [blame] | 137 | getContentResolver().getType(ContentUris.withAppendedId(Contacts.CONTENT_URI, 1)); |
Walter Jang | dccfa94 | 2015-07-23 15:08:00 -0700 | [diff] [blame^] | 138 | |
Makoto Onuki | fc0a89f | 2012-05-14 17:37:34 -0700 | [diff] [blame] | 139 | return null; |
| 140 | } |
| 141 | |
| 142 | public void execute() { |
| 143 | executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, |
| 144 | (Void[]) null); |
| 145 | } |
| 146 | } |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 147 | } |