• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 @android.ravenwood.annotation.RavenwoodKeepWholeClass
32 public final class AppCompatCallbacks implements Compatibility.BehaviorChangeDelegate {
33     private final long[] mDisabledChanges;
34     private final long[] mLoggableChanges;
35     private final ChangeReporter mChangeReporter;
36 
37     /**
38      * Install this class into the current process using the disabled and loggable changes lists.
39      *
40      * @param disabledChanges Set of compatibility changes that are disabled for this process.
41      * @param loggableChanges Set of compatibility changes that we want to log.
42      */
install(long[] disabledChanges, long[] loggableChanges)43     public static void install(long[] disabledChanges, long[] loggableChanges) {
44         Compatibility.setBehaviorChangeDelegate(
45                 new AppCompatCallbacks(disabledChanges, loggableChanges));
46     }
47 
AppCompatCallbacks(long[] disabledChanges, long[] loggableChanges)48     private AppCompatCallbacks(long[] disabledChanges, long[] loggableChanges) {
49         mDisabledChanges = Arrays.copyOf(disabledChanges, disabledChanges.length);
50         mLoggableChanges = Arrays.copyOf(loggableChanges, loggableChanges.length);
51         Arrays.sort(mDisabledChanges);
52         Arrays.sort(mLoggableChanges);
53         mChangeReporter = new ChangeReporter(ChangeReporter.SOURCE_APP_PROCESS);
54     }
55 
56     /**
57      * Helper to determine if a list contains a changeId.
58      *
59      * @param list to search through
60      * @param changeId for which to search in the list
61      * @return true if the given changeId is found in the provided array.
62      */
changeIdInChangeList(long[] list, long changeId)63     private boolean changeIdInChangeList(long[] list, long changeId) {
64         return Arrays.binarySearch(list, changeId) >= 0;
65     }
66 
onChangeReported(long changeId)67     public void onChangeReported(long changeId) {
68         boolean isLoggable = changeIdInChangeList(mLoggableChanges, changeId);
69         reportChange(changeId, ChangeReporter.STATE_LOGGED, isLoggable);
70     }
71 
isChangeEnabled(long changeId)72     public boolean isChangeEnabled(long changeId) {
73         boolean isEnabled = !changeIdInChangeList(mDisabledChanges, changeId);
74         boolean isLoggable = changeIdInChangeList(mLoggableChanges, changeId);
75         if (isEnabled) {
76             // Not present in the disabled changeId array
77             reportChange(changeId, ChangeReporter.STATE_ENABLED, isLoggable);
78             return true;
79         }
80         reportChange(changeId, ChangeReporter.STATE_DISABLED, isLoggable);
81         return false;
82     }
83 
reportChange(long changeId, int state, boolean isLoggable)84     private void reportChange(long changeId, int state, boolean isLoggable) {
85         int uid = Process.myUid();
86         mChangeReporter.reportChange(uid, changeId, state, false, isLoggable);
87     }
88 
89 }
90