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