• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
51             setAppSetIdText("Device not supported.");
52             return;
53         }
54 
55         mAppSetIdManager =
56                 (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU)
57                         ? this.getSystemService(AppSetIdManager.class)
58                         : AppSetIdManager.get(this);
59         registerAppSetIdButton();
60     }
61 
registerAppSetIdButton()62     private void registerAppSetIdButton() {
63         OutcomeReceiver<AppSetId, Exception> appSetIdCallback =
64                 new OutcomeReceiver<AppSetId, Exception>() {
65                     @Override
66                     public void onResult(@NonNull AppSetId appSetId) {
67                         setAppSetIdText(getAppSetIdDisplayString(appSetId));
68                     }
69 
70                     @Override
71                     public void onError(@NonNull Exception error) {
72                         setAppSetIdText(error.toString());
73                     }
74                 };
75 
76         mAppSetIdButton.setOnClickListener(v -> getAppSetId(mExecutor, appSetIdCallback));
77     }
78 
79     @TargetApi(Build.VERSION_CODES.S)
80     @SuppressWarnings("NewApi")
getAppSetId(Executor executor, OutcomeReceiver<AppSetId, Exception> callback)81     private void getAppSetId(Executor executor, OutcomeReceiver<AppSetId, Exception> callback) {
82         // getService() in AdIdManager throws on main thread and doesn't offload the error to the
83         // callback. Catch it to avoid app to crash.
84         try {
85             mAppSetIdManager.getAppSetId(executor, callback);
86         } catch (IllegalStateException e) {
87             callback.onError(e);
88         }
89     }
90 
setAppSetIdText(String text)91     private void setAppSetIdText(String text) {
92         runOnUiThread(() -> mAppSetIdTextView.setText(text));
93     }
94 
95     @SuppressWarnings("NewApi")
getAppSetIdDisplayString(AppSetId appSetId)96     private String getAppSetIdDisplayString(AppSetId appSetId) {
97         return "AppSetId: " + appSetId.getId() + "\n" + "AppSetId Scope: " + appSetId.getScope();
98     }
99 }
100