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 android.app;
18 
19 import static android.app.AppOpsManager.OP_NONE;
20 
21 import android.annotation.NonNull;
22 
23 import java.util.Objects;
24 
25 /**
26  * Information about a particular app op.
27  */
28 class AppOpInfo {
29 
30     /**
31      * A unique constant identifying this app op.
32      */
33     public final int code;
34 
35     /**
36      * This maps each operation to the operation that serves as the
37      * switch to determine whether it is allowed.  Generally this is
38      * a 1:1 mapping, but for some things (like location) that have
39      * multiple low-level operations being tracked that should be
40      * presented to the user as one switch then this can be used to
41      * make them all controlled by the same single operation.
42      */
43     public final int switchCode;
44 
45     /**
46      * This maps each operation to the public string constant for it.
47      */
48     public final String name;
49 
50     /**
51      * This provides a simple name for each operation to be used
52      * in debug output.
53      */
54     public final String simpleName;
55 
56     /**
57      * This optionally maps a permission to an operation.  If there
58      * is no permission associated with an operation, it is null.
59      */
60     public final String permission;
61 
62     /**
63      * Specifies whether an Op should be restricted by a user restriction.
64      * Each Op should be filled with a restriction string from UserManager or
65      * null to specify it is not affected by any user restriction.
66      */
67     public final String restriction;
68 
69     /**
70      * In which cases should an app be allowed to bypass the
71      * {@link AppOpsManager#setUserRestriction user restriction} for a certain app-op.
72      */
73     public final AppOpsManager.RestrictionBypass allowSystemRestrictionBypass;
74 
75     /**
76      * This specifies the default mode for each operation.
77      */
78     public final int defaultMode;
79 
80     /**
81      * This specifies whether each option is allowed to be reset
82      * when resetting all app preferences.  Disable reset for
83      * app ops that are under strong control of some part of the
84      * system (such as OP_WRITE_SMS, which should be allowed only
85      * for whichever app is selected as the current SMS app).
86      */
87     public final boolean disableReset;
88 
89     /**
90      * This specifies whether each option is only allowed to be read
91      * by apps with privileged appops permission.
92      */
93     public final boolean restrictRead;
94 
95     /**
96      * Whether to collect noteOp instances, and send them to callbacks.
97      */
98     public final boolean forceCollectNotes;
99 
AppOpInfo(int code, int switchCode, @NonNull String name, @NonNull String simpleName, String permission, String restriction, AppOpsManager.RestrictionBypass allowSystemRestrictionBypass, int defaultMode, boolean disableReset, boolean restrictRead, boolean forceCollectNotes)100     AppOpInfo(int code,
101             int switchCode,
102             @NonNull String name,
103             @NonNull String simpleName,
104             String permission,
105             String restriction,
106             AppOpsManager.RestrictionBypass allowSystemRestrictionBypass,
107             int defaultMode,
108             boolean disableReset,
109             boolean restrictRead,
110             boolean forceCollectNotes) {
111         if (code < OP_NONE) throw new IllegalArgumentException();
112         if (switchCode < OP_NONE) throw new IllegalArgumentException();
113         Objects.requireNonNull(name);
114         Objects.requireNonNull(simpleName);
115         this.code = code;
116         this.switchCode = switchCode;
117         this.name = name;
118         this.simpleName = simpleName;
119         this.permission = permission;
120         this.restriction = restriction;
121         this.allowSystemRestrictionBypass = allowSystemRestrictionBypass;
122         this.defaultMode = defaultMode;
123         this.disableReset = disableReset;
124         this.restrictRead = restrictRead;
125         this.forceCollectNotes = forceCollectNotes;
126     }
127 
128     static class Builder {
129         private int mCode;
130         private int mSwitchCode;
131         private String mName;
132         private String mSimpleName;
133         private String mPermission = null;
134         private String mRestriction = null;
135         private AppOpsManager.RestrictionBypass mAllowSystemRestrictionBypass = null;
136         private int mDefaultMode = AppOpsManager.MODE_DEFAULT;
137         private boolean mDisableReset = false;
138         private boolean mRestrictRead = false;
139         private boolean mForceCollectNotes = false;
140 
Builder(int code, @NonNull String name, @NonNull String simpleName)141         Builder(int code, @NonNull String name, @NonNull String simpleName) {
142             if (code < OP_NONE) throw new IllegalArgumentException();
143             Objects.requireNonNull(name);
144             Objects.requireNonNull(simpleName);
145             this.mCode = code;
146             this.mSwitchCode = code;
147             this.mName = name;
148             this.mSimpleName = simpleName;
149         }
150 
setCode(int value)151         public Builder setCode(int value) {
152             this.mCode = value;
153             return this;
154         }
155 
setSwitchCode(int value)156         public Builder setSwitchCode(int value) {
157             this.mSwitchCode = value;
158             return this;
159         }
160 
setName(String value)161         public Builder setName(String value) {
162             this.mName = value;
163             return this;
164         }
165 
setSimpleName(String value)166         public Builder setSimpleName(String value) {
167             this.mSimpleName = value;
168             return this;
169         }
170 
setPermission(String value)171         public Builder setPermission(String value) {
172             this.mPermission = value;
173             return this;
174         }
175 
setRestriction(String value)176         public Builder setRestriction(String value) {
177             this.mRestriction = value;
178             return this;
179         }
180 
setAllowSystemRestrictionBypass( AppOpsManager.RestrictionBypass value)181         public Builder setAllowSystemRestrictionBypass(
182                 AppOpsManager.RestrictionBypass value) {
183             this.mAllowSystemRestrictionBypass = value;
184             return this;
185         }
186 
setDefaultMode(int value)187         public Builder setDefaultMode(int value) {
188             this.mDefaultMode = value;
189             return this;
190         }
191 
setDisableReset(boolean value)192         public Builder setDisableReset(boolean value) {
193             this.mDisableReset = value;
194             return this;
195         }
196 
setRestrictRead(boolean value)197         public Builder setRestrictRead(boolean value) {
198             this.mRestrictRead = value;
199             return this;
200         }
201 
setForceCollectNotes(boolean value)202         public Builder setForceCollectNotes(boolean value) {
203             this.mForceCollectNotes = value;
204             return this;
205         }
206 
build()207         public AppOpInfo build() {
208             return new AppOpInfo(mCode, mSwitchCode, mName, mSimpleName, mPermission, mRestriction,
209                 mAllowSystemRestrictionBypass, mDefaultMode, mDisableReset, mRestrictRead,
210                     mForceCollectNotes);
211         }
212     }
213 }
214