blob: 9c7f739a68a83e2e6bb9b28508dd2d9c78c95fc5 [file] [log] [blame]
Amith Yamasani43c69782010-12-01 09:04:36 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.settings.accounts;
18
Jeff Sharkey7a7ea2b2011-08-30 19:59:36 -070019import android.accounts.Account;
Guang Zhu167ba2a2012-10-19 17:55:05 -070020import android.app.ActivityManager;
Amith Yamasani43c69782010-12-01 09:04:36 -080021import android.content.Context;
Fan Zhang76dbb212018-04-05 14:44:13 -070022import android.text.TextUtils;
Amith Yamasani43c69782010-12-01 09:04:36 -080023import android.util.AttributeSet;
Guang Zhu167ba2a2012-10-19 17:55:05 -070024import android.util.Log;
Amith Yamasani43c69782010-12-01 09:04:36 -080025import android.view.View;
Amith Yamasani43c69782010-12-01 09:04:36 -080026import android.widget.TextView;
Jeff Sharkey7a7ea2b2011-08-30 19:59:36 -070027
Fan Zhangc7162cd2018-06-18 15:21:41 -070028import androidx.preference.PreferenceViewHolder;
29import androidx.preference.SwitchPreference;
30
Fan Zhang23f8d592018-08-28 15:11:40 -070031import com.android.settings.R;
32import com.android.settingslib.widget.AnimatedImageView;
33
Fabrice Di Meglio1bc99652014-10-16 17:16:26 -070034public class SyncStateSwitchPreference extends SwitchPreference {
Amith Yamasani43c69782010-12-01 09:04:36 -080035
36 private boolean mIsActive = false;
37 private boolean mIsPending = false;
38 private boolean mFailed = false;
39 private Account mAccount;
40 private String mAuthority;
Svetoslav Ganov6f9bf1d2016-07-11 19:34:21 -070041 private String mPackageName;
42 private int mUid;
Amith Yamasani43c69782010-12-01 09:04:36 -080043
44 /**
45 * A mode for this preference where clicking does a one-time sync instead of
46 * toggling whether the provider will do autosync.
47 */
48 private boolean mOneTimeSyncMode = false;
Alon Albert5b698302011-02-07 12:33:49 -080049
Fabrice Di Meglio1bc99652014-10-16 17:16:26 -070050 public SyncStateSwitchPreference(Context context, AttributeSet attrs) {
Jason Monkfb6e1182015-06-25 15:03:26 -040051 super(context, attrs, 0, R.style.SyncSwitchPreference);
Amith Yamasani43c69782010-12-01 09:04:36 -080052 mAccount = null;
53 mAuthority = null;
Svetoslav Ganov6f9bf1d2016-07-11 19:34:21 -070054 mPackageName = null;
55 mUid = 0;
Amith Yamasani43c69782010-12-01 09:04:36 -080056 }
57
Svetoslav Ganov6f9bf1d2016-07-11 19:34:21 -070058 public SyncStateSwitchPreference(Context context, Account account, String authority,
59 String packageName, int uid) {
Jason Monkfb6e1182015-06-25 15:03:26 -040060 super(context, null, 0, R.style.SyncSwitchPreference);
Svetoslav Ganov6f9bf1d2016-07-11 19:34:21 -070061 setup(account, authority, packageName, uid);
Jason Monkcd66b4a2016-05-31 16:18:17 -040062 }
63
Svetoslav Ganov6f9bf1d2016-07-11 19:34:21 -070064 public void setup(Account account, String authority, String packageName, int uid) {
Amith Yamasani43c69782010-12-01 09:04:36 -080065 mAccount = account;
66 mAuthority = authority;
Svetoslav Ganov6f9bf1d2016-07-11 19:34:21 -070067 mPackageName = packageName;
68 mUid = uid;
Fan Zhang76dbb212018-04-05 14:44:13 -070069 setVisible(!TextUtils.isEmpty(mAuthority));
Jason Monkcd66b4a2016-05-31 16:18:17 -040070 notifyChanged();
Amith Yamasani43c69782010-12-01 09:04:36 -080071 }
72
73 @Override
Jason Monk39b46742015-09-10 15:52:51 -040074 public void onBindViewHolder(PreferenceViewHolder view) {
75 super.onBindViewHolder(view);
Jeff Sharkey7a7ea2b2011-08-30 19:59:36 -070076 final AnimatedImageView syncActiveView = (AnimatedImageView) view.findViewById(
77 R.id.sync_active);
78 final View syncFailedView = view.findViewById(R.id.sync_failed);
Amith Yamasani43c69782010-12-01 09:04:36 -080079
Jeff Sharkey7a7ea2b2011-08-30 19:59:36 -070080 final boolean activeVisible = mIsActive || mIsPending;
81 syncActiveView.setVisibility(activeVisible ? View.VISIBLE : View.GONE);
82 syncActiveView.setAnimating(mIsActive);
Amith Yamasani43c69782010-12-01 09:04:36 -080083
Jeff Sharkey7a7ea2b2011-08-30 19:59:36 -070084 final boolean failedVisible = mFailed && !activeVisible;
85 syncFailedView.setVisibility(failedVisible ? View.VISIBLE : View.GONE);
Alon Albert5b698302011-02-07 12:33:49 -080086
Jason Monk52a302e2015-10-28 14:34:53 -040087 View switchView = view.findViewById(com.android.internal.R.id.switch_widget);
Amith Yamasani43c69782010-12-01 09:04:36 -080088 if (mOneTimeSyncMode) {
Fabrice Di Meglio1bc99652014-10-16 17:16:26 -070089 switchView.setVisibility(View.GONE);
Alon Albert5b698302011-02-07 12:33:49 -080090
Amith Yamasani43c69782010-12-01 09:04:36 -080091 /*
92 * Override the summary. Fill in the %1$s with the existing summary
93 * (what ends up happening is the old summary is shown on the next
94 * line).
95 */
96 TextView summary = (TextView) view.findViewById(android.R.id.summary);
97 summary.setText(getContext().getString(R.string.sync_one_time_sync, getSummary()));
98 } else {
Fabrice Di Meglio1bc99652014-10-16 17:16:26 -070099 switchView.setVisibility(View.VISIBLE);
Amith Yamasani43c69782010-12-01 09:04:36 -0800100 }
101 }
102
103 /**
104 * Set whether the sync is active.
105 * @param isActive whether or not the sync is active
106 */
107 public void setActive(boolean isActive) {
108 mIsActive = isActive;
109 notifyChanged();
110 }
111
112 /**
113 * Set whether a sync is pending.
114 * @param isPending whether or not the sync is pending
115 */
116 public void setPending(boolean isPending) {
117 mIsPending = isPending;
118 notifyChanged();
119 }
120
121 /**
122 * Set whether the corresponding sync failed.
123 * @param failed whether or not the sync failed
124 */
125 public void setFailed(boolean failed) {
126 mFailed = failed;
127 notifyChanged();
128 }
129
130 /**
131 * Sets whether the preference is in one-time sync mode.
132 */
133 public void setOneTimeSyncMode(boolean oneTimeSyncMode) {
134 mOneTimeSyncMode = oneTimeSyncMode;
135 notifyChanged();
136 }
Alon Albert5b698302011-02-07 12:33:49 -0800137
Amith Yamasani43c69782010-12-01 09:04:36 -0800138 /**
139 * Gets whether the preference is in one-time sync mode.
140 */
141 public boolean isOneTimeSyncMode() {
142 return mOneTimeSyncMode;
143 }
144
145 @Override
146 protected void onClick() {
147 // When we're in one-time sync mode, we don't want a click to change the
Fabrice Di Meglio1bc99652014-10-16 17:16:26 -0700148 // Switch state
Amith Yamasani43c69782010-12-01 09:04:36 -0800149 if (!mOneTimeSyncMode) {
Guang Zhu167ba2a2012-10-19 17:55:05 -0700150 if (ActivityManager.isUserAMonkey()) {
151 Log.d("SyncState", "ignoring monkey's attempt to flip sync state");
152 } else {
153 super.onClick();
154 }
Alon Albert5b698302011-02-07 12:33:49 -0800155 }
Amith Yamasani43c69782010-12-01 09:04:36 -0800156 }
157
158 public Account getAccount() {
159 return mAccount;
160 }
161
162 public String getAuthority() {
163 return mAuthority;
164 }
Svetoslav Ganov6f9bf1d2016-07-11 19:34:21 -0700165
166 public String getPackageName() {
167 return mPackageName;
168 };
169
170 public int getUid() {
171 return mUid;
172 };
Amith Yamasani43c69782010-12-01 09:04:36 -0800173}