blob: 6cc890b06f18464168529677053f70f58316c456 [file] [log] [blame]
John Spurlockbf370992014-06-17 13:58:31 -04001/*
2 * Copyright (C) 2014 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.systemui.doze;
18
John Spurlockcb566aa2014-08-03 22:58:28 -040019import android.app.AlarmManager;
20import android.app.PendingIntent;
John Spurlockbf370992014-06-17 13:58:31 -040021import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.hardware.Sensor;
26import android.hardware.SensorManager;
27import android.hardware.TriggerEvent;
28import android.hardware.TriggerEventListener;
John Spurlock559d9592014-08-09 12:04:36 -040029import android.media.AudioAttributes;
John Spurlockbf370992014-06-17 13:58:31 -040030import android.os.PowerManager;
31import android.os.Vibrator;
John Spurlockbf370992014-06-17 13:58:31 -040032import android.service.dreams.DreamService;
33import android.util.Log;
John Spurlocked69bd62014-07-23 11:09:02 -040034import android.view.Display;
John Spurlockbf370992014-06-17 13:58:31 -040035
36import com.android.systemui.SystemUIApplication;
John Spurlockd06aa572014-09-10 10:40:49 -040037import com.android.systemui.statusbar.phone.DozeParameters;
John Spurlock190d0262014-09-14 15:39:13 -040038import com.android.systemui.statusbar.phone.DozeParameters.PulseSchedule;
John Spurlockbf370992014-06-17 13:58:31 -040039
John Spurlock66127272014-06-28 11:27:17 -040040import java.io.FileDescriptor;
41import java.io.PrintWriter;
John Spurlockcb566aa2014-08-03 22:58:28 -040042import java.util.Date;
John Spurlock66127272014-06-28 11:27:17 -040043
John Spurlockbf370992014-06-17 13:58:31 -040044public class DozeService extends DreamService {
John Spurlocked69bd62014-07-23 11:09:02 -040045 private static final String TAG = "DozeService";
46 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
John Spurlockbf370992014-06-17 13:58:31 -040047
John Spurlockcb566aa2014-08-03 22:58:28 -040048 private static final String ACTION_BASE = "com.android.systemui.doze";
49 private static final String PULSE_ACTION = ACTION_BASE + ".pulse";
50 private static final String NOTIFICATION_PULSE_ACTION = ACTION_BASE + ".notification_pulse";
John Spurlock813552c2014-09-19 08:30:21 -040051 private static final String EXTRA_INSTANCE = "instance";
John Spurlockbf370992014-06-17 13:58:31 -040052
John Spurlocked69bd62014-07-23 11:09:02 -040053 private final String mTag = String.format(TAG + ".%08x", hashCode());
John Spurlockbf370992014-06-17 13:58:31 -040054 private final Context mContext = this;
John Spurlockd06aa572014-09-10 10:40:49 -040055 private final DozeParameters mDozeParameters = new DozeParameters(mContext);
John Spurlockbf370992014-06-17 13:58:31 -040056
Jeff Brown4d69e222014-09-18 15:27:50 -070057 private DozeHost mHost;
John Spurlockbf370992014-06-17 13:58:31 -040058 private SensorManager mSensors;
John Spurlock559d9592014-08-09 12:04:36 -040059 private TriggerSensor mSigMotionSensor;
60 private TriggerSensor mPickupSensor;
John Spurlockbf370992014-06-17 13:58:31 -040061 private PowerManager mPowerManager;
62 private PowerManager.WakeLock mWakeLock;
John Spurlockcb566aa2014-08-03 22:58:28 -040063 private AlarmManager mAlarmManager;
John Spurlockbf370992014-06-17 13:58:31 -040064 private boolean mDreaming;
Jeff Brown4d69e222014-09-18 15:27:50 -070065 private boolean mPulsing;
John Spurlockcb566aa2014-08-03 22:58:28 -040066 private boolean mBroadcastReceiverRegistered;
John Spurlocked69bd62014-07-23 11:09:02 -040067 private boolean mDisplayStateSupported;
John Spurlockcb566aa2014-08-03 22:58:28 -040068 private boolean mNotificationLightOn;
John Spurlockd96179e2014-08-21 16:43:45 -040069 private boolean mPowerSaveActive;
John Spurlockd06aa572014-09-10 10:40:49 -040070 private long mNotificationPulseTime;
John Spurlock190d0262014-09-14 15:39:13 -040071 private int mScheduleResetsRemaining;
John Spurlockbf370992014-06-17 13:58:31 -040072
73 public DozeService() {
74 if (DEBUG) Log.d(mTag, "new DozeService()");
75 setDebug(DEBUG);
76 }
77
78 @Override
John Spurlock66127272014-06-28 11:27:17 -040079 protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) {
80 super.dumpOnHandler(fd, pw, args);
81 pw.print(" mDreaming: "); pw.println(mDreaming);
Jeff Brown4d69e222014-09-18 15:27:50 -070082 pw.print(" mPulsing: "); pw.println(mPulsing);
83 pw.print(" mWakeLock: held="); pw.println(mWakeLock.isHeld());
John Spurlockc6eed842014-08-25 12:19:41 -040084 pw.print(" mHost: "); pw.println(mHost);
John Spurlockcb566aa2014-08-03 22:58:28 -040085 pw.print(" mBroadcastReceiverRegistered: "); pw.println(mBroadcastReceiverRegistered);
John Spurlock66127272014-06-28 11:27:17 -040086 pw.print(" mSigMotionSensor: "); pw.println(mSigMotionSensor);
John Spurlock559d9592014-08-09 12:04:36 -040087 pw.print(" mPickupSensor:"); pw.println(mPickupSensor);
John Spurlocked69bd62014-07-23 11:09:02 -040088 pw.print(" mDisplayStateSupported: "); pw.println(mDisplayStateSupported);
John Spurlockcb566aa2014-08-03 22:58:28 -040089 pw.print(" mNotificationLightOn: "); pw.println(mNotificationLightOn);
John Spurlockd96179e2014-08-21 16:43:45 -040090 pw.print(" mPowerSaveActive: "); pw.println(mPowerSaveActive);
John Spurlockd06aa572014-09-10 10:40:49 -040091 pw.print(" mNotificationPulseTime: "); pw.println(mNotificationPulseTime);
John Spurlock190d0262014-09-14 15:39:13 -040092 pw.print(" mScheduleResetsRemaining: "); pw.println(mScheduleResetsRemaining);
John Spurlockd06aa572014-09-10 10:40:49 -040093 mDozeParameters.dump(pw);
John Spurlock66127272014-06-28 11:27:17 -040094 }
95
96 @Override
John Spurlockbf370992014-06-17 13:58:31 -040097 public void onCreate() {
98 if (DEBUG) Log.d(mTag, "onCreate");
99 super.onCreate();
100
101 if (getApplication() instanceof SystemUIApplication) {
102 final SystemUIApplication app = (SystemUIApplication) getApplication();
Jeff Brown4d69e222014-09-18 15:27:50 -0700103 mHost = app.getComponent(DozeHost.class);
John Spurlockbf370992014-06-17 13:58:31 -0400104 }
John Spurlockc6eed842014-08-25 12:19:41 -0400105 if (mHost == null) Log.w(TAG, "No doze service host found.");
John Spurlockbf370992014-06-17 13:58:31 -0400106
107 setWindowless(true);
108
109 mSensors = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE);
John Spurlock190d0262014-09-14 15:39:13 -0400110 mSigMotionSensor = new TriggerSensor(Sensor.TYPE_SIGNIFICANT_MOTION,
111 mDozeParameters.getPulseOnSigMotion(), mDozeParameters.getVibrateOnSigMotion());
112 mPickupSensor = new TriggerSensor(Sensor.TYPE_PICK_UP_GESTURE,
113 mDozeParameters.getPulseOnPickup(), mDozeParameters.getVibrateOnPickup());
John Spurlockbf370992014-06-17 13:58:31 -0400114 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
115 mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, mTag);
Jeff Brown4d69e222014-09-18 15:27:50 -0700116 mWakeLock.setReferenceCounted(true);
John Spurlockcb566aa2014-08-03 22:58:28 -0400117 mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE);
John Spurlock190d0262014-09-14 15:39:13 -0400118 mDisplayStateSupported = mDozeParameters.getDisplayStateSupported();
Jeff Brown4d69e222014-09-18 15:27:50 -0700119 turnDisplayOff();
John Spurlockbf370992014-06-17 13:58:31 -0400120 }
121
122 @Override
123 public void onAttachedToWindow() {
124 if (DEBUG) Log.d(mTag, "onAttachedToWindow");
125 super.onAttachedToWindow();
126 }
127
128 @Override
129 public void onDreamingStarted() {
130 super.onDreamingStarted();
Jeff Brown4d69e222014-09-18 15:27:50 -0700131
132 if (mHost == null) {
133 finish();
134 return;
135 }
136
137 mPowerSaveActive = mHost.isPowerSaveActive();
John Spurlockd96179e2014-08-21 16:43:45 -0400138 if (DEBUG) Log.d(mTag, "onDreamingStarted canDoze=" + canDoze() + " mPowerSaveActive="
139 + mPowerSaveActive);
140 if (mPowerSaveActive) {
141 finishToSavePower();
142 return;
143 }
Jeff Brown4d69e222014-09-18 15:27:50 -0700144
John Spurlockbf370992014-06-17 13:58:31 -0400145 mDreaming = true;
John Spurlockcb566aa2014-08-03 22:58:28 -0400146 listenForPulseSignals(true);
John Spurlock190d0262014-09-14 15:39:13 -0400147 rescheduleNotificationPulse(false /*predicate*/); // cancel any pending pulse alarms
John Spurlockbf370992014-06-17 13:58:31 -0400148
Jeff Brown4d69e222014-09-18 15:27:50 -0700149 // Ask the host to get things ready to start dozing.
150 // Once ready, we call startDozing() at which point the CPU may suspend
151 // and we will need to acquire a wakelock to do work.
152 mHost.startDozing(new Runnable() {
153 @Override
154 public void run() {
155 if (mDreaming) {
156 startDozing();
John Spurlockbf370992014-06-17 13:58:31 -0400157
Jeff Brown4d69e222014-09-18 15:27:50 -0700158 // From this point until onDreamingStopped we will need to hold a
159 // wakelock whenever we are doing work. Note that we never call
160 // stopDozing because can we just keep dozing until the bitter end.
161 }
162 }
163 });
John Spurlocked69bd62014-07-23 11:09:02 -0400164 }
165
John Spurlockbf370992014-06-17 13:58:31 -0400166 @Override
167 public void onDreamingStopped() {
168 if (DEBUG) Log.d(mTag, "onDreamingStopped isDozing=" + isDozing());
169 super.onDreamingStopped();
170
Jeff Brown4d69e222014-09-18 15:27:50 -0700171 if (mHost == null) {
172 return;
173 }
174
John Spurlockbf370992014-06-17 13:58:31 -0400175 mDreaming = false;
John Spurlockcb566aa2014-08-03 22:58:28 -0400176 listenForPulseSignals(false);
John Spurlockbf370992014-06-17 13:58:31 -0400177
Jeff Brown4d69e222014-09-18 15:27:50 -0700178 // Tell the host that it's over.
179 mHost.stopDozing();
John Spurlockbf370992014-06-17 13:58:31 -0400180 }
181
John Spurlock190d0262014-09-14 15:39:13 -0400182 private void requestPulse() {
Jeff Brown4d69e222014-09-18 15:27:50 -0700183 if (mHost != null && mDreaming && !mPulsing) {
184 // Let the host know we want to pulse. Wait for it to be ready, then
185 // turn the screen on. When finished, turn the screen off again.
186 // Here we need a wakelock to stay awake until the pulse is finished.
187 mWakeLock.acquire();
188 mPulsing = true;
189 mHost.pulseWhileDozing(new DozeHost.PulseCallback() {
190 @Override
191 public void onPulseStarted() {
192 if (mPulsing && mDreaming) {
193 turnDisplayOn();
194 }
195 }
196
197 @Override
198 public void onPulseFinished() {
199 if (mPulsing && mDreaming) {
200 mPulsing = false;
201 turnDisplayOff();
Jeff Brown4d69e222014-09-18 15:27:50 -0700202 }
Jeff Browna2739242014-09-22 22:30:22 -0700203 mWakeLock.release(); // needs to be unconditional to balance acquire
Jeff Brown4d69e222014-09-18 15:27:50 -0700204 }
205 });
John Spurlockbf370992014-06-17 13:58:31 -0400206 }
207 }
208
Jeff Brown4d69e222014-09-18 15:27:50 -0700209 private void turnDisplayOff() {
210 if (DEBUG) Log.d(TAG, "Display off");
211 setDozeScreenState(Display.STATE_OFF);
212 }
213
214 private void turnDisplayOn() {
215 if (DEBUG) Log.d(TAG, "Display on");
216 setDozeScreenState(mDisplayStateSupported ? Display.STATE_DOZE : Display.STATE_ON);
John Spurlockbf370992014-06-17 13:58:31 -0400217 }
218
John Spurlockd96179e2014-08-21 16:43:45 -0400219 private void finishToSavePower() {
220 Log.w(mTag, "Exiting ambient mode due to low power battery saver");
221 finish();
222 }
223
John Spurlockcb566aa2014-08-03 22:58:28 -0400224 private void listenForPulseSignals(boolean listen) {
225 if (DEBUG) Log.d(mTag, "listenForPulseSignals: " + listen);
John Spurlock559d9592014-08-09 12:04:36 -0400226 mSigMotionSensor.setListening(listen);
227 mPickupSensor.setListening(listen);
John Spurlockcb566aa2014-08-03 22:58:28 -0400228 listenForBroadcasts(listen);
John Spurlock66127272014-06-28 11:27:17 -0400229 listenForNotifications(listen);
230 }
231
John Spurlockcb566aa2014-08-03 22:58:28 -0400232 private void listenForBroadcasts(boolean listen) {
John Spurlockbf370992014-06-17 13:58:31 -0400233 if (listen) {
John Spurlockcb566aa2014-08-03 22:58:28 -0400234 final IntentFilter filter = new IntentFilter(PULSE_ACTION);
235 filter.addAction(NOTIFICATION_PULSE_ACTION);
236 mContext.registerReceiver(mBroadcastReceiver, filter);
237 mBroadcastReceiverRegistered = true;
John Spurlockbf370992014-06-17 13:58:31 -0400238 } else {
John Spurlockcb566aa2014-08-03 22:58:28 -0400239 if (mBroadcastReceiverRegistered) {
240 mContext.unregisterReceiver(mBroadcastReceiver);
John Spurlockbf370992014-06-17 13:58:31 -0400241 }
John Spurlockcb566aa2014-08-03 22:58:28 -0400242 mBroadcastReceiverRegistered = false;
John Spurlockbf370992014-06-17 13:58:31 -0400243 }
244 }
245
John Spurlock66127272014-06-28 11:27:17 -0400246 private void listenForNotifications(boolean listen) {
John Spurlockbf370992014-06-17 13:58:31 -0400247 if (listen) {
John Spurlock190d0262014-09-14 15:39:13 -0400248 resetNotificationResets();
John Spurlock66127272014-06-28 11:27:17 -0400249 mHost.addCallback(mHostCallback);
John Spurlockbf370992014-06-17 13:58:31 -0400250 } else {
John Spurlock66127272014-06-28 11:27:17 -0400251 mHost.removeCallback(mHostCallback);
John Spurlockbf370992014-06-17 13:58:31 -0400252 }
253 }
254
John Spurlock190d0262014-09-14 15:39:13 -0400255 private void resetNotificationResets() {
256 if (DEBUG) Log.d(TAG, "resetNotificationResets");
257 mScheduleResetsRemaining = mDozeParameters.getPulseScheduleResets();
258 }
259
260 private void updateNotificationPulse() {
261 if (DEBUG) Log.d(TAG, "updateNotificationPulse");
262 if (!mDozeParameters.getPulseOnNotifications()) return;
263 if (mScheduleResetsRemaining <= 0) {
264 if (DEBUG) Log.d(TAG, "No more schedule resets remaining");
265 return;
John Spurlockcb566aa2014-08-03 22:58:28 -0400266 }
John Spurlock190d0262014-09-14 15:39:13 -0400267 final long now = System.currentTimeMillis();
268 if ((now - mNotificationPulseTime) < mDozeParameters.getPulseDuration()) {
269 if (DEBUG) Log.d(TAG, "Recently updated, not resetting schedule");
270 return;
271 }
272 mScheduleResetsRemaining--;
273 if (DEBUG) Log.d(TAG, "mScheduleResetsRemaining = " + mScheduleResetsRemaining);
274 mNotificationPulseTime = now;
275 rescheduleNotificationPulse(true /*predicate*/);
276 }
277
John Spurlock813552c2014-09-19 08:30:21 -0400278 private PendingIntent notificationPulseIntent(long instance) {
279 return PendingIntent.getBroadcast(mContext, 0,
Jeff Brown4d69e222014-09-18 15:27:50 -0700280 new Intent(NOTIFICATION_PULSE_ACTION)
281 .setPackage(getPackageName())
282 .putExtra(EXTRA_INSTANCE, instance)
283 .setFlags(Intent.FLAG_RECEIVER_FOREGROUND),
John Spurlock813552c2014-09-19 08:30:21 -0400284 PendingIntent.FLAG_UPDATE_CURRENT);
285 }
286
John Spurlock190d0262014-09-14 15:39:13 -0400287 private void rescheduleNotificationPulse(boolean predicate) {
288 if (DEBUG) Log.d(TAG, "rescheduleNotificationPulse predicate=" + predicate);
John Spurlock813552c2014-09-19 08:30:21 -0400289 final PendingIntent notificationPulseIntent = notificationPulseIntent(0);
290 mAlarmManager.cancel(notificationPulseIntent);
John Spurlock190d0262014-09-14 15:39:13 -0400291 if (!predicate) {
292 if (DEBUG) Log.d(TAG, " don't reschedule: predicate is false");
293 return;
294 }
295 final PulseSchedule schedule = mDozeParameters.getPulseSchedule();
296 if (schedule == null) {
297 if (DEBUG) Log.d(TAG, " don't reschedule: schedule is null");
298 return;
299 }
300 final long now = System.currentTimeMillis();
301 final long time = schedule.getNextTime(now, mNotificationPulseTime);
302 if (time <= 0) {
303 if (DEBUG) Log.d(TAG, " don't reschedule: time is " + time);
304 return;
305 }
306 final long delta = time - now;
307 if (delta <= 0) {
308 if (DEBUG) Log.d(TAG, " don't reschedule: delta is " + delta);
309 return;
310 }
John Spurlock813552c2014-09-19 08:30:21 -0400311 final long instance = time - mNotificationPulseTime;
312 if (DEBUG) Log.d(TAG, "Scheduling pulse " + instance + " in " + delta + "ms for "
313 + new Date(time));
314 mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, time, notificationPulseIntent(instance));
John Spurlockcb566aa2014-08-03 22:58:28 -0400315 }
316
John Spurlockbf370992014-06-17 13:58:31 -0400317 private static String triggerEventToString(TriggerEvent event) {
318 if (event == null) return null;
319 final StringBuilder sb = new StringBuilder("TriggerEvent[")
320 .append(event.timestamp).append(',')
321 .append(event.sensor.getName());
322 if (event.values != null) {
323 for (int i = 0; i < event.values.length; i++) {
324 sb.append(',').append(event.values[i]);
325 }
326 }
327 return sb.append(']').toString();
328 }
329
John Spurlockcb566aa2014-08-03 22:58:28 -0400330 private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
John Spurlockbf370992014-06-17 13:58:31 -0400331 @Override
332 public void onReceive(Context context, Intent intent) {
John Spurlockcb566aa2014-08-03 22:58:28 -0400333 if (PULSE_ACTION.equals(intent.getAction())) {
334 if (DEBUG) Log.d(mTag, "Received pulse intent");
John Spurlock190d0262014-09-14 15:39:13 -0400335 requestPulse();
John Spurlockcb566aa2014-08-03 22:58:28 -0400336 }
337 if (NOTIFICATION_PULSE_ACTION.equals(intent.getAction())) {
John Spurlock813552c2014-09-19 08:30:21 -0400338 final long instance = intent.getLongExtra(EXTRA_INSTANCE, -1);
339 if (DEBUG) Log.d(mTag, "Received notification pulse intent instance=" + instance);
340 DozeLog.traceNotificationPulse(instance);
John Spurlock190d0262014-09-14 15:39:13 -0400341 requestPulse();
342 rescheduleNotificationPulse(mNotificationLightOn);
John Spurlockcb566aa2014-08-03 22:58:28 -0400343 }
John Spurlockbf370992014-06-17 13:58:31 -0400344 }
345 };
346
Jeff Brown4d69e222014-09-18 15:27:50 -0700347 private final DozeHost.Callback mHostCallback = new DozeHost.Callback() {
John Spurlockbf370992014-06-17 13:58:31 -0400348 @Override
349 public void onNewNotifications() {
350 if (DEBUG) Log.d(mTag, "onNewNotifications");
John Spurlockcad57682014-07-26 17:09:56 -0400351 // noop for now
352 }
John Spurlockcb566aa2014-08-03 22:58:28 -0400353
John Spurlockcad57682014-07-26 17:09:56 -0400354 @Override
355 public void onBuzzBeepBlinked() {
356 if (DEBUG) Log.d(mTag, "onBuzzBeepBlinked");
John Spurlock190d0262014-09-14 15:39:13 -0400357 updateNotificationPulse();
John Spurlockcb566aa2014-08-03 22:58:28 -0400358 }
359
360 @Override
361 public void onNotificationLight(boolean on) {
362 if (DEBUG) Log.d(mTag, "onNotificationLight on=" + on);
363 if (mNotificationLightOn == on) return;
364 mNotificationLightOn = on;
John Spurlockd06aa572014-09-10 10:40:49 -0400365 if (mNotificationLightOn) {
John Spurlock190d0262014-09-14 15:39:13 -0400366 updateNotificationPulse();
John Spurlockd06aa572014-09-10 10:40:49 -0400367 }
John Spurlockbf370992014-06-17 13:58:31 -0400368 }
John Spurlockd96179e2014-08-21 16:43:45 -0400369
370 @Override
371 public void onPowerSaveChanged(boolean active) {
372 mPowerSaveActive = active;
373 if (mPowerSaveActive && mDreaming) {
374 finishToSavePower();
375 }
376 }
John Spurlockbf370992014-06-17 13:58:31 -0400377 };
378
John Spurlock559d9592014-08-09 12:04:36 -0400379 private class TriggerSensor extends TriggerEventListener {
380 private final Sensor mSensor;
381 private final boolean mConfigured;
John Spurlock190d0262014-09-14 15:39:13 -0400382 private final boolean mDebugVibrate;
John Spurlock559d9592014-08-09 12:04:36 -0400383
384 private boolean mEnabled;
385
John Spurlock190d0262014-09-14 15:39:13 -0400386 public TriggerSensor(int type, boolean configured, boolean debugVibrate) {
John Spurlock559d9592014-08-09 12:04:36 -0400387 mSensor = mSensors.getDefaultSensor(type);
John Spurlock190d0262014-09-14 15:39:13 -0400388 mConfigured = configured;
389 mDebugVibrate = debugVibrate;
John Spurlock559d9592014-08-09 12:04:36 -0400390 }
391
392 public void setListening(boolean listen) {
393 if (!mConfigured || mSensor == null) return;
394 if (listen) {
395 mEnabled = mSensors.requestTriggerSensor(this, mSensor);
396 } else if (mEnabled) {
397 mSensors.cancelTriggerSensor(this, mSensor);
398 mEnabled = false;
399 }
400 }
401
402 @Override
403 public String toString() {
404 return new StringBuilder("{mEnabled=").append(mEnabled).append(", mConfigured=")
John Spurlock190d0262014-09-14 15:39:13 -0400405 .append(mConfigured).append(", mDebugVibrate=").append(mDebugVibrate)
406 .append(", mSensor=").append(mSensor).append("}").toString();
John Spurlock559d9592014-08-09 12:04:36 -0400407 }
408
409 @Override
410 public void onTrigger(TriggerEvent event) {
Jeff Brown4d69e222014-09-18 15:27:50 -0700411 mWakeLock.acquire();
412 try {
413 if (DEBUG) Log.d(mTag, "onTrigger: " + triggerEventToString(event));
414 if (mDebugVibrate) {
415 final Vibrator v = (Vibrator) mContext.getSystemService(
416 Context.VIBRATOR_SERVICE);
417 if (v != null) {
418 v.vibrate(1000, new AudioAttributes.Builder()
419 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
420 .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION).build());
421 }
John Spurlock559d9592014-08-09 12:04:36 -0400422 }
John Spurlock50a8ea62014-09-16 09:12:03 -0400423
Jeff Brown4d69e222014-09-18 15:27:50 -0700424 requestPulse();
425 setListening(true); // reregister, this sensor only fires once
426
427 // reset the notification pulse schedule, but only if we think we were not triggered
428 // by a notification-related vibration
429 final long timeSinceNotification = System.currentTimeMillis() - mNotificationPulseTime;
430 final boolean withinVibrationThreshold =
431 timeSinceNotification < mDozeParameters.getPickupVibrationThreshold();
432 if (withinVibrationThreshold) {
433 if (DEBUG) Log.d(mTag, "Not resetting schedule, recent notification");
434 } else {
435 resetNotificationResets();
436 }
437 if (mSensor.getType() == Sensor.TYPE_PICK_UP_GESTURE) {
438 DozeLog.tracePickupPulse(withinVibrationThreshold);
439 }
440 } finally {
441 mWakeLock.release();
John Spurlock813552c2014-09-19 08:30:21 -0400442 }
John Spurlock559d9592014-08-09 12:04:36 -0400443 }
444 }
John Spurlockbf370992014-06-17 13:58:31 -0400445}