• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 import android.app.Application;
20 import android.app.FragmentManager;
21 import android.app.LoaderManager;
22 import android.content.ContentResolver;
23 import android.content.ContentUris;
24 import android.content.Context;
25 import android.content.SharedPreferences;
26 import android.os.AsyncTask;
27 import android.os.StrictMode;
28 import android.preference.PreferenceManager;
29 import android.provider.ContactsContract.Contacts;
30 import android.util.Log;
31 
32 import com.android.contacts.testing.InjectedServices;
33 import com.android.contacts.util.Constants;
34 import com.android.contactsbind.analytics.AnalyticsUtil;
35 
36 import com.google.common.annotations.VisibleForTesting;
37 
38 public class ContactsApplication extends Application {
39     private static final boolean ENABLE_LOADER_LOG = false; // Don't submit with true
40     private static final boolean ENABLE_FRAGMENT_LOG = false; // Don't submit with true
41 
42     private static InjectedServices sInjectedServices;
43     /**
44      * Log tag for enabling/disabling StrictMode violation log.
45      * To enable: adb shell setprop log.tag.ContactsStrictMode DEBUG
46      */
47     public static final String STRICT_MODE_TAG = "ContactsStrictMode";
48 
49     /**
50      * Overrides the system services with mocks for testing.
51      */
52     @VisibleForTesting
injectServices(InjectedServices services)53     public static void injectServices(InjectedServices services) {
54         sInjectedServices = services;
55     }
56 
getInjectedServices()57     public static InjectedServices getInjectedServices() {
58         return sInjectedServices;
59     }
60 
61     @Override
getContentResolver()62     public ContentResolver getContentResolver() {
63         if (sInjectedServices != null) {
64             ContentResolver resolver = sInjectedServices.getContentResolver();
65             if (resolver != null) {
66                 return resolver;
67             }
68         }
69         return super.getContentResolver();
70     }
71 
72     @Override
getSharedPreferences(String name, int mode)73     public SharedPreferences getSharedPreferences(String name, int mode) {
74         if (sInjectedServices != null) {
75             SharedPreferences prefs = sInjectedServices.getSharedPreferences();
76             if (prefs != null) {
77                 return prefs;
78             }
79         }
80 
81         return super.getSharedPreferences(name, mode);
82     }
83 
84     @Override
getSystemService(String name)85     public Object getSystemService(String name) {
86         if (sInjectedServices != null) {
87             Object service = sInjectedServices.getSystemService(name);
88             if (service != null) {
89                 return service;
90             }
91         }
92 
93         return super.getSystemService(name);
94     }
95 
96     @Override
onCreate()97     public void onCreate() {
98         super.onCreate();
99 
100         if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
101             Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start");
102         }
103 
104         if (ENABLE_FRAGMENT_LOG) FragmentManager.enableDebugLogging(true);
105         if (ENABLE_LOADER_LOG) LoaderManager.enableDebugLogging(true);
106 
107         if (Log.isLoggable(STRICT_MODE_TAG, Log.DEBUG)) {
108             StrictMode.setThreadPolicy(
109                     new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
110         }
111 
112         // Perform the initialization that doesn't have to finish immediately.
113         // We use an async task here just to avoid creating a new thread.
114         (new DelayedInitializer()).execute();
115 
116         if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
117             Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish");
118         }
119 
120         AnalyticsUtil.initialize(this);
121     }
122 
123     private class DelayedInitializer extends AsyncTask<Void, Void, Void> {
124         @Override
doInBackground(Void... params)125         protected Void doInBackground(Void... params) {
126             final Context context = ContactsApplication.this;
127 
128             // Warm up the preferences and the contacts provider.  We delay initialization
129             // of the account type manager because we may not have the contacts group permission
130             // (and thus not have the get accounts permission).
131             PreferenceManager.getDefaultSharedPreferences(context);
132             getContentResolver().getType(ContentUris.withAppendedId(Contacts.CONTENT_URI, 1));
133 
134             return null;
135         }
136 
execute()137         public void execute() {
138             executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
139                     (Void[]) null);
140         }
141     }
142 }
143