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.alarmmanager.util;
18 
19 import android.Manifest;
20 import android.app.compat.CompatChanges;
21 import android.content.Context;
22 import android.os.UserHandle;
23 
24 import androidx.test.InstrumentationRegistry;
25 
26 import com.android.compatibility.common.util.SystemUtil;
27 
28 public class Utils {
29     private static final Context sContext = InstrumentationRegistry.getTargetContext();
30 
Utils()31     private Utils() {
32         // Empty to ensure no one can instantiate it.
33     }
34 
isChangeEnabled(long changeId, String packageName, UserHandle user)35     private static boolean isChangeEnabled(long changeId, String packageName, UserHandle user) {
36         try {
37             return SystemUtil.callWithShellPermissionIdentity(
38                     () -> CompatChanges.isChangeEnabled(changeId, packageName, user),
39                     Manifest.permission.READ_COMPAT_CHANGE_CONFIG,
40                     Manifest.permission.LOG_COMPAT_CHANGE);
41         } catch (Exception e) {
42             throw new RuntimeException("Exception while reading compat config", e);
43         }
44     }
45 
enableChange(long changeId, String packageName, int userId)46     public static void enableChange(long changeId, String packageName, int userId) {
47         if (!isChangeEnabled(changeId, packageName, UserHandle.of(userId))) {
48             SystemUtil.runShellCommand("am compat enable --no-kill " + changeId + " "
49                     + packageName, output -> output.contains("Enabled"));
50         }
51     }
52 
enableChangeForSelf(long changeId)53     public static void enableChangeForSelf(long changeId) {
54         if (!CompatChanges.isChangeEnabled(changeId)) {
55             SystemUtil.runShellCommand("am compat enable --no-kill " + changeId + " "
56                     + sContext.getOpPackageName(), output -> output.contains("Enabled"));
57         }
58     }
59 
resetChange(long changeId, String packageName)60     public static void resetChange(long changeId, String packageName) {
61         SystemUtil.runShellCommand("am compat reset --no-kill " + changeId + " " + packageName);
62     }
63 
getPackageUid(String packageName)64     public static int getPackageUid(String packageName) {
65         try {
66             return sContext.getPackageManager().getPackageUid(packageName, 0);
67         } catch (Exception e) {
68             throw new RuntimeException(e);
69         }
70     }
71 }
72