public class

BasicScreenCaptureProcessor

extends java.lang.Object

implements ScreenCaptureProcessor

 java.lang.Object

↳androidx.test.runner.screenshot.BasicScreenCaptureProcessor

Gradle dependencies

compile group: 'androidx.test', name: 'runner', version: '1.5.0-alpha03'

  • groupId: androidx.test
  • artifactId: runner
  • version: 1.5.0-alpha03

Artifact androidx.test:runner:1.5.0-alpha03 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test:runner com.android.support.test:runner

Androidx class mapping:

androidx.test.runner.screenshot.BasicScreenCaptureProcessor android.support.test.runner.screenshot.BasicScreenCaptureProcessor

Overview

A basic ScreenCaptureProcessor for processing a ScreenCapture.

This will perform basic processing on the given ScreenCapture such as saving to the public Pictures directory, given by android.os.Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES), with a simple name that includes a few characteristics about the device it was saved on followed by a UUID.

This API is currently in beta.

Summary

Fields
protected java.lang.StringmDefaultFilenamePrefix

protected java.io.FilemDefaultScreenshotPath

protected java.lang.StringmFileNameDelimiter

protected java.lang.StringmTag

Constructors
publicBasicScreenCaptureProcessor()

Methods
protected java.lang.StringgetDefaultFilename()

Returns the default filename for this class suffixed with a UUID.

protected java.lang.StringgetFilename(java.lang.String prefix)

Returns the filename created from the given prifix and suffixed with a UUID.

public java.lang.Stringprocess(ScreenCapture capture)

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Fields

protected java.lang.String mTag

protected java.lang.String mFileNameDelimiter

protected java.lang.String mDefaultFilenamePrefix

protected java.io.File mDefaultScreenshotPath

Constructors

public BasicScreenCaptureProcessor()

Methods

public java.lang.String process(ScreenCapture capture)

protected java.lang.String getDefaultFilename()

Returns the default filename for this class suffixed with a UUID.

protected java.lang.String getFilename(java.lang.String prefix)

Returns the filename created from the given prifix and suffixed with a UUID.

Source

/*
 * Copyright (C) 2016 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 androidx.test.runner.screenshot;

import static android.os.Environment.DIRECTORY_PICTURES;
import static android.os.Environment.getExternalStoragePublicDirectory;

import android.os.Build;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import androidx.test.annotation.ExperimentalTestApi;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * A basic {@link ScreenCaptureProcessor} for processing a {@link ScreenCapture}.
 *
 * <p>This will perform basic processing on the given {@link ScreenCapture} such as saving to the
 * public Pictures directory, given by
 * android.os.Environment.getExternalStoragePublicDirectory(DIRECTORY_PICTURES), with a simple name
 * that includes a few characteristics about the device it was saved on followed by a UUID.
 *
 * <p><b>This API is currently in beta.</b>
 */
@ExperimentalTestApi
public class BasicScreenCaptureProcessor implements ScreenCaptureProcessor {
  private static int sAndroidRuntimeVersion = Build.VERSION.SDK_INT;
  private static String sAndroidDeviceName = Build.DEVICE;

  protected String mTag;
  protected String mFileNameDelimiter;
  protected String mDefaultFilenamePrefix;
  protected File mDefaultScreenshotPath;

  public BasicScreenCaptureProcessor() {
    this(new File(getExternalStoragePublicDirectory(DIRECTORY_PICTURES), "screenshots"));
  }

  BasicScreenCaptureProcessor(File defaultScreenshotPath) {
    mTag = "BasicScreenCaptureProcessor";
    mFileNameDelimiter = "-";
    mDefaultFilenamePrefix = "screenshot";
    mDefaultScreenshotPath = defaultScreenshotPath;
  }

  @Override
  public String process(ScreenCapture capture) throws IOException {
    String filename =
        capture.getName() == null ? getDefaultFilename() : getFilename(capture.getName());
    filename += "." + capture.getFormat().toString().toLowerCase();
    File imageFolder = mDefaultScreenshotPath;
    imageFolder.mkdirs();
    if (!imageFolder.isDirectory() && !imageFolder.canWrite()) {
      throw new IOException(
          String.format(
              "The directory %s does not exist and could not be created or is not " + "writable.",
              imageFolder));
    }

    File imageFile = new File(imageFolder, filename);
    BufferedOutputStream out = null;
    try {
      out = new BufferedOutputStream(new FileOutputStream(imageFile));
      capture.getBitmap().compress(capture.getFormat(), 100, out);
      out.flush();
    } finally {
      try {
        if (out != null) {
          out.close();
        }
      } catch (IOException e) {
        Log.e(mTag, "Could not close output steam.", e);
      }
    }
    return filename;
  }

  /** Returns the default filename for this class suffixed with a UUID. */
  protected String getDefaultFilename() {
    return getFilename(
        mDefaultFilenamePrefix
            + mFileNameDelimiter
            + sAndroidDeviceName
            + mFileNameDelimiter
            + sAndroidRuntimeVersion);
  }

  /** Returns the filename created from the given prifix and suffixed with a UUID. */
  protected String getFilename(String prefix) {
    return prefix + mFileNameDelimiter + UUID.randomUUID();
  }

  @VisibleForTesting
  static void setAndroidDeviceName(String deviceName) {
    sAndroidDeviceName = deviceName;
  }

  @VisibleForTesting
  static void setAndroidRuntimeVersion(int sdkInt) {
    sAndroidRuntimeVersion = sdkInt;
  }
}