ContactsApplication.java revision 7edf238cb6e051bb53d327d9b435bbf5b4aa11c4
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
17package com.android.contacts;
18
19import com.android.contacts.model.AccountTypeManager;
20import com.android.contacts.test.InjectedServices;
21
22import android.app.Application;
23import android.app.LoaderManager;
24import android.content.ContentResolver;
25import android.content.Context;
26import android.content.SharedPreferences;
27import android.os.StrictMode;
28import android.preference.PreferenceManager;
29
30public final class ContactsApplication extends Application {
31
32    private static InjectedServices sInjectedServices;
33    private AccountTypeManager mAccountTypeManager;
34    private ContactPhotoManager mContactPhotoManager;
35
36    /**
37     * Overrides the system services with mocks for testing.
38     */
39    public static void injectServices(InjectedServices services) {
40        sInjectedServices = services;
41    }
42
43    public static InjectedServices getInjectedServices() {
44        return sInjectedServices;
45    }
46
47    @Override
48    public ContentResolver getContentResolver() {
49        if (sInjectedServices != null) {
50            ContentResolver resolver = sInjectedServices.getContentResolver();
51            if (resolver != null) {
52                return resolver;
53            }
54        }
55        return super.getContentResolver();
56    }
57
58    @Override
59    public SharedPreferences getSharedPreferences(String name, int mode) {
60        if (sInjectedServices != null) {
61            SharedPreferences prefs = sInjectedServices.getSharedPreferences();
62            if (prefs != null) {
63                return prefs;
64            }
65        }
66
67        return super.getSharedPreferences(name, mode);
68    }
69
70    @Override
71    public Object getSystemService(String name) {
72        if (sInjectedServices != null) {
73            Object service = sInjectedServices.getSystemService(name);
74            if (service != null) {
75                return service;
76            }
77        }
78
79        if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) {
80            if (mAccountTypeManager == null) {
81                mAccountTypeManager = AccountTypeManager.createAccountTypeManager(this);
82            }
83            return mAccountTypeManager;
84        }
85
86        if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) {
87            if (mContactPhotoManager == null) {
88                mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this);
89                mContactPhotoManager.preloadPhotosInBackground();
90            }
91            return mContactPhotoManager;
92        }
93
94        return super.getSystemService(name);
95    }
96
97    @Override
98    public void onCreate() {
99        super.onCreate();
100
101        // Priming caches to placate the StrictMode police
102        Context context = getApplicationContext();
103        PreferenceManager.getDefaultSharedPreferences(context);
104        AccountTypeManager.getInstance(context);
105        LoaderManager.enableDebugLogging(true);
106
107        StrictMode.setThreadPolicy(
108                new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
109    }
110}
111