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 | |
Hugo Hudson | a2625e4 | 2011-08-10 21:13:23 +0100 | [diff] [blame^] | 19 | import static com.android.contacts.util.BackgroundTaskService.createAsyncTaskBackgroundTaskService; |
| 20 | |
Dmitri Plotnikov | a07fa5f | 2011-01-09 11:56:50 -0800 | [diff] [blame] | 21 | import com.android.contacts.model.AccountTypeManager; |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 22 | import com.android.contacts.test.InjectedServices; |
Hugo Hudson | 51ada36 | 2011-08-05 14:36:14 +0100 | [diff] [blame] | 23 | import com.android.contacts.util.BackgroundTaskService; |
Flavio Lerda | 37a2684 | 2011-06-27 11:36:52 +0100 | [diff] [blame] | 24 | import com.google.common.annotations.VisibleForTesting; |
Dmitri Plotnikov | 169ff2a | 2010-11-29 16:58:31 -0800 | [diff] [blame] | 25 | |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 26 | import android.app.Application; |
Dmitri Plotnikov | 53a04d6 | 2011-01-25 12:04:59 -0800 | [diff] [blame] | 27 | import android.app.LoaderManager; |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 28 | import android.content.ContentResolver; |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 29 | import android.content.Context; |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame] | 30 | import android.content.SharedPreferences; |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 31 | import android.os.StrictMode; |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 32 | import android.preference.PreferenceManager; |
| 33 | |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 34 | public final class ContactsApplication extends Application { |
| 35 | |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 36 | private static InjectedServices sInjectedServices; |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 37 | private AccountTypeManager mAccountTypeManager; |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 38 | private ContactPhotoManager mContactPhotoManager; |
Hugo Hudson | 51ada36 | 2011-08-05 14:36:14 +0100 | [diff] [blame] | 39 | private BackgroundTaskService mBackgroundTaskService; |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Overrides the system services with mocks for testing. |
| 43 | */ |
Flavio Lerda | 37a2684 | 2011-06-27 11:36:52 +0100 | [diff] [blame] | 44 | @VisibleForTesting |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 45 | public static void injectServices(InjectedServices services) { |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 46 | sInjectedServices = services; |
| 47 | } |
| 48 | |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 49 | public static InjectedServices getInjectedServices() { |
| 50 | return sInjectedServices; |
| 51 | } |
| 52 | |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 53 | @Override |
| 54 | public ContentResolver getContentResolver() { |
| 55 | if (sInjectedServices != null) { |
| 56 | ContentResolver resolver = sInjectedServices.getContentResolver(); |
| 57 | if (resolver != null) { |
| 58 | return resolver; |
| 59 | } |
| 60 | } |
| 61 | return super.getContentResolver(); |
| 62 | } |
| 63 | |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 64 | @Override |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame] | 65 | public SharedPreferences getSharedPreferences(String name, int mode) { |
| 66 | if (sInjectedServices != null) { |
| 67 | SharedPreferences prefs = sInjectedServices.getSharedPreferences(); |
| 68 | if (prefs != null) { |
| 69 | return prefs; |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return super.getSharedPreferences(name, mode); |
| 74 | } |
| 75 | |
| 76 | @Override |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 77 | public Object getSystemService(String name) { |
| 78 | if (sInjectedServices != null) { |
| 79 | Object service = sInjectedServices.getSystemService(name); |
| 80 | if (service != null) { |
| 81 | return service; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) { |
| 86 | if (mAccountTypeManager == null) { |
| 87 | mAccountTypeManager = AccountTypeManager.createAccountTypeManager(this); |
| 88 | } |
| 89 | return mAccountTypeManager; |
| 90 | } |
| 91 | |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 92 | if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) { |
| 93 | if (mContactPhotoManager == null) { |
| 94 | mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this); |
Dmitri Plotnikov | 7edf238 | 2011-01-31 16:15:48 -0800 | [diff] [blame] | 95 | mContactPhotoManager.preloadPhotosInBackground(); |
Dmitri Plotnikov | 34b24ef | 2011-01-28 13:41:07 -0800 | [diff] [blame] | 96 | } |
| 97 | return mContactPhotoManager; |
| 98 | } |
| 99 | |
Hugo Hudson | 51ada36 | 2011-08-05 14:36:14 +0100 | [diff] [blame] | 100 | if (BackgroundTaskService.BACKGROUND_TASK_SERVICE.equals(name)) { |
| 101 | if (mBackgroundTaskService == null) { |
Hugo Hudson | a2625e4 | 2011-08-10 21:13:23 +0100 | [diff] [blame^] | 102 | mBackgroundTaskService = createAsyncTaskBackgroundTaskService(); |
Hugo Hudson | 51ada36 | 2011-08-05 14:36:14 +0100 | [diff] [blame] | 103 | } |
| 104 | return mBackgroundTaskService; |
| 105 | } |
| 106 | |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 107 | return super.getSystemService(name); |
| 108 | } |
| 109 | |
| 110 | @Override |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 111 | public void onCreate() { |
| 112 | super.onCreate(); |
| 113 | |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 114 | // Priming caches to placate the StrictMode police |
| 115 | Context context = getApplicationContext(); |
| 116 | PreferenceManager.getDefaultSharedPreferences(context); |
Dmitri Plotnikov | a07fa5f | 2011-01-09 11:56:50 -0800 | [diff] [blame] | 117 | AccountTypeManager.getInstance(context); |
Dmitri Plotnikov | 53a04d6 | 2011-01-25 12:04:59 -0800 | [diff] [blame] | 118 | LoaderManager.enableDebugLogging(true); |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 119 | |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 120 | StrictMode.setThreadPolicy( |
| 121 | new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); |
| 122 | } |
| 123 | } |