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.adid.app; 17 18 import android.adservices.adid.AdId; 19 import android.adservices.adid.AdIdManager; 20 import android.adservices.common.AdServicesOutcomeReceiver; 21 import android.os.Build; 22 import android.os.Bundle; 23 import android.widget.Button; 24 import android.widget.TextView; 25 26 import androidx.annotation.NonNull; 27 import androidx.appcompat.app.AppCompatActivity; 28 29 import java.util.concurrent.Executor; 30 import java.util.concurrent.Executors; 31 32 /** 33 * Android application activity for testing reading AdId. It displays the adId on a textView on the 34 * screen. If there is an error, it displays the error. 35 */ 36 public class MainActivity extends AppCompatActivity { 37 private Button mAdIdButton; 38 private TextView mAdIdTextView; 39 private AdIdManager mAdIdManager; 40 private final Executor mExecutor = Executors.newCachedThreadPool(); 41 42 @Override onCreate(Bundle savedInstanceState)43 protected void onCreate(Bundle savedInstanceState) { 44 super.onCreate(savedInstanceState); 45 setContentView(R.layout.activity_main); 46 mAdIdTextView = findViewById(R.id.adIdTextView); 47 mAdIdButton = findViewById(R.id.adIdButton); 48 49 mAdIdManager = 50 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) 51 ? this.getSystemService(AdIdManager.class) 52 : AdIdManager.get(this); 53 registerAdIdButton(); 54 } 55 56 @SuppressWarnings("NewApi") registerAdIdButton()57 private void registerAdIdButton() { 58 AdServicesOutcomeReceiver<AdId, Exception> adIdCallback = 59 new AdServicesOutcomeReceiver<AdId, Exception>() { 60 @Override 61 public void onResult(@NonNull AdId adId) { 62 setAdIdText(getAdIdDisplayString(adId)); 63 } 64 65 @Override 66 public void onError(@NonNull Exception error) { 67 setAdIdText(error.toString()); 68 } 69 }; 70 71 mAdIdButton.setOnClickListener(v -> getAdId(mExecutor, adIdCallback)); 72 } 73 74 @SuppressWarnings("NewApi") getAdId(Executor executor, AdServicesOutcomeReceiver<AdId, Exception> callback)75 private void getAdId(Executor executor, AdServicesOutcomeReceiver<AdId, Exception> callback) { 76 // getService() in AdIdManager throws on main thread and doesn't offload the error to the 77 // callback. Catch it to avoid app to crash. 78 try { 79 mAdIdManager.getAdId(executor, callback); 80 } catch (IllegalStateException e) { 81 callback.onError(e); 82 } 83 } 84 setAdIdText(String text)85 private void setAdIdText(String text) { 86 runOnUiThread(() -> mAdIdTextView.setText(text)); 87 } 88 89 @SuppressWarnings("NewApi") getAdIdDisplayString(AdId adId)90 private String getAdIdDisplayString(AdId adId) { 91 return "AdId: " + adId.getAdId() + "\n" + "LAT: " + adId.isLimitAdTrackingEnabled(); 92 } 93 } 94