OSDN Git Service

Import translations. DO NOT MERGE
[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 import com.android.settingslib.accounts.AuthenticatorHelper;
41
42 import java.util.ArrayList;
43 import java.util.Date;
44
45 abstract class AccountPreferenceBase extends SettingsPreferenceFragment
46         implements AuthenticatorHelper.OnAccountsUpdateListener {
47
48     protected static final String TAG = "AccountSettings";
49
50     public static final String AUTHORITIES_FILTER_KEY = "authorities";
51     public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types";
52
53     private final Handler mHandler = new Handler();
54
55     private UserManager mUm;
56     private Object mStatusChangeListenerHandle;
57     protected AuthenticatorHelper mAuthenticatorHelper;
58     protected UserHandle mUserHandle;
59
60     private java.text.DateFormat mDateFormat;
61     private java.text.DateFormat mTimeFormat;
62
63     @Override
64     public void onCreate(Bundle icicle) {
65         super.onCreate(icicle);
66         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
67         final Activity activity = getActivity();
68         mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), mUm, getArguments(),
69                 activity.getIntent().getExtras());
70         mAuthenticatorHelper = new AuthenticatorHelper(activity, mUserHandle, this);
71     }
72
73     /**
74      * Overload to handle account updates.
75      */
76     @Override
77     public void onAccountsUpdate(UserHandle userHandle) {
78
79     }
80
81     /**
82      * Overload to handle authenticator description updates
83      */
84     protected void onAuthDescriptionsUpdated() {
85
86     }
87
88     /**
89      * Overload to handle sync state updates.
90      */
91     protected void onSyncStateUpdated() {
92
93     }
94
95     @Override
96     public void onActivityCreated(Bundle savedInstanceState) {
97         super.onActivityCreated(savedInstanceState);
98
99         final Activity activity = getActivity();
100
101         mDateFormat = DateFormat.getDateFormat(activity);
102         mTimeFormat = DateFormat.getTimeFormat(activity);
103     }
104
105     @Override
106     public void onResume() {
107         super.onResume();
108         mStatusChangeListenerHandle = ContentResolver.addStatusChangeListener(
109                 ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
110                 | ContentResolver.SYNC_OBSERVER_TYPE_STATUS
111                 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS,
112                 mSyncStatusObserver);
113         onSyncStateUpdated();
114     }
115
116     @Override
117     public void onPause() {
118         super.onPause();
119         ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle);
120     }
121
122     private SyncStatusObserver mSyncStatusObserver = new SyncStatusObserver() {
123         public void onStatusChanged(int which) {
124             mHandler.post(new Runnable() {
125                 public void run() {
126                     onSyncStateUpdated();
127                 }
128             });
129         }
130     };
131
132     public ArrayList<String> getAuthoritiesForAccountType(String type) {
133         return mAuthenticatorHelper.getAuthoritiesForAccountType(type);
134     }
135
136     /**
137      * Gets the preferences.xml file associated with a particular account type.
138      * @param accountType the type of account
139      * @return a PreferenceScreen inflated from accountPreferenceId.
140      */
141     public PreferenceScreen addPreferencesForType(final String accountType,
142             PreferenceScreen parent) {
143         PreferenceScreen prefs = null;
144         if (mAuthenticatorHelper.containsAccountType(accountType)) {
145             AuthenticatorDescription desc = null;
146             try {
147                 desc = mAuthenticatorHelper.getAccountTypeDescription(accountType);
148                 if (desc != null && desc.accountPreferencesId != 0) {
149                     // Load the context of the target package, then apply the
150                     // base Settings theme (no references to local resources)
151                     // and create a context theme wrapper so that we get the
152                     // correct text colors. Control colors will still be wrong,
153                     // but there's not much we can do about it since we can't
154                     // reference local color resources.
155                     final Context targetCtx = getActivity().createPackageContextAsUser(
156                             desc.packageName, 0, mUserHandle);
157                     final Theme baseTheme = getResources().newTheme();
158                     baseTheme.applyStyle(com.android.settings.R.style.Theme_SettingsBase, true);
159                     final Context themedCtx = new ContextThemeWrapper(targetCtx, 0);
160                     themedCtx.getTheme().setTo(baseTheme);
161                     prefs = getPreferenceManager().inflateFromResource(themedCtx,
162                             desc.accountPreferencesId, parent);
163                 }
164             } catch (PackageManager.NameNotFoundException e) {
165                 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
166             } catch (Resources.NotFoundException e) {
167                 Log.w(TAG, "Couldn't load preferences.xml file from " + desc.packageName);
168             }
169         }
170         return prefs;
171     }
172
173     public void updateAuthDescriptions() {
174         mAuthenticatorHelper.updateAuthDescriptions(getActivity());
175         onAuthDescriptionsUpdated();
176     }
177
178     protected Drawable getDrawableForType(final String accountType) {
179         return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType);
180     }
181
182     protected CharSequence getLabelForType(final String accountType) {
183         return mAuthenticatorHelper.getLabelForType(getActivity(), accountType);
184     }
185
186     protected String formatSyncDate(Date date) {
187         // TODO: Switch to using DateUtils.formatDateTime
188         return mDateFormat.format(date) + " " + mTimeFormat.format(date);
189     }
190 }