1 /* 2 * Copyright (C) 2016 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.shell; 18 19 import android.graphics.Bitmap; 20 import android.os.IBinder; 21 import android.util.Log; 22 import android.view.SurfaceControl; 23 24 /** 25 * Helper class used to take screenshots. 26 * 27 * TODO: logic below was copied and pasted from UiAutomation; it should be refactored into a common 28 * component that could be used by both (Shell and UiAutomation). 29 */ 30 final class Screenshooter { 31 32 private static final String TAG = "Screenshooter"; 33 34 /** 35 * Takes a screenshot. 36 * 37 * @return The screenshot bitmap on success, null otherwise. 38 */ takeScreenshot()39 static Bitmap takeScreenshot() { 40 Log.d(TAG, "Taking fullscreen screenshot"); 41 // Take the screenshot 42 final IBinder displayToken = SurfaceControl.getInternalDisplayToken(); 43 final SurfaceControl.DisplayCaptureArgs captureArgs = 44 new SurfaceControl.DisplayCaptureArgs.Builder(displayToken) 45 .build(); 46 final SurfaceControl.ScreenshotHardwareBuffer screenshotBuffer = 47 SurfaceControl.captureDisplay(captureArgs); 48 final Bitmap screenShot = screenshotBuffer == null ? null : screenshotBuffer.asBitmap(); 49 if (screenShot == null) { 50 Log.e(TAG, "Failed to take fullscreen screenshot"); 51 return null; 52 } 53 54 // Optimization 55 screenShot.setHasAlpha(false); 56 57 return screenShot; 58 } 59 } 60