blob: 1414f805355eabe10ee9150a7edae1bd82651bb4 [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
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080019import com.android.contacts.test.InjectedServices;
20
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080021import android.app.Activity;
Makoto Onuki50445e92011-07-12 10:28:12 -070022import android.app.Fragment;
23import android.app.FragmentManager;
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080024import android.content.ContentResolver;
Dmitri Plotnikov3a6a9052011-03-02 10:14:43 -080025import android.content.Intent;
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080026import android.content.SharedPreferences;
Dmitri Plotnikov3a6a9052011-03-02 10:14:43 -080027import android.os.Bundle;
Makoto Onuki50445e92011-07-12 10:28:12 -070028import android.view.View;
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080029
30/**
31 * A common superclass for Contacts activities that handles application-wide services.
32 */
Dmitri Plotnikov3a6a9052011-03-02 10:14:43 -080033public abstract class ContactsActivity extends Activity
34 implements ContactSaveService.Listener
35{
Dmitri Plotnikov6f667b52011-01-09 12:53:13 -080036
Dmitri Plotnikov1173ae22011-01-09 14:00:39 -080037 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 Plotnikov6f667b52011-01-09 12:53:13 -080053 @Override
Dmitri Plotnikov072d9112011-01-09 15:48:11 -080054 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 Plotnikov6f667b52011-01-09 12:53:13 -080067 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 Plotnikov3a6a9052011-03-02 10:14:43 -080075
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 Onuki50445e92011-07-12 10:28:12 -070092
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 Plotnikov6f667b52011-01-09 12:53:13 -0800120}