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