summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/statusbar/powerwidget/LockScreenButton.java
blob: a36c495497f129c0c42cef3db80e0d13b7477a5b (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
package com.android.systemui.statusbar.powerwidget;

import com.android.systemui.R;

import android.app.Activity;
import android.app.KeyguardManager;
import android.app.KeyguardManager.KeyguardLock;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.Gravity;
import android.widget.Toast;

public class LockScreenButton extends PowerButton {

    private static Boolean LOCK_SCREEN_STATE = null;

    private KeyguardLock mLock = null;

    public LockScreenButton() { mType = BUTTON_LOCKSCREEN; }

    @Override
    protected void updateState() {
        getState();
        if (LOCK_SCREEN_STATE == null) {
            mIcon = R.drawable.stat_lock_screen_off;
            mState = STATE_INTERMEDIATE;
        } else if (LOCK_SCREEN_STATE) {
            mIcon = R.drawable.stat_lock_screen_on;
            mState = STATE_ENABLED;
        } else {
            mIcon = R.drawable.stat_lock_screen_off;
            mState = STATE_DISABLED;
        }
    }

    @Override
    protected void toggleState() {
        Context context = mView.getContext();
        getState();
        if(LOCK_SCREEN_STATE == null) {
            Toast msg = Toast.makeText(context, "Not yet initialized", Toast.LENGTH_LONG);
            msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg.getYOffset() / 2);
            msg.show();
        } else {
            getLock(context);
            if (mLock != null) {
                if (LOCK_SCREEN_STATE) {
                  mLock.disableKeyguard();
                  LOCK_SCREEN_STATE = false;
                } else {
                  mLock.reenableKeyguard();
                  LOCK_SCREEN_STATE = true;
                }
            }
        }

        // we're handling this, so just update our buttons now
        // this is UGLY, do it better later >.>
        update();
    }

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

    private KeyguardLock getLock(Context context) {
        if (mLock == null) {
            KeyguardManager keyguardManager = (KeyguardManager)context.
                    getSystemService(Activity.KEYGUARD_SERVICE);
            mLock = keyguardManager.newKeyguardLock(Context.KEYGUARD_SERVICE);
        }
        return mLock;
    }

    private static boolean getState() {
        if (LOCK_SCREEN_STATE == null) {
            LOCK_SCREEN_STATE = true;
        }
        return LOCK_SCREEN_STATE;
    }
}