1 /*
2  * Copyright (C) 2019 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 android.compat.Compatibility;
20 import android.os.Process;
21 
22 import com.android.internal.compat.ChangeReporter;
23 
24 import java.util.Arrays;
25 
26 /**
27  * App process implementation of the {@link Compatibility} API.
28  *
29  * @hide
30  */
31 public final class AppCompatCallbacks extends Compatibility.Callbacks {
32     private final long[] mDisabledChanges;
33     private final ChangeReporter mChangeReporter;
34 
35     /**
36      * Install this class into the current process.
37      *
38      * @param disabledChanges Set of compatibility changes that are disabled for this process.
39      */
install(long[] disabledChanges)40     public static void install(long[] disabledChanges) {
41         Compatibility.setCallbacks(new AppCompatCallbacks(disabledChanges));
42     }
43 
AppCompatCallbacks(long[] disabledChanges)44     private AppCompatCallbacks(long[] disabledChanges) {
45         mDisabledChanges = Arrays.copyOf(disabledChanges, disabledChanges.length);
46         Arrays.sort(mDisabledChanges);
47         mChangeReporter = new ChangeReporter(
48                 ChangeReporter.SOURCE_APP_PROCESS);
49     }
50 
reportChange(long changeId)51     protected void reportChange(long changeId) {
52         reportChange(changeId, ChangeReporter.STATE_LOGGED);
53     }
54 
isChangeEnabled(long changeId)55     protected boolean isChangeEnabled(long changeId) {
56         if (Arrays.binarySearch(mDisabledChanges, changeId) < 0) {
57             // Not present in the disabled array
58             reportChange(changeId, ChangeReporter.STATE_ENABLED);
59             return true;
60         }
61         reportChange(changeId, ChangeReporter.STATE_DISABLED);
62         return false;
63     }
64 
reportChange(long changeId, int state)65     private void reportChange(long changeId, int state) {
66         int uid = Process.myUid();
67         mChangeReporter.reportChange(uid, changeId, state);
68     }
69 
70 }
71