1c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/*
2c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * Copyright (C) Texas Instruments - http://www.ti.com/
3c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *
4c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * Licensed under the Apache License, Version 2.0 (the "License");
5c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * you may not use this file except in compliance with the License.
6c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * You may obtain a copy of the License at
7c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *
8c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *      http://www.apache.org/licenses/LICENSE-2.0
9c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev *
10c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * Unless required by applicable law or agreed to in writing, software
11c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * distributed under the License is distributed on an "AS IS" BASIS,
12c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * See the License for the specific language governing permissions and
14c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * limitations under the License.
15c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev */
16c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
17c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/**
18c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev* @file SensorListener.h
19c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev*
20c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev* This defines API for camerahal to get sensor events
21c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev*
22c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev*/
23c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
24c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#ifndef ANDROID_CAMERA_HARDWARE_SENSOR_LISTENER_H
25c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#define ANDROID_CAMERA_HARDWARE_SENSOR_LISTENER_H
26c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
27c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <android/sensor.h>
28c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <gui/Sensor.h>
29c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <gui/SensorManager.h>
30c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <gui/SensorEventQueue.h>
31c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#include <utils/Looper.h>
32c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
33c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevnamespace android {
34c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
35c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/**
36c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev * SensorListner class - Registers with sensor manager to get sensor events
37c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev */
38c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
39c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevtypedef void (*orientation_callback_t) (uint32_t orientation, uint32_t tilt, void* cookie);
40c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
41c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevclass SensorLooperThread : public Thread {
42c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    public:
43c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        SensorLooperThread(Looper* looper)
44c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            : Thread(false) {
45c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mLooper = sp<Looper>(looper);
46c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
47c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        ~SensorLooperThread() {
48c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mLooper.clear();
49c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
50c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
51c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        virtual bool threadLoop() {
52c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            int32_t ret = mLooper->pollOnce(-1);
53c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            return true;
54c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
55c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
56c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        // force looper wake up
57c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        void wake() {
58c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev            mLooper->wake();
59c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        }
60c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    private:
61c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        sp<Looper> mLooper;
62c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev};
63c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
64c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
65c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevclass SensorListener : public RefBase
66c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev{
67c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* public - types */
68c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevpublic:
69c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    typedef enum {
70c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        SENSOR_ACCELEROMETER  = 1 << 0,
71c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        SENSOR_MAGNETIC_FIELD = 1 << 1,
72c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        SENSOR_GYROSCOPE      = 1 << 2,
73c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        SENSOR_LIGHT          = 1 << 3,
74c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        SENSOR_PROXIMITY      = 1 << 4,
75c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev        SENSOR_ORIENTATION    = 1 << 5,
76c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    } sensor_type_t;
77c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* public - functions */
78c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevpublic:
79c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    SensorListener();
80c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    ~SensorListener();
81c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    status_t initialize();
82c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    void setCallbacks(orientation_callback_t orientation_cb, void *cookie);
83c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    void enableSensor(sensor_type_t type);
84c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    void disableSensor(sensor_type_t type);
85c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    void handleOrientation(uint32_t orientation, uint32_t tilt);
86c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* public - member variables */
87c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevpublic:
88c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    sp<SensorEventQueue> mSensorEventQueue;
89c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev/* private - member variables */
90c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchevprivate:
91c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    int sensorsEnabled;
92c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    orientation_callback_t mOrientationCb;
93c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    void *mCbCookie;
94c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    sp<Looper> mLooper;
95c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    sp<SensorLooperThread> mSensorLooperThread;
96c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev    Mutex mLock;
97c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev};
98c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
99c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev}
100c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev
101c322989ae6ff6769490828de1b5eda12b749cce9Iliyan Malchev#endif
102