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