Home | History | Annotate | Download | only in compat
      1 /*
      2  * Copyright (C) 2015 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 package com.android.dialer.compat;
     17 
     18 import android.content.Context;
     19 import android.os.Process;
     20 import android.os.UserHandle;
     21 import android.os.UserManager;
     22 
     23 import com.android.contacts.common.compat.CompatUtils;
     24 
     25 /**
     26  * Compatibility class for {@link UserManager}.
     27  */
     28 public class UserManagerCompat {
     29     /**
     30      * A user id constant to indicate the "system" user of the device. Copied from
     31      * {@link UserHandle}.
     32      */
     33     private static final int USER_SYSTEM = 0;
     34     /**
     35      * Range of uids allocated for a user.
     36      */
     37     private static final int PER_USER_RANGE = 100000;
     38 
     39     /**
     40      * Used to check if this process is running under the system user. The system user is the
     41      * initial user that is implicitly created on first boot and hosts most of the system services.
     42      *
     43      * @return whether this process is running under the system user.
     44      */
     45     public static boolean isSystemUser(UserManager userManager) {
     46         if (CompatUtils.isMarshmallowCompatible()) {
     47             return userManager.isSystemUser();
     48         }
     49         // Adapted from {@link UserManager} and {@link UserHandle}.
     50         return (Process.myUid() / PER_USER_RANGE) == USER_SYSTEM;
     51     }
     52 
     53     /**
     54      * Return whether the calling user is running in an "unlocked" state. A user
     55      * is unlocked only after they've entered their credentials (such as a lock
     56      * pattern or PIN), and credential-encrypted private app data storage is
     57      * available.
     58      *
     59      * TODO b/26688153
     60      *
     61      * @param context the current context
     62      * @return {@code true} if the user is unlocked, {@code false} otherwise
     63      * @throws NullPointerException if context is null
     64      */
     65     public static boolean isUserUnlocked(Context context) {
     66         if (CompatUtils.isNCompatible()) {
     67             return UserManagerSdkCompat.isUserUnlocked(context);
     68         }
     69         return true;
     70     }
     71 }
     72