| Index: device/sensors/android/java/src/org/chromium/device/sensors/SensorFactoryImpl_Internal.java
|
| diff --git a/device/sensors/android/java/src/org/chromium/device/sensors/SensorFactoryImpl_Internal.java b/device/sensors/android/java/src/org/chromium/device/sensors/SensorFactoryImpl_Internal.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6723ffb62ddefdb332430edf1bbea4a0f13c61a7
|
| --- /dev/null
|
| +++ b/device/sensors/android/java/src/org/chromium/device/sensors/SensorFactoryImpl_Internal.java
|
| @@ -0,0 +1,127 @@
|
| +// Copyright 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.device.sensors;
|
| +
|
| +import android.content.Context;
|
| +import android.hardware.SensorManager;
|
| +import android.os.Handler;
|
| +import android.os.HandlerThread;
|
| +import android.util.SparseArray;
|
| +
|
| +import org.chromium.mojo.bindings.Callbacks;
|
| +import org.chromium.mojo.bindings.InterfaceRequest;
|
| +import org.chromium.mojo.system.Core;
|
| +import org.chromium.mojo.system.MessagePipeHandle;
|
| +import org.chromium.mojo.system.SharedBufferHandle;
|
| +import org.chromium.mojom.device.sensors.Sensor;
|
| +import org.chromium.mojom.device.sensors.SensorConstants;
|
| +import org.chromium.mojom.device.sensors.SensorFactory.CreateSensorResponse;
|
| +import org.chromium.mojom.device.sensors.SensorType;
|
| +import org.chromium.mojom.device.sensors.Result;
|
| +
|
| +
|
| +/**
|
| + * Internal implementation of SensorFactory.
|
| + */
|
| +public final class SensorFactoryImpl_Internal implements SensorStateObserver {
|
| + private static SensorFactoryImpl_Internal mInstance = null;
|
| + private final SensorManager mSensorManager;
|
| + private final HandlerThread mSensorsThread;
|
| + private final Handler mHandler;
|
| + private final SparseArray<SensorBase> mActiveSensors = new SparseArray<>();
|
| + private SharedBufferHandle mSharedBufferHandle;
|
| + private static final long READING_SIZE_IN_BYTES = SensorConstants.SENSOR_READING_FIELD_SIZE * SensorConstants.SENSOR_READING_FIELDS_COUNT;
|
| + private static final long SHARED_BUFFER_SIZE_IN_BYTES = READING_SIZE_IN_BYTES * (long)SensorType.LAST;
|
| +
|
| + public static SensorFactoryImpl_Internal getInstance(Context context) {
|
| + if (mInstance == null) mInstance = new SensorFactoryImpl_Internal(context);
|
| + return mInstance;
|
| + }
|
| +
|
| + public static void reset() {
|
| + mInstance = null;
|
| + }
|
| +
|
| + public void onSensorNotInUse(SensorBase sensor) {
|
| + if (mActiveSensors.indexOfKey(sensor.getType()) >= 0) {
|
| + mActiveSensors.remove(sensor.getType());
|
| + }
|
| + }
|
| +
|
| + public void createSensor(int type, InterfaceRequest<Sensor> request, CreateSensorResponse callback) {
|
| +
|
| + if (mSensorManager == null) {
|
| + callback.call(Result.ERROR, null, 0L, 0L, 0);
|
| + return;
|
| + }
|
| +
|
| + try {
|
| + long offset = (type - (long)SensorType.FIRST) * READING_SIZE_IN_BYTES;
|
| +
|
| + if (mActiveSensors.indexOfKey(type) >= 0) {
|
| + SensorBase sensor = mActiveSensors.get(type);
|
| + Sensor.MANAGER.bind(new SensorImpl(sensor), request);
|
| + callback.call(Result.SUCCESS,
|
| + mSharedBufferHandle.duplicate(new SharedBufferHandle.DuplicateOptions()),
|
| + offset, READING_SIZE_IN_BYTES, sensor.reportingMode());
|
| + return;
|
| + }
|
| +
|
| + MessagePipeHandle messagePipeHandle = request.passHandle();
|
| + createSharedBuffer(messagePipeHandle.getCore());
|
| +
|
| + SensorBase sensor = createSensor(type, offset);
|
| + Sensor.MANAGER.bind(new SensorImpl(sensor), messagePipeHandle.pass());
|
| + mActiveSensors.put(type, sensor);
|
| + callback.call(Result.SUCCESS,
|
| + mSharedBufferHandle.duplicate(new SharedBufferHandle.DuplicateOptions()),
|
| + offset, READING_SIZE_IN_BYTES, sensor.reportingMode());
|
| + } catch (SensorException e) {
|
| + callback.call(Result.ERROR, null, 0L, 0L, 0);
|
| + }
|
| + }
|
| +
|
| + private SensorBase createSensor(int type, long offset)
|
| + throws SensorException {
|
| + switch (type) {
|
| + case SensorType.AMBIENT_LIGHT:
|
| + return new AmbientLightSensorImpl(mSensorManager, mHandler, mSharedBufferHandle,
|
| + offset, READING_SIZE_IN_BYTES, this);
|
| + case SensorType.PROXIMITY:
|
| + return new ProximitySensorImpl(mSensorManager, mHandler, mSharedBufferHandle,
|
| + offset, READING_SIZE_IN_BYTES, this);
|
| + case SensorType.PRESSURE:
|
| + return new PressureSensorImpl(mSensorManager, mHandler, mSharedBufferHandle,
|
| + offset, READING_SIZE_IN_BYTES, this);
|
| + case SensorType.GYROSCOPE:
|
| + return new GyroscopeSensorImpl(mSensorManager, mHandler, mSharedBufferHandle,
|
| + offset, READING_SIZE_IN_BYTES, this);
|
| + case SensorType.LINEAR_ACCELERATION:
|
| + default:
|
| + throw new SensorException();
|
| + }
|
| + }
|
| +
|
| + private void createSharedBuffer(Core core) throws SensorException {
|
| + if (mSharedBufferHandle != null) return;
|
| +
|
| + mSharedBufferHandle = core.createSharedBuffer(new SharedBufferHandle.CreateOptions(),
|
| + SHARED_BUFFER_SIZE_IN_BYTES);
|
| + if (mSharedBufferHandle == null) throw new SensorException();
|
| + }
|
| +
|
| + private SensorFactoryImpl_Internal(Context context) {
|
| + mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
|
| + if (mSensorManager != null) {
|
| + mSensorsThread = new HandlerThread("SensorsHandlerThread");
|
| + mSensorsThread.start();
|
| + mHandler = new Handler(mSensorsThread.getLooper());
|
| + } else {
|
| + mSensorsThread = null;
|
| + mHandler = null;
|
| + }
|
| + }
|
| +
|
| +}
|
|
|