1 /* 2 * Copyright 2019 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.car.settings.testutils; 18 19 import android.app.AppOpsManager; 20 import android.app.AppOpsManager.OpEntry; 21 import android.app.AppOpsManager.PackageOps; 22 import android.util.Pair; 23 24 import com.google.common.collect.HashBasedTable; 25 import com.google.common.collect.ImmutableList; 26 import com.google.common.collect.Table; 27 28 import org.robolectric.annotation.Implementation; 29 import org.robolectric.annotation.Implements; 30 31 import java.util.Collections; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.Objects; 35 36 @Implements(value = AppOpsManager.class) 37 public class ShadowAppOpsManager { 38 39 private Table<Integer, InternalKey, Integer> mOpToKeyToMode = HashBasedTable.create(); 40 41 @Implementation setMode(int code, int uid, String packageName, int mode)42 protected void setMode(int code, int uid, String packageName, int mode) { 43 InternalKey key = new InternalKey(uid, packageName); 44 mOpToKeyToMode.put(code, key, mode); 45 } 46 47 /** Convenience method to get the mode directly instead of wrapped in an op list. */ getMode(int code, int uid, String packageName)48 public int getMode(int code, int uid, String packageName) { 49 Integer mode = mOpToKeyToMode.get(code, new InternalKey(uid, packageName)); 50 return mode == null ? AppOpsManager.opToDefaultMode(code) : mode; 51 } 52 53 @Implementation getPackagesForOps(int[] ops)54 protected List<PackageOps> getPackagesForOps(int[] ops) { 55 if (ops == null) { 56 return Collections.emptyList(); 57 } 58 ImmutableList.Builder<PackageOps> result = new ImmutableList.Builder<>(); 59 for (int i = 0; i < ops.length; i++) { 60 int op = ops[i]; 61 Map<InternalKey, Integer> keyToModeMap = mOpToKeyToMode.rowMap().get(op); 62 if (keyToModeMap == null) { 63 continue; 64 } 65 for (InternalKey key : keyToModeMap.keySet()) { 66 Integer mode = keyToModeMap.get(key); 67 if (mode == null) { 68 mode = AppOpsManager.opToDefaultMode(op); 69 } 70 OpEntry opEntry = new OpEntry(op, mode, Collections.emptyMap()); 71 PackageOps packageOp = new PackageOps(key.mPackageName, key.mUid, 72 Collections.singletonList(opEntry)); 73 result.add(packageOp); 74 } 75 } 76 return result.build(); 77 } 78 79 private static class InternalKey { 80 private int mUid; 81 private String mPackageName; 82 InternalKey(int uid, String packageName)83 InternalKey(int uid, String packageName) { 84 mUid = uid; 85 mPackageName = packageName; 86 } 87 88 @Override equals(Object obj)89 public boolean equals(Object obj) { 90 if (obj instanceof InternalKey) { 91 InternalKey that = (InternalKey) obj; 92 return mUid == that.mUid && mPackageName.equals(that.mPackageName); 93 } 94 return false; 95 } 96 97 @Override hashCode()98 public int hashCode() { 99 return Objects.hash(mUid, mPackageName); 100 } 101 } 102 } 103