1 /* 2 * Copyright (C) 2021 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.bedstead.nene.users; 18 19 import android.util.Log; 20 21 import androidx.annotation.Nullable; 22 23 /** 24 * Representation of a user on an Android device. 25 */ 26 public final class AdbUser { 27 28 private static final String LOG_TAG = "AdbUser"; 29 30 /* From UserInfo */ 31 static final int FLAG_MANAGED_PROFILE = 0x00000020; 32 33 public enum UserState { 34 NOT_RUNNING, 35 RUNNING_LOCKED, 36 RUNNING_UNLOCKED, 37 RUNNING_UNLOCKING, 38 STOPPING, 39 SHUTDOWN, 40 UNKNOWN; 41 fromDumpSysValue(String value)42 static UserState fromDumpSysValue(String value) { 43 try { 44 return UserState.valueOf(value); 45 } catch (IllegalArgumentException e) { 46 if (value.equals("-1")) { 47 return NOT_RUNNING; 48 } 49 Log.w(LOG_TAG, "Unknown user state string: " + value); 50 return UNKNOWN; 51 } 52 } 53 } 54 55 static final class MutableUser { 56 Integer mId; 57 @Nullable Integer mSerialNo; 58 @Nullable String mName; 59 @Nullable UserType mType; 60 @Nullable Boolean mHasProfileOwner; 61 @Nullable Boolean mIsPrimary; 62 @Nullable UserState mState; 63 @Nullable Boolean mIsRemoving; 64 @Nullable Integer mFlags; 65 @Nullable UserReference mParent; 66 } 67 68 final MutableUser mMutableUser; 69 AdbUser(MutableUser mutableUser)70 AdbUser(MutableUser mutableUser) { 71 mMutableUser = mutableUser; 72 } 73 74 /** Get the id of the user. */ id()75 public int id() { 76 return mMutableUser.mId; 77 } 78 79 /** Get the serial number of the user. */ serialNo()80 public Integer serialNo() { 81 return mMutableUser.mSerialNo; 82 } 83 84 /** Get the name of the user. */ name()85 public String name() { 86 return mMutableUser.mName; 87 } 88 89 /** Get the {@link UserState} of the user. */ state()90 public UserState state() { 91 return mMutableUser.mState; 92 } 93 94 /** True if the user is currently being removed. */ isRemoving()95 public boolean isRemoving() { 96 return mMutableUser.mIsRemoving; 97 } 98 99 /** 100 * Get the user type. 101 */ type()102 public UserType type() { 103 return mMutableUser.mType; 104 } 105 106 /** {@code true} if the user has a profile owner. */ hasProfileOwner()107 public Boolean hasProfileOwner() { 108 return mMutableUser.mHasProfileOwner; 109 } 110 111 /** 112 * Return {@code true} if this is the primary user. 113 */ isPrimary()114 public Boolean isPrimary() { 115 return mMutableUser.mIsPrimary; 116 } 117 hasFlag(int flag)118 boolean hasFlag(int flag) { 119 if (mMutableUser.mFlags == null) { 120 return false; 121 } 122 return (mMutableUser.mFlags & flag) != 0; 123 } 124 125 /** 126 * Return the parent of this profile. 127 * 128 * <p>Returns {@code null} if this user is not a profile. 129 */ 130 @Nullable parent()131 public UserReference parent() { 132 return mMutableUser.mParent; 133 } 134 135 @Override toString()136 public String toString() { 137 StringBuilder stringBuilder = new StringBuilder("User{"); 138 stringBuilder.append("id=" + mMutableUser.mId); 139 stringBuilder.append(", serialNo=" + mMutableUser.mSerialNo); 140 stringBuilder.append(", name=" + mMutableUser.mName); 141 stringBuilder.append(", type=" + mMutableUser.mType); 142 stringBuilder.append(", hasProfileOwner" + mMutableUser.mHasProfileOwner); 143 stringBuilder.append(", isPrimary=" + mMutableUser.mIsPrimary); 144 stringBuilder.append(", state=" + mMutableUser.mState); 145 stringBuilder.append(", parent=" + mMutableUser.mParent); 146 stringBuilder.append("}"); 147 return stringBuilder.toString(); 148 } 149 } 150