blob: 87c0d625c3a5bff4eac5688d52439636322a2a78 [file] [log] [blame]
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -08001/*
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
Makoto Onuki50445e92011-07-12 10:28:12 -070019import android.app.Fragment;
20import android.app.FragmentManager;
Makoto Onuki4d788fc2011-07-12 10:00:10 -070021import android.app.FragmentTransaction;
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080022import android.content.ContentResolver;
Dmitri Plotnikov3a6a9052011-03-02 10:14:43 -080023import android.content.Intent;
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080024import android.content.SharedPreferences;
Dmitri Plotnikov3a6a9052011-03-02 10:14:43 -080025import android.os.Bundle;
Makoto Onuki50445e92011-07-12 10:28:12 -070026import android.view.View;
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080027
Gary Mai0a49afa2016-12-05 15:53:58 -080028import com.android.contacts.activities.TransactionSafeActivity;
Gary Mai69c182a2016-12-05 13:07:03 -080029import com.android.contacts.testing.InjectedServices;
Chiao Chenge0b2f1e2012-06-12 13:07:56 -070030
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080031/**
32 * A common superclass for Contacts activities that handles application-wide services.
33 */
Katherine Kuan18e2b6f2011-10-31 16:48:44 -070034public abstract class ContactsActivity extends TransactionSafeActivity
Dmitri Plotnikov3a6a9052011-03-02 10:14:43 -080035 implements ContactSaveService.Listener
36{
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080037
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080038 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 Plotnikov6f667b52011-01-09 12:53:13 -080054 @Override
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080055 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 Plotnikov6f667b52011-01-09 12:53:13 -080068 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 Plotnikov3a6a9052011-03-02 10:14:43 -080076
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 Onuki50445e92011-07-12 10:28:12 -070093
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 Onuki4d788fc2011-07-12 10:00:10 -0700121
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 Plotnikov6f667b52011-01-09 12:53:13 -0800129}