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 com.android.adservices.service.measurement.aggregation; 18 19 import android.annotation.NonNull; 20 import android.net.Uri; 21 22 import org.json.JSONArray; 23 import org.json.JSONException; 24 import org.json.JSONObject; 25 26 import java.math.BigInteger; 27 import java.util.ArrayList; 28 import java.util.List; 29 import java.util.Objects; 30 31 /** POJO for AggregateDebugReporting. */ 32 public class AggregateDebugReporting { 33 34 private final int mBudget; 35 private final BigInteger mKeyPiece; 36 private final List<AggregateDebugReportData> mAggregateDebugReportDataList; 37 private final Uri mAggregationCoordinatorOrigin; 38 AggregateDebugReporting(@onNull AggregateDebugReporting.Builder builder)39 private AggregateDebugReporting(@NonNull AggregateDebugReporting.Builder builder) { 40 mBudget = builder.mBudget; 41 mKeyPiece = builder.mKeyPiece; 42 mAggregateDebugReportDataList = builder.mAggregateDebugReportDataList; 43 mAggregationCoordinatorOrigin = builder.mAggregationCoordinatorOrigin; 44 } 45 46 @Override equals(Object obj)47 public boolean equals(Object obj) { 48 if (!(obj instanceof AggregateDebugReporting aggregateDebugReporting)) { 49 return false; 50 } 51 return mBudget == aggregateDebugReporting.mBudget 52 && Objects.equals(mKeyPiece, aggregateDebugReporting.mKeyPiece) 53 && Objects.equals( 54 mAggregateDebugReportDataList, 55 aggregateDebugReporting.mAggregateDebugReportDataList) 56 && Objects.equals( 57 mAggregationCoordinatorOrigin, 58 aggregateDebugReporting.mAggregationCoordinatorOrigin); 59 } 60 61 @Override hashCode()62 public int hashCode() { 63 return Objects.hash( 64 mBudget, mKeyPiece, mAggregateDebugReportDataList, mAggregationCoordinatorOrigin); 65 } 66 67 /** Returns the value of the aggregate_debug_report key budget. */ getBudget()68 public int getBudget() { 69 return mBudget; 70 } 71 72 /** Returns the value of the aggregate_debug_report key key_piece. */ getKeyPiece()73 public BigInteger getKeyPiece() { 74 return mKeyPiece; 75 } 76 77 /** Returns the value of the aggregate_debug_report key data. */ getAggregateDebugReportDataList()78 public List<AggregateDebugReportData> getAggregateDebugReportDataList() { 79 return mAggregateDebugReportDataList; 80 } 81 82 /** Returns the value of the aggregate_debug_report key aggregation_coordinator_origin. */ getAggregationCoordinatorOrigin()83 public Uri getAggregationCoordinatorOrigin() { 84 return mAggregationCoordinatorOrigin; 85 } 86 87 /** Builder for {@link AggregateDebugReporting}. */ 88 public static final class Builder { 89 private int mBudget; 90 private BigInteger mKeyPiece; 91 private List<AggregateDebugReportData> mAggregateDebugReportDataList; 92 private Uri mAggregationCoordinatorOrigin; 93 Builder( int budget, BigInteger keyPiece, List<AggregateDebugReportData> aggregateDebugReportDataList, Uri aggregationCoordinatorOrigin)94 public Builder( 95 int budget, 96 BigInteger keyPiece, 97 List<AggregateDebugReportData> aggregateDebugReportDataList, 98 Uri aggregationCoordinatorOrigin) { 99 mBudget = budget; 100 mKeyPiece = keyPiece; 101 mAggregateDebugReportDataList = aggregateDebugReportDataList; 102 mAggregationCoordinatorOrigin = aggregationCoordinatorOrigin; 103 } 104 Builder(JSONObject aggregateDebugReporting)105 public Builder(JSONObject aggregateDebugReporting) throws JSONException { 106 if (!aggregateDebugReporting.isNull(AggregateDebugReportingHeaderContract.BUDGET)) { 107 mBudget = 108 aggregateDebugReporting.getInt( 109 AggregateDebugReportingHeaderContract.BUDGET); 110 } 111 if (!aggregateDebugReporting.isNull(AggregateDebugReportingHeaderContract.KEY_PIECE)) { 112 String hexString = 113 aggregateDebugReporting 114 .getString(AggregateDebugReportingHeaderContract.KEY_PIECE) 115 .substring(2); 116 mKeyPiece = new BigInteger(hexString, 16); 117 } 118 if (!aggregateDebugReporting.isNull(AggregateDebugReportingHeaderContract.DEBUG_DATA)) { 119 mAggregateDebugReportDataList = new ArrayList<>(); 120 JSONArray aggregateDebugReportDataArray = 121 aggregateDebugReporting.getJSONArray( 122 AggregateDebugReportingHeaderContract.DEBUG_DATA); 123 for (int i = 0; i < aggregateDebugReportDataArray.length(); i++) { 124 mAggregateDebugReportDataList.add( 125 new AggregateDebugReportData.Builder( 126 aggregateDebugReportDataArray.getJSONObject(i)) 127 .build()); 128 } 129 } 130 if (!aggregateDebugReporting.isNull( 131 AggregateDebugReportingHeaderContract.AGGREGATION_COORDINATOR_ORIGIN)) { 132 mAggregationCoordinatorOrigin = 133 Uri.parse( 134 aggregateDebugReporting.getString( 135 AggregateDebugReportingHeaderContract 136 .AGGREGATION_COORDINATOR_ORIGIN)); 137 } 138 } 139 140 /** Build the {@link AggregateDebugReporting} */ 141 @NonNull build()142 public AggregateDebugReporting build() { 143 return new AggregateDebugReporting(this); 144 } 145 } 146 147 public interface AggregateDebugReportingHeaderContract { 148 String BUDGET = "budget"; 149 String KEY_PIECE = "key_piece"; 150 String DEBUG_DATA = "debug_data"; 151 String AGGREGATION_COORDINATOR_ORIGIN = "aggregation_coordinator_origin"; 152 } 153 } 154