• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.util.AttributeSet;
23 import android.util.Log;
24 import android.view.View;
25 import android.widget.TextView;
26 
27 import androidx.preference.PreferenceViewHolder;
28 import androidx.preference.SwitchPreference;
29 
30 import com.android.settingslib.widget.AnimatedImageView;
31 import com.android.tv.settings.R;
32 
33 public class SyncStateSwitchPreference extends SwitchPreference {
34 
35     private boolean mIsActive = false;
36     private boolean mIsPending = false;
37     private boolean mFailed = false;
38     private Account mAccount;
39     private String mAuthority;
40 
41     /**
42      * A mode for this preference where clicking does a one-time sync instead of
43      * toggling whether the provider will do autosync.
44      */
45     private boolean mOneTimeSyncMode = false;
46 
SyncStateSwitchPreference(Context context, AttributeSet attrs)47     public SyncStateSwitchPreference(Context context, AttributeSet attrs) {
48         super(context, attrs, 0, R.style.SyncSwitchPreference);
49         mAccount = null;
50         mAuthority = null;
51     }
52 
SyncStateSwitchPreference(Context context, Account account, String authority)53     public SyncStateSwitchPreference(Context context, Account account, String authority) {
54         super(context, null, 0, R.style.SyncSwitchPreference);
55         mAccount = account;
56         mAuthority = authority;
57     }
58 
59     @Override
onBindViewHolder(PreferenceViewHolder view)60     public void onBindViewHolder(PreferenceViewHolder view) {
61         super.onBindViewHolder(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(R.id.switch_widget_container);
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      */
setActive(boolean isActive)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      */
setPending(boolean isPending)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      */
setFailed(boolean failed)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      * @param oneTimeSyncMode true for one-time sync mode
119      */
setOneTimeSyncMode(boolean oneTimeSyncMode)120     public void setOneTimeSyncMode(boolean oneTimeSyncMode) {
121         mOneTimeSyncMode = oneTimeSyncMode;
122         notifyChanged();
123     }
124 
125     /**
126      * Gets whether the preference is in one-time sync mode.
127      */
isOneTimeSyncMode()128     public boolean isOneTimeSyncMode() {
129         return mOneTimeSyncMode;
130     }
131 
132     @Override
onClick()133     protected void onClick() {
134         // When we're in one-time sync mode, we don't want a click to change the
135         // Switch state
136         if (!mOneTimeSyncMode) {
137             if (ActivityManager.isUserAMonkey()) {
138                 Log.d("SyncState", "ignoring monkey's attempt to flip sync state");
139             } else {
140                 super.onClick();
141             }
142         }
143     }
144 
getAccount()145     public Account getAccount() {
146         return mAccount;
147     }
148 
getAuthority()149     public String getAuthority() {
150         return mAuthority;
151     }
152 
153 }
154