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;
40 import com.android.settingslib.accounts.AuthenticatorHelper;
42 import java.util.ArrayList;
43 import java.util.Date;
45 abstract class AccountPreferenceBase extends SettingsPreferenceFragment
46 implements AuthenticatorHelper.OnAccountsUpdateListener {
48 protected static final String TAG = "AccountSettings";
50 public static final String AUTHORITIES_FILTER_KEY = "authorities";
51 public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types";
53 private final Handler mHandler = new Handler();
55 private UserManager mUm;
56 private Object mStatusChangeListenerHandle;
57 protected AuthenticatorHelper mAuthenticatorHelper;
58 protected UserHandle mUserHandle;
60 private java.text.DateFormat mDateFormat;
61 private java.text.DateFormat mTimeFormat;
64 public void onCreate(Bundle icicle) {
65 super.onCreate(icicle);
66 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
67 final Activity activity = getActivity();
68 mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), mUm, getArguments(),
69 activity.getIntent().getExtras());
70 mAuthenticatorHelper = new AuthenticatorHelper(activity, mUserHandle, this);
74 * Overload to handle account updates.
77 public void onAccountsUpdate(UserHandle userHandle) {
82 * Overload to handle authenticator description updates
84 protected void onAuthDescriptionsUpdated() {
89 * Overload to handle sync state updates.
91 protected void onSyncStateUpdated() {
96 public void onActivityCreated(Bundle savedInstanceState) {
97 super.onActivityCreated(savedInstanceState);
99 final Activity activity = getActivity();
101 mDateFormat = DateFormat.getDateFormat(activity);
102 mTimeFormat = DateFormat.getTimeFormat(activity);
106 public void onResume() {
108 mStatusChangeListenerHandle = ContentResolver.addStatusChangeListener(
109 ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
110 | ContentResolver.SYNC_OBSERVER_TYPE_STATUS
111 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS,
112 mSyncStatusObserver);
113 onSyncStateUpdated();
117 public void onPause() {
119 ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle);
122 private SyncStatusObserver mSyncStatusObserver = new SyncStatusObserver() {
123 public void onStatusChanged(int which) {
124 mHandler.post(new Runnable() {
126 onSyncStateUpdated();
132 public ArrayList<String> getAuthoritiesForAccountType(String type) {
133 return mAuthenticatorHelper.getAuthoritiesForAccountType(type);
137 * Gets the preferences.xml file associated with a particular account type.
138 * @param accountType the type of account
139 * @return a PreferenceScreen inflated from accountPreferenceId.
141 public PreferenceScreen addPreferencesForType(final String accountType,
142 PreferenceScreen parent) {
143 PreferenceScreen prefs = null;
144 if (mAuthenticatorHelper.containsAccountType(accountType)) {
145 AuthenticatorDescription desc = null;
147 desc = mAuthenticatorHelper.getAccountTypeDescription(accountType);
148 if (desc != null && desc.accountPreferencesId != 0) {
149 // Load the context of the target package, then apply the
150 // base Settings theme (no references to local resources)
151 // and create a context theme wrapper so that we get the
152 // correct text colors. Control colors will still be wrong,
153 // but there's not much we can do about it since we can't
154 // reference local color resources.
155 final Context targetCtx = getActivity().createPackageContextAsUser(
156 desc.packageName, 0, mUserHandle);
157 final Theme baseTheme = getResources().newTheme();
158 baseTheme.applyStyle(com.android.settings.R.style.Theme_SettingsBase, true);
159 final Context themedCtx = new ContextThemeWrapper(targetCtx, 0);
160 themedCtx.getTheme().setTo(baseTheme);
161 prefs = getPreferenceManager().inflateFromResource(themedCtx,
162 desc.accountPreferencesId, parent);
164 } catch (PackageManager.NameNotFoundException e) {
165 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
166 } catch (Resources.NotFoundException e) {
167 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
173 public void updateAuthDescriptions() {
174 mAuthenticatorHelper.updateAuthDescriptions(getActivity());
175 onAuthDescriptionsUpdated();
178 protected Drawable getDrawableForType(final String accountType) {
179 return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType);
182 protected CharSequence getLabelForType(final String accountType) {
183 return mAuthenticatorHelper.getLabelForType(getActivity(), accountType);
186 protected String formatSyncDate(Date date) {
187 // TODO: Switch to using DateUtils.formatDateTime
188 return mDateFormat.format(date) + " " + mTimeFormat.format(date);