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 
17 package com.android.server.appop;
18 
19 import android.companion.virtual.VirtualDeviceManager;
20 import android.os.RemoteException;
21 
22 import java.util.Objects;
23 
24 /**
25  * Listener for mode changes, encapsulates methods that should be triggered in the event of a mode
26  * change.
27  */
28 public abstract class OnOpModeChangedListener {
29 
30     // Constant meaning that any UID should be matched when dispatching callbacks
31     private static final int UID_ANY = -2;
32 
33     private int mWatchingUid;
34     private int mFlags;
35     private int mWatchedOpCode;
36     private int mCallingUid;
37     private int mCallingPid;
38 
OnOpModeChangedListener(int watchingUid, int flags, int watchedOpCode, int callingUid, int callingPid)39     OnOpModeChangedListener(int watchingUid, int flags, int watchedOpCode, int callingUid,
40             int callingPid) {
41         this.mWatchingUid = watchingUid;
42         this.mFlags = flags;
43         this.mWatchedOpCode = watchedOpCode;
44         this.mCallingUid = callingUid;
45         this.mCallingPid = callingPid;
46     }
47 
48     /**
49      * Returns the user id that is watching for the mode change.
50      */
getWatchingUid()51     public int getWatchingUid() {
52         return mWatchingUid;
53     }
54 
55     /**
56      * Returns the flags associated with the mode change listener.
57      */
getFlags()58     public int getFlags() {
59         return mFlags;
60     }
61 
62     /**
63      * Get the app-op whose mode change should trigger the callback.
64      */
getWatchedOpCode()65     public int getWatchedOpCode() {
66         return mWatchedOpCode;
67     }
68 
69     /**
70      * Get the user-id that triggered the app-op mode change to be watched.
71      */
getCallingUid()72     public int getCallingUid() {
73         return mCallingUid;
74     }
75 
76     /**
77      * Get the process-id that triggered the app-op mode change to be watched.
78      */
getCallingPid()79     public int getCallingPid() {
80         return mCallingPid;
81     }
82 
83     /**
84      * returns true if the user id passed in the param is the one that is watching for op mode
85      * changed.
86      */
isWatchingUid(int uid)87     public boolean isWatchingUid(int uid) {
88         return uid == UID_ANY || mWatchingUid < 0 || mWatchingUid == uid;
89     }
90 
91     /**
92      * Method that should be triggered when the app-op's mode is changed.
93      * @param op app-op whose mode-change is being listened to.
94      * @param uid user-is associated with the app-op.
95      * @param packageName package name associated with the app-op.
96      */
onOpModeChanged(int op, int uid, String packageName)97     public abstract void onOpModeChanged(int op, int uid, String packageName)
98             throws RemoteException;
99 
100     /**
101      * Method that should be triggered when the app-op's mode is changed.
102      * @param op app-op whose mode-change is being listened to.
103      * @param uid user-is associated with the app-op.
104      * @param packageName package name associated with the app-op.
105      * @param persistentDeviceId device associated with the app-op.
106      */
onOpModeChanged(int op, int uid, String packageName, String persistentDeviceId)107     public void onOpModeChanged(int op, int uid, String packageName, String persistentDeviceId)
108             throws RemoteException {
109         if (Objects.equals(persistentDeviceId, VirtualDeviceManager.PERSISTENT_DEVICE_ID_DEFAULT)) {
110             onOpModeChanged(op, uid, packageName);
111         }
112     }
113 
114     /**
115      * Return human readable string representing the listener.
116      */
toString()117     public abstract String toString();
118 
119 }
120