• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.inputmethod.compat;
18 
19 import android.content.Context;
20 import android.os.Build;
21 import android.os.UserManager;
22 import androidx.annotation.IntDef;
23 
24 import java.lang.annotation.Retention;
25 import java.lang.reflect.Method;
26 
27 import static java.lang.annotation.RetentionPolicy.SOURCE;
28 
29 /**
30  * A temporary solution until {@code UserManagerCompat.isUserUnlocked()} in the support-v4 library
31  * becomes publicly available.
32  */
33 public final class UserManagerCompatUtils {
34     private static final Method METHOD_isUserUnlocked;
35 
36     static {
37         // We do not try to search the method in Android M and prior.
38         if (BuildCompatUtils.EFFECTIVE_SDK_INT <= Build.VERSION_CODES.M) {
39             METHOD_isUserUnlocked = null;
40         } else {
41             METHOD_isUserUnlocked = CompatUtils.getMethod(UserManager.class, "isUserUnlocked");
42         }
43     }
44 
UserManagerCompatUtils()45     private UserManagerCompatUtils() {
46         // This utility class is not publicly instantiable.
47     }
48 
49     public static final int LOCK_STATE_UNKNOWN = 0;
50     public static final int LOCK_STATE_UNLOCKED = 1;
51     public static final int LOCK_STATE_LOCKED = 2;
52 
53     @Retention(SOURCE)
54     @IntDef({LOCK_STATE_UNKNOWN, LOCK_STATE_UNLOCKED, LOCK_STATE_LOCKED})
55     public @interface LockState {}
56 
57     /**
58      * Check if the calling user is running in an "unlocked" state. A user is unlocked only after
59      * they've entered their credentials (such as a lock pattern or PIN), and credential-encrypted
60      * private app data storage is available.
61      * @param context context from which {@link UserManager} should be obtained.
62      * @return One of {@link LockState}.
63      */
64     @LockState
getUserLockState(final Context context)65     public static int getUserLockState(final Context context) {
66         if (METHOD_isUserUnlocked == null) {
67             return LOCK_STATE_UNKNOWN;
68         }
69         final UserManager userManager = context.getSystemService(UserManager.class);
70         if (userManager == null) {
71             return LOCK_STATE_UNKNOWN;
72         }
73         final Boolean result =
74                 (Boolean) CompatUtils.invoke(userManager, null, METHOD_isUserUnlocked);
75         if (result == null) {
76             return LOCK_STATE_UNKNOWN;
77         }
78         return result ? LOCK_STATE_UNLOCKED : LOCK_STATE_LOCKED;
79     }
80 }
81