1a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu/*
2a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu * Copyright (C) 2016 The Android Open Source Project
3a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu *
4a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu * Licensed under the Apache License, Version 2.0 (the "License");
5a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu * you may not use this file except in compliance with the License.
6a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu * You may obtain a copy of the License at
7a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu *
8a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu *      http://www.apache.org/licenses/LICENSE-2.0
9a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu *
10a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu * Unless required by applicable law or agreed to in writing, software
11a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu * distributed under the License is distributed on an "AS IS" BASIS,
12a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu * See the License for the specific language governing permissions and
14a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu * limitations under the License.
15a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu */
16a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
17a35b5539a95342799a18e95616c5e5751a198e4cPeng Xupackage com.android.server;
18a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
19a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.content.BroadcastReceiver;
20a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.content.Context;
21a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.content.Intent;
22a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.content.IntentFilter;
23a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.hardware.Sensor;
24a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.hardware.SensorEvent;
25a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.hardware.SensorEventListener;
26a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.hardware.SensorManager;
27a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.os.SystemClock;
28a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.os.SystemProperties;
29a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.os.UserHandle;
30a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.provider.Settings;
31a35b5539a95342799a18e95616c5e5751a198e4cPeng Xuimport android.util.Slog;
32a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
33a35b5539a95342799a18e95616c5e5751a198e4cPeng Xupublic class SensorNotificationService extends SystemService implements SensorEventListener {
34a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    //TODO: set DBG to false or remove Slog before release
35a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    private static final boolean DBG = true;
36a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    private static final String TAG = "SensorNotificationService";
37a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    private Context mContext;
38a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
39a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    private SensorManager mSensorManager;
40a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    private Sensor mMetaSensor;
41a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
42a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    public SensorNotificationService(Context context) {
43a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        super(context);
44a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        mContext = context;
45a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    }
46a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
47a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    public void onStart() {
48a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        LocalServices.addService(SensorNotificationService.class, this);
49a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    }
50a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
51a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    public void onBootPhase(int phase) {
52a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
53a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu            // start
54a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu            mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
55a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu            mMetaSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_DYNAMIC_SENSOR_META);
56a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu            if (mMetaSensor == null) {
57a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu                if (DBG) Slog.d(TAG, "Cannot obtain dynamic meta sensor, not supported.");
58a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu            } else {
59a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu                mSensorManager.registerListener(this, mMetaSensor,
60a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu                        SensorManager.SENSOR_DELAY_FASTEST);
61a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu            }
62a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        }
63a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    }
64a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
65a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    private void broadcastDynamicSensorChanged() {
66a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        Intent i = new Intent(Intent.ACTION_DYNAMIC_SENSOR_CHANGED);
67a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        i.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); // avoid waking up manifest receivers
68a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        mContext.sendBroadcastAsUser(i, UserHandle.ALL);
69a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        if (DBG) Slog.d(TAG, "DYNS sent dynamic sensor broadcast");
70a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    }
71a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
72a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    @Override
73a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    public void onSensorChanged(SensorEvent event) {
74a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        if (event.sensor == mMetaSensor) {
75a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu            broadcastDynamicSensorChanged();
76a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu        }
77a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    }
78a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
79a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    @Override
80a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    public void onAccuracyChanged(Sensor sensor, int accuracy) {
81a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
82a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu    }
83a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu}
84a35b5539a95342799a18e95616c5e5751a198e4cPeng Xu
85