1/* 2 * Copyright (C) 2016 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 17package com.android.server; 18 19import android.content.BroadcastReceiver; 20import android.content.Context; 21import android.content.Intent; 22import android.content.IntentFilter; 23import android.hardware.Sensor; 24import android.hardware.SensorEvent; 25import android.hardware.SensorEventListener; 26import android.hardware.SensorManager; 27import android.os.SystemClock; 28import android.os.SystemProperties; 29import android.os.UserHandle; 30import android.provider.Settings; 31import android.util.Slog; 32 33public class SensorNotificationService extends SystemService implements SensorEventListener { 34 //TODO: set DBG to false or remove Slog before release 35 private static final boolean DBG = true; 36 private static final String TAG = "SensorNotificationService"; 37 private Context mContext; 38 39 private SensorManager mSensorManager; 40 private Sensor mMetaSensor; 41 42 public SensorNotificationService(Context context) { 43 super(context); 44 mContext = context; 45 } 46 47 public void onStart() { 48 LocalServices.addService(SensorNotificationService.class, this); 49 } 50 51 public void onBootPhase(int phase) { 52 if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) { 53 // start 54 mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); 55 mMetaSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_DYNAMIC_SENSOR_META); 56 if (mMetaSensor == null) { 57 if (DBG) Slog.d(TAG, "Cannot obtain dynamic meta sensor, not supported."); 58 } else { 59 mSensorManager.registerListener(this, mMetaSensor, 60 SensorManager.SENSOR_DELAY_FASTEST); 61 } 62 } 63 } 64 65 private void broadcastDynamicSensorChanged() { 66 Intent i = new Intent(Intent.ACTION_DYNAMIC_SENSOR_CHANGED); 67 i.setFlags(Intent.FLAG_RECEIVER_REGISTERED_ONLY); // avoid waking up manifest receivers 68 mContext.sendBroadcastAsUser(i, UserHandle.ALL); 69 if (DBG) Slog.d(TAG, "DYNS sent dynamic sensor broadcast"); 70 } 71 72 @Override 73 public void onSensorChanged(SensorEvent event) { 74 if (event.sensor == mMetaSensor) { 75 broadcastDynamicSensorChanged(); 76 } 77 } 78 79 @Override 80 public void onAccuracyChanged(Sensor sensor, int accuracy) { 81 82 } 83} 84 85