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

Commit 857efd19 authored by Anh Pham's avatar Anh Pham Committed by Android (Google) Code Review
Browse files

Merge changes from topic "sensor_sampling_rate_throttling-sc-dev" into sc-dev

* changes:
  Throw SecurityException regarding sampling rates.
  Add support for retrieving the Debuggable flag.
  Add HIGH_SAMPLING_RATE_SENSORS permission.
parents 37dbb4e5 97f66fd2
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -88,6 +88,7 @@ package android {
    field @Deprecated public static final String GET_TASKS = "android.permission.GET_TASKS";
    field public static final String GLOBAL_SEARCH = "android.permission.GLOBAL_SEARCH";
    field public static final String HIDE_OVERLAY_WINDOWS = "android.permission.HIDE_OVERLAY_WINDOWS";
    field public static final String HIGH_SAMPLING_RATE_SENSORS = "android.permission.HIGH_SAMPLING_RATE_SENSORS";
    field public static final String INSTALL_LOCATION_PROVIDER = "android.permission.INSTALL_LOCATION_PROVIDER";
    field public static final String INSTALL_PACKAGES = "android.permission.INSTALL_PACKAGES";
    field public static final String INSTALL_SHORTCUT = "com.android.launcher.permission.INSTALL_SHORTCUT";
+48 −2
Original line number Diff line number Diff line
@@ -16,11 +16,18 @@

package android.hardware;

import static android.content.pm.PackageManager.PERMISSION_GRANTED;

import android.compat.Compatibility;
import android.compat.annotation.ChangeId;
import android.compat.annotation.EnabledAfter;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
@@ -54,6 +61,19 @@ public class SystemSensorManager extends SensorManager {
    private static final boolean DEBUG_DYNAMIC_SENSOR = true;
    private static final int MIN_DIRECT_CHANNEL_BUFFER_SIZE = 104;
    private static final int MAX_LISTENER_COUNT = 128;
    private static final int CAPPED_SAMPLING_PERIOD_US = 5000;
    private static final int CAPPED_SAMPLING_RATE_LEVEL = SensorDirectChannel.RATE_NORMAL;

    private static final String HIGH_SAMPLING_RATE_SENSORS_PERMISSION =
                                        "android.permisison.HIGH_SAMPLING_RATE_SENSORS";
    /**
     * For apps targeting S and above, a SecurityException is thrown when they do not have
     * HIGH_SAMPLING_RATE_SENSORS permission, run in debug mode, and request sampling rates that
     * are faster than 200 Hz.
     */
    @ChangeId
    @EnabledAfter(targetSdkVersion = Build.VERSION_CODES.R)
    static final long CHANGE_ID_SAMPLING_RATE_SENSORS_PERMISSION = 136069189L;

    private static native void nativeClassInit();
    private static native long nativeCreate(String opPackageName);
@@ -98,6 +118,8 @@ public class SystemSensorManager extends SensorManager {
    // Looper associated with the context in which this instance was created.
    private final Looper mMainLooper;
    private final int mTargetSdkLevel;
    private final boolean mIsPackageDebuggable;
    private final boolean mHasHighSamplingRateSensorsPermission;
    private final Context mContext;
    private final long mNativeInstance;

@@ -111,9 +133,16 @@ public class SystemSensorManager extends SensorManager {
        }

        mMainLooper = mainLooper;
        mTargetSdkLevel = context.getApplicationInfo().targetSdkVersion;
        ApplicationInfo appInfo = context.getApplicationInfo();
        mTargetSdkLevel = appInfo.targetSdkVersion;
        mContext = context;
        mNativeInstance = nativeCreate(context.getOpPackageName());
        mIsPackageDebuggable = (0 != (appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE));
        PackageManager packageManager = context.getPackageManager();
        mHasHighSamplingRateSensorsPermission =
                (PERMISSION_GRANTED == packageManager.checkPermission(
                        HIGH_SAMPLING_RATE_SENSORS_PERMISSION,
                        appInfo.packageName));

        // initialize the sensor list
        for (int index = 0;; ++index) {
@@ -542,10 +571,18 @@ public class SystemSensorManager extends SensorManager {
        }

        int sensorHandle = (sensor == null) ? -1 : sensor.getHandle();
        if (Compatibility.isChangeEnabled(CHANGE_ID_SAMPLING_RATE_SENSORS_PERMISSION)
                && rate > CAPPED_SAMPLING_RATE_LEVEL
                && mIsPackageDebuggable
                && !mHasHighSamplingRateSensorsPermission) {
            Compatibility.reportChange(CHANGE_ID_SAMPLING_RATE_SENSORS_PERMISSION);
            throw new SecurityException("To use the sampling rate level " + rate
                    + ", app needs to declare the normal permission"
                    + " HIGH_SAMPLING_RATE_SENSORS.");
        }

        int ret = nativeConfigDirectChannel(
                mNativeInstance, channel.getNativeHandle(), sensorHandle, rate);

        if (rate == SensorDirectChannel.RATE_STOP) {
            return (ret == 0) ? 1 : 0;
        } else {
@@ -745,6 +782,15 @@ public class SystemSensorManager extends SensorManager {
                Sensor sensor, int rateUs, int maxBatchReportLatencyUs) {
            if (mNativeSensorEventQueue == 0) throw new NullPointerException();
            if (sensor == null) throw new NullPointerException();
            if (Compatibility.isChangeEnabled(CHANGE_ID_SAMPLING_RATE_SENSORS_PERMISSION)
                    && rateUs < CAPPED_SAMPLING_PERIOD_US
                    && mManager.mIsPackageDebuggable
                    && !mManager.mHasHighSamplingRateSensorsPermission) {
                Compatibility.reportChange(CHANGE_ID_SAMPLING_RATE_SENSORS_PERMISSION);
                throw new SecurityException("To use the sampling rate of " + rateUs
                        + " microseconds, app needs to declare the normal permission"
                        + " HIGH_SAMPLING_RATE_SENSORS.");
            }
            return nativeEnableSensor(mNativeSensorEventQueue, sensor.getHandle(), rateUs,
                    maxBatchReportLatencyUs);
        }
+9 −0
Original line number Diff line number Diff line
@@ -1403,6 +1403,15 @@
        android:description="@string/permgroupdesc_sensors"
        android:priority="800" />

    <!-- Allows an app to access sensor data with a sampling rate greater than 200 Hz.
        <p>Protection level: normal
    -->
    <permission android:name="android.permission.HIGH_SAMPLING_RATE_SENSORS"
                android:permissionGroup="android.permission-group.SENSORS"
                android:label="@string/permlab_highSamplingRateSensors"
                android:description="@string/permdesc_highSamplingRateSensors"
                android:protectionLevel="normal" />

    <!-- Allows an application to access data from sensors that the user uses to
         measure what is happening inside their body, such as heart rate.
         <p>Protection level: dangerous -->
+5 −0
Original line number Diff line number Diff line
@@ -1830,6 +1830,11 @@
    <!-- Description of an application permission, listed so the user can choose whether they want to allow the application to do this. -->
    <string name="permdesc_startViewPermissionUsage">Allows the holder to start the permission usage for an app. Should never be needed for normal apps.</string>

    <!-- Title of an application permission, listed so the user can choose whether they want to allow the application to do this. [CHAR_LIMIT=NONE] -->
    <string name="permlab_highSamplingRateSensors">access sensor data at a high sampling rate</string>
    <!-- Description of an application permission, listed so the user can choose whether they want to allow the application to do this.[CHAR_LIMIT=NONE] -->
    <string name="permdesc_highSamplingRateSensors">Allows the app to sample sensor data at a rate greater than 200 Hz</string>

    <!-- Policy administration -->

    <!-- Title of policy access to limiting the user's password choices -->
+11 −0
Original line number Diff line number Diff line
@@ -25924,6 +25924,17 @@ public class PackageManagerService extends IPackageManager.Stub
            throw new RemoteException("Couldn't get targetSdkVersion for package " + packageName);
        }
        @Override
        public boolean isPackageDebuggable(String packageName) throws RemoteException {
            int callingUser = UserHandle.getCallingUserId();
            ApplicationInfo appInfo = getApplicationInfo(packageName, 0, callingUser);
            if (appInfo != null) {
                return (0 != (appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE));
            }
            throw new RemoteException("Couldn't get debug flag for package " + packageName);
        }
        @Override
        public boolean[] isAudioPlaybackCaptureAllowed(String[] packageNames)
                throws RemoteException {