summaryrefslogtreecommitdiff
path: root/adservices/samples/topics/sampleapp1/java/com/example/adservices/samples/topics/sampleapp1/MainActivity.java
blob: 93ed3f3ef0812b0573d0199499ed4efb93539105 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.adservices.samples.topics.sampleapp1;

import static com.google.common.util.concurrent.MoreExecutors.directExecutor;

import android.adservices.clients.topics.AdvertisingTopicsClient;
import android.adservices.topics.GetTopicsResponse;
import android.adservices.topics.Topic;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import java.io.StringWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

/**
 * Android application activity for testing Topics API by providing a button in UI that initiate
 * user's interaction with Topics Manager in the background. Response from Topics API will be shown
 * in the app as text as well as toast message. In case anything goes wrong in this process, error
 * message will also be shown in toast to suggest the Exception encountered.
 */
public class MainActivity extends AppCompatActivity {

    private static final Executor CALLBACK_EXECUTOR = Executors.newCachedThreadPool();
    private static final String NEWLINE = "\n";
    private static final String TAG = "SampleApp1";
    private static final String RB_SETTING_APP_INTENT = "android.adservices.ui.SETTINGS";
    private static final List<String> SDK_NAMES = new ArrayList<>(Arrays.asList("SdkName1"));
    private Button mTopicsClientButton;
    private TextView mResultTextView;
    private Button mSettingsAppButton;
    private AdvertisingTopicsClient mAdvertisingTopicsClient;
    private Handler mHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mTopicsClientButton = findViewById(R.id.topics_client_button);
        mSettingsAppButton = findViewById(R.id.settings_app_launch_button);
        mResultTextView = findViewById(R.id.textView);
        registerGetTopicsButton();
        registerLauchSettingsAppButton();
        mHandler = new Handler();
    }

    private void registerGetTopicsButton() {
        mTopicsClientButton.setOnClickListener(
                v -> {
                    mResultTextView.setText("");
                    for (String sdkName : SDK_NAMES) {
                        mAdvertisingTopicsClient =
                                new AdvertisingTopicsClient.Builder()
                                        .setContext(this)
                                        .setSdkName(sdkName)
                                        .setExecutor(CALLBACK_EXECUTOR)
                                        .build();
                        ListenableFuture<GetTopicsResponse> getTopicsResponseFuture =
                                mAdvertisingTopicsClient.getTopics();

                        Futures.addCallback(
                                getTopicsResponseFuture,
                                new FutureCallback<GetTopicsResponse>() {
                                    @Override
                                    public void onSuccess(GetTopicsResponse result) {
                                        Log.d(TAG, "GetTopics for sdk " + sdkName + " succeeded!");
                                        String topics = getTopics(result.getTopics());
                                        mHandler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                              mResultTextView.append(
                                                sdkName
                                                        + "'s topics: "
                                                        + NEWLINE
                                                        + topics
                                                        + NEWLINE);
                                            }
                                        });
                                        Log.d(
                                                TAG,
                                                sdkName
                                                        + "'s topics: "
                                                        + NEWLINE
                                                        + topics
                                                        + NEWLINE);
                                    }

                                    @Override
                                    public void onFailure(Throwable t) {
                                        StringWriter sw = new StringWriter();
                                        PrintWriter pw = new PrintWriter(sw);
                                        t.printStackTrace(pw);
                                        Log.e(
                                                TAG,
                                                "Failed to getTopics for sdk "
                                                        + sdkName
                                                        + ": "
                                                        + t.getMessage());
                                        mHandler.post(new Runnable() {
                                            @Override
                                            public void run() {
                                              mResultTextView.append(
                                                "Failed to getTopics for sdk "
                                                        + sdkName
                                                        + ": "
                                                        + t.toString()
                                                        + NEWLINE);
                                            }
                                        });
                                    }
                                },
                                directExecutor());
                    }
                });
    }

    private String getTopics(List<Topic> arr) {
        StringBuilder sb = new StringBuilder();
        int index = 1;
        for (Topic topic : arr) {
            sb.append(index++).append(". ").append(topic.toString()).append(NEWLINE);
        }
        return sb.toString();
    }

    private void registerLauchSettingsAppButton() {
        mSettingsAppButton.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View view) {
                        Context context = getApplicationContext();
                        Intent activity2Intent = new Intent(RB_SETTING_APP_INTENT);
                        activity2Intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        context.startActivity(activity2Intent);
                    }
                });
    }
}