1/* 2 * Copyright (C) 2012 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17// Make values from a motion sensor (e.g., accelerometer) available as filter outputs. 18 19package androidx.media.filterpacks.sensors; 20 21import android.content.Context; 22import android.hardware.Sensor; 23import android.hardware.SensorEvent; 24import android.hardware.SensorEventListener; 25import android.hardware.SensorManager; 26 27import androidx.media.filterfw.Filter; 28import androidx.media.filterfw.FrameType; 29import androidx.media.filterfw.FrameValues; 30import androidx.media.filterfw.MffContext; 31import androidx.media.filterfw.OutputPort; 32import androidx.media.filterfw.Signature; 33 34public final class MotionSensor extends Filter implements SensorEventListener { 35 36 private SensorManager mSensorManager = null; 37 private Sensor mSensor = null; 38 39 private float[] mValues = new float[3]; 40 41 public MotionSensor(MffContext context, String name) { 42 super(context, name); 43 } 44 45 @Override 46 public Signature getSignature() { 47 return new Signature() 48 .addOutputPort("values", Signature.PORT_REQUIRED, FrameType.array(float.class)) 49 .disallowOtherPorts(); 50 } 51 52 @Override 53 protected void onPrepare() { 54 mSensorManager = (SensorManager)getContext().getApplicationContext() 55 .getSystemService(Context.SENSOR_SERVICE); 56 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_LINEAR_ACCELERATION); 57 // TODO: currently, the type of sensor is hardcoded. Should be able to set the sensor 58 // type as filter input! 59 mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_UI); 60 } 61 62 @Override 63 protected void onTearDown() { 64 mSensorManager.unregisterListener(this); 65 } 66 67 @Override 68 public final void onAccuracyChanged(Sensor sensor, int accuracy) { 69 // (Do we need to do something when sensor accuracy changes?) 70 } 71 72 @Override 73 public final void onSensorChanged(SensorEvent event) { 74 synchronized(mValues) { 75 mValues[0] = event.values[0]; 76 mValues[1] = event.values[1]; 77 mValues[2] = event.values[2]; 78 } 79 } 80 81 @Override 82 protected void onProcess() { 83 OutputPort outPort = getConnectedOutputPort("values"); 84 FrameValues outFrame = outPort.fetchAvailableFrame(null).asFrameValues(); 85 synchronized(mValues) { 86 outFrame.setValues(mValues); 87 } 88 outFrame.setTimestamp(System.currentTimeMillis() * 1000000L); 89 outPort.pushFrame(outFrame); 90 } 91} 92 93