public class

UiAutomationPermissionGranter

extends java.lang.Object

implements PermissionGranter

 java.lang.Object

↳androidx.test.runner.permission.UiAutomationPermissionGranter

Gradle dependencies

compile group: 'androidx.test', name: 'runner', version: '1.6.2'

  • groupId: androidx.test
  • artifactId: runner
  • version: 1.6.2

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

Androidx artifact mapping:

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

Overview

A PermissionGranter that uses android.app.UiAutomation

Summary

Constructors
publicUiAutomationPermissionGranter()

Methods
public voidaddPermissions(java.lang.String permissions[])

public voidrequestPermissions()

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

Constructors

public UiAutomationPermissionGranter()

Methods

public void addPermissions(java.lang.String permissions[])

public void requestPermissions()

Source

package androidx.test.runner.permission;

import android.app.UiAutomation;
import android.content.Context;
import android.content.pm.PackageManager;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.annotation.RestrictTo.Scope;
import androidx.test.internal.platform.content.PermissionGranter;
import androidx.test.platform.app.InstrumentationRegistry;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;

/**
 * A {@link PermissionGranter} that uses {@link
 * android.app.UiAutomation#grantRuntimePermission(String, String)}
 *
 * @hide
 */
@RestrictTo(Scope.LIBRARY_GROUP)
@RequiresApi(28)
public class UiAutomationPermissionGranter implements PermissionGranter {
  private static final String TAG = "UiAutomationPermGranter";
  private final Set<String> permissionsToGrant = new LinkedHashSet<>();

  @Override
  public void addPermissions(@NonNull String... permissions) {
    Collections.addAll(permissionsToGrant, permissions);
  }

  @Override
  public void requestPermissions() {
    Context targetContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
    UiAutomation uiAutomation = InstrumentationRegistry.getInstrumentation().getUiAutomation();
    for (String permission : permissionsToGrant) {
      if (!isPermissionGranted(targetContext, permission)) {
        Log.i(
            TAG,
            String.format(
                "Attempting to grant %s to %s", permission, targetContext.getPackageName()));
        uiAutomation.grantRuntimePermission(targetContext.getPackageName(), permission);
      } else {
        Log.i(TAG, "Permission: " + permission + " is already granted!");
      }
    }
  }

  private boolean isPermissionGranted(Context targetContext, String permission) {
    return targetContext.checkCallingOrSelfPermission(permission)
        == PackageManager.PERMISSION_GRANTED;
  }
}