1 /* 2 * Copyright (C) 2020 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.server.location.injector; 18 19 import android.util.IndentingPrintWriter; 20 import android.util.IntArray; 21 import android.util.SparseArray; 22 23 import com.android.internal.util.ArrayUtils; 24 import com.android.internal.util.Preconditions; 25 26 import java.io.FileDescriptor; 27 28 /** 29 * Version of UserInfoHelper for testing. By default there is one user that starts in a running 30 * state with a userId of 0; 31 */ 32 public class FakeUserInfoHelper extends UserInfoHelper { 33 34 public static final int DEFAULT_USERID = 0; 35 36 private final IntArray mRunningUserIds; 37 private final SparseArray<IntArray> mProfiles; 38 39 private int mCurrentUserId; 40 FakeUserInfoHelper()41 public FakeUserInfoHelper() { 42 mCurrentUserId = DEFAULT_USERID; 43 mRunningUserIds = IntArray.wrap(new int[]{DEFAULT_USERID}); 44 mProfiles = new SparseArray<>(); 45 } 46 startUser(int userId)47 public void startUser(int userId) { 48 startUserInternal(userId, true); 49 } 50 startUserInternal(int userId, boolean alwaysDispatch)51 private void startUserInternal(int userId, boolean alwaysDispatch) { 52 int idx = mRunningUserIds.indexOf(userId); 53 if (idx < 0) { 54 mRunningUserIds.add(userId); 55 } else if (!alwaysDispatch) { 56 return; 57 } 58 59 dispatchOnUserStarted(userId); 60 } 61 stopUser(int userId)62 public void stopUser(int userId) { 63 int idx = mRunningUserIds.indexOf(userId); 64 if (idx >= 0) { 65 mRunningUserIds.remove(idx); 66 } 67 68 dispatchOnUserStopped(userId); 69 } 70 setCurrentUserId(int parentUser)71 public void setCurrentUserId(int parentUser) { 72 setCurrentUserIds(parentUser, new int[]{parentUser}); 73 } 74 setCurrentUserIds(int parentUser, int[] currentProfileUserIds)75 public void setCurrentUserIds(int parentUser, int[] currentProfileUserIds) { 76 Preconditions.checkArgument(ArrayUtils.contains(currentProfileUserIds, parentUser)); 77 int oldUserId = mCurrentUserId; 78 mCurrentUserId = parentUser; 79 mProfiles.put(parentUser, 80 IntArray.fromArray(currentProfileUserIds, currentProfileUserIds.length)); 81 82 // ensure all profiles are started if they didn't exist before... 83 for (int userId : currentProfileUserIds) { 84 startUserInternal(userId, false); 85 } 86 87 if (oldUserId != mCurrentUserId) { 88 dispatchOnCurrentUserChanged(oldUserId, mCurrentUserId); 89 } 90 } 91 92 @Override getRunningUserIds()93 public int[] getRunningUserIds() { 94 return mRunningUserIds.toArray(); 95 } 96 97 @Override isCurrentUserId(int userId)98 public boolean isCurrentUserId(int userId) { 99 return ArrayUtils.contains(getProfileIds(mCurrentUserId), userId); 100 } 101 102 @Override getCurrentUserId()103 public int getCurrentUserId() { 104 return mCurrentUserId; 105 } 106 107 @Override getProfileIds(int userId)108 protected int[] getProfileIds(int userId) { 109 IntArray profiles = mProfiles.get(userId); 110 if (profiles != null) { 111 return profiles.toArray(); 112 } else { 113 return new int[] {userId}; 114 } 115 } 116 117 @Override dump(FileDescriptor fd, IndentingPrintWriter pw, String[] args)118 public void dump(FileDescriptor fd, IndentingPrintWriter pw, String[] args) {} 119 } 120