Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 1 | /* |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 2 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 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 | |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 20 | import android.app.Activity; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 21 | import android.content.ContentResolver; |
| 22 | import android.content.Context; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 23 | import android.content.SyncStatusObserver; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 24 | import android.graphics.drawable.Drawable; |
| 25 | import android.os.Bundle; |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 26 | import android.os.UserHandle; |
| 27 | import android.os.UserManager; |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 28 | import android.text.format.DateFormat; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 31 | import com.android.settings.SettingsPreferenceFragment; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 32 | import com.android.settings.Utils; |
Tony Mantler | b280b0a | 2015-11-17 15:13:29 -0800 | [diff] [blame] | 33 | import com.android.settingslib.accounts.AuthenticatorHelper; |
Fan Zhang | a7f1355 | 2017-10-31 12:46:23 -0700 | [diff] [blame] | 34 | import com.android.settingslib.utils.ThreadUtils; |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 35 | |
Chris Wren | 8a963ba | 2015-03-20 10:29:14 -0400 | [diff] [blame] | 36 | abstract class AccountPreferenceBase extends SettingsPreferenceFragment |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 37 | implements AuthenticatorHelper.OnAccountsUpdateListener { |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 38 | |
Doris Ling | f074f0f | 2017-02-24 15:20:05 -0800 | [diff] [blame] | 39 | protected static final String TAG = "AccountPreferenceBase"; |
Fan Zhang | 2829958 | 2016-12-08 11:28:31 -0800 | [diff] [blame] | 40 | protected static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 41 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 42 | public static final String AUTHORITIES_FILTER_KEY = "authorities"; |
| 43 | public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types"; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 44 | |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 45 | private UserManager mUm; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 46 | private Object mStatusChangeListenerHandle; |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 47 | protected AuthenticatorHelper mAuthenticatorHelper; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 48 | protected UserHandle mUserHandle; |
Doris Ling | 13ac884 | 2017-02-10 14:22:58 -0800 | [diff] [blame] | 49 | protected AccountTypePreferenceLoader mAccountTypePreferenceLoader; |
Alexandra Gherghina | 1eb3f31 | 2014-06-10 14:01:10 +0100 | [diff] [blame] | 50 | |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 51 | private java.text.DateFormat mDateFormat; |
| 52 | private java.text.DateFormat mTimeFormat; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 53 | |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 54 | @Override |
| 55 | public void onCreate(Bundle icicle) { |
| 56 | super.onCreate(icicle); |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 57 | mUm = (UserManager) getSystemService(Context.USER_SERVICE); |
Alexandra Gherghina | 7d748c0 | 2014-06-27 12:33:42 +0100 | [diff] [blame] | 58 | final Activity activity = getActivity(); |
| 59 | mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), mUm, getArguments(), |
| 60 | activity.getIntent().getExtras()); |
Tony Mantler | b280b0a | 2015-11-17 15:13:29 -0800 | [diff] [blame] | 61 | mAuthenticatorHelper = new AuthenticatorHelper(activity, mUserHandle, this); |
Doris Ling | 13ac884 | 2017-02-10 14:22:58 -0800 | [diff] [blame] | 62 | mAccountTypePreferenceLoader = |
| 63 | new AccountTypePreferenceLoader(this, mAuthenticatorHelper, mUserHandle); |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 64 | } |
| 65 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Overload to handle account updates. |
| 68 | */ |
Alexandra Gherghina | 3939cd7 | 2014-06-04 10:02:55 +0100 | [diff] [blame] | 69 | @Override |
| 70 | public void onAccountsUpdate(UserHandle userHandle) { |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 71 | |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Overload to handle authenticator description updates |
| 76 | */ |
| 77 | protected void onAuthDescriptionsUpdated() { |
| 78 | |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Overload to handle sync state updates. |
| 83 | */ |
| 84 | protected void onSyncStateUpdated() { |
| 85 | |
| 86 | } |
| 87 | |
| 88 | @Override |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 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 |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 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 |
| 110 | public void onPause() { |
| 111 | super.onPause(); |
| 112 | ContentResolver.removeStatusChangeListener(mStatusChangeListenerHandle); |
| 113 | } |
| 114 | |
Fan Zhang | a7f1355 | 2017-10-31 12:46:23 -0700 | [diff] [blame] | 115 | private SyncStatusObserver mSyncStatusObserver = |
| 116 | which -> ThreadUtils.postOnMainThread(() -> onSyncStateUpdated()); |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 117 | |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 118 | public void updateAuthDescriptions() { |
| 119 | mAuthenticatorHelper.updateAuthDescriptions(getActivity()); |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 120 | onAuthDescriptionsUpdated(); |
| 121 | } |
Amith Yamasani | d1ab828 | 2012-05-18 09:50:08 -0700 | [diff] [blame] | 122 | |
| 123 | protected Drawable getDrawableForType(final String accountType) { |
| 124 | return mAuthenticatorHelper.getDrawableForType(getActivity(), accountType); |
| 125 | } |
| 126 | |
| 127 | protected CharSequence getLabelForType(final String accountType) { |
| 128 | return mAuthenticatorHelper.getLabelForType(getActivity(), accountType); |
| 129 | } |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 130 | } |