Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 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 | |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 19 | import com.android.contacts.test.InjectedServices; |
| 20 | |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 21 | import android.app.Activity; |
Makoto Onuki | 50445e9 | 2011-07-12 10:28:12 -0700 | [diff] [blame^] | 22 | import android.app.Fragment; |
| 23 | import android.app.FragmentManager; |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 24 | import android.content.ContentResolver; |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 25 | import android.content.Intent; |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame] | 26 | import android.content.SharedPreferences; |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 27 | import android.os.Bundle; |
Makoto Onuki | 50445e9 | 2011-07-12 10:28:12 -0700 | [diff] [blame^] | 28 | import android.view.View; |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 29 | |
| 30 | /** |
| 31 | * A common superclass for Contacts activities that handles application-wide services. |
| 32 | */ |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 33 | public abstract class ContactsActivity extends Activity |
| 34 | implements ContactSaveService.Listener |
| 35 | { |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 36 | |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 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 | |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 53 | @Override |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame] | 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 |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 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 | } |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 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 | } |
Makoto Onuki | 50445e9 | 2011-07-12 10:28:12 -0700 | [diff] [blame^] | 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 | } |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 120 | } |