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 com.android.contacts.test.InjectedServices;
     20 import com.android.contacts.activities.TransactionSafeActivity;
     21 
     22 import android.app.Fragment;
     23 import android.app.FragmentManager;
     24 import android.app.FragmentTransaction;
     25 import android.content.ContentResolver;
     26 import android.content.Intent;
     27 import android.content.SharedPreferences;
     28 import android.os.Bundle;
     29 import android.view.View;
     30 
     31 /**
     32  * A common superclass for Contacts activities that handles application-wide services.
     33  */
     34 public abstract class ContactsActivity extends TransactionSafeActivity
     35     implements ContactSaveService.Listener
     36 {
     37 
     38     private ContentResolver mContentResolver;
     39 
     40     @Override
     41     public ContentResolver getContentResolver() {
     42         if (mContentResolver == null) {
     43             InjectedServices services = ContactsApplication.getInjectedServices();
     44             if (services != null) {
     45                 mContentResolver = services.getContentResolver();
     46             }
     47             if (mContentResolver == null) {
     48                 mContentResolver = super.getContentResolver();
     49             }
     50         }
     51         return mContentResolver;
     52     }
     53 
     54     @Override
     55     public SharedPreferences getSharedPreferences(String name, int mode) {
     56         InjectedServices services = ContactsApplication.getInjectedServices();
     57         if (services != null) {
     58             SharedPreferences prefs = services.getSharedPreferences();
     59             if (prefs != null) {
     60                 return prefs;
     61             }
     62         }
     63 
     64         return super.getSharedPreferences(name, mode);
     65     }
     66 
     67     @Override
     68     public Object getSystemService(String name) {
     69         Object service = super.getSystemService(name);
     70         if (service != null) {
     71             return service;
     72         }
     73 
     74         return getApplicationContext().getSystemService(name);
     75     }
     76 
     77     @Override
     78     protected void onCreate(Bundle savedInstanceState) {
     79         ContactSaveService.registerListener(this);
     80         super.onCreate(savedInstanceState);
     81     }
     82 
     83     @Override
     84     protected void onDestroy() {
     85         ContactSaveService.unregisterListener(this);
     86         super.onDestroy();
     87     }
     88 
     89     @Override
     90     public void onServiceCompleted(Intent callbackIntent) {
     91         onNewIntent(callbackIntent);
     92     }
     93 
     94     /**
     95      * Convenient version of {@link FragmentManager#findFragmentById(int)}, which throws
     96      * an exception if the fragment doesn't exist.
     97      */
     98     @SuppressWarnings("unchecked")
     99     public <T extends Fragment> T getFragment(int id) {
    100         T result = (T)getFragmentManager().findFragmentById(id);
    101         if (result == null) {
    102             throw new IllegalArgumentException("fragment 0x" + Integer.toHexString(id)
    103                     + " doesn't exist");
    104         }
    105         return result;
    106     }
    107 
    108     /**
    109      * Convenient version of {@link #findViewById(int)}, which throws
    110      * an exception if the view doesn't exist.
    111      */
    112     @SuppressWarnings("unchecked")
    113     public <T extends View> T getView(int id) {
    114         T result = (T)findViewById(id);
    115         if (result == null) {
    116             throw new IllegalArgumentException("view 0x" + Integer.toHexString(id)
    117                     + " doesn't exist");
    118         }
    119         return result;
    120     }
    121 
    122     protected static void showFragment(FragmentTransaction ft, Fragment f) {
    123         if ((f != null) && f.isHidden()) ft.show(f);
    124     }
    125 
    126     protected static void hideFragment(FragmentTransaction ft, Fragment f) {
    127         if ((f != null) && !f.isHidden()) ft.hide(f);
    128     }
    129 }
    130