John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.doze; |
| 18 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 19 | import android.app.AlarmManager; |
| 20 | import android.app.PendingIntent; |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 21 | import android.app.UiModeManager; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 26 | import android.content.res.Configuration; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 27 | import android.hardware.Sensor; |
| 28 | import android.hardware.SensorManager; |
| 29 | import android.hardware.TriggerEvent; |
| 30 | import android.hardware.TriggerEventListener; |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 31 | import android.media.AudioAttributes; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 32 | import android.os.PowerManager; |
| 33 | import android.os.Vibrator; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 34 | import android.service.dreams.DreamService; |
| 35 | import android.util.Log; |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 36 | import android.view.Display; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 37 | |
| 38 | import com.android.systemui.SystemUIApplication; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 39 | import com.android.systemui.statusbar.phone.DozeParameters; |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 40 | import com.android.systemui.statusbar.phone.DozeParameters.PulseSchedule; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 41 | |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 42 | import java.io.FileDescriptor; |
| 43 | import java.io.PrintWriter; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 44 | import java.util.Date; |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 45 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 46 | public class DozeService extends DreamService { |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 47 | private static final String TAG = "DozeService"; |
| 48 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 49 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 50 | private static final String ACTION_BASE = "com.android.systemui.doze"; |
| 51 | private static final String PULSE_ACTION = ACTION_BASE + ".pulse"; |
| 52 | private static final String NOTIFICATION_PULSE_ACTION = ACTION_BASE + ".notification_pulse"; |
John Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 53 | private static final String EXTRA_INSTANCE = "instance"; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 54 | |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 55 | private final String mTag = String.format(TAG + ".%08x", hashCode()); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 56 | private final Context mContext = this; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 57 | private final DozeParameters mDozeParameters = new DozeParameters(mContext); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 58 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 59 | private DozeHost mHost; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 60 | private SensorManager mSensors; |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 61 | private TriggerSensor mSigMotionSensor; |
| 62 | private TriggerSensor mPickupSensor; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 63 | private PowerManager mPowerManager; |
| 64 | private PowerManager.WakeLock mWakeLock; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 65 | private AlarmManager mAlarmManager; |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 66 | private UiModeManager mUiModeManager; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 67 | private boolean mDreaming; |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 68 | private boolean mPulsing; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 69 | private boolean mBroadcastReceiverRegistered; |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 70 | private boolean mDisplayStateSupported; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 71 | private boolean mNotificationLightOn; |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 72 | private boolean mPowerSaveActive; |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 73 | private boolean mCarMode; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 74 | private long mNotificationPulseTime; |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 75 | private int mScheduleResetsRemaining; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 76 | |
| 77 | public DozeService() { |
| 78 | if (DEBUG) Log.d(mTag, "new DozeService()"); |
| 79 | setDebug(DEBUG); |
| 80 | } |
| 81 | |
| 82 | @Override |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 83 | protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 84 | super.dumpOnHandler(fd, pw, args); |
| 85 | pw.print(" mDreaming: "); pw.println(mDreaming); |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 86 | pw.print(" mPulsing: "); pw.println(mPulsing); |
| 87 | pw.print(" mWakeLock: held="); pw.println(mWakeLock.isHeld()); |
John Spurlock | c6eed84 | 2014-08-25 12:19:41 -0400 | [diff] [blame] | 88 | pw.print(" mHost: "); pw.println(mHost); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 89 | pw.print(" mBroadcastReceiverRegistered: "); pw.println(mBroadcastReceiverRegistered); |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 90 | pw.print(" mSigMotionSensor: "); pw.println(mSigMotionSensor); |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 91 | pw.print(" mPickupSensor:"); pw.println(mPickupSensor); |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 92 | pw.print(" mDisplayStateSupported: "); pw.println(mDisplayStateSupported); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 93 | pw.print(" mNotificationLightOn: "); pw.println(mNotificationLightOn); |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 94 | pw.print(" mPowerSaveActive: "); pw.println(mPowerSaveActive); |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 95 | pw.print(" mCarMode: "); pw.println(mCarMode); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 96 | pw.print(" mNotificationPulseTime: "); pw.println(mNotificationPulseTime); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 97 | pw.print(" mScheduleResetsRemaining: "); pw.println(mScheduleResetsRemaining); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 98 | mDozeParameters.dump(pw); |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | @Override |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 102 | public void onCreate() { |
| 103 | if (DEBUG) Log.d(mTag, "onCreate"); |
| 104 | super.onCreate(); |
| 105 | |
| 106 | if (getApplication() instanceof SystemUIApplication) { |
| 107 | final SystemUIApplication app = (SystemUIApplication) getApplication(); |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 108 | mHost = app.getComponent(DozeHost.class); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 109 | } |
John Spurlock | c6eed84 | 2014-08-25 12:19:41 -0400 | [diff] [blame] | 110 | if (mHost == null) Log.w(TAG, "No doze service host found."); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 111 | |
| 112 | setWindowless(true); |
| 113 | |
| 114 | mSensors = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 115 | mSigMotionSensor = new TriggerSensor(Sensor.TYPE_SIGNIFICANT_MOTION, |
| 116 | mDozeParameters.getPulseOnSigMotion(), mDozeParameters.getVibrateOnSigMotion()); |
| 117 | mPickupSensor = new TriggerSensor(Sensor.TYPE_PICK_UP_GESTURE, |
| 118 | mDozeParameters.getPulseOnPickup(), mDozeParameters.getVibrateOnPickup()); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 119 | mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 120 | mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, mTag); |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 121 | mWakeLock.setReferenceCounted(true); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 122 | mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 123 | mDisplayStateSupported = mDozeParameters.getDisplayStateSupported(); |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 124 | mUiModeManager = (UiModeManager) mContext.getSystemService(Context.UI_MODE_SERVICE); |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 125 | turnDisplayOff(); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void onAttachedToWindow() { |
| 130 | if (DEBUG) Log.d(mTag, "onAttachedToWindow"); |
| 131 | super.onAttachedToWindow(); |
| 132 | } |
| 133 | |
| 134 | @Override |
| 135 | public void onDreamingStarted() { |
| 136 | super.onDreamingStarted(); |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 137 | |
| 138 | if (mHost == null) { |
| 139 | finish(); |
| 140 | return; |
| 141 | } |
| 142 | |
| 143 | mPowerSaveActive = mHost.isPowerSaveActive(); |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 144 | mCarMode = mUiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR; |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 145 | if (DEBUG) Log.d(mTag, "onDreamingStarted canDoze=" + canDoze() + " mPowerSaveActive=" |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 146 | + mPowerSaveActive + " mCarMode=" + mCarMode); |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 147 | if (mPowerSaveActive) { |
| 148 | finishToSavePower(); |
| 149 | return; |
| 150 | } |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 151 | if (mCarMode) { |
| 152 | finishForCarMode(); |
| 153 | return; |
| 154 | } |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 155 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 156 | mDreaming = true; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 157 | listenForPulseSignals(true); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 158 | rescheduleNotificationPulse(false /*predicate*/); // cancel any pending pulse alarms |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 159 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 160 | // Ask the host to get things ready to start dozing. |
| 161 | // Once ready, we call startDozing() at which point the CPU may suspend |
| 162 | // and we will need to acquire a wakelock to do work. |
| 163 | mHost.startDozing(new Runnable() { |
| 164 | @Override |
| 165 | public void run() { |
| 166 | if (mDreaming) { |
| 167 | startDozing(); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 168 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 169 | // From this point until onDreamingStopped we will need to hold a |
| 170 | // wakelock whenever we are doing work. Note that we never call |
| 171 | // stopDozing because can we just keep dozing until the bitter end. |
| 172 | } |
| 173 | } |
| 174 | }); |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 175 | } |
| 176 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 177 | @Override |
| 178 | public void onDreamingStopped() { |
| 179 | if (DEBUG) Log.d(mTag, "onDreamingStopped isDozing=" + isDozing()); |
| 180 | super.onDreamingStopped(); |
| 181 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 182 | if (mHost == null) { |
| 183 | return; |
| 184 | } |
| 185 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 186 | mDreaming = false; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 187 | listenForPulseSignals(false); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 188 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 189 | // Tell the host that it's over. |
| 190 | mHost.stopDozing(); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 191 | } |
| 192 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 193 | private void requestPulse() { |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 194 | if (mHost != null && mDreaming && !mPulsing) { |
| 195 | // Let the host know we want to pulse. Wait for it to be ready, then |
| 196 | // turn the screen on. When finished, turn the screen off again. |
| 197 | // Here we need a wakelock to stay awake until the pulse is finished. |
| 198 | mWakeLock.acquire(); |
| 199 | mPulsing = true; |
| 200 | mHost.pulseWhileDozing(new DozeHost.PulseCallback() { |
| 201 | @Override |
| 202 | public void onPulseStarted() { |
| 203 | if (mPulsing && mDreaming) { |
| 204 | turnDisplayOn(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | @Override |
| 209 | public void onPulseFinished() { |
| 210 | if (mPulsing && mDreaming) { |
| 211 | mPulsing = false; |
| 212 | turnDisplayOff(); |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 213 | } |
Jeff Brown | a273924 | 2014-09-22 22:30:22 -0700 | [diff] [blame] | 214 | mWakeLock.release(); // needs to be unconditional to balance acquire |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 215 | } |
| 216 | }); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 217 | } |
| 218 | } |
| 219 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 220 | private void turnDisplayOff() { |
| 221 | if (DEBUG) Log.d(TAG, "Display off"); |
| 222 | setDozeScreenState(Display.STATE_OFF); |
| 223 | } |
| 224 | |
| 225 | private void turnDisplayOn() { |
| 226 | if (DEBUG) Log.d(TAG, "Display on"); |
| 227 | setDozeScreenState(mDisplayStateSupported ? Display.STATE_DOZE : Display.STATE_ON); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 228 | } |
| 229 | |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 230 | private void finishToSavePower() { |
| 231 | Log.w(mTag, "Exiting ambient mode due to low power battery saver"); |
| 232 | finish(); |
| 233 | } |
| 234 | |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 235 | private void finishForCarMode() { |
| 236 | Log.w(mTag, "Exiting ambient mode, not allowed in car mode"); |
| 237 | finish(); |
| 238 | } |
| 239 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 240 | private void listenForPulseSignals(boolean listen) { |
| 241 | if (DEBUG) Log.d(mTag, "listenForPulseSignals: " + listen); |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 242 | mSigMotionSensor.setListening(listen); |
| 243 | mPickupSensor.setListening(listen); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 244 | listenForBroadcasts(listen); |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 245 | listenForNotifications(listen); |
| 246 | } |
| 247 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 248 | private void listenForBroadcasts(boolean listen) { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 249 | if (listen) { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 250 | final IntentFilter filter = new IntentFilter(PULSE_ACTION); |
| 251 | filter.addAction(NOTIFICATION_PULSE_ACTION); |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 252 | filter.addAction(UiModeManager.ACTION_ENTER_CAR_MODE); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 253 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| 254 | mBroadcastReceiverRegistered = true; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 255 | } else { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 256 | if (mBroadcastReceiverRegistered) { |
| 257 | mContext.unregisterReceiver(mBroadcastReceiver); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 258 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 259 | mBroadcastReceiverRegistered = false; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 260 | } |
| 261 | } |
| 262 | |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 263 | private void listenForNotifications(boolean listen) { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 264 | if (listen) { |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 265 | resetNotificationResets(); |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 266 | mHost.addCallback(mHostCallback); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 267 | } else { |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 268 | mHost.removeCallback(mHostCallback); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 272 | private void resetNotificationResets() { |
| 273 | if (DEBUG) Log.d(TAG, "resetNotificationResets"); |
| 274 | mScheduleResetsRemaining = mDozeParameters.getPulseScheduleResets(); |
| 275 | } |
| 276 | |
| 277 | private void updateNotificationPulse() { |
| 278 | if (DEBUG) Log.d(TAG, "updateNotificationPulse"); |
| 279 | if (!mDozeParameters.getPulseOnNotifications()) return; |
| 280 | if (mScheduleResetsRemaining <= 0) { |
| 281 | if (DEBUG) Log.d(TAG, "No more schedule resets remaining"); |
| 282 | return; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 283 | } |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 284 | final long now = System.currentTimeMillis(); |
| 285 | if ((now - mNotificationPulseTime) < mDozeParameters.getPulseDuration()) { |
| 286 | if (DEBUG) Log.d(TAG, "Recently updated, not resetting schedule"); |
| 287 | return; |
| 288 | } |
| 289 | mScheduleResetsRemaining--; |
| 290 | if (DEBUG) Log.d(TAG, "mScheduleResetsRemaining = " + mScheduleResetsRemaining); |
| 291 | mNotificationPulseTime = now; |
| 292 | rescheduleNotificationPulse(true /*predicate*/); |
| 293 | } |
| 294 | |
John Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 295 | private PendingIntent notificationPulseIntent(long instance) { |
| 296 | return PendingIntent.getBroadcast(mContext, 0, |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 297 | new Intent(NOTIFICATION_PULSE_ACTION) |
| 298 | .setPackage(getPackageName()) |
| 299 | .putExtra(EXTRA_INSTANCE, instance) |
| 300 | .setFlags(Intent.FLAG_RECEIVER_FOREGROUND), |
John Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 301 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 302 | } |
| 303 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 304 | private void rescheduleNotificationPulse(boolean predicate) { |
| 305 | if (DEBUG) Log.d(TAG, "rescheduleNotificationPulse predicate=" + predicate); |
John Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 306 | final PendingIntent notificationPulseIntent = notificationPulseIntent(0); |
| 307 | mAlarmManager.cancel(notificationPulseIntent); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 308 | if (!predicate) { |
| 309 | if (DEBUG) Log.d(TAG, " don't reschedule: predicate is false"); |
| 310 | return; |
| 311 | } |
| 312 | final PulseSchedule schedule = mDozeParameters.getPulseSchedule(); |
| 313 | if (schedule == null) { |
| 314 | if (DEBUG) Log.d(TAG, " don't reschedule: schedule is null"); |
| 315 | return; |
| 316 | } |
| 317 | final long now = System.currentTimeMillis(); |
| 318 | final long time = schedule.getNextTime(now, mNotificationPulseTime); |
| 319 | if (time <= 0) { |
| 320 | if (DEBUG) Log.d(TAG, " don't reschedule: time is " + time); |
| 321 | return; |
| 322 | } |
| 323 | final long delta = time - now; |
| 324 | if (delta <= 0) { |
| 325 | if (DEBUG) Log.d(TAG, " don't reschedule: delta is " + delta); |
| 326 | return; |
| 327 | } |
John Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 328 | final long instance = time - mNotificationPulseTime; |
| 329 | if (DEBUG) Log.d(TAG, "Scheduling pulse " + instance + " in " + delta + "ms for " |
| 330 | + new Date(time)); |
| 331 | mAlarmManager.setExact(AlarmManager.RTC_WAKEUP, time, notificationPulseIntent(instance)); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 332 | } |
| 333 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 334 | private static String triggerEventToString(TriggerEvent event) { |
| 335 | if (event == null) return null; |
| 336 | final StringBuilder sb = new StringBuilder("TriggerEvent[") |
| 337 | .append(event.timestamp).append(',') |
| 338 | .append(event.sensor.getName()); |
| 339 | if (event.values != null) { |
| 340 | for (int i = 0; i < event.values.length; i++) { |
| 341 | sb.append(',').append(event.values[i]); |
| 342 | } |
| 343 | } |
| 344 | return sb.append(']').toString(); |
| 345 | } |
| 346 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 347 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 348 | @Override |
| 349 | public void onReceive(Context context, Intent intent) { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 350 | if (PULSE_ACTION.equals(intent.getAction())) { |
| 351 | if (DEBUG) Log.d(mTag, "Received pulse intent"); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 352 | requestPulse(); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 353 | } |
| 354 | if (NOTIFICATION_PULSE_ACTION.equals(intent.getAction())) { |
John Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 355 | final long instance = intent.getLongExtra(EXTRA_INSTANCE, -1); |
| 356 | if (DEBUG) Log.d(mTag, "Received notification pulse intent instance=" + instance); |
| 357 | DozeLog.traceNotificationPulse(instance); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 358 | requestPulse(); |
| 359 | rescheduleNotificationPulse(mNotificationLightOn); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 360 | } |
John Spurlock | 870f153 | 2014-09-25 20:34:31 -0400 | [diff] [blame] | 361 | if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(intent.getAction())) { |
| 362 | mCarMode = true; |
| 363 | if (mCarMode && mDreaming) { |
| 364 | finishForCarMode(); |
| 365 | } |
| 366 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 367 | } |
| 368 | }; |
| 369 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 370 | private final DozeHost.Callback mHostCallback = new DozeHost.Callback() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 371 | @Override |
| 372 | public void onNewNotifications() { |
| 373 | if (DEBUG) Log.d(mTag, "onNewNotifications"); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 374 | // noop for now |
| 375 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 376 | |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 377 | @Override |
| 378 | public void onBuzzBeepBlinked() { |
| 379 | if (DEBUG) Log.d(mTag, "onBuzzBeepBlinked"); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 380 | updateNotificationPulse(); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | @Override |
| 384 | public void onNotificationLight(boolean on) { |
| 385 | if (DEBUG) Log.d(mTag, "onNotificationLight on=" + on); |
| 386 | if (mNotificationLightOn == on) return; |
| 387 | mNotificationLightOn = on; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 388 | if (mNotificationLightOn) { |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 389 | updateNotificationPulse(); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 390 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 391 | } |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 392 | |
| 393 | @Override |
| 394 | public void onPowerSaveChanged(boolean active) { |
| 395 | mPowerSaveActive = active; |
| 396 | if (mPowerSaveActive && mDreaming) { |
| 397 | finishToSavePower(); |
| 398 | } |
| 399 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 400 | }; |
| 401 | |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 402 | private class TriggerSensor extends TriggerEventListener { |
| 403 | private final Sensor mSensor; |
| 404 | private final boolean mConfigured; |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 405 | private final boolean mDebugVibrate; |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 406 | |
| 407 | private boolean mEnabled; |
| 408 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 409 | public TriggerSensor(int type, boolean configured, boolean debugVibrate) { |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 410 | mSensor = mSensors.getDefaultSensor(type); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 411 | mConfigured = configured; |
| 412 | mDebugVibrate = debugVibrate; |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | public void setListening(boolean listen) { |
| 416 | if (!mConfigured || mSensor == null) return; |
| 417 | if (listen) { |
| 418 | mEnabled = mSensors.requestTriggerSensor(this, mSensor); |
| 419 | } else if (mEnabled) { |
| 420 | mSensors.cancelTriggerSensor(this, mSensor); |
| 421 | mEnabled = false; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | @Override |
| 426 | public String toString() { |
| 427 | return new StringBuilder("{mEnabled=").append(mEnabled).append(", mConfigured=") |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 428 | .append(mConfigured).append(", mDebugVibrate=").append(mDebugVibrate) |
| 429 | .append(", mSensor=").append(mSensor).append("}").toString(); |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | @Override |
| 433 | public void onTrigger(TriggerEvent event) { |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 434 | mWakeLock.acquire(); |
| 435 | try { |
| 436 | if (DEBUG) Log.d(mTag, "onTrigger: " + triggerEventToString(event)); |
| 437 | if (mDebugVibrate) { |
| 438 | final Vibrator v = (Vibrator) mContext.getSystemService( |
| 439 | Context.VIBRATOR_SERVICE); |
| 440 | if (v != null) { |
| 441 | v.vibrate(1000, new AudioAttributes.Builder() |
| 442 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 443 | .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION).build()); |
| 444 | } |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 445 | } |
John Spurlock | 50a8ea6 | 2014-09-16 09:12:03 -0400 | [diff] [blame] | 446 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 447 | requestPulse(); |
| 448 | setListening(true); // reregister, this sensor only fires once |
| 449 | |
| 450 | // reset the notification pulse schedule, but only if we think we were not triggered |
| 451 | // by a notification-related vibration |
| 452 | final long timeSinceNotification = System.currentTimeMillis() - mNotificationPulseTime; |
| 453 | final boolean withinVibrationThreshold = |
| 454 | timeSinceNotification < mDozeParameters.getPickupVibrationThreshold(); |
| 455 | if (withinVibrationThreshold) { |
| 456 | if (DEBUG) Log.d(mTag, "Not resetting schedule, recent notification"); |
| 457 | } else { |
| 458 | resetNotificationResets(); |
| 459 | } |
| 460 | if (mSensor.getType() == Sensor.TYPE_PICK_UP_GESTURE) { |
| 461 | DozeLog.tracePickupPulse(withinVibrationThreshold); |
| 462 | } |
| 463 | } finally { |
| 464 | mWakeLock.release(); |
John Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 465 | } |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 466 | } |
| 467 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 468 | } |