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.UserIdInt;
20 import android.app.AppOpsManager.Mode;
21 import android.util.SparseBooleanArray;
22 import android.util.SparseIntArray;
23 
24 import com.android.internal.annotations.VisibleForTesting;
25 
26 /**
27  * Interface for accessing and modifying modes for app-ops i.e. package and uid modes.
28  * This interface also includes functions for added and removing op mode watchers.
29  * In the future this interface will also include op restrictions.
30  */
31 public interface AppOpsCheckingServiceInterface {
32 
33     /**
34      * Tells the checking service to write its state to persistence (unconditionally).
35      * This is only made visible for testing.
36      */
37     @VisibleForTesting
writeState()38     void writeState();
39 
40     /**
41      * Tells the checking service to read its state from persistence. This is generally called
42      * shortly after instantiation. If extra system services need to be guaranteed to be published
43      * that work should be done in {@link #systemReady()}
44      */
readState()45     void readState();
46 
47     /**
48      * Tells the checking service that a shutdown is occurring. This gives it a chance to write its
49      * state to persistence (if there are any pending changes).
50      */
shutdown()51     void shutdown();
52 
53     /**
54      * Do additional initialization work that is dependent on external system services.
55      */
systemReady()56     void systemReady();
57 
58     /**
59      * Returns a copy of non-default app-ops with op as keys and their modes as values for a uid.
60      * Returns an empty SparseIntArray if nothing is set.
61      * @param uid for which we need the app-ops and their modes.
62      * @param persistentDeviceId device for which we need the app-ops and their modes
63      */
getNonDefaultUidModes(int uid, String persistentDeviceId)64     SparseIntArray getNonDefaultUidModes(int uid, String persistentDeviceId);
65 
66     /**
67      * Returns a copy of non-default app-ops with op as keys and their modes as values for a package
68      * and user.
69      * Returns an empty SparseIntArray if nothing is set.
70      * @param packageName for which we need the app-ops and their modes.
71      * @param userId for which the package is installed in.
72      */
getNonDefaultPackageModes(String packageName, int userId)73     SparseIntArray getNonDefaultPackageModes(String packageName, int userId);
74 
75     /**
76      * Returns the app-op mode for a particular app-op of a uid.
77      * Returns default op mode if the op mode for particular uid and op is not set.
78      * @param uid user id for which we need the mode.
79      * @param persistentDeviceId device for which we need the mode
80      * @param op app-op for which we need the mode.
81      * @return mode of the app-op.
82      */
getUidMode(int uid, String persistentDeviceId, int op)83     int getUidMode(int uid, String persistentDeviceId, int op);
84 
85     /**
86      * Set the app-op mode for a particular uid and op.
87      * The mode is not set if the mode is the same as the default mode for the op.
88      * @param uid user id for which we want to set the mode.
89      * @param persistentDeviceId device for which we want to set the mode.
90      * @param op app-op for which we want to set the mode.
91      * @param mode mode for the app-op.
92      * @return true if op mode is changed.
93      */
setUidMode(int uid, String persistentDeviceId, int op, @Mode int mode)94     boolean setUidMode(int uid, String persistentDeviceId, int op, @Mode int mode);
95 
96     /**
97      * Gets the app-op mode for a particular package.
98      * Returns default op mode if the op mode for the particular package is not set.
99      * @param packageName package name for which we need the op mode.
100      * @param op app-op for which we need the mode.
101      * @param userId user id associated with the package.
102      * @return the mode of the app-op.
103      */
getPackageMode(@onNull String packageName, int op, @UserIdInt int userId)104     int getPackageMode(@NonNull String packageName, int op, @UserIdInt int userId);
105 
106     /**
107      * Sets the app-op mode for a particular package.
108      * @param packageName package name for which we need to set the op mode.
109      * @param op app-op for which we need to set the mode.
110      * @param mode the mode of the app-op.
111      * @param userId user id associated with the package.
112      *
113      */
setPackageMode(@onNull String packageName, int op, @Mode int mode, @UserIdInt int userId)114     void setPackageMode(@NonNull String packageName, int op, @Mode int mode, @UserIdInt int userId);
115 
116     /**
117      * Stop tracking any app-op modes for a package.
118      * @param packageName Name of the package for which we want to remove all mode tracking.
119      * @param userId user id associated with the package.
120      */
removePackage(@onNull String packageName, @UserIdInt int userId)121     boolean removePackage(@NonNull String packageName,  @UserIdInt int userId);
122 
123     /**
124      * Stop tracking any app-op modes for this uid.
125      * @param uid user id for which we want to remove all tracking.
126      */
removeUid(int uid)127     void removeUid(int uid);
128 
129     /**
130      * Stop tracking app-op modes for all uid and packages.
131      */
clearAllModes()132     void clearAllModes();
133 
134     /**
135      * @param uid UID to query foreground ops for.
136      * @param persistentDeviceId device to query foreground ops for
137      * @return SparseBooleanArray where the keys are the op codes for which their modes are
138      * MODE_FOREGROUND for the passed UID.
139      */
getForegroundOps(int uid, String persistentDeviceId)140     SparseBooleanArray getForegroundOps(int uid, String persistentDeviceId);
141 
142     /**
143      *
144      * @param packageName Package name to check for.
145      * @param userId User ID to check for.
146      * @return SparseBooleanArray where the keys are the op codes for which their modes are
147      * MODE_FOREGROUND for the passed package name and user ID.
148      */
getForegroundOps(String packageName, int userId)149     SparseBooleanArray getForegroundOps(String packageName, int userId);
150 
151     /**
152      * Adds a listener for changes in appop modes. These callbacks should be dispatched
153      * synchronously.
154      *
155      * @param listener The listener to be added.
156      * @return true if the listener was added.
157      */
addAppOpsModeChangedListener(@onNull AppOpsModeChangedListener listener)158     boolean addAppOpsModeChangedListener(@NonNull AppOpsModeChangedListener listener);
159 
160     /**
161      * Removes a listener for changes in appop modes.
162      *
163      * @param listener The listener to be removed.
164      * @return true if the listener was removed.
165      */
removeAppOpsModeChangedListener(@onNull AppOpsModeChangedListener listener)166     boolean removeAppOpsModeChangedListener(@NonNull AppOpsModeChangedListener listener);
167 
168     /**
169      * A listener for changes to the AppOps mode.
170      */
171     interface AppOpsModeChangedListener {
172 
173         /**
174          * Invoked when a UID's appop mode is changed.
175          *
176          * @param uid The UID whose appop mode was changed.
177          * @param code The op code that was changed.
178          * @param mode The new mode.
179          * @param persistentDeviceId the device whose mode was changed
180          */
onUidModeChanged(int uid, int code, int mode, String persistentDeviceId)181         void onUidModeChanged(int uid, int code, int mode, String persistentDeviceId);
182 
183         /**
184          * Invoked when a package's appop mode is changed.
185          *
186          * @param packageName The package name whose appop mode was changed.
187          * @param userId The user ID for the package.
188          * @param code The op code that was changed.
189          * @param mode The new mode.
190          */
onPackageModeChanged(@onNull String packageName, int userId, int code, int mode)191         void onPackageModeChanged(@NonNull String packageName, int userId, int code, int mode);
192     }
193 }
194