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 com.android.ondevicepersonalization.services.util; 18 19 import android.adservices.ondevicepersonalization.IsolatedServiceException; 20 import android.annotation.NonNull; 21 import android.content.ComponentName; 22 import android.content.ContentResolver; 23 import android.content.Context; 24 import android.content.pm.PackageManager; 25 import android.os.Build; 26 import android.os.SystemProperties; 27 import android.provider.Settings; 28 29 import com.android.odp.module.common.PackageUtils; 30 import com.android.ondevicepersonalization.internal.util.ExceptionInfo; 31 import com.android.ondevicepersonalization.internal.util.LoggerFactory; 32 import com.android.ondevicepersonalization.services.FlagsFactory; 33 import com.android.ondevicepersonalization.services.OdpServiceException; 34 import com.android.ondevicepersonalization.services.OnDevicePersonalizationApplication; 35 36 import java.util.Objects; 37 38 /** Fuctions for testing and debugging. */ 39 public class DebugUtils { 40 private static final LoggerFactory.Logger sLogger = LoggerFactory.getLogger(); 41 private static final String TAG = DebugUtils.class.getSimpleName(); 42 private static final int MAX_EXCEPTION_CHAIN_DEPTH = 3; 43 44 private static final String OVERRIDE_FC_SERVER_URL_PACKAGE = 45 "debug.ondevicepersonalization.override_fc_server_url_package"; 46 private static final String OVERRIDE_FC_SERVER_URL = 47 "debug.ondevicepersonalization.override_fc_server_url"; 48 49 /** Returns true if the device is debuggable. */ isDeveloperModeEnabled(@onNull Context context)50 public static boolean isDeveloperModeEnabled(@NonNull Context context) { 51 ContentResolver resolver = Objects.requireNonNull(context.getContentResolver()); 52 return Build.isDebuggable() 53 || Settings.Global.getInt( 54 resolver, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0; 55 } 56 57 /** Returns the exception code reported by the service if debugging is allowed. */ getIsolatedServiceExceptionCode( @onNull Context context, @NonNull ComponentName service, @NonNull OdpServiceException e)58 public static int getIsolatedServiceExceptionCode( 59 @NonNull Context context, 60 @NonNull ComponentName service, 61 @NonNull OdpServiceException e) { 62 try { 63 if (!FlagsFactory.getFlags().isIsolatedServiceDebuggingEnabled()) { 64 return 0; 65 } 66 if (isDeveloperModeEnabled(context) 67 && PackageUtils.isPackageDebuggable(context, service.getPackageName())) { 68 if (e.getCause() != null && e.getCause() instanceof IsolatedServiceException) { 69 return ((IsolatedServiceException) e.getCause()).getErrorCode(); 70 } 71 } 72 } catch (Exception e2) { 73 sLogger.e(e2, TAG + ": failed to get code"); 74 } 75 return 0; 76 } 77 78 /** Serializes an exception chain to a byte[] */ serializeExceptionInfo( ComponentName service, Throwable t)79 public static byte[] serializeExceptionInfo( 80 ComponentName service, Throwable t) { 81 try { 82 Context context = OnDevicePersonalizationApplication.getAppContext(); 83 if (t == null || !isDeveloperModeEnabled(context) 84 || !FlagsFactory.getFlags().isIsolatedServiceDebuggingEnabled() 85 || !PackageUtils.isPackageDebuggable(context, service.getPackageName())) { 86 return null; 87 } 88 89 return ExceptionInfo.toByteArray(t, MAX_EXCEPTION_CHAIN_DEPTH); 90 } catch (Exception e) { 91 sLogger.e(e, TAG + ": failed to serialize exception info"); 92 return null; 93 } 94 } 95 96 /** 97 * Returns an override URL for federated compute for the provided package if one exists, else 98 * returns empty if a matching override is not found. 99 * 100 * @param applicationContext the application context. 101 * @param packageName the package for which to check for override. 102 * @return override URL or empty string if an override is not found. 103 */ getFcServerOverrideUrl(Context applicationContext, String packageName)104 public static String getFcServerOverrideUrl(Context applicationContext, String packageName) { 105 String url = ""; 106 // Check for override manifest url property, if package is debuggable 107 try { 108 if (!PackageUtils.isPackageDebuggable(applicationContext, packageName)) { 109 return url; 110 } 111 } catch (PackageManager.NameNotFoundException nne) { 112 sLogger.e(TAG + ": failed to get override URL for package." + nne); 113 return url; 114 } 115 116 // Check system properties first 117 if (SystemProperties.get(OVERRIDE_FC_SERVER_URL_PACKAGE, "").equals(packageName)) { 118 String overrideManifestUrl = SystemProperties.get(OVERRIDE_FC_SERVER_URL, ""); 119 if (!overrideManifestUrl.isEmpty()) { 120 sLogger.d( 121 TAG 122 + ": Overriding FC server URL from system properties for package" 123 + packageName 124 + " to " 125 + overrideManifestUrl); 126 url = overrideManifestUrl; 127 } 128 } 129 130 return url; 131 } 132 DebugUtils()133 private DebugUtils() {} 134 } 135