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.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 AdId. It displays the adId on a textView on the 35 * screen. If there is an error, it displays the error. 36 */ 37 public class MainActivity extends AppCompatActivity { 38 private Button mAdIdButton; 39 private TextView mAdIdTextView; 40 private AdIdManager mAdIdManager; 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 mAdIdTextView = findViewById(R.id.adIdTextView); 48 mAdIdButton = findViewById(R.id.adIdButton); 49 50 // AdIdManager can not be called on R until OutcomeReceiver dependencies are removed. 51 if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.R) { 52 setAdIdText("Device not supported."); 53 return; 54 } 55 56 mAdIdManager = 57 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) 58 ? this.getSystemService(AdIdManager.class) 59 : AdIdManager.get(this); 60 registerAdIdButton(); 61 } 62 registerAdIdButton()63 private void registerAdIdButton() { 64 OutcomeReceiver<AdId, Exception> adIdCallback = 65 new OutcomeReceiver<AdId, Exception>() { 66 @Override 67 public void onResult(@NonNull AdId adId) { 68 setAdIdText(getAdIdDisplayString(adId)); 69 } 70 71 @Override 72 public void onError(@NonNull Exception error) { 73 setAdIdText(error.toString()); 74 } 75 }; 76 77 mAdIdButton.setOnClickListener(v -> getAdId(mExecutor, adIdCallback)); 78 } 79 80 @TargetApi(Build.VERSION_CODES.S) 81 @SuppressWarnings("NewApi") getAdId(Executor executor, OutcomeReceiver<AdId, Exception> callback)82 private void getAdId(Executor executor, OutcomeReceiver<AdId, 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 mAdIdManager.getAdId(executor, callback); 87 } catch (IllegalStateException e) { 88 callback.onError(e); 89 } 90 } 91 setAdIdText(String text)92 private void setAdIdText(String text) { 93 runOnUiThread(() -> mAdIdTextView.setText(text)); 94 } 95 96 @SuppressWarnings("NewApi") getAdIdDisplayString(AdId adId)97 private String getAdIdDisplayString(AdId adId) { 98 return "AdId: " + adId.getAdId() + "\n" + "LAT: " + adId.isLimitAdTrackingEnabled(); 99 } 100 } 101