summaryrefslogtreecommitdiffstats
path: root/services/java/com/android/server/status/widget/LockScreenButton.java
blob: e1b205d7b833937bc360419ebdebaba896de5f91 (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
package com.android.server.status.widget;

import com.android.internal.R;
import com.android.server.status.widget.PowerButton;

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

public class LockScreenButton extends PowerButton {

    static Boolean lockScreen = null;

    public static final String LOCK_SCREEN = "lockScreen";
    static LockScreenButton ownButton = null;
    KeyguardLock lock;

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

    public void updateState(Context context) {
        getState(context);
        if (lockScreen == null) {
            currentIcon = R.drawable.stat_lock_screen_off;
            currentState = PowerButton.STATE_INTERMEDIATE;
        } else if (lockScreen) {
            currentIcon = R.drawable.stat_lock_screen_on;
            currentState = PowerButton.STATE_ENABLED;
        } else {
            currentIcon = R.drawable.stat_lock_screen_off;
            currentState = PowerButton.STATE_DISABLED;
        }
    }

    /**
     * Toggles the state of GPS.
     *
     * @param context
     */
    public void toggleState(Context context) {
        getState(context);
        if(lockScreen == 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 (lockScreen && lock != null) {
                lock.disableKeyguard();
                lockScreen = false;
            } else if (lock != null) {
                lock.reenableKeyguard();
                lockScreen = true;
            }
        }
    }

    /**
     * Gets the state of GPS location.
     *
     * @param context
     * @return true if enabled.
     */
    private static boolean getState(Context context) {
        if (lockScreen == null) {
            lockScreen = true;
        }
        return lockScreen;
    }


    public static LockScreenButton getInstance() {
        if (ownButton==null) ownButton = new LockScreenButton();

        return ownButton;
    }

    @Override
    void initButton(int position) {
    }
}