• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.Activity;
21 import android.content.ContentResolver;
22 import android.content.Context;
23 import android.content.SyncStatusObserver;
24 import android.graphics.drawable.Drawable;
25 import android.os.Bundle;
26 import android.os.Handler;
27 import android.os.UserHandle;
28 import android.os.UserManager;
29 import android.support.v7.preference.PreferenceScreen;
30 import android.text.format.DateFormat;
31 import android.util.Log;
32 
33 import com.android.settings.SettingsPreferenceFragment;
34 import com.android.settings.Utils;
35 import com.android.settingslib.accounts.AuthenticatorHelper;
36 
37 import java.util.ArrayList;
38 import java.util.Date;
39 
40 abstract class AccountPreferenceBase extends SettingsPreferenceFragment
41         implements AuthenticatorHelper.OnAccountsUpdateListener {
42 
43     protected static final String TAG = "AccountPreferenceBase";
44     protected static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
45 
46     public static final String AUTHORITIES_FILTER_KEY = "authorities";
47     public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types";
48 
49     private final Handler mHandler = new Handler();
50 
51     private UserManager mUm;
52     private Object mStatusChangeListenerHandle;
53     protected AuthenticatorHelper mAuthenticatorHelper;
54     protected UserHandle mUserHandle;
55     protected AccountTypePreferenceLoader mAccountTypePreferenceLoader;
56 
57     private java.text.DateFormat mDateFormat;
58     private java.text.DateFormat mTimeFormat;
59 
60     @Override
onCreate(Bundle icicle)61     public void onCreate(Bundle icicle) {
62         super.onCreate(icicle);
63         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
64         final Activity activity = getActivity();
65         mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), mUm, getArguments(),
66                 activity.getIntent().getExtras());
67         mAuthenticatorHelper = new AuthenticatorHelper(activity, mUserHandle, this);
68         mAccountTypePreferenceLoader =
69             new AccountTypePreferenceLoader(this, mAuthenticatorHelper, mUserHandle);
70     }
71 
72     /**
73      * Overload to handle account updates.
74      */
75     @Override
onAccountsUpdate(UserHandle userHandle)76     public void onAccountsUpdate(UserHandle userHandle) {
77 
78     }
79 
80     /**
81      * Overload to handle authenticator description updates
82      */
onAuthDescriptionsUpdated()83     protected void onAuthDescriptionsUpdated() {
84 
85     }
86 
87     /**
88      * Overload to handle sync state updates.
89      */
onSyncStateUpdated()90     protected void onSyncStateUpdated() {
91 
92     }
93 
94     @Override
onActivityCreated(Bundle savedInstanceState)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
onResume()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
onPause()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 
getAuthoritiesForAccountType(String type)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      */
addPreferencesForType(final String accountType, PreferenceScreen parent)140     public PreferenceScreen addPreferencesForType(final String accountType,
141             PreferenceScreen parent) {
142         return mAccountTypePreferenceLoader.addPreferencesForType(accountType, parent);
143     }
144 
updateAuthDescriptions()145     public void updateAuthDescriptions() {
146         mAuthenticatorHelper.updateAuthDescriptions(getActivity());
147         onAuthDescriptionsUpdated();
148     }
149 
getDrawableForType(final String accountType)150     protected Drawable getDrawableForType(final String accountType) {
151         return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType);
152     }
153 
getLabelForType(final String accountType)154     protected CharSequence getLabelForType(final String accountType) {
155         return mAuthenticatorHelper.getLabelForType(getActivity(), accountType);
156     }
157 
formatSyncDate(Date date)158     protected String formatSyncDate(Date date) {
159         // TODO: Switch to using DateUtils.formatDateTime
160         return mDateFormat.format(date) + " " + mTimeFormat.format(date);
161     }
162 }
163