• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package com.android.settings.accounts;
18 
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.Map;
22 
23 import com.android.settings.SettingsPreferenceFragment;
24 import com.google.android.collect.Maps;
25 
26 import android.accounts.Account;
27 import android.accounts.AccountManager;
28 import android.accounts.AuthenticatorDescription;
29 import android.accounts.OnAccountsUpdateListener;
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import android.content.SyncAdapterType;
33 import android.content.SyncStatusObserver;
34 import android.content.pm.PackageManager;
35 import android.content.res.Resources;
36 import android.graphics.drawable.Drawable;
37 import android.os.Bundle;
38 import android.os.Handler;
39 import android.preference.PreferenceActivity;
40 import android.preference.PreferenceScreen;
41 import android.util.Log;
42 
43 class AccountPreferenceBase extends SettingsPreferenceFragment
44         implements OnAccountsUpdateListener {
45 
46     protected static final String TAG = "AccountSettings";
47     public static final String AUTHORITIES_FILTER_KEY = "authorities";
48     public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types";
49     private Map<String, AuthenticatorDescription> mTypeToAuthDescription
50             = new HashMap<String, AuthenticatorDescription>();
51     protected AuthenticatorDescription[] mAuthDescs;
52     private final Handler mHandler = new Handler();
53     private Object mStatusChangeListenerHandle;
54     private HashMap<String, ArrayList<String>> mAccountTypeToAuthorities = null;
55 
56     /**
57      * Overload to handle account updates.
58      */
onAccountsUpdated(Account[] accounts)59     public void onAccountsUpdated(Account[] accounts) {
60 
61     }
62 
63     /**
64      * Overload to handle authenticator description updates
65      */
onAuthDescriptionsUpdated()66     protected void onAuthDescriptionsUpdated() {
67 
68     }
69 
70     /**
71      * Overload to handle sync state updates.
72      */
onSyncStateUpdated()73     protected void onSyncStateUpdated() {
74 
75     }
76 
77     @Override
onResume()78     public void onResume() {
79         super.onResume();
80         mStatusChangeListenerHandle = ContentResolver.addStatusChangeListener(
81                 ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
82                 | ContentResolver.SYNC_OBSERVER_TYPE_STATUS
83                 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS,
84                 mSyncStatusObserver);
85         onSyncStateUpdated();
86     }
87 
88     @Override
onPause()89     public void onPause() {
90         super.onPause();
91         ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle);
92     }
93 
94 
95     private SyncStatusObserver mSyncStatusObserver = new SyncStatusObserver() {
96         public void onStatusChanged(int which) {
97             mHandler.post(new Runnable() {
98                 public void run() {
99                     onSyncStateUpdated();
100                 }
101             });
102         }
103     };
104 
getAuthoritiesForAccountType(String type)105     public ArrayList<String> getAuthoritiesForAccountType(String type) {
106         if (mAccountTypeToAuthorities == null) {
107             mAccountTypeToAuthorities = Maps.newHashMap();
108             SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes();
109             for (int i = 0, n = syncAdapters.length; i < n; i++) {
110                 final SyncAdapterType sa = syncAdapters[i];
111                 ArrayList<String> authorities = mAccountTypeToAuthorities.get(sa.accountType);
112                 if (authorities == null) {
113                     authorities = new ArrayList<String>();
114                     mAccountTypeToAuthorities.put(sa.accountType, authorities);
115                 }
116                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
117                     Log.d(TAG, "added authority " + sa.authority + " to accountType "
118                             + sa.accountType);
119                 }
120                 authorities.add(sa.authority);
121             }
122         }
123         return mAccountTypeToAuthorities.get(type);
124     }
125 
126     /**
127      * Gets an icon associated with a particular account type. If none found, return null.
128      * @param accountType the type of account
129      * @return a drawable for the icon or null if one cannot be found.
130      */
getDrawableForType(final String accountType)131     protected Drawable getDrawableForType(final String accountType) {
132         Drawable icon = null;
133         if (mTypeToAuthDescription.containsKey(accountType)) {
134             try {
135                 AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
136                 Context authContext = getActivity().createPackageContext(desc.packageName, 0);
137                 icon = authContext.getResources().getDrawable(desc.iconId);
138             } catch (PackageManager.NameNotFoundException e) {
139                 // TODO: place holder icon for missing account icons?
140                 Log.w(TAG, "No icon name for account type " + accountType);
141             } catch (Resources.NotFoundException e) {
142                 // TODO: place holder icon for missing account icons?
143                 Log.w(TAG, "No icon resource for account type " + accountType);
144             }
145         }
146         return icon;
147     }
148 
149     /**
150      * Gets the label associated with a particular account type. If none found, return null.
151      * @param accountType the type of account
152      * @return a CharSequence for the label or null if one cannot be found.
153      */
getLabelForType(final String accountType)154     protected CharSequence getLabelForType(final String accountType) {
155         CharSequence label = null;
156         if (mTypeToAuthDescription.containsKey(accountType)) {
157             try {
158                 AuthenticatorDescription desc = mTypeToAuthDescription.get(accountType);
159                 Context authContext = getActivity().createPackageContext(desc.packageName, 0);
160                 label = authContext.getResources().getText(desc.labelId);
161             } catch (PackageManager.NameNotFoundException e) {
162                 Log.w(TAG, "No label name for account type " + accountType);
163             } catch (Resources.NotFoundException e) {
164                 Log.w(TAG, "No label icon for account type " + accountType);
165             }
166         }
167         return label;
168     }
169 
170     /**
171      * Gets the preferences.xml file associated with a particular account type.
172      * @param accountType the type of account
173      * @return a PreferenceScreen inflated from accountPreferenceId.
174      */
addPreferencesForType(final String accountType)175     protected PreferenceScreen addPreferencesForType(final String accountType) {
176         PreferenceScreen prefs = null;
177         if (mTypeToAuthDescription.containsKey(accountType)) {
178             AuthenticatorDescription desc = null;
179             try {
180                 desc = mTypeToAuthDescription.get(accountType);
181                 if (desc != null && desc.accountPreferencesId != 0) {
182                     Context authContext = getActivity().createPackageContext(desc.packageName, 0);
183                     prefs = getPreferenceManager().inflateFromResource(authContext,
184                             desc.accountPreferencesId, getPreferenceScreen());
185                 }
186             } catch (PackageManager.NameNotFoundException e) {
187                 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
188             } catch (Resources.NotFoundException e) {
189                 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
190             }
191         }
192         return prefs;
193     }
194 
195     /**
196      * Updates provider icons. Subclasses should call this in onCreate()
197      * and update any UI that depends on AuthenticatorDescriptions in onAuthDescriptionsUpdated().
198      */
updateAuthDescriptions()199     protected void updateAuthDescriptions() {
200         mAuthDescs = AccountManager.get(getActivity()).getAuthenticatorTypes();
201         for (int i = 0; i < mAuthDescs.length; i++) {
202             mTypeToAuthDescription.put(mAuthDescs[i].type, mAuthDescs[i]);
203         }
204         onAuthDescriptionsUpdated();
205     }
206 }
207