blob: ffb587761f680e6c96c9027cb4cb2dff0981d267 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
package com.android.systemui.statusbar.powerwidget;
import com.android.systemui.R;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SyncStatusObserver;
import android.net.ConnectivityManager;
import android.util.Log;
import android.view.View;
public class SyncButton extends PowerButton {
private static final String TAG = "SyncButton";
public SyncButton() { mType = BUTTON_SYNC; }
private SyncStatusObserver mSyncObserver = new SyncStatusObserver() {
public void onStatusChanged(int which) {
// update state/view if something happened
update();
}
};
private Object mSyncObserverHandle = null;
@Override
protected void setupButton(View view) {
super.setupButton(view);
if(mView == null && mSyncObserverHandle != null) {
Log.i(TAG, "Unregistering sync state listener");
ContentResolver.removeStatusChangeListener(mSyncObserverHandle);
mSyncObserverHandle = null;
} else if(mView != null && mSyncObserverHandle == null) {
Log.i(TAG, "Registering sync state listener");
mSyncObserverHandle = ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS, mSyncObserver);
}
}
@Override
protected void updateState() {
if (getSyncState(mView.getContext())) {
mIcon = R.drawable.stat_sync_on;
mState = STATE_ENABLED;
} else {
mIcon = R.drawable.stat_sync_off;
mState = STATE_DISABLED;
}
}
@Override
protected void toggleState() {
Context context = mView.getContext();
ConnectivityManager connManager = (ConnectivityManager)context
.getSystemService(Context.CONNECTIVITY_SERVICE);
boolean backgroundData = getBackgroundDataState(context);
boolean sync = ContentResolver.getMasterSyncAutomatically();
// four cases to handle:
// setting toggled from off to on:
// 1. background data was off, sync was off: turn on both
if (!backgroundData && !sync) {
connManager.setBackgroundDataSetting(true);
ContentResolver.setMasterSyncAutomatically(true);
}
// 2. background data was off, sync was on: turn on background data
if (!backgroundData && sync) {
connManager.setBackgroundDataSetting(true);
}
// 3. background data was on, sync was off: turn on sync
if (backgroundData && !sync) {
ContentResolver.setMasterSyncAutomatically(true);
}
// setting toggled from on to off:
// 4. background data was on, sync was on: turn off sync
if (backgroundData && sync) {
ContentResolver.setMasterSyncAutomatically(false);
}
}
@Override
protected boolean handleLongClick() {
Intent intent = new Intent("android.settings.SYNC_SETTINGS");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mView.getContext().startActivity(intent);
return true;
}
private static boolean getBackgroundDataState(Context context) {
ConnectivityManager connManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
return connManager.getBackgroundDataSetting();
}
private static boolean getSyncState(Context context) {
boolean backgroundData = getBackgroundDataState(context);
boolean sync = ContentResolver.getMasterSyncAutomatically();
return backgroundData && sync;
}
}
|