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