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

Commit b81d9784 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes from topic 'sensor_direct_report'

* changes:
  Sensor event direct report mode implementation
  Sensor event direct report mode support API
parents 6bb61a7d fa2672b7
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -14208,6 +14208,7 @@ package android.hardware {
  public final class Sensor {
    method public int getFifoMaxEventCount();
    method public int getFifoReservedEventCount();
    method public int getHighestDirectReportRateLevel();
    method public int getId();
    method public int getMaxDelay();
    method public float getMaximumRange();
@@ -14221,6 +14222,7 @@ package android.hardware {
    method public java.lang.String getVendor();
    method public int getVersion();
    method public boolean isAdditionalInfoSupported();
    method public boolean isDirectChannelTypeSupported(int);
    method public boolean isDynamicSensor();
    method public boolean isWakeUpSensor();
    field public static final int REPORTING_MODE_CONTINUOUS = 0; // 0x0
@@ -14298,6 +14300,17 @@ package android.hardware {
    field public final int type;
  }
  public final class SensorDirectChannel implements java.lang.AutoCloseable {
    method public void close();
    method public boolean isValid();
    field public static final int RATE_FAST = 2; // 0x2
    field public static final int RATE_NORMAL = 1; // 0x1
    field public static final int RATE_STOP = 0; // 0x0
    field public static final int RATE_VERY_FAST = 3; // 0x3
    field public static final int TYPE_ASHMEM = 1; // 0x1
    field public static final int TYPE_HARDWARE_BUFFER = 2; // 0x2
  }
  public class SensorEvent {
    field public int accuracy;
    field public android.hardware.Sensor sensor;
@@ -14329,6 +14342,9 @@ package android.hardware {
  public abstract class SensorManager {
    method public boolean cancelTriggerSensor(android.hardware.TriggerEventListener, android.hardware.Sensor);
    method public int configureDirectChannel(android.hardware.SensorDirectChannel, android.hardware.Sensor, int);
    method public android.hardware.SensorDirectChannel createDirectChannel(android.os.MemoryFile);
    method public android.hardware.SensorDirectChannel createDirectChannel(android.hardware.HardwareBuffer);
    method public boolean flush(android.hardware.SensorEventListener);
    method public static float getAltitude(float, float);
    method public static void getAngleChange(float[], float[], float[]);
+16 −0
Original line number Diff line number Diff line
@@ -14766,6 +14766,7 @@ package android.hardware {
  public final class Sensor {
    method public int getFifoMaxEventCount();
    method public int getFifoReservedEventCount();
    method public int getHighestDirectReportRateLevel();
    method public int getId();
    method public int getMaxDelay();
    method public float getMaximumRange();
@@ -14781,6 +14782,7 @@ package android.hardware {
    method public int getVersion();
    method public boolean isAdditionalInfoSupported();
    method public boolean isDataInjectionSupported();
    method public boolean isDirectChannelTypeSupported(int);
    method public boolean isDynamicSensor();
    method public boolean isWakeUpSensor();
    field public static final int REPORTING_MODE_CONTINUOUS = 0; // 0x0
@@ -14862,6 +14864,17 @@ package android.hardware {
    field public final int type;
  }
  public final class SensorDirectChannel implements java.lang.AutoCloseable {
    method public void close();
    method public boolean isValid();
    field public static final int RATE_FAST = 2; // 0x2
    field public static final int RATE_NORMAL = 1; // 0x1
    field public static final int RATE_STOP = 0; // 0x0
    field public static final int RATE_VERY_FAST = 3; // 0x3
    field public static final int TYPE_ASHMEM = 1; // 0x1
    field public static final int TYPE_HARDWARE_BUFFER = 2; // 0x2
  }
  public class SensorEvent {
    field public int accuracy;
    field public android.hardware.Sensor sensor;
@@ -14893,6 +14906,9 @@ package android.hardware {
  public abstract class SensorManager {
    method public boolean cancelTriggerSensor(android.hardware.TriggerEventListener, android.hardware.Sensor);
    method public int configureDirectChannel(android.hardware.SensorDirectChannel, android.hardware.Sensor, int);
    method public android.hardware.SensorDirectChannel createDirectChannel(android.os.MemoryFile);
    method public android.hardware.SensorDirectChannel createDirectChannel(android.hardware.HardwareBuffer);
    method public boolean flush(android.hardware.SensorEventListener);
    method public static float getAltitude(float, float);
    method public static void getAngleChange(float[], float[], float[]);
+16 −0
Original line number Diff line number Diff line
@@ -14240,6 +14240,7 @@ package android.hardware {
  public final class Sensor {
    method public int getFifoMaxEventCount();
    method public int getFifoReservedEventCount();
    method public int getHighestDirectReportRateLevel();
    method public int getId();
    method public int getMaxDelay();
    method public float getMaximumRange();
@@ -14253,6 +14254,7 @@ package android.hardware {
    method public java.lang.String getVendor();
    method public int getVersion();
    method public boolean isAdditionalInfoSupported();
    method public boolean isDirectChannelTypeSupported(int);
    method public boolean isDynamicSensor();
    method public boolean isWakeUpSensor();
    field public static final int REPORTING_MODE_CONTINUOUS = 0; // 0x0
@@ -14330,6 +14332,17 @@ package android.hardware {
    field public final int type;
  }
  public final class SensorDirectChannel implements java.lang.AutoCloseable {
    method public void close();
    method public boolean isValid();
    field public static final int RATE_FAST = 2; // 0x2
    field public static final int RATE_NORMAL = 1; // 0x1
    field public static final int RATE_STOP = 0; // 0x0
    field public static final int RATE_VERY_FAST = 3; // 0x3
    field public static final int TYPE_ASHMEM = 1; // 0x1
    field public static final int TYPE_HARDWARE_BUFFER = 2; // 0x2
  }
  public class SensorEvent {
    field public int accuracy;
    field public android.hardware.Sensor sensor;
@@ -14361,6 +14374,9 @@ package android.hardware {
  public abstract class SensorManager {
    method public boolean cancelTriggerSensor(android.hardware.TriggerEventListener, android.hardware.Sensor);
    method public int configureDirectChannel(android.hardware.SensorDirectChannel, android.hardware.Sensor, int);
    method public android.hardware.SensorDirectChannel createDirectChannel(android.os.MemoryFile);
    method public android.hardware.SensorDirectChannel createDirectChannel(android.hardware.HardwareBuffer);
    method public boolean flush(android.hardware.SensorEventListener);
    method public static float getAltitude(float, float);
    method public static void getAngleChange(float[], float[], float[]);
+46 −2
Original line number Diff line number Diff line
@@ -729,14 +729,22 @@ public final class Sensor {
    private static final int DATA_INJECTION_MASK = 0x10;
    private static final int DATA_INJECTION_SHIFT = 4;

    // MASK for dynamic sensor (sensor that added during runtime), bit 6.
    // MASK for dynamic sensor (sensor that added during runtime), bit 5.
    private static final int DYNAMIC_SENSOR_MASK = 0x20;
    private static final int DYNAMIC_SENSOR_SHIFT = 5;

    // MASK for indication bit of sensor additional information support (bit 7).
    // MASK for indication bit of sensor additional information support, bit 6.
    private static final int ADDITIONAL_INFO_MASK = 0x40;
    private static final int ADDITIONAL_INFO_SHIFT = 6;

    // Mask for direct mode highest rate level, bit 7, 8, 9.
    private static final int DIRECT_REPORT_MASK = 0x380;
    private static final int DIRECT_REPORT_SHIFT = 7;

    // Mask for supported direct channel, bit 10, 11
    private static final int DIRECT_CHANNEL_MASK = 0xC00;
    private static final int DIRECT_CHANNEL_SHIFT = 10;

    // TODO(): The following arrays are fragile and error-prone. This needs to be refactored.

    // Note: This needs to be updated, whenever a new sensor is added.
@@ -796,6 +804,42 @@ public final class Sensor {
        return ((mFlags & REPORTING_MODE_MASK) >> REPORTING_MODE_SHIFT);
    }

    /**
     * Get the highest supported direct report mode rate level of the sensor.
     *
     * @return Highest direct report rate level of this sensor. If the sensor does not support
     * direct report mode, this returns {@link SensorDirectChannel#RATE_STOP}.
     * @see SensorDirectChannel#RATE_STOP
     * @see SensorDirectChannel#RATE_NORMAL
     * @see SensorDirectChannel#RATE_FAST
     * @see SensorDirectChannel#RATE_VERY_FAST
     */
    @SensorDirectChannel.RateLevel
    public int getHighestDirectReportRateLevel() {
        int rateLevel = ((mFlags & DIRECT_REPORT_MASK) >> DIRECT_REPORT_SHIFT);
        return rateLevel <= SensorDirectChannel.RATE_VERY_FAST
                ? rateLevel : SensorDirectChannel.RATE_VERY_FAST;
    }

    /**
     * Test if sensor support direct channel backed by a specific type of shared memory.
     *
     * @param sharedMemType type of shared memory used by direct channel.
     * @return <code>true</code> if the shared memory type is supported.
     * @see SensorDirectChannel#TYPE_ASHMEM
     * @see SensorDirectChannel#TYPE_HARDWARE_BUFFER
     */
    public boolean isDirectChannelTypeSupported(@SensorDirectChannel.MemoryType int sharedMemType) {
        switch (sharedMemType) {
            case SensorDirectChannel.TYPE_ASHMEM:
                return (mFlags & (1 << DIRECT_CHANNEL_SHIFT)) > 0;
            case SensorDirectChannel.TYPE_HARDWARE_BUFFER:
                return (mFlags & (1 << DIRECT_CHANNEL_SHIFT + 1)) > 0;
            default:
                return false;
        }
    }

    static int getMaxLengthValuesArray(Sensor sensor, int sdkLevel) {
        // RotationVector length has changed to 3 to 5 for API level 18
        // Set it to 3 for backward compatibility.
+168 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 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 android.hardware;

import android.annotation.IntDef;
import android.os.MemoryFile;

import dalvik.system.CloseGuard;

import java.io.IOException;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * Class representing a sensor direct channel. Use {@link
 * SensorManager#createDirectChannel(android.os.MemoryFile)} to obtain object.
 */
public final class SensorDirectChannel implements AutoCloseable {

    // shared memory types

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(flag = true, value = {TYPE_ASHMEM, TYPE_HARDWARE_BUFFER})
    public @interface MemoryType {};
    /**
     * Shared memory type ashmem, wrapped in MemoryFile object.
     *
     * @see SensorManager#createDirectChannel(MemoryFile)
     */
    public static final int TYPE_ASHMEM = 1;

    /**
     * Shared memory type wrapped by HardwareBuffer object.
     *
     * @see SensorManager#createDirectChannel(HardwareBuffer)
     */
    public static final int TYPE_HARDWARE_BUFFER = 2;

    // sensor rate levels

    /** @hide */
    @Retention(RetentionPolicy.SOURCE)
    @IntDef(flag = true, value = {RATE_STOP, RATE_NORMAL, RATE_FAST, RATE_VERY_FAST})
    public @interface RateLevel {};

    /**
     * Sensor stopped (no event output).
     *
     * @see SensorManager#configureDirectChannel(SensorDirectChannel, Sensor, int)
     */
    public static final int RATE_STOP = 0;
    /**
     * Sensor operates at nominal rate of 50Hz.
     *
     * The actual rate is expected to be between 55% to 220% of nominal rate, thus between 27.5Hz to
     * 110Hz.
     *
     * @see SensorManager#configureDirectChannel(SensorDirectChannel, Sensor, int)
     */
    public static final int RATE_NORMAL = 1; //50Hz
    /**
     * Sensor operates at nominal rate of 200Hz.
     *
     * The actual rate is expected to be between 55% to 220% of nominal rate, thus between 110Hz to
     * 440Hz.
     *
     * @see SensorManager#configureDirectChannel(SensorDirectChannel, Sensor, int)
     */
    public static final int RATE_FAST = 2; // ~200Hz
    /**
     * Sensor operates at nominal rate of 800Hz.
     *
     * The actual rate is expected to be between 55% to 220% of nominal rate, thus between 440Hz to
     * 1760Hz.
     *
     * @see SensorManager#configureDirectChannel(SensorDirectChannel, Sensor, int)
     */
    public static final int RATE_VERY_FAST = 3; // ~800Hz

    /**
     * Determine if a channel is still valid. A channel is invalidated after {@link #close()} is
     * called.
     *
     * @return <code>true</code> if channel is valid.
     */
    public boolean isValid() {
        return !mClosed.get();
    }

    /**
     * Close sensor direct channel.
     *
     * Stop all active sensor in the channel and free sensor system resource related to channel.
     * Shared memory used for creating the direct channel need to be closed or freed separately.
     *
     * @see SensorManager#createDirectChannel(MemoryFile)
     * @see SensorManager#createDirectChannel(HardwareBuffer)
     */
    @Override
    public void close() {
        mCloseGuard.close();
        if (mClosed.compareAndSet(false, true)) {
            // actual close action
            mManager.destroyDirectChannel(this);
        }
    }

    /** @hide */
    SensorDirectChannel(SensorManager manager, int id, int type, long size) {
        mManager = manager;
        mNativeHandle = id;
        mType = type;
        mSize = size;
        mCloseGuard.open("SensorDirectChannel");
    }

    /** @hide */
    int getNativeHandle() {
        return mNativeHandle;
    }

    /**
     * This function encode handle information in {@link android.os.Memory} into a long array to be
     * passed down to native methods.
     *
     * @hide */
    static long[] encodeData(MemoryFile ashmem) {
        int fd;
        try {
            fd = ashmem.getFileDescriptor().getInt$();
        } catch (IOException e) {
            fd = -1;
        }
        return new long[] { 1 /*numFds*/, 0 /*numInts*/, fd };
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            mCloseGuard.warnIfOpen();
            close();
        } finally {
            super.finalize();
        }
    }

    private final AtomicBoolean mClosed = new AtomicBoolean();
    private final CloseGuard mCloseGuard = CloseGuard.get();
    private final SensorManager mManager;
    private final int mNativeHandle;
    private final long mSize;
    private final int mType;
}
Loading