Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0ac02444 authored by kholoud mohamed's avatar kholoud mohamed Committed by Kholoud Mohamed
Browse files

Add testAPI to get adopted shell permissions.

Added a new testAPI in UiAutomation to get the currently adopted
shell permissions using adoptShellPermissionIdentity.

Test: atest UiAutomationTest
Bug: 183716601
Change-Id: I66a6c753480cecf971f7354daaddbb404f5c1453
parent db94cf9c
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -355,11 +355,13 @@ package android.app {
  public final class UiAutomation {
    method public void destroy();
    method @NonNull public android.os.ParcelFileDescriptor[] executeShellCommandRwe(@NonNull String);
    method @NonNull public java.util.Set<java.lang.String> getAdoptedShellPermissions();
    method @Deprecated public boolean grantRuntimePermission(String, String, android.os.UserHandle);
    method public boolean injectInputEvent(@NonNull android.view.InputEvent, boolean, boolean);
    method @Deprecated public boolean revokeRuntimePermission(String, String, android.os.UserHandle);
    method public void syncInputTransactions();
    method public void syncInputTransactions(boolean);
    field @NonNull public static final java.util.Set<java.lang.String> ALL_PERMISSIONS;
  }

  public class UiModeManager {
+6 −0
Original line number Diff line number Diff line
@@ -571,6 +571,12 @@ interface IActivityManager {
     */
    void stopDelegateShellPermissionIdentity();

    /**
     * Method for the shell UID to get currently adopted permissions for an active instrumentation.
     * An active instrumentation is one running and started from the shell.
     */
    List<String> getDelegatedShellPermissions();

    /** Returns a file descriptor that'll be closed when the system server process dies. */
    ParcelFileDescriptor getLifeMonitor();

+3 −0
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ import android.view.WindowContentFrameStats;
import android.view.WindowAnimationFrameStats;
import android.os.ParcelFileDescriptor;

import java.util.List;

/**
 * This interface contains privileged operations a shell program can perform
 * on behalf of an instrumentation that it runs. These operations require
@@ -54,4 +56,5 @@ interface IUiAutomationConnection {
    oneway void shutdown();
    void executeShellCommandWithStderr(String command, in ParcelFileDescriptor sink,
                in ParcelFileDescriptor source, in ParcelFileDescriptor stderrSink);
    List<String> getAdoptedShellPermissions();
}
+32 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@ import android.os.Process;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.UserHandle;
import android.util.ArraySet;
import android.util.Log;
import android.util.SparseArray;
import android.view.Display;
@@ -66,7 +67,9 @@ import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeoutException;

/**
@@ -160,6 +163,16 @@ public final class UiAutomation {
     */
    public static final int FLAG_DONT_USE_ACCESSIBILITY = 0x00000002;

    /**
     * Returned by {@link #getAdoptedShellPermissions} to indicate that all permissions have been
     * adopted using {@link #adoptShellPermissionIdentity}.
     *
     * @hide
     */
    @TestApi
    @NonNull
    public static final Set<String> ALL_PERMISSIONS = Set.of("_ALL_PERMISSIONS_");

    private final Object mLock = new Object();

    private final ArrayList<AccessibilityEvent> mEventQueue = new ArrayList<AccessibilityEvent>();
@@ -498,6 +511,25 @@ public final class UiAutomation {
        }
    }

    /**
     * Returns a list of adopted shell permissions using {@link #adoptShellPermissionIdentity},
     * returns and empty set if no permissions are adopted and {@link #ALL_PERMISSIONS} if all
     * permissions are adopted.
     *
     * @hide
     */
    @TestApi
    @NonNull
    public Set<String> getAdoptedShellPermissions() {
        try {
            final List<String> permissions = mUiAutomationConnection.getAdoptedShellPermissions();
            return permissions == null ? ALL_PERMISSIONS : new ArraySet<>(permissions);
        } catch (RemoteException re) {
            Log.e(LOG_TAG, "Error getting adopted shell permissions", re);
            return Collections.emptySet();
        }
    }

    /**
     * Performs a global action. Such an action can be performed at any moment
     * regardless of the current application or user location in that application.
+17 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

/**
 * This is a remote object that is passed from the shell to an instrumentation
@@ -340,6 +341,22 @@ public final class UiAutomationConnection extends IUiAutomationConnection.Stub {
        }
    }

    @Override
    @Nullable
    public List<String> getAdoptedShellPermissions() throws RemoteException {
        synchronized (mLock) {
            throwIfCalledByNotTrustedUidLocked();
            throwIfShutdownLocked();
            throwIfNotConnectedLocked();
        }
        final long identity = Binder.clearCallingIdentity();
        try {
            return mActivityManager.getDelegatedShellPermissions();
        } finally {
            Binder.restoreCallingIdentity(identity);
        }
    }

    public class Repeater implements Runnable {
        // Continuously read readFrom and write back to writeTo until EOF is encountered
        private final InputStream readFrom;
Loading