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 19 import android.accounts.Account; 20 import android.app.ActivityManager; 21 import android.content.Context; 22 import android.text.TextUtils; 23 import android.util.AttributeSet; 24 import android.util.Log; 25 import android.view.View; 26 import android.widget.TextView; 27 28 import androidx.preference.PreferenceViewHolder; 29 import androidx.preference.SwitchPreference; 30 31 import com.android.settings.R; 32 import com.android.settingslib.widget.AnimatedImageView; 33 34 public class SyncStateSwitchPreference extends SwitchPreference { 35 36 private boolean mIsActive = false; 37 private boolean mIsPending = false; 38 private boolean mFailed = false; 39 private Account mAccount; 40 private String mAuthority; 41 private String mPackageName; 42 private int mUid; 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; 49 SyncStateSwitchPreference(Context context, AttributeSet attrs)50 public SyncStateSwitchPreference(Context context, AttributeSet attrs) { 51 super(context, attrs, 0, R.style.SyncSwitchPreference); 52 mAccount = null; 53 mAuthority = null; 54 mPackageName = null; 55 mUid = 0; 56 } 57 SyncStateSwitchPreference(Context context, Account account, String authority, String packageName, int uid)58 public SyncStateSwitchPreference(Context context, Account account, String authority, 59 String packageName, int uid) { 60 super(context, null, 0, R.style.SyncSwitchPreference); 61 setup(account, authority, packageName, uid); 62 } 63 setup(Account account, String authority, String packageName, int uid)64 public void setup(Account account, String authority, String packageName, int uid) { 65 mAccount = account; 66 mAuthority = authority; 67 mPackageName = packageName; 68 mUid = uid; 69 setVisible(!TextUtils.isEmpty(mAuthority)); 70 notifyChanged(); 71 } 72 73 @Override onBindViewHolder(PreferenceViewHolder view)74 public void onBindViewHolder(PreferenceViewHolder view) { 75 super.onBindViewHolder(view); 76 final AnimatedImageView syncActiveView = (AnimatedImageView) view.findViewById( 77 R.id.sync_active); 78 final View syncFailedView = view.findViewById(R.id.sync_failed); 79 80 final boolean activeVisible = mIsActive || mIsPending; 81 syncActiveView.setVisibility(activeVisible ? View.VISIBLE : View.GONE); 82 syncActiveView.setAnimating(mIsActive); 83 84 final boolean failedVisible = mFailed && !activeVisible; 85 syncFailedView.setVisibility(failedVisible ? View.VISIBLE : View.GONE); 86 87 View switchView = view.findViewById(com.android.internal.R.id.switch_widget); 88 if (mOneTimeSyncMode) { 89 switchView.setVisibility(View.GONE); 90 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 { 99 switchView.setVisibility(View.VISIBLE); 100 } 101 } 102 103 /** 104 * Set whether the sync is active. 105 * @param isActive whether or not the sync is active 106 */ setActive(boolean isActive)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 */ setPending(boolean isPending)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 */ setFailed(boolean failed)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 */ setOneTimeSyncMode(boolean oneTimeSyncMode)133 public void setOneTimeSyncMode(boolean oneTimeSyncMode) { 134 mOneTimeSyncMode = oneTimeSyncMode; 135 notifyChanged(); 136 } 137 138 /** 139 * Gets whether the preference is in one-time sync mode. 140 */ isOneTimeSyncMode()141 public boolean isOneTimeSyncMode() { 142 return mOneTimeSyncMode; 143 } 144 145 @Override onClick()146 protected void onClick() { 147 // When we're in one-time sync mode, we don't want a click to change the 148 // Switch state 149 if (!mOneTimeSyncMode) { 150 if (ActivityManager.isUserAMonkey()) { 151 Log.d("SyncState", "ignoring monkey's attempt to flip sync state"); 152 } else { 153 super.onClick(); 154 } 155 } 156 } 157 getAccount()158 public Account getAccount() { 159 return mAccount; 160 } 161 getAuthority()162 public String getAuthority() { 163 return mAuthority; 164 } 165 getPackageName()166 public String getPackageName() { 167 return mPackageName; 168 }; 169 getUid()170 public int getUid() { 171 return mUid; 172 }; 173 } 174