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