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 private static final boolean DEVELOPER_MODE_FEATURE_ENABLED = false; 31 32 /** Method to create DevOptions Enabled rule.. */ createDevOptionsAvailableRule( Context context, String logTag)33 public static SupportedByConditionRule createDevOptionsAvailableRule( 34 Context context, String logTag) { 35 return new SupportedByConditionRule( 36 "Developer Options are not enabled or the calling app is not debuggable", 37 () -> isDevOptionsEnabled(context, logTag)); 38 } 39 40 /** Method to check if Dev Options are enabled based on DevContext. */ isDevOptionsEnabled(Context context, String logTag)41 public static boolean isDevOptionsEnabled(Context context, String logTag) { 42 DevContextFilter devContextFilter = 43 DevContextFilter.create(context, DEVELOPER_MODE_FEATURE_ENABLED); 44 DevContext mDevContext = devContextFilter.createDevContext(Process.myUid()); 45 boolean isDebuggable = 46 devContextFilter.isDebuggable(mDevContext.getCallingAppPackageName()); 47 boolean isDeveloperMode = devContextFilter.isDeviceDevOptionsEnabledOrDebuggable(); 48 Log.d( 49 logTag, 50 String.format("Debuggable: %b\n", isDebuggable) 51 + String.format("Developer options on: %b", isDeveloperMode)); 52 return mDevContext.getDeviceDevOptionsEnabled(); 53 } 54 } 55