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.support.v14.preference.SwitchPreference; 23 import android.support.v7.preference.PreferenceViewHolder; 24 import android.util.AttributeSet; 25 import android.util.Log; 26 import android.view.View; 27 import android.widget.TextView; 28 29 import com.android.settings.R; 30 import com.android.settingslib.widget.AnimatedImageView; 31 32 public class SyncStateSwitchPreference extends SwitchPreference { 33 34 private boolean mIsActive = false; 35 private boolean mIsPending = false; 36 private boolean mFailed = false; 37 private Account mAccount; 38 private String mAuthority; 39 40 /** 41 * A mode for this preference where clicking does a one-time sync instead of 42 * toggling whether the provider will do autosync. 43 */ 44 private boolean mOneTimeSyncMode = false; 45 46 public SyncStateSwitchPreference(Context context, AttributeSet attrs) { 47 super(context, attrs, 0, R.style.SyncSwitchPreference); 48 mAccount = null; 49 mAuthority = null; 50 } 51 52 public SyncStateSwitchPreference(Context context, Account account, String authority) { 53 super(context, null, 0, R.style.SyncSwitchPreference); 54 setup(account, authority); 55 } 56 57 public void setup(Account account, String authority) { 58 mAccount = account; 59 mAuthority = authority; 60 notifyChanged(); 61 } 62 63 @Override 64 public void onBindViewHolder(PreferenceViewHolder view) { 65 super.onBindViewHolder(view); 66 final AnimatedImageView syncActiveView = (AnimatedImageView) view.findViewById( 67 R.id.sync_active); 68 final View syncFailedView = view.findViewById(R.id.sync_failed); 69 70 final boolean activeVisible = mIsActive || mIsPending; 71 syncActiveView.setVisibility(activeVisible ? View.VISIBLE : View.GONE); 72 syncActiveView.setAnimating(mIsActive); 73 74 final boolean failedVisible = mFailed && !activeVisible; 75 syncFailedView.setVisibility(failedVisible ? View.VISIBLE : View.GONE); 76 77 View switchView = view.findViewById(com.android.internal.R.id.switch_widget); 78 if (mOneTimeSyncMode) { 79 switchView.setVisibility(View.GONE); 80 81 /* 82 * Override the summary. Fill in the %1$s with the existing summary 83 * (what ends up happening is the old summary is shown on the next 84 * line). 85 */ 86 TextView summary = (TextView) view.findViewById(android.R.id.summary); 87 summary.setText(getContext().getString(R.string.sync_one_time_sync, getSummary())); 88 } else { 89 switchView.setVisibility(View.VISIBLE); 90 } 91 } 92 93 /** 94 * Set whether the sync is active. 95 * @param isActive whether or not the sync is active 96 */ 97 public void setActive(boolean isActive) { 98 mIsActive = isActive; 99 notifyChanged(); 100 } 101 102 /** 103 * Set whether a sync is pending. 104 * @param isPending whether or not the sync is pending 105 */ 106 public void setPending(boolean isPending) { 107 mIsPending = isPending; 108 notifyChanged(); 109 } 110 111 /** 112 * Set whether the corresponding sync failed. 113 * @param failed whether or not the sync failed 114 */ 115 public void setFailed(boolean failed) { 116 mFailed = failed; 117 notifyChanged(); 118 } 119 120 /** 121 * Sets whether the preference is in one-time sync mode. 122 */ 123 public void setOneTimeSyncMode(boolean oneTimeSyncMode) { 124 mOneTimeSyncMode = oneTimeSyncMode; 125 notifyChanged(); 126 } 127 128 /** 129 * Gets whether the preference is in one-time sync mode. 130 */ 131 public boolean isOneTimeSyncMode() { 132 return mOneTimeSyncMode; 133 } 134 135 @Override 136 protected void onClick() { 137 // When we're in one-time sync mode, we don't want a click to change the 138 // Switch state 139 if (!mOneTimeSyncMode) { 140 if (ActivityManager.isUserAMonkey()) { 141 Log.d("SyncState", "ignoring monkey's attempt to flip sync state"); 142 } else { 143 super.onClick(); 144 } 145 } 146 } 147 148 public Account getAccount() { 149 return mAccount; 150 } 151 152 public String getAuthority() { 153 return mAuthority; 154 } 155 } 156