Home | History | Annotate | Download | only in accounts
      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.Date;
     21 import java.util.HashMap;
     22 import java.util.Map;
     23 
     24 import com.android.settings.SettingsPreferenceFragment;
     25 import com.google.android.collect.Maps;
     26 
     27 import android.accounts.Account;
     28 import android.accounts.AccountManager;
     29 import android.accounts.AuthenticatorDescription;
     30 import android.accounts.OnAccountsUpdateListener;
     31 import android.app.Activity;
     32 import android.content.ContentResolver;
     33 import android.content.Context;
     34 import android.content.SyncAdapterType;
     35 import android.content.SyncStatusObserver;
     36 import android.content.pm.PackageManager;
     37 import android.content.res.Resources;
     38 import android.graphics.drawable.Drawable;
     39 import android.os.Bundle;
     40 import android.os.Handler;
     41 import android.preference.PreferenceActivity;
     42 import android.preference.PreferenceScreen;
     43 import android.text.format.DateFormat;
     44 import android.util.Log;
     45 
     46 class AccountPreferenceBase extends SettingsPreferenceFragment
     47         implements OnAccountsUpdateListener {
     48 
     49     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";
     52     private final Handler mHandler = new Handler();
     53     private Object mStatusChangeListenerHandle;
     54     private HashMap<String, ArrayList<String>> mAccountTypeToAuthorities = null;
     55     private AuthenticatorHelper mAuthenticatorHelper = new AuthenticatorHelper();
     56     private java.text.DateFormat mDateFormat;
     57     private java.text.DateFormat mTimeFormat;
     58 
     59     /**
     60      * Overload to handle account updates.
     61      */
     62     public void onAccountsUpdated(Account[] accounts) {
     63 
     64     }
     65 
     66     /**
     67      * Overload to handle authenticator description updates
     68      */
     69     protected void onAuthDescriptionsUpdated() {
     70 
     71     }
     72 
     73     /**
     74      * Overload to handle sync state updates.
     75      */
     76     protected void onSyncStateUpdated() {
     77 
     78     }
     79 
     80     @Override
     81     public void onActivityCreated(Bundle savedInstanceState) {
     82         super.onActivityCreated(savedInstanceState);
     83 
     84         final Activity activity = getActivity();
     85 
     86         mDateFormat = DateFormat.getDateFormat(activity);
     87         mTimeFormat = DateFormat.getTimeFormat(activity);
     88     }
     89 
     90     @Override
     91     public void onResume() {
     92         super.onResume();
     93         mStatusChangeListenerHandle = ContentResolver.addStatusChangeListener(
     94                 ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
     95                 | ContentResolver.SYNC_OBSERVER_TYPE_STATUS
     96                 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS,
     97                 mSyncStatusObserver);
     98         onSyncStateUpdated();
     99     }
    100 
    101     @Override
    102     public void onPause() {
    103         super.onPause();
    104         ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle);
    105     }
    106 
    107     private SyncStatusObserver mSyncStatusObserver = new SyncStatusObserver() {
    108         public void onStatusChanged(int which) {
    109             mHandler.post(new Runnable() {
    110                 public void run() {
    111                     onSyncStateUpdated();
    112                 }
    113             });
    114         }
    115     };
    116 
    117     public ArrayList<String> getAuthoritiesForAccountType(String type) {
    118         if (mAccountTypeToAuthorities == null) {
    119             mAccountTypeToAuthorities = Maps.newHashMap();
    120             SyncAdapterType[] syncAdapters = ContentResolver.getSyncAdapterTypes();
    121             for (int i = 0, n = syncAdapters.length; i < n; i++) {
    122                 final SyncAdapterType sa = syncAdapters[i];
    123                 ArrayList<String> authorities = mAccountTypeToAuthorities.get(sa.accountType);
    124                 if (authorities == null) {
    125                     authorities = new ArrayList<String>();
    126                     mAccountTypeToAuthorities.put(sa.accountType, authorities);
    127                 }
    128                 if (Log.isLoggable(TAG, Log.VERBOSE)) {
    129                     Log.d(TAG, "added authority " + sa.authority + " to accountType "
    130                             + sa.accountType);
    131                 }
    132                 authorities.add(sa.authority);
    133             }
    134         }
    135         return mAccountTypeToAuthorities.get(type);
    136     }
    137 
    138     /**
    139      * Gets the preferences.xml file associated with a particular account type.
    140      * @param accountType the type of account
    141      * @return a PreferenceScreen inflated from accountPreferenceId.
    142      */
    143     public PreferenceScreen addPreferencesForType(final String accountType,
    144             PreferenceScreen parent) {
    145         PreferenceScreen prefs = null;
    146         if (mAuthenticatorHelper.containsAccountType(accountType)) {
    147             AuthenticatorDescription desc = null;
    148             try {
    149                 desc = mAuthenticatorHelper.getAccountTypeDescription(accountType);
    150                 if (desc != null && desc.accountPreferencesId != 0) {
    151                     Context authContext = getActivity().createPackageContext(desc.packageName, 0);
    152                     prefs = getPreferenceManager().inflateFromResource(authContext,
    153                             desc.accountPreferencesId, parent);
    154                 }
    155             } catch (PackageManager.NameNotFoundException e) {
    156                 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
    157             } catch (Resources.NotFoundException e) {
    158                 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
    159             }
    160         }
    161         return prefs;
    162     }
    163 
    164     public void updateAuthDescriptions() {
    165         mAuthenticatorHelper.updateAuthDescriptions(getActivity());
    166         onAuthDescriptionsUpdated();
    167     }
    168 
    169     protected Drawable getDrawableForType(final String accountType) {
    170         return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType);
    171     }
    172 
    173     protected CharSequence getLabelForType(final String accountType) {
    174         return mAuthenticatorHelper.getLabelForType(getActivity(), accountType);
    175     }
    176 
    177     protected String formatSyncDate(Date date) {
    178         // TODO: Switch to using DateUtils.formatDateTime
    179         return mDateFormat.format(date) + " " + mTimeFormat.format(date);
    180     }
    181 }
    182