OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.device.sensors; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.hardware.SensorManager; |
| 9 import android.os.Handler; |
| 10 import android.os.HandlerThread; |
| 11 import android.util.SparseArray; |
| 12 |
| 13 import org.chromium.mojo.bindings.Callbacks; |
| 14 import org.chromium.mojo.bindings.InterfaceRequest; |
| 15 import org.chromium.mojo.system.Core; |
| 16 import org.chromium.mojo.system.MessagePipeHandle; |
| 17 import org.chromium.mojo.system.SharedBufferHandle; |
| 18 import org.chromium.mojom.device.sensors.Sensor; |
| 19 import org.chromium.mojom.device.sensors.SensorConstants; |
| 20 import org.chromium.mojom.device.sensors.SensorFactory.CreateSensorResponse; |
| 21 import org.chromium.mojom.device.sensors.SensorType; |
| 22 import org.chromium.mojom.device.sensors.Result; |
| 23 |
| 24 |
| 25 /** |
| 26 * Internal implementation of SensorFactory. |
| 27 */ |
| 28 public final class SensorFactoryImpl_Internal implements SensorStateObserver { |
| 29 private static SensorFactoryImpl_Internal mInstance = null; |
| 30 private final SensorManager mSensorManager; |
| 31 private final HandlerThread mSensorsThread; |
| 32 private final Handler mHandler; |
| 33 private final SparseArray<SensorBase> mActiveSensors = new SparseArray<>(); |
| 34 private SharedBufferHandle mSharedBufferHandle; |
| 35 private static final long READING_SIZE_IN_BYTES = SensorConstants.SENSOR_REA
DING_FIELD_SIZE * SensorConstants.SENSOR_READING_FIELDS_COUNT; |
| 36 private static final long SHARED_BUFFER_SIZE_IN_BYTES = READING_SIZE_IN_BYTE
S * (long)SensorType.LAST; |
| 37 |
| 38 public static SensorFactoryImpl_Internal getInstance(Context context) { |
| 39 if (mInstance == null) mInstance = new SensorFactoryImpl_Internal(contex
t); |
| 40 return mInstance; |
| 41 } |
| 42 |
| 43 public static void reset() { |
| 44 mInstance = null; |
| 45 } |
| 46 |
| 47 public void onSensorNotInUse(SensorBase sensor) { |
| 48 if (mActiveSensors.indexOfKey(sensor.getType()) >= 0) { |
| 49 mActiveSensors.remove(sensor.getType()); |
| 50 } |
| 51 } |
| 52 |
| 53 public void createSensor(int type, InterfaceRequest<Sensor> request, CreateS
ensorResponse callback) { |
| 54 |
| 55 if (mSensorManager == null) { |
| 56 callback.call(Result.ERROR, null, 0L, 0L, 0); |
| 57 return; |
| 58 } |
| 59 |
| 60 try { |
| 61 long offset = (type - (long)SensorType.FIRST) * READING_SIZE_IN_BYTE
S; |
| 62 |
| 63 if (mActiveSensors.indexOfKey(type) >= 0) { |
| 64 SensorBase sensor = mActiveSensors.get(type); |
| 65 Sensor.MANAGER.bind(new SensorImpl(sensor), request); |
| 66 callback.call(Result.SUCCESS, |
| 67 mSharedBufferHandle.duplicate(new SharedBufferHandle.Dup
licateOptions()), |
| 68 offset, READING_SIZE_IN_BYTES, sensor.reportingMode()); |
| 69 return; |
| 70 } |
| 71 |
| 72 MessagePipeHandle messagePipeHandle = request.passHandle(); |
| 73 createSharedBuffer(messagePipeHandle.getCore()); |
| 74 |
| 75 SensorBase sensor = createSensor(type, offset); |
| 76 Sensor.MANAGER.bind(new SensorImpl(sensor), messagePipeHandle.pass()
); |
| 77 mActiveSensors.put(type, sensor); |
| 78 callback.call(Result.SUCCESS, |
| 79 mSharedBufferHandle.duplicate(new SharedBufferHandle.Duplica
teOptions()), |
| 80 offset, READING_SIZE_IN_BYTES, sensor.reportingMode()); |
| 81 } catch (SensorException e) { |
| 82 callback.call(Result.ERROR, null, 0L, 0L, 0); |
| 83 } |
| 84 } |
| 85 |
| 86 private SensorBase createSensor(int type, long offset) |
| 87 throws SensorException { |
| 88 switch (type) { |
| 89 case SensorType.AMBIENT_LIGHT: |
| 90 return new AmbientLightSensorImpl(mSensorManager, mHandler, mSharedB
ufferHandle, |
| 91 offset, READING_SIZE_IN_BYTES, this); |
| 92 case SensorType.PROXIMITY: |
| 93 return new ProximitySensorImpl(mSensorManager, mHandler, mSharedBuff
erHandle, |
| 94 offset, READING_SIZE_IN_BYTES, this); |
| 95 case SensorType.PRESSURE: |
| 96 return new PressureSensorImpl(mSensorManager, mHandler, mSharedBuffe
rHandle, |
| 97 offset, READING_SIZE_IN_BYTES, this); |
| 98 case SensorType.GYROSCOPE: |
| 99 return new GyroscopeSensorImpl(mSensorManager, mHandler, mSharedBuff
erHandle, |
| 100 offset, READING_SIZE_IN_BYTES, this); |
| 101 case SensorType.LINEAR_ACCELERATION: |
| 102 default: |
| 103 throw new SensorException(); |
| 104 } |
| 105 } |
| 106 |
| 107 private void createSharedBuffer(Core core) throws SensorException { |
| 108 if (mSharedBufferHandle != null) return; |
| 109 |
| 110 mSharedBufferHandle = core.createSharedBuffer(new SharedBufferHandle.Cre
ateOptions(), |
| 111 SHARED_BUFFER_SIZE_IN_BYTES); |
| 112 if (mSharedBufferHandle == null) throw new SensorException(); |
| 113 } |
| 114 |
| 115 private SensorFactoryImpl_Internal(Context context) { |
| 116 mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_
SERVICE); |
| 117 if (mSensorManager != null) { |
| 118 mSensorsThread = new HandlerThread("SensorsHandlerThread"); |
| 119 mSensorsThread.start(); |
| 120 mHandler = new Handler(mSensorsThread.getLooper()); |
| 121 } else { |
| 122 mSensorsThread = null; |
| 123 mHandler = null; |
| 124 } |
| 125 } |
| 126 |
| 127 } |
OLD | NEW |