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;
25import com.google.android.collect.Maps;
26
27import android.accounts.Account;
28import android.accounts.AccountManager;
29import android.accounts.AuthenticatorDescription;
30import android.accounts.OnAccountsUpdateListener;
31import android.app.Activity;
32import android.content.ContentResolver;
33import android.content.Context;
34import android.content.SyncAdapterType;
35import android.content.SyncStatusObserver;
36import android.content.pm.PackageManager;
37import android.content.res.Resources;
38import android.graphics.drawable.Drawable;
39import android.os.Bundle;
40import android.os.Handler;
41import android.preference.PreferenceActivity;
42import android.preference.PreferenceScreen;
43import android.text.format.DateFormat;
44import android.util.Log;
45
46class 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