1 /*
2  * Copyright (C) 2015 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.tv.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.settingslib.widget.AnimatedImageView;
30 import com.android.tv.settings.R;
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 
SyncStateSwitchPreference(Context context, AttributeSet attrs)46     public SyncStateSwitchPreference(Context context, AttributeSet attrs) {
47         super(context, attrs, 0, R.style.SyncSwitchPreference);
48         mAccount = null;
49         mAuthority = null;
50     }
51 
SyncStateSwitchPreference(Context context, Account account, String authority)52     public SyncStateSwitchPreference(Context context, Account account, String authority) {
53         super(context, null, 0, R.style.SyncSwitchPreference);
54         mAccount = account;
55         mAuthority = authority;
56     }
57 
58     @Override
onBindViewHolder(PreferenceViewHolder view)59     public void onBindViewHolder(PreferenceViewHolder view) {
60         super.onBindViewHolder(view);
61         final AnimatedImageView syncActiveView = (AnimatedImageView) view.findViewById(
62                 R.id.sync_active);
63         final View syncFailedView = view.findViewById(R.id.sync_failed);
64 
65         final boolean activeVisible = mIsActive || mIsPending;
66         syncActiveView.setVisibility(activeVisible ? View.VISIBLE : View.GONE);
67         syncActiveView.setAnimating(mIsActive);
68 
69         final boolean failedVisible = mFailed && !activeVisible;
70         syncFailedView.setVisibility(failedVisible ? View.VISIBLE : View.GONE);
71 
72         View switchView = view.findViewById(R.id.switch_widget_container);
73         if (mOneTimeSyncMode) {
74             switchView.setVisibility(View.GONE);
75 
76             /*
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
79              * line).
80              */
81             TextView summary = (TextView) view.findViewById(android.R.id.summary);
82             summary.setText(getContext().getString(R.string.sync_one_time_sync, getSummary()));
83         } else {
84             switchView.setVisibility(View.VISIBLE);
85         }
86     }
87 
88     /**
89      * Set whether the sync is active.
90      * @param isActive whether or not the sync is active
91      */
setActive(boolean isActive)92     public void setActive(boolean isActive) {
93         mIsActive = isActive;
94         notifyChanged();
95     }
96 
97     /**
98      * Set whether a sync is pending.
99      * @param isPending whether or not the sync is pending
100      */
setPending(boolean isPending)101     public void setPending(boolean isPending) {
102         mIsPending = isPending;
103         notifyChanged();
104     }
105 
106     /**
107      * Set whether the corresponding sync failed.
108      * @param failed whether or not the sync failed
109      */
setFailed(boolean failed)110     public void setFailed(boolean failed) {
111         mFailed = failed;
112         notifyChanged();
113     }
114 
115     /**
116      * Sets whether the preference is in one-time sync mode.
117      * @param oneTimeSyncMode true for one-time sync mode
118      */
setOneTimeSyncMode(boolean oneTimeSyncMode)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      */
isOneTimeSyncMode()127     public boolean isOneTimeSyncMode() {
128         return mOneTimeSyncMode;
129     }
130 
131     @Override
onClick()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 
getAccount()144     public Account getAccount() {
145         return mAccount;
146     }
147 
getAuthority()148     public String getAuthority() {
149         return mAuthority;
150     }
151 
152 }
153