AccountPreferenceBase.java revision 1eb528318cdeb5c5ff1be469435538dad133882b
1/* 2 * Copyright (C) 2008 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.settings.accounts; 18 19import java.util.ArrayList; 20import java.util.Date; 21import java.util.HashMap; 22import java.util.Map; 23 24import com.android.settings.SettingsPreferenceFragment; 25 26import com.google.android.collect.Maps; 27 28import android.accounts.Account; 29import android.accounts.AccountManager; 30import android.accounts.AuthenticatorDescription; 31import android.accounts.OnAccountsUpdateListener; 32import android.app.Activity; 33import android.content.ContentResolver; 34import android.content.Context; 35import android.content.SyncAdapterType; 36import android.content.SyncStatusObserver; 37import android.content.pm.PackageManager; 38import android.content.res.Resources; 39import android.content.res.Resources.Theme; 40import android.graphics.drawable.Drawable; 41import android.os.Bundle; 42import android.os.Handler; 43import android.preference.PreferenceScreen; 44import android.text.format.DateFormat; 45import android.util.Log; 46import android.view.ContextThemeWrapper; 47 48class AccountPreferenceBase extends SettingsPreferenceFragment 49 implements OnAccountsUpdateListener { 50 51 protected static final String TAG = "AccountSettings"; 52 public static final String AUTHORITIES_FILTER_KEY = "authorities"; 53 public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types"; 54 private final Handler mHandler = new Handler(); 55 private Object mStatusChangeListenerHandle; 56 private HashMap<String, ArrayList<String>> mAccountTypeToAuthorities = null; 57 private AuthenticatorHelper mAuthenticatorHelper = new AuthenticatorHelper(); 58 private java.text.DateFormat mDateFormat; 59 private java.text.DateFormat mTimeFormat; 60 61 /** 62 * Overload to handle account updates. 63 */ 64 public void onAccountsUpdated(Account[] accounts) { 65 66 } 67 68 /** 69 * Overload to handle authenticator description updates 70 */ 71 protected void onAuthDescriptionsUpdated() { 72 73 } 74 75 /** 76 * Overload to handle sync state updates. 77 */ 78 protected void onSyncStateUpdated() { 79 80 } 81 82 @Override 83 public void onActivityCreated(Bundle savedInstanceState) { 84 super.onActivityCreated(savedInstanceState); 85 86 final Activity activity = getActivity(); 87 88 mDateFormat = DateFormat.getDateFormat(activity); 89 mTimeFormat = DateFormat.getTimeFormat(activity); 90 } 91 92 @Override 93 public void onResume() { 94 super.onResume(); 95 mStatusChangeListenerHandle = ContentResolver.addStatusChangeListener( 96 ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE 97 | ContentResolver.SYNC_OBSERVER_TYPE_STATUS 98 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, 99 mSyncStatusObserver); 100 onSyncStateUpdated(); 101 } 102 103 @Override 104 public void onPause() { 105 super.onPause(); 106 ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle); 107 } 108 109 private SyncStatusObserver mSyncStatusObserver = new SyncStatusObserver() { 110 public void onStatusChanged(int which) { 111 mHandler.post(new Runnable() { 112 public void run() { 113 onSyncStateUpdated(); 114 } 115 }); 116 } 117 }; 118 119 public ArrayList<String> getAuthoritiesForAccountType(String type) { 120 if (mAccountTypeToAuthorities == null) { 121 mAccountTypeToAuthorities = Maps.newHashMap(); 122 SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes(); 123 for (int i = 0, n = syncAdapters.length; i < n; i++) { 124 final SyncAdapterType sa = syncAdapters[i]; 125 ArrayList<String> authorities = mAccountTypeToAuthorities.get(sa.accountType); 126 if (authorities == null) { 127 authorities = new ArrayList<String>(); 128 mAccountTypeToAuthorities.put(sa.accountType, authorities); 129 } 130 if (Log.isLoggable(TAG, Log.VERBOSE)) { 131 Log.d(TAG, "added authority " + sa.authority + " to accountType " 132 + sa.accountType); 133 } 134 authorities.add(sa.authority); 135 } 136 } 137 return mAccountTypeToAuthorities.get(type); 138 } 139 140 /** 141 * Gets the preferences.xml file associated with a particular account type. 142 * @param accountType the type of account 143 * @return a PreferenceScreen inflated from accountPreferenceId. 144 */ 145 public PreferenceScreen addPreferencesForType(final String accountType, 146 PreferenceScreen parent) { 147 PreferenceScreen prefs = null; 148 if (mAuthenticatorHelper.containsAccountType(accountType)) { 149 AuthenticatorDescription desc = null; 150 try { 151 desc = mAuthenticatorHelper.getAccountTypeDescription(accountType); 152 if (desc != null && desc.accountPreferencesId != 0) { 153 // Load the context of the target package, then apply the 154 // base Settings theme (no references to local resources) 155 // and create a context theme wrapper so that we get the 156 // correct text colors. Control colors will still be wrong, 157 // but there's not much we can do about it since we can't 158 // reference local color resources. 159 final Context targetCtx = getActivity().createPackageContext( 160 desc.packageName, 0); 161 final Theme baseTheme = getResources().newTheme(); 162 baseTheme.applyStyle(com.android.settings.R.style.Theme_SettingsBase, true); 163 final Context themedCtx = new ContextThemeWrapper(targetCtx, 0); 164 themedCtx.getTheme().setTo(baseTheme); 165 prefs = getPreferenceManager().inflateFromResource(themedCtx, 166 desc.accountPreferencesId, parent); 167 } 168 } catch (PackageManager.NameNotFoundException e) { 169 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName); 170 } catch (Resources.NotFoundException e) { 171 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName); 172 } 173 } 174 return prefs; 175 } 176 177 public void updateAuthDescriptions() { 178 mAuthenticatorHelper.updateAuthDescriptions(getActivity()); 179 onAuthDescriptionsUpdated(); 180 } 181 182 protected Drawable getDrawableForType(final String accountType) { 183 return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType); 184 } 185 186 protected CharSequence getLabelForType(final String accountType) { 187 return mAuthenticatorHelper.getLabelForType(getActivity(), accountType); 188 } 189 190 protected String formatSyncDate(Date date) { 191 // TODO: Switch to using DateUtils.formatDateTime 192 return mDateFormat.format(date) + " " + mTimeFormat.format(date); 193 } 194} 195