Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.settings.accounts; |
| 18 | |
Jeff Sharkey | 7a7ea2b | 2011-08-30 19:59:36 -0700 | [diff] [blame] | 19 | import android.accounts.Account; |
Guang Zhu | 167ba2a | 2012-10-19 17:55:05 -0700 | [diff] [blame] | 20 | import android.app.ActivityManager; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 21 | import android.content.Context; |
Fan Zhang | 76dbb21 | 2018-04-05 14:44:13 -0700 | [diff] [blame] | 22 | import android.text.TextUtils; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 23 | import android.util.AttributeSet; |
Guang Zhu | 167ba2a | 2012-10-19 17:55:05 -0700 | [diff] [blame] | 24 | import android.util.Log; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 25 | import android.view.View; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 26 | import android.widget.TextView; |
Jeff Sharkey | 7a7ea2b | 2011-08-30 19:59:36 -0700 | [diff] [blame] | 27 | |
Fan Zhang | c7162cd | 2018-06-18 15:21:41 -0700 | [diff] [blame] | 28 | import androidx.preference.PreferenceViewHolder; |
| 29 | import androidx.preference.SwitchPreference; |
| 30 | |
Fan Zhang | 23f8d59 | 2018-08-28 15:11:40 -0700 | [diff] [blame] | 31 | import com.android.settings.R; |
| 32 | import com.android.settingslib.widget.AnimatedImageView; |
| 33 | |
Fabrice Di Meglio | 1bc9965 | 2014-10-16 17:16:26 -0700 | [diff] [blame] | 34 | public class SyncStateSwitchPreference extends SwitchPreference { |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 35 | |
| 36 | private boolean mIsActive = false; |
| 37 | private boolean mIsPending = false; |
| 38 | private boolean mFailed = false; |
| 39 | private Account mAccount; |
| 40 | private String mAuthority; |
Svetoslav Ganov | 6f9bf1d | 2016-07-11 19:34:21 -0700 | [diff] [blame] | 41 | private String mPackageName; |
| 42 | private int mUid; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 43 | |
| 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 Albert | 5b69830 | 2011-02-07 12:33:49 -0800 | [diff] [blame] | 49 | |
Fabrice Di Meglio | 1bc9965 | 2014-10-16 17:16:26 -0700 | [diff] [blame] | 50 | public SyncStateSwitchPreference(Context context, AttributeSet attrs) { |
Jason Monk | fb6e118 | 2015-06-25 15:03:26 -0400 | [diff] [blame] | 51 | super(context, attrs, 0, R.style.SyncSwitchPreference); |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 52 | mAccount = null; |
| 53 | mAuthority = null; |
Svetoslav Ganov | 6f9bf1d | 2016-07-11 19:34:21 -0700 | [diff] [blame] | 54 | mPackageName = null; |
| 55 | mUid = 0; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Svetoslav Ganov | 6f9bf1d | 2016-07-11 19:34:21 -0700 | [diff] [blame] | 58 | public SyncStateSwitchPreference(Context context, Account account, String authority, |
| 59 | String packageName, int uid) { |
Jason Monk | fb6e118 | 2015-06-25 15:03:26 -0400 | [diff] [blame] | 60 | super(context, null, 0, R.style.SyncSwitchPreference); |
Svetoslav Ganov | 6f9bf1d | 2016-07-11 19:34:21 -0700 | [diff] [blame] | 61 | setup(account, authority, packageName, uid); |
Jason Monk | cd66b4a | 2016-05-31 16:18:17 -0400 | [diff] [blame] | 62 | } |
| 63 | |
Svetoslav Ganov | 6f9bf1d | 2016-07-11 19:34:21 -0700 | [diff] [blame] | 64 | public void setup(Account account, String authority, String packageName, int uid) { |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 65 | mAccount = account; |
| 66 | mAuthority = authority; |
Svetoslav Ganov | 6f9bf1d | 2016-07-11 19:34:21 -0700 | [diff] [blame] | 67 | mPackageName = packageName; |
| 68 | mUid = uid; |
Fan Zhang | 76dbb21 | 2018-04-05 14:44:13 -0700 | [diff] [blame] | 69 | setVisible(!TextUtils.isEmpty(mAuthority)); |
Jason Monk | cd66b4a | 2016-05-31 16:18:17 -0400 | [diff] [blame] | 70 | notifyChanged(); |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
Jason Monk | 39b4674 | 2015-09-10 15:52:51 -0400 | [diff] [blame] | 74 | public void onBindViewHolder(PreferenceViewHolder view) { |
| 75 | super.onBindViewHolder(view); |
Jeff Sharkey | 7a7ea2b | 2011-08-30 19:59:36 -0700 | [diff] [blame] | 76 | final AnimatedImageView syncActiveView = (AnimatedImageView) view.findViewById( |
| 77 | R.id.sync_active); |
| 78 | final View syncFailedView = view.findViewById(R.id.sync_failed); |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 79 | |
Jeff Sharkey | 7a7ea2b | 2011-08-30 19:59:36 -0700 | [diff] [blame] | 80 | final boolean activeVisible = mIsActive || mIsPending; |
| 81 | syncActiveView.setVisibility(activeVisible ? View.VISIBLE : View.GONE); |
| 82 | syncActiveView.setAnimating(mIsActive); |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 83 | |
Jeff Sharkey | 7a7ea2b | 2011-08-30 19:59:36 -0700 | [diff] [blame] | 84 | final boolean failedVisible = mFailed && !activeVisible; |
| 85 | syncFailedView.setVisibility(failedVisible ? View.VISIBLE : View.GONE); |
Alon Albert | 5b69830 | 2011-02-07 12:33:49 -0800 | [diff] [blame] | 86 | |
Jason Monk | 52a302e | 2015-10-28 14:34:53 -0400 | [diff] [blame] | 87 | View switchView = view.findViewById(com.android.internal.R.id.switch_widget); |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 88 | if (mOneTimeSyncMode) { |
Fabrice Di Meglio | 1bc9965 | 2014-10-16 17:16:26 -0700 | [diff] [blame] | 89 | switchView.setVisibility(View.GONE); |
Alon Albert | 5b69830 | 2011-02-07 12:33:49 -0800 | [diff] [blame] | 90 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 91 | /* |
| 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 Meglio | 1bc9965 | 2014-10-16 17:16:26 -0700 | [diff] [blame] | 99 | switchView.setVisibility(View.VISIBLE); |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 100 | } |
| 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 Albert | 5b69830 | 2011-02-07 12:33:49 -0800 | [diff] [blame] | 137 | |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 138 | /** |
| 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 Meglio | 1bc9965 | 2014-10-16 17:16:26 -0700 | [diff] [blame] | 148 | // Switch state |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 149 | if (!mOneTimeSyncMode) { |
Guang Zhu | 167ba2a | 2012-10-19 17:55:05 -0700 | [diff] [blame] | 150 | if (ActivityManager.isUserAMonkey()) { |
| 151 | Log.d("SyncState", "ignoring monkey's attempt to flip sync state"); |
| 152 | } else { |
| 153 | super.onClick(); |
| 154 | } |
Alon Albert | 5b69830 | 2011-02-07 12:33:49 -0800 | [diff] [blame] | 155 | } |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | public Account getAccount() { |
| 159 | return mAccount; |
| 160 | } |
| 161 | |
| 162 | public String getAuthority() { |
| 163 | return mAuthority; |
| 164 | } |
Svetoslav Ganov | 6f9bf1d | 2016-07-11 19:34:21 -0700 | [diff] [blame] | 165 | |
| 166 | public String getPackageName() { |
| 167 | return mPackageName; |
| 168 | }; |
| 169 | |
| 170 | public int getUid() { |
| 171 | return mUid; |
| 172 | }; |
Amith Yamasani | 43c6978 | 2010-12-01 09:04:36 -0800 | [diff] [blame] | 173 | } |