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 | |
Makoto Onuki | 50445e9 | 2011-07-12 10:28:12 -0700 | [diff] [blame] | 19 | import android.app.Fragment; |
| 20 | import android.app.FragmentManager; |
Makoto Onuki | 4d788fc | 2011-07-12 10:00:10 -0700 | [diff] [blame] | 21 | import android.app.FragmentTransaction; |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 22 | import android.content.ContentResolver; |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 23 | import android.content.Intent; |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame] | 24 | import android.content.SharedPreferences; |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 25 | import android.os.Bundle; |
Makoto Onuki | 50445e9 | 2011-07-12 10:28:12 -0700 | [diff] [blame] | 26 | import android.view.View; |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 27 | |
Chiao Cheng | d7ca03e | 2012-10-24 15:14:08 -0700 | [diff] [blame] | 28 | import com.android.contacts.common.activity.TransactionSafeActivity; |
Gary Mai | 69c182a | 2016-12-05 13:07:03 -0800 | [diff] [blame^] | 29 | import com.android.contacts.testing.InjectedServices; |
Chiao Cheng | e0b2f1e | 2012-06-12 13:07:56 -0700 | [diff] [blame] | 30 | |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 31 | /** |
| 32 | * A common superclass for Contacts activities that handles application-wide services. |
| 33 | */ |
Katherine Kuan | 18e2b6f | 2011-10-31 16:48:44 -0700 | [diff] [blame] | 34 | public abstract class ContactsActivity extends TransactionSafeActivity |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 35 | implements ContactSaveService.Listener |
| 36 | { |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 37 | |
Dmitri Plotnikov | 1173ae2 | 2011-01-09 14:00:39 -0800 | [diff] [blame] | 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 | |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 54 | @Override |
Dmitri Plotnikov | 072d911 | 2011-01-09 15:48:11 -0800 | [diff] [blame] | 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 |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 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 | } |
Dmitri Plotnikov | 3a6a905 | 2011-03-02 10:14:43 -0800 | [diff] [blame] | 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 | } |
Makoto Onuki | 50445e9 | 2011-07-12 10:28:12 -0700 | [diff] [blame] | 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 | } |
Makoto Onuki | 4d788fc | 2011-07-12 10:00:10 -0700 | [diff] [blame] | 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 | } |
Dmitri Plotnikov | 6f667b5 | 2011-01-09 12:53:13 -0800 | [diff] [blame] | 129 | } |