1 package com.android.nfc;
2 
3 import android.app.KeyguardManager;
4 import android.content.Context;
5 import android.os.PowerManager;
6 
7 /**
8  * Helper class for determining the current screen state for NFC activities.
9  */
10 class ScreenStateHelper {
11 
12     static final int SCREEN_STATE_UNKNOWN = 0x00;
13     static final int SCREEN_STATE_OFF_UNLOCKED = 0x01;
14     static final int SCREEN_STATE_OFF_LOCKED = 0x02;
15     static final int SCREEN_STATE_ON_LOCKED = 0x04;
16     static final int SCREEN_STATE_ON_UNLOCKED = 0x08;
17 
18     //Polling mask
19     static final int SCREEN_POLLING_TAG_MASK = 0x10;
20     static final int SCREEN_POLLING_READER_MASK = 0x40;
21 
22     private final PowerManager mPowerManager;
23     private final KeyguardManager mKeyguardManager;
24 
ScreenStateHelper(Context context)25     ScreenStateHelper(Context context) {
26         mKeyguardManager = context.getSystemService(KeyguardManager.class);
27         mPowerManager = context.getSystemService(PowerManager.class);
28     }
29 
checkScreenState()30     int checkScreenState() {
31         if (!mPowerManager.isInteractive()) {
32             if (mKeyguardManager.isKeyguardLocked()) {
33                 return SCREEN_STATE_OFF_LOCKED;
34             } else {
35                 return SCREEN_STATE_OFF_UNLOCKED;
36             }
37         } else if (mKeyguardManager.isKeyguardLocked()) {
38             return SCREEN_STATE_ON_LOCKED;
39         } else {
40             return SCREEN_STATE_ON_UNLOCKED;
41         }
42     }
43 
checkScreenStateProvisionMode()44     int checkScreenStateProvisionMode() {
45         if (!mPowerManager.isInteractive()) {
46             if (mKeyguardManager.isDeviceLocked()) {
47                 return SCREEN_STATE_OFF_LOCKED;
48             } else {
49                 return SCREEN_STATE_OFF_UNLOCKED;
50             }
51         } else if (mKeyguardManager.isDeviceLocked()) {
52             return SCREEN_STATE_ON_LOCKED;
53         } else {
54             return SCREEN_STATE_ON_UNLOCKED;
55         }
56     }
57 
58     /**
59      * For debugging only - no i18n
60      */
screenStateToString(int screenState)61     static String screenStateToString(int screenState) {
62         switch (screenState) {
63             case SCREEN_STATE_OFF_LOCKED:
64                 return "OFF_LOCKED";
65             case SCREEN_STATE_ON_LOCKED:
66                 return "ON_LOCKED";
67             case SCREEN_STATE_ON_UNLOCKED:
68                 return "ON_UNLOCKED";
69             case SCREEN_STATE_OFF_UNLOCKED:
70                 return "OFF_UNLOCKED";
71             default:
72                 return "UNKNOWN";
73         }
74     }
75 
screenStateToProtoEnum(int screenState)76     static int screenStateToProtoEnum(int screenState) {
77         switch (screenState) {
78             case SCREEN_STATE_OFF_LOCKED:
79                 return NfcServiceDumpProto.SCREEN_STATE_OFF_LOCKED;
80             case SCREEN_STATE_ON_LOCKED:
81                 return NfcServiceDumpProto.SCREEN_STATE_ON_LOCKED;
82             case SCREEN_STATE_ON_UNLOCKED:
83                 return NfcServiceDumpProto.SCREEN_STATE_ON_UNLOCKED;
84             case SCREEN_STATE_OFF_UNLOCKED:
85                 return NfcServiceDumpProto.SCREEN_STATE_OFF_UNLOCKED;
86             default:
87                 return NfcServiceDumpProto.SCREEN_STATE_UNKNOWN;
88         }
89     }
90 }
91