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 com.android.adservices.service.devapi; 18 19 import android.annotation.Nullable; 20 21 import com.android.adservices.shared.common.ApplicationContextSingleton; 22 23 import com.google.auto.value.AutoValue; 24 25 import java.util.Objects; 26 27 /** 28 * Instances of this class are used to hold information required by the developer features supported 29 * by the AdSelection and CustomAudience API. An instance of this class is created while serving 30 * each client call and will be used by underlying services to understand if they need to activate 31 * development features and to provide information useful for those features. 32 */ 33 @AutoValue 34 public abstract class DevContext { 35 36 /** 37 * Bogus package name used on test cases that emulate cases where the developer options is 38 * disabled. 39 */ 40 public static final String UNKNOWN_APP_BECAUSE_DEVICE_DEV_OPTIONS_IS_DISABLED = 41 "unknown.app.because.dev.options.is.disabled"; 42 43 /** 44 * @return {@code true} if the device's developer options are enabled for this service call. 45 */ getDeviceDevOptionsEnabled()46 public abstract boolean getDeviceDevOptionsEnabled(); 47 48 // TODO(b/356709022): remove @Nullable 49 50 /** 51 * @return The package name for the calling app or NULL if the dev options are not enabled. 52 */ 53 @Nullable getCallingAppPackageName()54 public abstract String getCallingAppPackageName(); 55 56 /** 57 * @return {@link DevSession} state which represents if we are in a development session. Note 58 * this is very different from {@link DevContext#getDeviceDevOptionsEnabled()}. 59 */ getDevSession()60 public abstract DevSession getDevSession(); 61 62 /** 63 * @deprecated use {@link #builder(String)} instead. 64 */ builder()65 public static DevContext.Builder builder() { 66 return new AutoValue_DevContext.Builder().setDevSession(DevSession.UNKNOWN); 67 } 68 69 // TODO(b/356709022): remove once all callers were refactored 70 71 /** Returns a new generic builder */ builder(String callingAppPackageName)72 public static DevContext.Builder builder(String callingAppPackageName) { 73 Objects.requireNonNull(callingAppPackageName, "callingAppPackageName cannot be null"); 74 return new AutoValue_DevContext.Builder() 75 .setCallingAppPackageName(callingAppPackageName) 76 .setDevSession(DevSession.UNKNOWN); 77 } 78 79 /** Returns a new instance of {@link DevContext} with developer options disabled. */ createForDevOptionsDisabled()80 public static DevContext createForDevOptionsDisabled() { 81 return DevContext.builder(UNKNOWN_APP_BECAUSE_DEVICE_DEV_OPTIONS_IS_DISABLED) 82 .setDeviceDevOptionsEnabled(false) 83 .build(); 84 } 85 86 /** 87 * Returns a new instance of {@link DevContext} with developer options enabled. 88 * 89 * <p>Used when the calling identity is an end-user interacting with the adservices module via a 90 * shell command. In this case we use the adservices package as the app package identity. 91 */ createForDevIdentity()92 public static DevContext createForDevIdentity() { 93 return DevContext.builder(ApplicationContextSingleton.get().getPackageName()) 94 .setDeviceDevOptionsEnabled(true) 95 .build(); 96 } 97 98 /** The Builder for {@link DevContext} */ 99 @AutoValue.Builder 100 public abstract static class Builder { 101 /** Sets the value for the dev options enabled flag */ setDeviceDevOptionsEnabled(boolean flag)102 public abstract DevContext.Builder setDeviceDevOptionsEnabled(boolean flag); 103 104 // TODO(b/356709022): remove @Nullable 105 106 /** Sets the value for the calling app package */ setCallingAppPackageName(@ullable String value)107 public abstract DevContext.Builder setCallingAppPackageName(@Nullable String value); 108 109 /** Sets the value for the dev session active flag */ setDevSession(DevSession devSession)110 public abstract DevContext.Builder setDevSession(DevSession devSession); 111 112 /** Builds it!. */ build()113 public abstract DevContext build(); 114 } 115 } 116