blob: bdf9c98a0543bb3db9391690c70400ded01d3daf [file] [log] [blame]
Amith Yamasani43c69782010-12-01 09:04:36 -08001/*
Alexandra Gherghina7d748c02014-06-27 12:33:42 +01002
Amith Yamasani43c69782010-12-01 09:04:36 -08003 * 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
18package com.android.settings.accounts;
19
Amith Yamasanid1ab8282012-05-18 09:50:08 -070020import android.app.Activity;
Amith Yamasani43c69782010-12-01 09:04:36 -080021import android.content.ContentResolver;
22import android.content.Context;
Amith Yamasani43c69782010-12-01 09:04:36 -080023import android.content.SyncStatusObserver;
Amith Yamasani43c69782010-12-01 09:04:36 -080024import android.graphics.drawable.Drawable;
25import android.os.Bundle;
Alexandra Gherghina3939cd72014-06-04 10:02:55 +010026import android.os.UserHandle;
27import android.os.UserManager;
Amith Yamasanid1ab8282012-05-18 09:50:08 -070028import android.text.format.DateFormat;
Amith Yamasani43c69782010-12-01 09:04:36 -080029import android.util.Log;
30
Alexandra Gherghina3939cd72014-06-04 10:02:55 +010031import com.android.settings.SettingsPreferenceFragment;
Alexandra Gherghina1eb3f312014-06-10 14:01:10 +010032import com.android.settings.Utils;
Tony Mantlerb280b0a2015-11-17 15:13:29 -080033import com.android.settingslib.accounts.AuthenticatorHelper;
Fan Zhanga7f13552017-10-31 12:46:23 -070034import com.android.settingslib.utils.ThreadUtils;
Alexandra Gherghina3939cd72014-06-04 10:02:55 +010035
Chris Wren8a963ba2015-03-20 10:29:14 -040036abstract class AccountPreferenceBase extends SettingsPreferenceFragment
Alexandra Gherghina3939cd72014-06-04 10:02:55 +010037 implements AuthenticatorHelper.OnAccountsUpdateListener {
Amith Yamasani43c69782010-12-01 09:04:36 -080038
Doris Lingf074f0f2017-02-24 15:20:05 -080039 protected static final String TAG = "AccountPreferenceBase";
Fan Zhang28299582016-12-08 11:28:31 -080040 protected static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
Alexandra Gherghina1eb3f312014-06-10 14:01:10 +010041
Amith Yamasani43c69782010-12-01 09:04:36 -080042 public static final String AUTHORITIES_FILTER_KEY = "authorities";
43 public static final String ACCOUNT_TYPES_FILTER_KEY = "account_types";
Alexandra Gherghina1eb3f312014-06-10 14:01:10 +010044
Alexandra Gherghina3939cd72014-06-04 10:02:55 +010045 private UserManager mUm;
Amith Yamasani43c69782010-12-01 09:04:36 -080046 private Object mStatusChangeListenerHandle;
Alexandra Gherghina3939cd72014-06-04 10:02:55 +010047 protected AuthenticatorHelper mAuthenticatorHelper;
Alexandra Gherghina1eb3f312014-06-10 14:01:10 +010048 protected UserHandle mUserHandle;
Doris Ling13ac8842017-02-10 14:22:58 -080049 protected AccountTypePreferenceLoader mAccountTypePreferenceLoader;
Alexandra Gherghina1eb3f312014-06-10 14:01:10 +010050
Amith Yamasanid1ab8282012-05-18 09:50:08 -070051 private java.text.DateFormat mDateFormat;
52 private java.text.DateFormat mTimeFormat;
Amith Yamasani43c69782010-12-01 09:04:36 -080053
Alexandra Gherghina3939cd72014-06-04 10:02:55 +010054 @Override
55 public void onCreate(Bundle icicle) {
56 super.onCreate(icicle);
Alexandra Gherghina3939cd72014-06-04 10:02:55 +010057 mUm = (UserManager) getSystemService(Context.USER_SERVICE);
Alexandra Gherghina7d748c02014-06-27 12:33:42 +010058 final Activity activity = getActivity();
59 mUserHandle = Utils.getSecureTargetUser(activity.getActivityToken(), mUm, getArguments(),
60 activity.getIntent().getExtras());
Tony Mantlerb280b0a2015-11-17 15:13:29 -080061 mAuthenticatorHelper = new AuthenticatorHelper(activity, mUserHandle, this);
Doris Ling13ac8842017-02-10 14:22:58 -080062 mAccountTypePreferenceLoader =
63 new AccountTypePreferenceLoader(this, mAuthenticatorHelper, mUserHandle);
Alexandra Gherghina3939cd72014-06-04 10:02:55 +010064 }
65
Amith Yamasani43c69782010-12-01 09:04:36 -080066 /**
67 * Overload to handle account updates.
68 */
Alexandra Gherghina3939cd72014-06-04 10:02:55 +010069 @Override
70 public void onAccountsUpdate(UserHandle userHandle) {
Amith Yamasani43c69782010-12-01 09:04:36 -080071
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 Yamasanid1ab8282012-05-18 09:50:08 -070089 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 Yamasani43c69782010-12-01 09:04:36 -080099 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 Zhanga7f13552017-10-31 12:46:23 -0700115 private SyncStatusObserver mSyncStatusObserver =
116 which -> ThreadUtils.postOnMainThread(() -> onSyncStateUpdated());
Amith Yamasani43c69782010-12-01 09:04:36 -0800117
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700118 public void updateAuthDescriptions() {
119 mAuthenticatorHelper.updateAuthDescriptions(getActivity());
Amith Yamasani43c69782010-12-01 09:04:36 -0800120 onAuthDescriptionsUpdated();
121 }
Amith Yamasanid1ab8282012-05-18 09:50:08 -0700122
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 Yamasani43c69782010-12-01 09:04:36 -0800130}