1 /*
2  * Copyright (C) 2023 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 
17 package com.android.cts.verifier.managedprovisioning;
18 
19 import android.app.Activity;
20 import android.content.Intent;
21 import android.net.Uri;
22 import android.os.Bundle;
23 import android.os.ParcelFileDescriptor;
24 import android.os.RemoteCallback;
25 import android.provider.DocumentsContract;
26 import android.provider.MediaStore;
27 import android.view.View;
28 import android.widget.Button;
29 import android.widget.TextView;
30 
31 import androidx.annotation.Nullable;
32 
33 import com.android.cts.verifier.R;
34 
35 /**
36  * Displays sample content and prompts the tester to take a screenshot.
37  * <p>
38  * Returns the captured image file descriptor via RemoteCallback.
39  */
40 public class ScreenshotCaptureActivity extends Activity {
41 
42     public static final String ACTION_CAPTURE_SCREENSHOT =
43             "com.android.cts.verifier.managedprovisioning.CAPTURE_SCREENSHOT";
44     public static final String EXTRA_CALLBACK = "remote_callback";
45     public static final String EXTRA_FILE_DESCRIPTOR = "file_descriptor";
46     public static final String EXTRA_SUCCESS = "success";
47     public static final String EXTRA_MESSAGE = "extra_message";
48 
49     private static final int REQUEST_OPEN_SCREEENSHOT = 100;
50 
51     private ScreenCaptureCallback mScreenCaptureCallback;
52     private RemoteCallback mCallback;
53 
54 
55     @Override
onCreate(@ullable Bundle savedInstanceState)56     protected void onCreate(@Nullable Bundle savedInstanceState) {
57         super.onCreate(savedInstanceState);
58         setContentView(R.layout.screenshot_capture);
59         Intent intent = getIntent();
60         mCallback = intent.getParcelableExtra(EXTRA_CALLBACK, RemoteCallback.class);
61 
62         Button b = requireViewById(R.id.provisioning_byod_screenshot_capture_button);
63         b.setVisibility(View.VISIBLE);
64         b.setOnClickListener((v) -> {
65             Bundle result = new Bundle();
66             result.putString(EXTRA_MESSAGE, "Screenshot capture was canceled");
67             mCallback.sendResult(result);
68             reset();
69             finish();
70         });
71 
72         mScreenCaptureCallback = () -> {
73             TextView info = requireViewById(R.id.provisioning_byod_screenshot_capture_text);
74             info.setText(R.string.provisioning_byod_screenshot_select_message);
75             b.setText(R.string.provisioning_byod_screenshot_capture_open);
76             b.setVisibility(View.VISIBLE);
77             b.setOnClickListener((v) -> openDocument());
78         };
79     }
80 
81     @Override
onResume()82     protected void onResume() {
83         super.onResume();
84         registerScreenCaptureCallback(getMainExecutor(), mScreenCaptureCallback);
85     }
86 
openDocument()87     private void openDocument() {
88         Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
89         intent.addCategory(Intent.CATEGORY_OPENABLE);
90         intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI,
91                 MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
92         intent.setType("image/*");
93         startActivityForResult(intent, REQUEST_OPEN_SCREEENSHOT);
94     }
95 
96     @Override
onActivityResult(int requestCode, int resultCode, Intent data)97     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
98         Button b = requireViewById(R.id.provisioning_byod_screenshot_capture_button);
99         b.setVisibility(View.GONE);
100 
101         Bundle result = new Bundle();
102         if (resultCode == RESULT_CANCELED) {
103             result.putString(EXTRA_MESSAGE, "Screenshot selection was canceled."
104                     + " Saved to wrong profile?");
105         } else {
106             Uri uri = data.getData();
107             try {
108                 ParcelFileDescriptor fd = getContentResolver().openFileDescriptor(uri, "r");
109                 result.putBoolean(EXTRA_SUCCESS, true);
110                 result.putParcelable(EXTRA_FILE_DESCRIPTOR, fd);
111             } catch (Exception e) {
112                 result.putString(EXTRA_MESSAGE, "Failed to read screenshot image: " + e.toString());
113             }
114         }
115         mCallback.sendResult(result);
116         reset();
117         finish();
118     }
119 
reset()120     private void reset() {
121         TextView info = requireViewById(R.id.provisioning_byod_screenshot_capture_text);
122         info.setText(R.string.provisioning_byod_screenshot_capture_title);
123 
124         Button b = requireViewById(R.id.provisioning_byod_screenshot_capture_button);
125         b.setVisibility(View.VISIBLE);
126         b.setText(R.string.provisioning_byod_screenshot_capture_cancel);
127     }
128 
129     @Override
onPause()130     protected void onPause() {
131         super.onPause();
132         unregisterScreenCaptureCallback(mScreenCaptureCallback);
133     }
134 }
135