2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.accounts;
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.view.ViewGroup;
27 import android.widget.TextView;
29 import com.android.settings.R;
30 import com.android.settings.widget.AnimatedImageView;
32 public class SyncStateSwitchPreference extends SwitchPreference {
34 private boolean mIsActive = false;
35 private boolean mIsPending = false;
36 private boolean mFailed = false;
37 private Account mAccount;
38 private String mAuthority;
41 * A mode for this preference where clicking does a one-time sync instead of
42 * toggling whether the provider will do autosync.
44 private boolean mOneTimeSyncMode = false;
46 public SyncStateSwitchPreference(Context context, AttributeSet attrs) {
47 super(context, attrs, 0, R.style.SyncSwitchPreference);
52 public SyncStateSwitchPreference(Context context, Account account, String authority) {
53 super(context, null, 0, R.style.SyncSwitchPreference);
55 mAuthority = authority;
59 public void onBindView(View view) {
60 super.onBindView(view);
61 final AnimatedImageView syncActiveView = (AnimatedImageView) view.findViewById(
63 final View syncFailedView = view.findViewById(R.id.sync_failed);
65 final boolean activeVisible = mIsActive || mIsPending;
66 syncActiveView.setVisibility(activeVisible ? View.VISIBLE : View.GONE);
67 syncActiveView.setAnimating(mIsActive);
69 final boolean failedVisible = mFailed && !activeVisible;
70 syncFailedView.setVisibility(failedVisible ? View.VISIBLE : View.GONE);
72 View switchView = view.findViewById(com.android.internal.R.id.switchWidget);
73 if (mOneTimeSyncMode) {
74 switchView.setVisibility(View.GONE);
77 * Override the summary. Fill in the %1$s with the existing summary
78 * (what ends up happening is the old summary is shown on the next
81 TextView summary = (TextView) view.findViewById(android.R.id.summary);
82 summary.setText(getContext().getString(R.string.sync_one_time_sync, getSummary()));
84 switchView.setVisibility(View.VISIBLE);
89 * Set whether the sync is active.
90 * @param isActive whether or not the sync is active
92 public void setActive(boolean isActive) {
98 * Set whether a sync is pending.
99 * @param isPending whether or not the sync is pending
101 public void setPending(boolean isPending) {
102 mIsPending = isPending;
107 * Set whether the corresponding sync failed.
108 * @param failed whether or not the sync failed
110 public void setFailed(boolean failed) {
116 * Sets whether the preference is in one-time sync mode.
118 public void setOneTimeSyncMode(boolean oneTimeSyncMode) {
119 mOneTimeSyncMode = oneTimeSyncMode;
124 * Gets whether the preference is in one-time sync mode.
126 public boolean isOneTimeSyncMode() {
127 return mOneTimeSyncMode;
131 protected void onClick() {
132 // When we're in one-time sync mode, we don't want a click to change the
134 if (!mOneTimeSyncMode) {
135 if (ActivityManager.isUserAMonkey()) {
136 Log.d("SyncState", "ignoring monkey's attempt to flip sync state");
143 public Account getAccount() {
147 public String getAuthority() {