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