1 /* 2 * Copyright (C) 2024 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.common; 18 19 import android.annotation.FlaggedApi; 20 import android.annotation.IntDef; 21 import android.annotation.NonNull; 22 import android.os.Parcel; 23 import android.os.Parcelable; 24 25 import com.android.adservices.flags.Flags; 26 27 import java.lang.annotation.Retention; 28 import java.lang.annotation.RetentionPolicy; 29 import java.util.Objects; 30 31 /** 32 * Represents the user's choice for the modules in AdServices. Can be unknown, opted-in, or 33 * opted-out. 34 * 35 * @hide 36 */ 37 @FlaggedApi(Flags.FLAG_ADSERVICES_ENABLE_PER_MODULE_OVERRIDES_API) 38 public final class AdServicesModuleUserChoice implements Parcelable { 39 40 /** Default user choice state */ 41 public static final int USER_CHOICE_UNKNOWN = AdServicesCommonManager.USER_CHOICE_UNKNOWN; 42 43 /** User opted in state */ 44 public static final int USER_CHOICE_OPTED_IN = AdServicesCommonManager.USER_CHOICE_OPTED_IN; 45 46 /** User opted out state */ 47 public static final int USER_CHOICE_OPTED_OUT = AdServicesCommonManager.USER_CHOICE_OPTED_OUT; 48 49 /** 50 * Result codes that are common across various modules. 51 * 52 * @hide 53 */ 54 @IntDef( 55 prefix = {""}, 56 value = {USER_CHOICE_UNKNOWN, USER_CHOICE_OPTED_IN, USER_CHOICE_OPTED_OUT}) 57 @Retention(RetentionPolicy.SOURCE) 58 public @interface ModuleUserChoiceCode {} 59 60 @Module.ModuleCode private int mModule; 61 62 @ModuleUserChoiceCode private int mUserChoice; 63 AdServicesModuleUserChoice(Parcel in)64 private AdServicesModuleUserChoice(Parcel in) { 65 Objects.requireNonNull(in, "Parcel is null"); 66 mModule = in.readInt(); 67 mUserChoice = in.readInt(); 68 } 69 70 /** 71 * Constructor for a user choice. 72 * 73 * @param module desired module 74 * @param userChoice desired user choice 75 */ AdServicesModuleUserChoice( @odule.ModuleCode int module, @ModuleUserChoiceCode int userChoice)76 public AdServicesModuleUserChoice( 77 @Module.ModuleCode int module, @ModuleUserChoiceCode int userChoice) { 78 this.mModule = Module.validate(module); 79 this.mUserChoice = validate(userChoice); 80 } 81 82 /** 83 * Validates a user choice. For this function doesn't alter the input and just returns it back, 84 * if not valid fails with {@link IllegalArgumentException}. 85 * 86 * @param userChoice user choice to validate 87 * @return user choice 88 */ 89 @ModuleUserChoiceCode validate(@oduleUserChoiceCode int userChoice)90 private static int validate(@ModuleUserChoiceCode int userChoice) { 91 return switch (userChoice) { 92 case USER_CHOICE_UNKNOWN, USER_CHOICE_OPTED_IN, USER_CHOICE_OPTED_OUT -> userChoice; 93 default -> throw new IllegalArgumentException("Invalid User Choice:" + userChoice); 94 }; 95 } 96 97 @NonNull 98 public static final Creator<AdServicesModuleUserChoice> CREATOR = 99 new Creator<>() { 100 @Override 101 public AdServicesModuleUserChoice createFromParcel(Parcel in) { 102 return new AdServicesModuleUserChoice(in); 103 } 104 105 @Override 106 public AdServicesModuleUserChoice[] newArray(int size) { 107 return new AdServicesModuleUserChoice[size]; 108 } 109 }; 110 111 @Override describeContents()112 public int describeContents() { 113 return 0; 114 } 115 116 @Override writeToParcel(@onNull Parcel dest, int flags)117 public void writeToParcel(@NonNull Parcel dest, int flags) { 118 Objects.requireNonNull(dest, "Parcel is null"); 119 dest.writeInt(mModule); 120 dest.writeInt(mUserChoice); 121 } 122 123 /** Gets the name of current module */ getModule()124 public @Module.ModuleCode int getModule() { 125 return mModule; 126 } 127 128 /** Gets the user opted in/out choice of current module */ getUserChoice()129 public @ModuleUserChoiceCode int getUserChoice() { 130 return mUserChoice; 131 } 132 } 133