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.UserHandle;
27 import android.os.UserManager;
28 import android.text.format.DateFormat;
29 import android.util.Log;
30 
31 import com.android.settings.SettingsPreferenceFragment;
32 import com.android.settings.Utils;
33 import com.android.settingslib.accounts.AuthenticatorHelper;
34 import com.android.settingslib.utils.ThreadUtils;
35 
36 abstract class AccountPreferenceBase extends SettingsPreferenceFragment
37         implements AuthenticatorHelper.OnAccountsUpdateListener {
38 
39     protected static final String TAG = "AccountPreferenceBase";
40     protected static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
41 
42     public static final String AUTHORITIES_FILTER_KEY = "authorities";
43     public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types";
44 
45     private UserManager mUm;
46     private Object mStatusChangeListenerHandle;
47     protected AuthenticatorHelper mAuthenticatorHelper;
48     protected UserHandle mUserHandle;
49     protected AccountTypePreferenceLoader mAccountTypePreferenceLoader;
50 
51     private java.text.DateFormat mDateFormat;
52     private java.text.DateFormat mTimeFormat;
53 
54     @Override
onCreate(Bundle icicle)55     public void onCreate(Bundle icicle) {
56         super.onCreate(icicle);
57         mUm = (UserManager) getSystemService(Context.USER_SERVICE);
58         final Activity activity = getActivity();
59         mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), mUm, getArguments(),
60                 activity.getIntent().getExtras());
61         mAuthenticatorHelper = new AuthenticatorHelper(activity, mUserHandle, this);
62         mAccountTypePreferenceLoader =
63             new AccountTypePreferenceLoader(this, mAuthenticatorHelper, mUserHandle);
64     }
65 
66     /**
67      * Overload to handle account updates.
68      */
69     @Override
onAccountsUpdate(UserHandle userHandle)70     public void onAccountsUpdate(UserHandle userHandle) {
71 
72     }
73 
74     /**
75      * Overload to handle authenticator description updates
76      */
onAuthDescriptionsUpdated()77     protected void onAuthDescriptionsUpdated() {
78 
79     }
80 
81     /**
82      * Overload to handle sync state updates.
83      */
onSyncStateUpdated()84     protected void onSyncStateUpdated() {
85 
86     }
87 
88     @Override
onActivityCreated(Bundle savedInstanceState)89     public void onActivityCreated(Bundle savedInstanceState) {
90         super.onActivityCreated(savedInstanceState);
91 
92         final Activity activity = getActivity();
93 
94         mDateFormat = DateFormat.getDateFormat(activity);
95         mTimeFormat = DateFormat.getTimeFormat(activity);
96     }
97 
98     @Override
onResume()99     public void onResume() {
100         super.onResume();
101         mStatusChangeListenerHandle = ContentResolver.addStatusChangeListener(
102                 ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE
103                 | ContentResolver.SYNC_OBSERVER_TYPE_STATUS
104                 | ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS,
105                 mSyncStatusObserver);
106         onSyncStateUpdated();
107     }
108 
109     @Override
onPause()110     public void onPause() {
111         super.onPause();
112         ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle);
113     }
114 
115     private SyncStatusObserver mSyncStatusObserver =
116             which -> ThreadUtils.postOnMainThread(() -> onSyncStateUpdated());
117 
updateAuthDescriptions()118     public void updateAuthDescriptions() {
119         mAuthenticatorHelper.updateAuthDescriptions(getActivity());
120         onAuthDescriptionsUpdated();
121     }
122 
getDrawableForType(final String accountType)123     protected Drawable getDrawableForType(final String accountType) {
124         return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType);
125     }
126 
getLabelForType(final String accountType)127     protected CharSequence getLabelForType(final String accountType) {
128         return mAuthenticatorHelper.getLabelForType(getActivity(), accountType);
129     }
130 }
131