3 * Copyright (C) 2008 The Android Open Source Project
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 package com.android.settings.accounts;
20 import android.accounts.AuthenticatorDescription;
21 import android.app.Activity;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.SyncStatusObserver;
25 import android.content.pm.PackageManager;
26 import android.content.res.Resources;
27 import android.content.res.Resources.Theme;
28 import android.graphics.drawable.Drawable;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.os.UserHandle;
32 import android.os.UserManager;
33 import android.support.v7.preference.PreferenceScreen;
34 import android.text.format.DateFormat;
35 import android.util.Log;
36 import android.view.ContextThemeWrapper;
38 import com.android.settings.SettingsPreferenceFragment;
39 import com.android.settings.Utils;
41 import java.util.ArrayList;
42 import java.util.Date;
44 abstract class AccountPreferenceBase extends SettingsPreferenceFragment
45 implements AuthenticatorHelper.OnAccountsUpdateListener {
47 protected static final String TAG = "AccountSettings";
49 public static final String AUTHORITIES_FILTER_KEY = "authorities";
50 public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types";
52 private final Handler mHandler = new Handler();
54 private UserManager mUm;
55 private Object mStatusChangeListenerHandle;
56 protected AuthenticatorHelper mAuthenticatorHelper;
57 protected UserHandle mUserHandle;
59 private java.text.DateFormat mDateFormat;
60 private java.text.DateFormat mTimeFormat;
63 public void onCreate(Bundle icicle) {
64 super.onCreate(icicle);
65 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
66 final Activity activity = getActivity();
67 mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), mUm, getArguments(),
68 activity.getIntent().getExtras());
69 mAuthenticatorHelper = new AuthenticatorHelper(activity, mUserHandle, mUm, this);
73 * Overload to handle account updates.
76 public void onAccountsUpdate(UserHandle userHandle) {
81 * Overload to handle authenticator description updates
83 protected void onAuthDescriptionsUpdated() {
88 * Overload to handle sync state updates.
90 protected void onSyncStateUpdated() {
95 public void onActivityCreated(Bundle savedInstanceState) {
96 super.onActivityCreated(savedInstanceState);
98 final Activity activity = getActivity();
100 mDateFormat = DateFormat.getDateFormat(activity);
101 mTimeFormat = DateFormat.getTimeFormat(activity);
105 public void onResume() {
107 mStatusChangeListenerHandle = ContentResolver.addStatusChangeListener(
108 ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
109 | ContentResolver.SYNC_OBSERVER_TYPE_STATUS
110 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS,
111 mSyncStatusObserver);
112 onSyncStateUpdated();
116 public void onPause() {
118 ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle);
121 private SyncStatusObserver mSyncStatusObserver = new SyncStatusObserver() {
122 public void onStatusChanged(int which) {
123 mHandler.post(new Runnable() {
125 onSyncStateUpdated();
131 public ArrayList<String> getAuthoritiesForAccountType(String type) {
132 return mAuthenticatorHelper.getAuthoritiesForAccountType(type);
136 * Gets the preferences.xml file associated with a particular account type.
137 * @param accountType the type of account
138 * @return a PreferenceScreen inflated from accountPreferenceId.
140 public PreferenceScreen addPreferencesForType(final String accountType,
141 PreferenceScreen parent) {
142 PreferenceScreen prefs = null;
143 if (mAuthenticatorHelper.containsAccountType(accountType)) {
144 AuthenticatorDescription desc = null;
146 desc = mAuthenticatorHelper.getAccountTypeDescription(accountType);
147 if (desc != null && desc.accountPreferencesId != 0) {
148 // Load the context of the target package, then apply the
149 // base Settings theme (no references to local resources)
150 // and create a context theme wrapper so that we get the
151 // correct text colors. Control colors will still be wrong,
152 // but there's not much we can do about it since we can't
153 // reference local color resources.
154 final Context targetCtx = getActivity().createPackageContextAsUser(
155 desc.packageName, 0, mUserHandle);
156 final Theme baseTheme = getResources().newTheme();
157 baseTheme.applyStyle(com.android.settings.R.style.Theme_SettingsBase, true);
158 final Context themedCtx = new ContextThemeWrapper(targetCtx, 0);
159 themedCtx.getTheme().setTo(baseTheme);
160 prefs = getPreferenceManager().inflateFromResource(themedCtx,
161 desc.accountPreferencesId, parent);
163 } catch (PackageManager.NameNotFoundException e) {
164 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
165 } catch (Resources.NotFoundException e) {
166 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
172 public void updateAuthDescriptions() {
173 mAuthenticatorHelper.updateAuthDescriptions(getActivity());
174 onAuthDescriptionsUpdated();
177 protected Drawable getDrawableForType(final String accountType) {
178 return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType);
181 protected CharSequence getLabelForType(final String accountType) {
182 return mAuthenticatorHelper.getLabelForType(getActivity(), accountType);
185 protected String formatSyncDate(Date date) {
186 // TODO: Switch to using DateUtils.formatDateTime
187 return mDateFormat.format(date) + " " + mTimeFormat.format(date);