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 package com.example.adservices.samples.appsetid.app; 17 18 import android.adservices.appsetid.AppSetId; 19 import android.adservices.appsetid.AppSetIdManager; 20 import android.annotation.TargetApi; 21 import android.os.Build; 22 import android.os.Bundle; 23 import android.os.OutcomeReceiver; 24 import android.widget.Button; 25 import android.widget.TextView; 26 27 import androidx.annotation.NonNull; 28 import androidx.appcompat.app.AppCompatActivity; 29 30 import java.util.concurrent.Executor; 31 import java.util.concurrent.Executors; 32 33 /** 34 * Android application activity for testing reading AppSetId. It displays the appSetId on a textView 35 * on the screen. If there is an error, it displays the error. 36 */ 37 public class MainActivity extends AppCompatActivity { 38 private Button mAppSetIdButton; 39 private TextView mAppSetIdTextView; 40 private AppSetIdManager mAppSetIdManager; 41 private final Executor mExecutor = Executors.newCachedThreadPool(); 42 43 @Override onCreate(Bundle savedInstanceState)44 protected void onCreate(Bundle savedInstanceState) { 45 super.onCreate(savedInstanceState); 46 setContentView(R.layout.activity_main); 47 mAppSetIdTextView = findViewById(R.id.appSetIdTextView); 48 mAppSetIdButton = findViewById(R.id.appSetIdButton); 49 50 // AppSetIdManager can not be called on R until OutcomeReceiver dependencies are removed. 51 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) { 52 setAppSetIdText("Device not supported."); 53 return; 54 } 55 56 mAppSetIdManager = 57 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) 58 ? this.getSystemService(AppSetIdManager.class) 59 : AppSetIdManager.get(this); 60 registerAppSetIdButton(); 61 } 62 registerAppSetIdButton()63 private void registerAppSetIdButton() { 64 OutcomeReceiver<AppSetId, Exception> appSetIdCallback = 65 new OutcomeReceiver<AppSetId, Exception>() { 66 @Override 67 public void onResult(@NonNull AppSetId appSetId) { 68 setAppSetIdText(getAppSetIdDisplayString(appSetId)); 69 } 70 71 @Override 72 public void onError(@NonNull Exception error) { 73 setAppSetIdText(error.toString()); 74 } 75 }; 76 77 mAppSetIdButton.setOnClickListener(v -> getAppSetId(mExecutor, appSetIdCallback)); 78 } 79 80 @TargetApi(Build.VERSION_CODES.S) 81 @SuppressWarnings("NewApi") getAppSetId(Executor executor, OutcomeReceiver<AppSetId, Exception> callback)82 private void getAppSetId(Executor executor, OutcomeReceiver<AppSetId, Exception> callback) { 83 // getService() in AdIdManager throws on main thread and doesn't offload the error to the 84 // callback. Catch it to avoid app to crash. 85 try { 86 mAppSetIdManager.getAppSetId(executor, callback); 87 } catch (IllegalStateException e) { 88 callback.onError(e); 89 } 90 } 91 setAppSetIdText(String text)92 private void setAppSetIdText(String text) { 93 runOnUiThread(() -> mAppSetIdTextView.setText(text)); 94 } 95 96 @SuppressWarnings("NewApi") getAppSetIdDisplayString(AppSetId appSetId)97 private String getAppSetIdDisplayString(AppSetId appSetId) { 98 return "AppSetId: " + appSetId.getId() + "\n" + "AppSetId Scope: " + appSetId.getScope(); 99 } 100 } 101