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.content.Intent; 20 import android.graphics.Bitmap; 21 import android.graphics.BitmapFactory; 22 import android.os.Bundle; 23 import android.os.Handler; 24 import android.os.Looper; 25 import android.os.ParcelFileDescriptor; 26 import android.os.RemoteCallback; 27 import android.view.View; 28 import android.widget.Button; 29 import android.widget.ImageView; 30 import android.widget.TextView; 31 32 import androidx.annotation.Nullable; 33 34 import com.android.compatibility.common.util.ResultType; 35 import com.android.compatibility.common.util.ResultUnit; 36 import com.android.cts.verifier.PassFailButtons; 37 import com.android.cts.verifier.R; 38 39 import java.io.IOException; 40 import java.util.Objects; 41 42 /** 43 * Test of work profile screenshots. 44 */ 45 public class ScreenshotTestActivity extends PassFailButtons.Activity { 46 47 public static final String ACTION_SCREENSHOT_TEST = 48 "com.android.cts.verifier.managedprovisioning.SCREENSHOT_TEST"; 49 50 private static final String KEY_CAPTURE_ERROR_MESSAGE = "key_capture_error_message"; 51 52 private ParcelFileDescriptor mScreenshotFile; 53 private String mErrorMessage; 54 private RemoteCallback mCallback; 55 private boolean mStartedCapture; 56 57 @Override onCreate(@ullable Bundle savedInstanceState)58 protected void onCreate(@Nullable Bundle savedInstanceState) { 59 super.onCreate(savedInstanceState); 60 setContentView(R.layout.screenshot_test); 61 setInfoResources(R.string.provisioning_byod_screenshot, 62 R.string.provisioning_byod_screenshot_info, -1); 63 setPassFailButtonClickListeners(); 64 setResult(RESULT_CANCELED); 65 Button capture = requireViewById(R.id.provisioning_byod_screenshot_capture); 66 capture.setOnClickListener((v) -> { 67 mStartedCapture = true; 68 startCaptureActivity(); 69 }); 70 } 71 72 // Note: This is an intentional workaround for b/274767956 startCaptureActivity()73 private void startCaptureActivity() { 74 mCallback = new RemoteCallback((bundle) -> { 75 boolean ok = bundle.getBoolean(ScreenshotCaptureActivity.EXTRA_SUCCESS, false); 76 if (ok) { 77 mScreenshotFile = bundle.getParcelable( 78 ScreenshotCaptureActivity.EXTRA_FILE_DESCRIPTOR, 79 ParcelFileDescriptor.class); 80 // ...flow continues in onResume 81 } else { 82 mErrorMessage = bundle.getString(ScreenshotCaptureActivity.EXTRA_MESSAGE); 83 } 84 }, 85 new Handler(Looper.getMainLooper())); 86 87 Intent intent = new Intent(ScreenshotCaptureActivity.ACTION_CAPTURE_SCREENSHOT); 88 intent.putExtra(ScreenshotCaptureActivity.EXTRA_CALLBACK, mCallback); 89 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 90 startActivity(intent); 91 } 92 93 @Override requiresReportLog()94 public boolean requiresReportLog() { 95 return true; 96 } 97 98 @Override onResume()99 protected void onResume() { 100 super.onResume(); 101 if (mScreenshotFile != null) { 102 try (ParcelFileDescriptor fd = mScreenshotFile) { 103 mScreenshotFile = null; 104 setScreenshotPreview(fd); 105 } catch (IOException e) { 106 throw new RuntimeException(e); 107 } 108 } else if (mStartedCapture) { 109 getReportLog().addValue( 110 KEY_CAPTURE_ERROR_MESSAGE, 111 Objects.requireNonNullElse(mErrorMessage, 112 "Screenshot capture step was canceled"), 113 ResultType.WARNING, 114 ResultUnit.NONE); 115 reset(); 116 } 117 } 118 reset()119 private void reset() { 120 Button capture = requireViewById(R.id.provisioning_byod_screenshot_capture); 121 capture.setVisibility(View.VISIBLE); 122 123 TextView description = requireViewById(R.id.provisioning_byod_screenshot_description); 124 description.setText(R.string.provisioning_byod_screenshot_start); 125 126 View view = findViewById(R.id.provisioning_byod_screenshot_preview_label); 127 view.setVisibility(View.GONE); 128 129 ImageView image = requireViewById(R.id.provisioning_byod_screenshot_preview_image); 130 image.setVisibility(View.GONE); 131 image.setImageDrawable(null); 132 } 133 setScreenshotPreview(ParcelFileDescriptor fd)134 private void setScreenshotPreview(ParcelFileDescriptor fd) { 135 final Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor()); 136 137 Button capture = requireViewById(R.id.provisioning_byod_screenshot_capture); 138 capture.setVisibility(View.GONE); 139 140 TextView description = requireViewById(R.id.provisioning_byod_screenshot_description); 141 description.setText(R.string.provisioning_byod_screenshot_verify); 142 143 View view = findViewById(R.id.provisioning_byod_screenshot_preview_label); 144 view.setVisibility(View.VISIBLE); 145 146 ImageView image = requireViewById(R.id.provisioning_byod_screenshot_preview_image); 147 image.setVisibility(View.VISIBLE); 148 149 image.setImageBitmap(bitmap); 150 } 151 } 152 153