package com.lukekorth.pebblelocker.helpers;

import android.app.KeyguardManager;
import android.content.Context;
import android.os.PowerManager;
import android.preference.PreferenceManager;

import com.lukekorth.pebblelocker.PebbleLockerApplication;
import com.lukekorth.pebblelocker.events.StatusChangedEvent;
import com.lukekorth.pebblelocker.logging.Logger;
import com.lukekorth.pebblelocker.receivers.ConnectionReceiver;

public class DeviceHelper {

    public static final String NEED_TO_UNLOCK_KEY = "unlock";

    private Context mContext;
    private Logger mLogger;

    public DeviceHelper(Context context, Logger logger) {
        mContext = context;
        mLogger = logger;
    }

    public boolean isOnLockscreen() {
        boolean keyguard = ((KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE))
                .inKeyguardRestrictedInputMode();
        mLogger.log("Keyguard is showing: " + keyguard);
        return keyguard;
    }

    public boolean isScreenOn() {
        boolean screen = ((PowerManager) mContext.getSystemService(Context.POWER_SERVICE))
                .isScreenOn();
        mLogger.log("Screen is on: " + screen);
        return screen;
    }

    public boolean isLocked(boolean defaultState) {
        boolean locked = PreferenceManager.getDefaultSharedPreferences(mContext)
                .getBoolean(ConnectionReceiver.LOCKED, defaultState);
        mLogger.log("Locked: " + locked);
        return locked;
    }

    public boolean isUnlockNeeded() {
        boolean needToUnlock = PreferenceManager.getDefaultSharedPreferences(mContext)
                .getBoolean(NEED_TO_UNLOCK_KEY, true);
        mLogger.log("Need to unlock: " + needToUnlock);

        return needToUnlock;
    }

    public void sendLockStatusChangedEvent() {
        PebbleLockerApplication.getBus().post(new StatusChangedEvent());
    }

}

@@ -106,16 +106,41 @@
 ontext;%0A
+import android.os.Build;%0A
 import a
@@ -155,32 +155,32 @@
 s.PowerManager;%0A
-
 import android.p
@@ -207,16 +207,80 @@
 Manager;
+%0Aimport android.view.Display;%0Aimport android.view.WindowManager;
 %0A%0Aimport
@@ -1084,24 +1084,565 @@
 creenOn() %7B%0A
+        if (Build.VERSION.SDK_INT %3E Build.VERSION_CODES.KITKAT) %7B%0A            int screen = ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE))%0A                    .getDefaultDisplay().getState();%0A            if (screen == Display.STATE_ON %7C%7C screen == Display.STATE_DOZING) %7B%0A                mLogger.log(%22Screen is on or dozing. %22 + screen);%0A                return true;%0A            %7D else %7B%0A                mLogger.log(%22Screen is off or unknown. %22 + screen);%0A                return false;%0A            %7D%0A        %7D else %7B%0A    
         bool
@@ -1736,16 +1736,20 @@
         
+    
 .isScree
@@ -1755,32 +1755,36 @@
 enOn();%0A        
+ 
+   
 mLogger.log(%22Scr
@@ -1811,24 +1811,28 @@
 n);%0A        
+    
 return scree
@@ -1834,16 +1834,26 @@
 screen;%0A
+        %7D%0A
     %7D%0A%0A 

Add better screen state detection for API >= 20