OSDN Git Service

6bb5b2853b63b8097ba7ff46021897970bc64f00
[android-x86/packages-apps-Settings.git] / src / com / android / settings / accounts / AccountPreferenceBase.java
1 /*
2
3  * Copyright (C) 2008 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 package com.android.settings.accounts;
19
20 import android.accounts.AuthenticatorDescription;
21 import android.app.Activity;
22 import android.content.ContentResolver;
23 import android.content.Context;
24 import android.content.SyncStatusObserver;
25 import android.content.pm.PackageManager;
26 import android.content.res.Resources;
27 import android.content.res.Resources.Theme;
28 import android.graphics.drawable.Drawable;
29 import android.os.Bundle;
30 import android.os.Handler;
31 import android.os.UserHandle;
32 import android.os.UserManager;
33 import android.support.v7.preference.PreferenceScreen;
34 import android.text.format.DateFormat;
35 import android.util.Log;
36 import android.view.ContextThemeWrapper;
37
38 import com.android.settings.SettingsPreferenceFragment;
39 import com.android.settings.Utils;
40
41 import java.util.ArrayList;
42 import java.util.Date;
43
44 abstract class AccountPreferenceBase extends SettingsPreferenceFragment
45         implements AuthenticatorHelper.OnAccountsUpdateListener {
46
47     protected static final String TAG = "AccountSettings";
48
49     public static final String AUTHORITIES_FILTER_KEY = "authorities";
50     public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types";
51
52     private final Handler mHandler = new Handler();
53
54     private UserManager mUm;
55     private Object mStatusChangeListenerHandle;
56     protected AuthenticatorHelper mAuthenticatorHelper;
57     protected UserHandle mUserHandle;
58
59     private java.text.DateFormat mDateFormat;
60     private java.text.DateFormat mTimeFormat;
61
62     @Override
63     public void onCreate(Bundle icicle) {
64         super.onCreate(icicle);
65         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
66         final Activity activity = getActivity();
67         mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), mUm, getArguments(),
68                 activity.getIntent().getExtras());
69         mAuthenticatorHelper = new AuthenticatorHelper(activity, mUserHandle, mUm, this);
70     }
71
72     /**
73      * Overload to handle account updates.
74      */
75     @Override
76     public void onAccountsUpdate(UserHandle userHandle) {
77
78     }
79
80     /**
81      * Overload to handle authenticator description updates
82      */
83     protected void onAuthDescriptionsUpdated() {
84
85     }
86
87     /**
88      * Overload to handle sync state updates.
89      */
90     protected void onSyncStateUpdated() {
91
92     }
93
94     @Override
95     public void onActivityCreated(Bundle savedInstanceState) {
96         super.onActivityCreated(savedInstanceState);
97
98         final Activity activity = getActivity();
99
100         mDateFormat = DateFormat.getDateFormat(activity);
101         mTimeFormat = DateFormat.getTimeFormat(activity);
102     }
103
104     @Override
105     public void onResume() {
106         super.onResume();
107         mStatusChangeListenerHandle = ContentResolver.addStatusChangeListener(
108                 ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
109                 | ContentResolver.SYNC_OBSERVER_TYPE_STATUS
110                 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS,
111                 mSyncStatusObserver);
112         onSyncStateUpdated();
113     }
114
115     @Override
116     public void onPause() {
117         super.onPause();
118         ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle);
119     }
120
121     private SyncStatusObserver mSyncStatusObserver = new SyncStatusObserver() {
122         public void onStatusChanged(int which) {
123             mHandler.post(new Runnable() {
124                 public void run() {
125                     onSyncStateUpdated();
126                 }
127             });
128         }
129     };
130
131     public ArrayList<String> getAuthoritiesForAccountType(String type) {
132         return mAuthenticatorHelper.getAuthoritiesForAccountType(type);
133     }
134
135     /**
136      * Gets the preferences.xml file associated with a particular account type.
137      * @param accountType the type of account
138      * @return a PreferenceScreen inflated from accountPreferenceId.
139      */
140     public PreferenceScreen addPreferencesForType(final String accountType,
141             PreferenceScreen parent) {
142         PreferenceScreen prefs = null;
143         if (mAuthenticatorHelper.containsAccountType(accountType)) {
144             AuthenticatorDescription desc = null;
145             try {
146                 desc = mAuthenticatorHelper.getAccountTypeDescription(accountType);
147                 if (desc != null && desc.accountPreferencesId != 0) {
148                     // Load the context of the target package, then apply the
149                     // base Settings theme (no references to local resources)
150                     // and create a context theme wrapper so that we get the
151                     // correct text colors. Control colors will still be wrong,
152                     // but there's not much we can do about it since we can't
153                     // reference local color resources.
154                     final Context targetCtx = getActivity().createPackageContextAsUser(
155                             desc.packageName, 0, mUserHandle);
156                     final Theme baseTheme = getResources().newTheme();
157                     baseTheme.applyStyle(com.android.settings.R.style.Theme_SettingsBase, true);
158                     final Context themedCtx = new ContextThemeWrapper(targetCtx, 0);
159                     themedCtx.getTheme().setTo(baseTheme);
160                     prefs = getPreferenceManager().inflateFromResource(themedCtx,
161                             desc.accountPreferencesId, parent);
162                 }
163             } catch (PackageManager.NameNotFoundException e) {
164                 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
165             } catch (Resources.NotFoundException e) {
166                 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
167             }
168         }
169         return prefs;
170     }
171
172     public void updateAuthDescriptions() {
173         mAuthenticatorHelper.updateAuthDescriptions(getActivity());
174         onAuthDescriptionsUpdated();
175     }
176
177     protected Drawable getDrawableForType(final String accountType) {
178         return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType);
179     }
180
181     protected CharSequence getLabelForType(final String accountType) {
182         return mAuthenticatorHelper.getLabelForType(getActivity(), accountType);
183     }
184
185     protected String formatSyncDate(Date date) {
186         // TODO: Switch to using DateUtils.formatDateTime
187         return mDateFormat.format(date) + " " + mTimeFormat.format(date);
188     }
189 }