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.Fragment;
20 import android.app.FragmentManager;
21 import android.app.FragmentTransaction;
22 import android.content.ContentResolver;
23 import android.content.Intent;
24 import android.content.SharedPreferences;
25 import android.os.Bundle;
26 import android.view.View;
27 
28 import com.android.contacts.activities.TransactionSafeActivity;
29 import com.android.contacts.testing.InjectedServices;
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
getContentResolver()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
getSharedPreferences(String name, int mode)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
getSystemService(String name)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
onCreate(Bundle savedInstanceState)78     protected void onCreate(Bundle savedInstanceState) {
79         ContactSaveService.registerListener(this);
80         super.onCreate(savedInstanceState);
81     }
82 
83     @Override
onDestroy()84     protected void onDestroy() {
85         ContactSaveService.unregisterListener(this);
86         super.onDestroy();
87     }
88 
89     @Override
onServiceCompleted(Intent callbackIntent)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")
getFragment(int id)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")
getView(int id)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 
showFragment(FragmentTransaction ft, Fragment f)122     protected static void showFragment(FragmentTransaction ft, Fragment f) {
123         if ((f != null) && f.isHidden()) ft.show(f);
124     }
125 
hideFragment(FragmentTransaction ft, Fragment f)126     protected static void hideFragment(FragmentTransaction ft, Fragment f) {
127         if ((f != null) && !f.isHidden()) ft.hide(f);
128     }
129 }
130