1 /*
2  * Copyright (C) 2023 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.adservices.utils;
18 
19 import android.content.Context;
20 import android.os.Process;
21 import android.util.Log;
22 
23 import com.android.adservices.service.devapi.DevContext;
24 import com.android.adservices.service.devapi.DevContextFilter;
25 import com.android.adservices.shared.testing.SupportedByConditionRule;
26 
27 /** Class to manage all utilities required for DevContext in CTS */
28 public class DevContextUtils {
29 
30     /** Method to create DevOptions Enabled rule.. */
createDevOptionsAvailableRule( Context context, String logTag)31     public static SupportedByConditionRule createDevOptionsAvailableRule(
32             Context context, String logTag) {
33         return new SupportedByConditionRule(
34                 "Developer Options are not enabled or the calling app is not debuggable",
35                 () -> isDevOptionsEnabled(context, logTag));
36     }
37 
38     /** Method to check if Dev Options are enabled based on DevContext. */
isDevOptionsEnabled(Context context, String logTag)39     public static boolean isDevOptionsEnabled(Context context, String logTag) {
40         DevContextFilter devContextFilter = DevContextFilter.create(context);
41         DevContext mDevContext = DevContextFilter.create(context).createDevContext(Process.myUid());
42         boolean isDebuggable =
43                 devContextFilter.isDebuggable(mDevContext.getCallingAppPackageName());
44         boolean isDeveloperMode = devContextFilter.isDeveloperMode();
45         Log.d(
46                 logTag,
47                 String.format("Debuggable: %b\n", isDebuggable)
48                         + String.format("Developer options on: %b", isDeveloperMode));
49         return mDevContext.getDevOptionsEnabled();
50     }
51 }
52