Home | History | Annotate | Download | only in contacts
      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.list.ContactListFilterController;
     33 import com.android.contacts.model.AccountTypeManager;
     34 import com.android.contacts.test.InjectedServices;
     35 import com.android.contacts.util.Constants;
     36 import com.google.common.annotations.VisibleForTesting;
     37 
     38 public final 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     private AccountTypeManager mAccountTypeManager;
     44     private ContactPhotoManager mContactPhotoManager;
     45     private ContactListFilterController mContactListFilterController;
     46 
     47     /**
     48      * Overrides the system services with mocks for testing.
     49      */
     50     @VisibleForTesting
     51     public static void injectServices(InjectedServices services) {
     52         sInjectedServices = services;
     53     }
     54 
     55     public static InjectedServices getInjectedServices() {
     56         return sInjectedServices;
     57     }
     58 
     59     @Override
     60     public ContentResolver getContentResolver() {
     61         if (sInjectedServices != null) {
     62             ContentResolver resolver = sInjectedServices.getContentResolver();
     63             if (resolver != null) {
     64                 return resolver;
     65             }
     66         }
     67         return super.getContentResolver();
     68     }
     69 
     70     @Override
     71     public SharedPreferences getSharedPreferences(String name, int mode) {
     72         if (sInjectedServices != null) {
     73             SharedPreferences prefs = sInjectedServices.getSharedPreferences();
     74             if (prefs != null) {
     75                 return prefs;
     76             }
     77         }
     78 
     79         return super.getSharedPreferences(name, mode);
     80     }
     81 
     82     @Override
     83     public Object getSystemService(String name) {
     84         if (sInjectedServices != null) {
     85             Object service = sInjectedServices.getSystemService(name);
     86             if (service != null) {
     87                 return service;
     88             }
     89         }
     90 
     91         if (AccountTypeManager.ACCOUNT_TYPE_SERVICE.equals(name)) {
     92             if (mAccountTypeManager == null) {
     93                 mAccountTypeManager = AccountTypeManager.createAccountTypeManager(this);
     94             }
     95             return mAccountTypeManager;
     96         }
     97 
     98         if (ContactPhotoManager.CONTACT_PHOTO_SERVICE.equals(name)) {
     99             if (mContactPhotoManager == null) {
    100                 mContactPhotoManager = ContactPhotoManager.createContactPhotoManager(this);
    101                 registerComponentCallbacks(mContactPhotoManager);
    102                 mContactPhotoManager.preloadPhotosInBackground();
    103             }
    104             return mContactPhotoManager;
    105         }
    106 
    107         if (ContactListFilterController.CONTACT_LIST_FILTER_SERVICE.equals(name)) {
    108             if (mContactListFilterController == null) {
    109                 mContactListFilterController =
    110                         ContactListFilterController.createContactListFilterController(this);
    111             }
    112             return mContactListFilterController;
    113         }
    114 
    115         return super.getSystemService(name);
    116     }
    117 
    118     @Override
    119     public void onCreate() {
    120         super.onCreate();
    121 
    122         if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
    123             Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate start");
    124         }
    125 
    126         if (ENABLE_FRAGMENT_LOG) FragmentManager.enableDebugLogging(true);
    127         if (ENABLE_LOADER_LOG) LoaderManager.enableDebugLogging(true);
    128 
    129         if (Log.isLoggable(Constants.STRICT_MODE_TAG, Log.DEBUG)) {
    130             StrictMode.setThreadPolicy(
    131                     new StrictMode.ThreadPolicy.Builder().detectAll().penaltyLog().build());
    132         }
    133 
    134         // Perform the initialization that doesn't have to finish immediately.
    135         // We use an async task here just to avoid creating a new thread.
    136         (new DelayedInitializer()).execute();
    137 
    138         if (Log.isLoggable(Constants.PERFORMANCE_TAG, Log.DEBUG)) {
    139             Log.d(Constants.PERFORMANCE_TAG, "ContactsApplication.onCreate finish");
    140         }
    141     }
    142 
    143     private class DelayedInitializer extends AsyncTask<Void, Void, Void> {
    144         @Override
    145         protected Void doInBackground(Void... params) {
    146             final Context context = ContactsApplication.this;
    147 
    148             // Warm up the preferences, the account type manager and the contacts provider.
    149             PreferenceManager.getDefaultSharedPreferences(context);
    150             AccountTypeManager.getInstance(context);
    151             getContentResolver().getType(ContentUris.withAppendedId(Contacts.CONTENT_URI, 1));
    152             return null;
    153         }
    154 
    155         public void execute() {
    156             executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
    157                     (Void[]) null);
    158         }
    159     }
    160 }
    161