• 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;
18 
19 import java.util.ArrayList;
20 import java.util.HashMap;
21 import java.util.Map;
22 
23 import com.google.android.collect.Maps;
24 
25 import android.accounts.Account;
26 import android.accounts.AccountManager;
27 import android.accounts.AuthenticatorDescription;
28 import android.accounts.OnAccountsUpdateListener;
29 import android.content.ContentResolver;
30 import android.content.Context;
31 import android.content.SyncAdapterType;
32 import android.content.SyncStatusObserver;
33 import android.content.pm.PackageManager;
34 import android.graphics.drawable.Drawable;
35 import android.os.Bundle;
36 import android.os.Handler;
37 import android.preference.PreferenceActivity;
38 import android.preference.PreferenceScreen;
39 import android.util.Log;
40 
41 class AccountPreferenceBase extends PreferenceActivity implements OnAccountsUpdateListener {
42     protected static final String TAG = "AccountSettings";
43     public static final String AUTHORITIES_FILTER_KEY = "authorities";
44     private Map<String, AuthenticatorDescription> mTypeToAuthDescription
45             = new HashMap<String, AuthenticatorDescription>();
46     protected AuthenticatorDescription[] mAuthDescs;
47     private final Handler mHandler = new Handler();
48     private Object mStatusChangeListenerHandle;
49     private HashMap<String, ArrayList<String>> mAccountTypeToAuthorities = null;
50 
51     @Override
onCreate(Bundle icicle)52     public void onCreate(Bundle icicle) {
53         super.onCreate(icicle);
54     }
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     protected 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     protected 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                 Log.d(TAG, "added authority " + sa.authority + " to accountType " + sa.accountType);
117                 authorities.add(sa.authority);
118             }
119         }
120         return mAccountTypeToAuthorities.get(type);
121     }
122 
123     /**
124      * Gets an icon associated with a particular account type. If none found, return null.
125      * @param accountType the type of account
126      * @return a drawable for the icon or null if one cannot be found.
127      */
getDrawableForType(final String accountType)128     protected Drawable getDrawableForType(final String accountType) {
129         Drawable icon = null;
130         if (mTypeToAuthDescription.containsKey(accountType)) {
131             try {
132                 AuthenticatorDescription desc = (AuthenticatorDescription)
133                         mTypeToAuthDescription.get(accountType);
134                 Context authContext = createPackageContext(desc.packageName, 0);
135                 icon = authContext.getResources().getDrawable(desc.iconId);
136             } catch (PackageManager.NameNotFoundException e) {
137                 // TODO: place holder icon for missing account icons?
138                 Log.w(TAG, "No icon for account type " + accountType);
139             }
140         }
141         return icon;
142     }
143 
144     /**
145      * Gets the label associated with a particular account type. If none found, return null.
146      * @param accountType the type of account
147      * @return a CharSequence for the label or null if one cannot be found.
148      */
getLabelForType(final String accountType)149     protected CharSequence getLabelForType(final String accountType) {
150         CharSequence label = null;
151         if (mTypeToAuthDescription.containsKey(accountType)) {
152              try {
153                  AuthenticatorDescription desc = (AuthenticatorDescription)
154                          mTypeToAuthDescription.get(accountType);
155                  Context authContext = createPackageContext(desc.packageName, 0);
156                  label = authContext.getResources().getText(desc.labelId);
157              } catch (PackageManager.NameNotFoundException e) {
158                  Log.w(TAG, "No label for account type " + ", type " + accountType);
159              }
160         }
161         return label;
162     }
163 
164     /**
165      * Gets the preferences.xml file associated with a particular account type.
166      * @param accountType the type of account
167      * @return a PreferenceScreen inflated from accountPreferenceId.
168      */
addPreferencesForType(final String accountType)169     protected PreferenceScreen addPreferencesForType(final String accountType) {
170         PreferenceScreen prefs = null;
171         if (mTypeToAuthDescription.containsKey(accountType)) {
172             AuthenticatorDescription desc = null;
173             try {
174                 desc = (AuthenticatorDescription) mTypeToAuthDescription.get(accountType);
175                 if (desc != null && desc.accountPreferencesId != 0) {
176                     Context authContext = createPackageContext(desc.packageName, 0);
177                     prefs = getPreferenceManager().inflateFromResource(authContext,
178                             desc.accountPreferencesId, getPreferenceScreen());
179                 }
180             } catch (PackageManager.NameNotFoundException e) {
181                 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
182             }
183         }
184         return prefs;
185     }
186 
187     /**
188      * Updates provider icons. Subclasses should call this in onCreate()
189      * and update any UI that depends on AuthenticatorDescriptions in onAuthDescriptionsUpdated().
190      */
updateAuthDescriptions()191     protected void updateAuthDescriptions() {
192         mAuthDescs = AccountManager.get(this).getAuthenticatorTypes();
193         for (int i = 0; i < mAuthDescs.length; i++) {
194             mTypeToAuthDescription.put(mAuthDescs[i].type, mAuthDescs[i]);
195         }
196         onAuthDescriptionsUpdated();
197     }
198 }
199