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