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