OSDN Git Service

Fix accessibility for account sync
[android-x86/packages-apps-Settings.git] / src / com / android / settings / accounts / SyncStateSwitchPreference.java
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.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;
28
29 import com.android.settings.R;
30 import com.android.settings.widget.AnimatedImageView;
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
46     public SyncStateSwitchPreference(Context context, AttributeSet attrs) {
47         super(context, attrs, 0, R.style.SyncSwitchPreference);
48         mAccount = null;
49         mAuthority = null;
50     }
51
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
59     public void onBindView(View view) {
60         super.onBindView(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(com.android.internal.R.id.switchWidget);
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      */
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      */
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      */
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      */
118     public void setOneTimeSyncMode(boolean oneTimeSyncMode) {
119         mOneTimeSyncMode = oneTimeSyncMode;
120         notifyChanged();
121     }
122
123     /**
124      * Gets whether the preference is in one-time sync mode.
125      */
126     public boolean isOneTimeSyncMode() {
127         return mOneTimeSyncMode;
128     }
129
130     @Override
131     protected void onClick() {
132         // When we're in one-time sync mode, we don't want a click to change the
133         // Switch state
134         if (!mOneTimeSyncMode) {
135             if (ActivityManager.isUserAMonkey()) {
136                 Log.d("SyncState", "ignoring monkey's attempt to flip sync state");
137             } else {
138                 super.onClick();
139             }
140         }
141     }
142
143     public Account getAccount() {
144         return mAccount;
145     }
146
147     public String getAuthority() {
148         return mAuthority;
149     }
150 }