• 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.topics.sampleappwithnoperm;
17 
18 import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
19 
20 import android.adservices.clients.topics.AdvertisingTopicsClient;
21 import android.adservices.topics.GetTopicsResponse;
22 import android.adservices.topics.Topic;
23 import android.os.Bundle;
24 import android.os.Handler;
25 import android.util.Log;
26 import android.widget.Button;
27 import android.widget.TextView;
28 
29 import androidx.appcompat.app.AppCompatActivity;
30 
31 import com.google.common.util.concurrent.FutureCallback;
32 import com.google.common.util.concurrent.Futures;
33 import com.google.common.util.concurrent.ListenableFuture;
34 
35 import java.io.StringWriter;
36 import java.io.PrintWriter;
37 import java.util.ArrayList;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.concurrent.Executor;
41 import java.util.concurrent.Executors;
42 
43 /**
44  * Android application activity for testing Topics API by providing a button in UI that initiate
45  * user's interaction with Topics Manager in the background. Response from Topics API will be shown
46  * in the app as text as well as toast message. In case anything goes wrong in this process, error
47  * message will also be shown in toast to suggest the Exception encountered.
48  */
49 public class MainActivity extends AppCompatActivity {
50 
51     private static final Executor CALLBACK_EXECUTOR = Executors.newCachedThreadPool();
52     private static final String NEWLINE = "\n";
53     private static final String TAG = "SampleApp";
54     private static final List<String> SDK_NAMES =
55             new ArrayList<>(Arrays.asList("SdkName1", "SdkName2"));
56     private Button mTopicsClientButton;
57     private TextView mResultTextView;
58     private AdvertisingTopicsClient mAdvertisingTopicsClient;
59     private Handler mHandler;
60 
61     @Override
onCreate(Bundle savedInstanceState)62     protected void onCreate(Bundle savedInstanceState) {
63         super.onCreate(savedInstanceState);
64         setContentView(R.layout.activity_main);
65         mTopicsClientButton = findViewById(R.id.topics_client_button);
66         mResultTextView = findViewById(R.id.textView);
67         registerGetTopicsButton();
68         mHandler = new Handler();
69     }
70 
registerGetTopicsButton()71     private void registerGetTopicsButton() {
72         mTopicsClientButton.setOnClickListener(
73                 v -> {
74                     mResultTextView.setText("");
75                     for (String sdkName : SDK_NAMES) {
76                         mAdvertisingTopicsClient =
77                                 new AdvertisingTopicsClient.Builder()
78                                         .setContext(this)
79                                         .setSdkName(sdkName)
80                                         .setExecutor(CALLBACK_EXECUTOR)
81                                         .build();
82                         ListenableFuture<GetTopicsResponse> getTopicsResponseFuture =
83                                 mAdvertisingTopicsClient.getTopics();
84 
85                         Futures.addCallback(
86                                 getTopicsResponseFuture,
87                                 new FutureCallback<GetTopicsResponse>() {
88                                     @Override
89                                     public void onSuccess(GetTopicsResponse result) {
90                                         Log.d(TAG, "GetTopics for sdk " + sdkName + " succeeded!");
91                                         String topics = getTopics(result.getTopics());
92 
93                                         mHandler.post(new Runnable() {
94                                             @Override
95                                             public void run() {
96                                               mResultTextView.append(
97                                                 sdkName
98                                                         + "'s topics: "
99                                                         + NEWLINE
100                                                         + topics
101                                                         + NEWLINE);
102                                             }
103                                         });
104 
105                                         Log.d(
106                                                 TAG,
107                                                 sdkName
108                                                         + "'s topics: "
109                                                         + NEWLINE
110                                                         + topics
111                                                         + NEWLINE);
112                                     }
113 
114                                     @Override
115                                     public void onFailure(Throwable t) {
116                                         StringWriter sw = new StringWriter();
117                                         PrintWriter pw = new PrintWriter(sw);
118                                         t.printStackTrace(pw);
119 
120                                         Log.e(
121                                                 TAG,
122                                                 "Failed to getTopics for sdk "
123                                                         + sdkName
124                                                         + ": "
125                                                         + t.getMessage());
126 
127                                         mHandler.post(new Runnable() {
128                                             @Override
129                                             public void run() {
130                                               mResultTextView.append(
131                                                 "Failed to getTopics for sdk "
132                                                         + sdkName
133                                                         + ": "
134                                                         + t.toString()
135                                                         + NEWLINE);
136                                             }
137                                         });
138                                     }
139                                 },
140                                 directExecutor());
141                     }
142                 });
143     }
144 
getTopics(List<Topic> arr)145     private String getTopics(List<Topic> arr) {
146         StringBuilder sb = new StringBuilder();
147         int index = 1;
148         for (Topic topic : arr) {
149             sb.append(index++).append(". ").append(topic.toString()).append(NEWLINE);
150         }
151         return sb.toString();
152     }
153 }
154