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.list.ContactListFilterController;
20import com.android.contacts.model.AccountTypeManager;
21import com.android.contacts.test.InjectedServices;
22import com.android.contacts.util.Constants;
23import com.google.common.annotations.VisibleForTesting;
24
25import android.app.Application;
26import android.app.FragmentManager;
27import android.app.LoaderManager;
28import android.content.ContentResolver;
29import android.content.Context;
30import android.content.SharedPreferences;
31import android.os.StrictMode;
32import android.preference.PreferenceManager;
33import android.util.Log;
34
35public final class ContactsApplication extends Application {
36    private static final boolean ENABLE_LOADER_LOG = false; // Don't submit with true
37    private static final boolean ENABLE_FRAGMENT_LOG = false; // Don't submit with true
38
39    private static InjectedServices sInjectedServices;
40    private AccountTypeManager mAccountTypeManager;
41    private ContactPhotoManager mContactPhotoManager;
42    private ContactListFilterController mContactListFilterController;
43
44    /**
45     * Overrides the system services with mocks for testing.
46     */
47    @VisibleForTesting
48    public static void injectServices(InjectedServices services) {
49        sInjectedServices = services;
50    }
51
52    public static InjectedServices getInjectedServices() {
53        return sInjectedServices;
54    }
55
56    @Override
57    public ContentResolver getContentResolver() {
58        if (sInjectedServices != null) {
59            ContentResolver resolver = sInjectedServices.getContentResolver();
60            if (resolver != null) {
61                return resolver;
62            }
63        }
64        return super.getContentResolver();
65    }
66
67    @Override
68    public SharedPreferences getSharedPreferences(String name, int mode) {
69        if (sInjectedServices != null) {
70            SharedPreferences prefs = sInjectedServices.getSharedPreferences();
71            if (prefs != null) {
72                return prefs;
73            }
74        }
75
76        return super.getSharedPreferences(name, mode);
77    }
78
79    @Override
80    public Object getSystemService(String name) {
81        if (sInjectedServices != null) {
82            Object service = sInjectedServices.getSystemService(name);
83            if (service != null) {
84                return service;
85            }
86        }
87
88        if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) {
89            if (mAccountTypeManager == null) {
90                mAccountTypeManager = AccountTypeManager.createAccountTypeManager(this);
91            }
92            return mAccountTypeManager;
93        }
94
95        if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) {
96            if (mContactPhotoManager == null) {
97                mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this);
98                registerComponentCallbacks(mContactPhotoManager);
99                mContactPhotoManager.preloadPhotosInBackground();
100            }
101            return mContactPhotoManager;
102        }
103
104        if (ContactListFilterController.CONTACT_LIST_FILTER_SERVICE.equals(name)) {
105            if (mContactListFilterController == null) {
106                mContactListFilterController =
107                        ContactListFilterController.createContactListFilterController(this);
108            }
109            return mContactListFilterController;
110        }
111
112        return super.getSystemService(name);
113    }
114
115    @Override
116    public void onCreate() {
117        super.onCreate();
118
119        if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
120            Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start");
121        }
122
123        // Priming caches to placate the StrictMode police
124        Context context = getApplicationContext();
125        PreferenceManager.getDefaultSharedPreferences(context);
126        AccountTypeManager.getInstance(context);
127        if (ENABLE_FRAGMENT_LOG) FragmentManager.enableDebugLogging(true);
128        if (ENABLE_LOADER_LOG) LoaderManager.enableDebugLogging(true);
129
130        if (Log.isLoggable(Constants.STRICT_MODE_TAG, Log.DEBUG)) {
131            StrictMode.setThreadPolicy(
132                    new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
133        }
134
135        if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
136            Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish");
137        }
138    }
139}
140