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