summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/SyncButton.java
blob: c95477d44f708d9f2590e9f5ebaf088082b788af (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
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
                if (mView != null) {
                    update(mView.getContext());
                }
            }
        };
    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(Context context) {
        if (getSyncState(context)) {
            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) {
        // If ON turn OFF else turn ON
        if (getSyncState(context)) {
            ContentResolver.setMasterSyncAutomatically(false);
        } else {
            ContentResolver.setMasterSyncAutomatically(true);
        }
    }

    @Override
    protected boolean handleLongClick(Context context) {
        Intent intent = new Intent("android.settings.SYNC_SETTINGS");
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
        return true;
    }

    private boolean getSyncState(Context context) {
        return ContentResolver.getMasterSyncAutomatically();
    }
}