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; |
Dmitri Plotnikov | 169ff2a | 2010-11-29 16:58:31 -0800 | [diff] [blame] | 21 | |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 22 | import android.app.Application; |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 23 | import android.content.ContentResolver; |
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; |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 26 | import android.os.StrictMode; |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 27 | import android.preference.PreferenceManager; |
| 28 | |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 29 | public final class ContactsApplication extends Application { |
| 30 | |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 31 | private static InjectedServices sInjectedServices; |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 32 | private AccountTypeManager mAccountTypeManager; |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 33 | |
| 34 | /** |
| 35 | * Overrides the system services with mocks for testing. |
| 36 | */ |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 37 | public static void injectServices(InjectedServices services) { |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 38 | sInjectedServices = services; |
| 39 | } |
| 40 | |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 41 | public static InjectedServices getInjectedServices() { |
| 42 | return sInjectedServices; |
| 43 | } |
| 44 | |
Dmitri Plotnikov | 43fd1e8 | 2011-01-09 11:29:39 -0800 | [diff] [blame] | 45 | @Override |
| 46 | public ContentResolver getContentResolver() { |
| 47 | if (sInjectedServices != null) { |
| 48 | ContentResolver resolver = sInjectedServices.getContentResolver(); |
| 49 | if (resolver != null) { |
| 50 | return resolver; |
| 51 | } |
| 52 | } |
| 53 | return super.getContentResolver(); |
| 54 | } |
| 55 | |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 56 | @Override |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame^] | 57 | public SharedPreferences getSharedPreferences(String name, int mode) { |
| 58 | if (sInjectedServices != null) { |
| 59 | SharedPreferences prefs = sInjectedServices.getSharedPreferences(); |
| 60 | if (prefs != null) { |
| 61 | return prefs; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return super.getSharedPreferences(name, mode); |
| 66 | } |
| 67 | |
| 68 | @Override |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 69 | public Object getSystemService(String name) { |
| 70 | if (sInjectedServices != null) { |
| 71 | Object service = sInjectedServices.getSystemService(name); |
| 72 | if (service != null) { |
| 73 | return service; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) { |
| 78 | if (mAccountTypeManager == null) { |
| 79 | mAccountTypeManager = AccountTypeManager.createAccountTypeManager(this); |
| 80 | } |
| 81 | return mAccountTypeManager; |
| 82 | } |
| 83 | |
| 84 | return super.getSystemService(name); |
| 85 | } |
| 86 | |
| 87 | @Override |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 88 | public void onCreate() { |
| 89 | super.onCreate(); |
| 90 | |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 91 | // Priming caches to placate the StrictMode police |
| 92 | Context context = getApplicationContext(); |
| 93 | PreferenceManager.getDefaultSharedPreferences(context); |
Dmitri Plotnikov | a07fa5f | 2011-01-09 11:56:50 -0800 | [diff] [blame] | 94 | AccountTypeManager.getInstance(context); |
Dmitri Plotnikov | 3b7dedd | 2010-11-29 14:46:10 -0800 | [diff] [blame] | 95 | |
Dmitri Plotnikov | f049ff0 | 2010-11-29 10:15:24 -0800 | [diff] [blame] | 96 | StrictMode.setThreadPolicy( |
| 97 | new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build()); |
| 98 | } |
| 99 | } |