1package foo.bar.fill;
2
3import android.annotation.Nullable;
4import android.app.Activity;
5import android.app.assist.AssistStructure;
6import android.app.assist.AssistStructure.ViewNode;
7import android.content.Intent;
8import android.os.Bundle;
9import android.service.autofill.Dataset;
10import android.service.autofill.FillResponse;
11import android.view.View;
12import android.view.autofill.AutofillManager;
13import android.view.autofill.AutofillValue;
14import android.widget.Button;
15import android.widget.RemoteViews;
16
17public class AuthActivity extends Activity {
18    @Override
19    protected void onCreate(@Nullable Bundle savedInstanceState) {
20        super.onCreate(savedInstanceState);
21
22        setContentView(R.layout.activity_main);
23
24        AssistStructure structure = getIntent().getParcelableExtra(
25                AutofillManager.EXTRA_ASSIST_STRUCTURE);
26
27        ViewNode username = FillService.findUsername(structure);
28        ViewNode password = FillService.findPassword(structure);
29
30        final FillResponse response;
31        final Dataset dataset;
32
33        if (FillService.TEST_RESPONSE_AUTH) {
34            RemoteViews presentation1 = new RemoteViews(getPackageName(), R.layout.list_item);
35            presentation1.setTextViewText(R.id.text1,FillService.DATASET1_NAME);
36
37            RemoteViews presentation2 = new RemoteViews(getPackageName(), R.layout.list_item);
38            presentation2.setTextViewText(R.id.text1,FillService.DATASET2_NAME);
39
40            response = new FillResponse.Builder()
41                    .addDataset(new Dataset.Builder(presentation1)
42                            .setValue(username.getAutofillId(),
43                                    AutofillValue.forText(FillService.DATASET1_USERNAME))
44                            .setValue(password.getAutofillId(),
45                                    AutofillValue.forText(FillService.DATASET1_PASSWORD))
46                            .build())
47                    .addDataset(new Dataset.Builder(presentation2)
48                            .setValue(username.getAutofillId(),
49                                    AutofillValue.forText(FillService.DATASET2_USERNAME))
50                            .setValue(password.getAutofillId(),
51                                    AutofillValue.forText(FillService.DATASET2_PASSWORD))
52                            .build())
53                    .build();
54            dataset = null;
55        } else {
56            RemoteViews presentation = new RemoteViews(getPackageName(), R.layout.list_item);
57            presentation.setTextViewText(R.id.text1,FillService.DATASET2_NAME);
58
59            dataset = new Dataset.Builder(presentation)
60                    .setValue(username.getAutofillId(),
61                            AutofillValue.forText(FillService.DATASET5_USERNAME))
62                    .setValue(password.getAutofillId(),
63                            AutofillValue.forText(FillService.DATASET5_PASSWORD))
64                    .build();
65            response = null;
66        }
67
68        Button button = (Button) findViewById(R.id.confirm);
69        button.setOnClickListener((View v) -> {
70            Intent result = new Intent();
71            if (FillService.TEST_RESPONSE_AUTH) {
72                result.putExtra(AutofillManager.EXTRA_AUTHENTICATION_RESULT, response);
73            } else {
74                result.putExtra(AutofillManager.EXTRA_AUTHENTICATION_RESULT, dataset);
75            }
76            setResult(RESULT_OK, result);
77            finish();
78        });
79    }
80}
81