1 /* 2 * Copyright (C) 2022 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.server.appop; 17 18 import android.annotation.NonNull; 19 import android.annotation.Nullable; 20 import android.annotation.UserIdInt; 21 import android.app.AppOpsManager.Mode; 22 import android.util.ArraySet; 23 import android.util.SparseBooleanArray; 24 import android.util.SparseIntArray; 25 26 import com.android.internal.annotations.VisibleForTesting; 27 28 import java.io.PrintWriter; 29 30 /** 31 * Interface for accessing and modifying modes for app-ops i.e. package and uid modes. 32 * This interface also includes functions for added and removing op mode watchers. 33 * In the future this interface will also include op restrictions. 34 */ 35 public interface AppOpsCheckingServiceInterface { 36 37 /** 38 * Tells the checking service to write its state to persistence (unconditionally). 39 * This is only made visible for testing. 40 */ 41 @VisibleForTesting writeState()42 void writeState(); 43 44 /** 45 * Tells the checking service to read its state from persistence. This is generally called 46 * shortly after instantiation. If extra system services need to be guaranteed to be published 47 * that work should be done in {@link #systemReady()} 48 */ readState()49 void readState(); 50 51 /** 52 * Tells the checking service that a shutdown is occurring. This gives it a chance to write its 53 * state to persistence (if there are any pending changes). 54 */ shutdown()55 void shutdown(); 56 57 /** 58 * Do additional initialization work that is dependent on external system services. 59 */ systemReady()60 void systemReady(); 61 62 /** 63 * Returns a copy of non-default app-ops with op as keys and their modes as values for a uid. 64 * Returns an empty SparseIntArray if nothing is set. 65 * @param uid for which we need the app-ops and their modes. 66 */ getNonDefaultUidModes(int uid)67 SparseIntArray getNonDefaultUidModes(int uid); 68 69 /** 70 * Returns a copy of non-default app-ops with op as keys and their modes as values for a package 71 * and user. 72 * Returns an empty SparseIntArray if nothing is set. 73 * @param packageName for which we need the app-ops and their modes. 74 * @param userId for which the package is installed in. 75 */ getNonDefaultPackageModes(String packageName, int userId)76 SparseIntArray getNonDefaultPackageModes(String packageName, int userId); 77 78 /** 79 * Returns the app-op mode for a particular app-op of a uid. 80 * Returns default op mode if the op mode for particular uid and op is not set. 81 * @param uid user id for which we need the mode. 82 * @param op app-op for which we need the mode. 83 * @return mode of the app-op. 84 */ getUidMode(int uid, int op)85 int getUidMode(int uid, int op); 86 87 /** 88 * Set the app-op mode for a particular uid and op. 89 * The mode is not set if the mode is the same as the default mode for the op. 90 * @param uid user id for which we want to set the mode. 91 * @param op app-op for which we want to set the mode. 92 * @param mode mode for the app-op. 93 * @return true if op mode is changed. 94 */ setUidMode(int uid, int op, @Mode int mode)95 boolean setUidMode(int uid, int op, @Mode int mode); 96 97 /** 98 * Gets the app-op mode for a particular package. 99 * Returns default op mode if the op mode for the particular package is not set. 100 * @param packageName package name for which we need the op mode. 101 * @param op app-op for which we need the mode. 102 * @param userId user id associated with the package. 103 * @return the mode of the app-op. 104 */ getPackageMode(@onNull String packageName, int op, @UserIdInt int userId)105 int getPackageMode(@NonNull String packageName, int op, @UserIdInt int userId); 106 107 /** 108 * Sets the app-op mode for a particular package. 109 * @param packageName package name for which we need to set the op mode. 110 * @param op app-op for which we need to set the mode. 111 * @param mode the mode of the app-op. 112 * @param userId user id associated with the package. 113 * 114 */ setPackageMode(@onNull String packageName, int op, @Mode int mode, @UserIdInt int userId)115 void setPackageMode(@NonNull String packageName, int op, @Mode int mode, @UserIdInt int userId); 116 117 /** 118 * Stop tracking any app-op modes for a package. 119 * @param packageName Name of the package for which we want to remove all mode tracking. 120 * @param userId user id associated with the package. 121 */ removePackage(@onNull String packageName, @UserIdInt int userId)122 boolean removePackage(@NonNull String packageName, @UserIdInt int userId); 123 124 /** 125 * Stop tracking any app-op modes for this uid. 126 * @param uid user id for which we want to remove all tracking. 127 */ removeUid(int uid)128 void removeUid(int uid); 129 130 /** 131 * Returns true if all uid modes for this uid are 132 * in default state. 133 * @param uid user id 134 */ areUidModesDefault(int uid)135 boolean areUidModesDefault(int uid); 136 137 /** 138 * Returns true if all package modes for this package name are 139 * in default state. 140 * @param packageName package name. 141 * @param userId user id associated with the package. 142 */ arePackageModesDefault(String packageName, @UserIdInt int userId)143 boolean arePackageModesDefault(String packageName, @UserIdInt int userId); 144 145 /** 146 * Stop tracking app-op modes for all uid and packages. 147 */ clearAllModes()148 void clearAllModes(); 149 150 /** 151 * Registers changedListener to listen to op's mode change. 152 * @param changedListener the listener that must be trigger on the op's mode change. 153 * @param op op representing the app-op whose mode change needs to be listened to. 154 */ startWatchingOpModeChanged(@onNull OnOpModeChangedListener changedListener, int op)155 void startWatchingOpModeChanged(@NonNull OnOpModeChangedListener changedListener, int op); 156 157 /** 158 * Registers changedListener to listen to package's app-op's mode change. 159 * @param changedListener the listener that must be trigger on the mode change. 160 * @param packageName of the package whose app-op's mode change needs to be listened to. 161 */ startWatchingPackageModeChanged(@onNull OnOpModeChangedListener changedListener, @NonNull String packageName)162 void startWatchingPackageModeChanged(@NonNull OnOpModeChangedListener changedListener, 163 @NonNull String packageName); 164 165 /** 166 * Stop the changedListener from triggering on any mode change. 167 * @param changedListener the listener that needs to be removed. 168 */ removeListener(@onNull OnOpModeChangedListener changedListener)169 void removeListener(@NonNull OnOpModeChangedListener changedListener); 170 171 /** 172 * Temporary API which will be removed once we can safely untangle the methods that use this. 173 * Returns a set of OnOpModeChangedListener that are listening for op's mode changes. 174 * @param op app-op whose mode change is being listened to. 175 */ getOpModeChangedListeners(int op)176 ArraySet<OnOpModeChangedListener> getOpModeChangedListeners(int op); 177 178 /** 179 * Temporary API which will be removed once we can safely untangle the methods that use this. 180 * Returns a set of OnOpModeChangedListener that are listening for package's op's mode changes. 181 * @param packageName of package whose app-op's mode change is being listened to. 182 */ getPackageModeChangedListeners(@onNull String packageName)183 ArraySet<OnOpModeChangedListener> getPackageModeChangedListeners(@NonNull String packageName); 184 185 /** 186 * Temporary API which will be removed once we can safely untangle the methods that use this. 187 * Notify that the app-op's mode is changed by triggering the change listener. 188 * @param op App-op whose mode has changed 189 * @param uid user id associated with the app-op (or, if UID_ANY, notifies all users) 190 */ notifyWatchersOfChange(int op, int uid)191 void notifyWatchersOfChange(int op, int uid); 192 193 /** 194 * Temporary API which will be removed once we can safely untangle the methods that use this. 195 * Notify that the app-op's mode is changed by triggering the change listener. 196 * @param changedListener the change listener. 197 * @param op App-op whose mode has changed 198 * @param uid user id associated with the app-op 199 * @param packageName package name that is associated with the app-op 200 */ notifyOpChanged(@onNull OnOpModeChangedListener changedListener, int op, int uid, @Nullable String packageName)201 void notifyOpChanged(@NonNull OnOpModeChangedListener changedListener, int op, int uid, 202 @Nullable String packageName); 203 204 /** 205 * Temporary API which will be removed once we can safely untangle the methods that use this. 206 * Notify that the app-op's mode is changed to all packages associated with the uid by 207 * triggering the appropriate change listener. 208 * @param op App-op whose mode has changed 209 * @param uid user id associated with the app-op 210 * @param onlyForeground true if only watchers that 211 * @param callbackToIgnore callback that should be ignored. 212 */ notifyOpChangedForAllPkgsInUid(int op, int uid, boolean onlyForeground, @Nullable OnOpModeChangedListener callbackToIgnore)213 void notifyOpChangedForAllPkgsInUid(int op, int uid, boolean onlyForeground, 214 @Nullable OnOpModeChangedListener callbackToIgnore); 215 216 /** 217 * TODO: Move hasForegroundWatchers and foregroundOps into this. 218 * Go over the list of app-ops for the uid and mark app-ops with MODE_FOREGROUND in 219 * foregroundOps. 220 * @param uid for which the app-op's mode needs to be marked. 221 * @param foregroundOps boolean array where app-ops that have MODE_FOREGROUND are marked true. 222 * @return foregroundOps. 223 */ evalForegroundUidOps(int uid, SparseBooleanArray foregroundOps)224 SparseBooleanArray evalForegroundUidOps(int uid, SparseBooleanArray foregroundOps); 225 226 /** 227 * Go over the list of app-ops for the package name and mark app-ops with MODE_FOREGROUND in 228 * foregroundOps. 229 * @param packageName for which the app-op's mode needs to be marked. 230 * @param foregroundOps boolean array where app-ops that have MODE_FOREGROUND are marked true. 231 * @param userId user id associated with the package. 232 * @return foregroundOps. 233 */ evalForegroundPackageOps(String packageName, SparseBooleanArray foregroundOps, @UserIdInt int userId)234 SparseBooleanArray evalForegroundPackageOps(String packageName, 235 SparseBooleanArray foregroundOps, @UserIdInt int userId); 236 237 /** 238 * Dump op mode and package mode listeners and their details. 239 * @param dumpOp if -1 then op mode listeners for all app-ops are dumped. If it's set to an 240 * app-op, only the watchers for that app-op are dumped. 241 * @param dumpUid uid for which we want to dump op mode watchers. 242 * @param dumpPackage if not null and if dumpOp is -1, dumps watchers for the package name. 243 * @param printWriter writer to dump to. 244 */ dumpListeners(int dumpOp, int dumpUid, String dumpPackage, PrintWriter printWriter)245 boolean dumpListeners(int dumpOp, int dumpUid, String dumpPackage, PrintWriter printWriter); 246 } 247