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 | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | import android.hardware.Sensor; |
| 26 | import android.hardware.SensorManager; |
| 27 | import android.hardware.TriggerEvent; |
| 28 | import android.hardware.TriggerEventListener; |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 29 | import android.media.AudioAttributes; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 30 | import android.os.PowerManager; |
| 31 | import android.os.Vibrator; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 32 | import android.service.dreams.DreamService; |
| 33 | import android.util.Log; |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 34 | import android.view.Display; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 35 | |
| 36 | import com.android.systemui.SystemUIApplication; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 37 | import com.android.systemui.statusbar.phone.DozeParameters; |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 38 | import com.android.systemui.statusbar.phone.DozeParameters.PulseSchedule; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 39 | |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 40 | import java.io.FileDescriptor; |
| 41 | import java.io.PrintWriter; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 42 | import java.util.Date; |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 43 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 44 | public class DozeService extends DreamService { |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 45 | private static final String TAG = "DozeService"; |
| 46 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 47 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 48 | 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 Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 51 | private static final String EXTRA_INSTANCE = "instance"; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 52 | |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 53 | private final String mTag = String.format(TAG + ".%08x", hashCode()); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 54 | private final Context mContext = this; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 55 | private final DozeParameters mDozeParameters = new DozeParameters(mContext); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 56 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 57 | private DozeHost mHost; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 58 | private SensorManager mSensors; |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 59 | private TriggerSensor mSigMotionSensor; |
| 60 | private TriggerSensor mPickupSensor; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 61 | private PowerManager mPowerManager; |
| 62 | private PowerManager.WakeLock mWakeLock; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 63 | private AlarmManager mAlarmManager; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 64 | private boolean mDreaming; |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 65 | private boolean mPulsing; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 66 | private boolean mBroadcastReceiverRegistered; |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 67 | private boolean mDisplayStateSupported; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 68 | private boolean mNotificationLightOn; |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 69 | private boolean mPowerSaveActive; |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 70 | private long mNotificationPulseTime; |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 71 | private int mScheduleResetsRemaining; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 72 | |
| 73 | public DozeService() { |
| 74 | if (DEBUG) Log.d(mTag, "new DozeService()"); |
| 75 | setDebug(DEBUG); |
| 76 | } |
| 77 | |
| 78 | @Override |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 79 | protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 80 | super.dumpOnHandler(fd, pw, args); |
| 81 | pw.print(" mDreaming: "); pw.println(mDreaming); |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 82 | pw.print(" mPulsing: "); pw.println(mPulsing); |
| 83 | pw.print(" mWakeLock: held="); pw.println(mWakeLock.isHeld()); |
John Spurlock | c6eed84 | 2014-08-25 12:19:41 -0400 | [diff] [blame] | 84 | pw.print(" mHost: "); pw.println(mHost); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 85 | pw.print(" mBroadcastReceiverRegistered: "); pw.println(mBroadcastReceiverRegistered); |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 86 | pw.print(" mSigMotionSensor: "); pw.println(mSigMotionSensor); |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 87 | pw.print(" mPickupSensor:"); pw.println(mPickupSensor); |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 88 | pw.print(" mDisplayStateSupported: "); pw.println(mDisplayStateSupported); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 89 | pw.print(" mNotificationLightOn: "); pw.println(mNotificationLightOn); |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 90 | pw.print(" mPowerSaveActive: "); pw.println(mPowerSaveActive); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 91 | pw.print(" mNotificationPulseTime: "); pw.println(mNotificationPulseTime); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 92 | pw.print(" mScheduleResetsRemaining: "); pw.println(mScheduleResetsRemaining); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 93 | mDozeParameters.dump(pw); |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 97 | 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 Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 103 | mHost = app.getComponent(DozeHost.class); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 104 | } |
John Spurlock | c6eed84 | 2014-08-25 12:19:41 -0400 | [diff] [blame] | 105 | if (mHost == null) Log.w(TAG, "No doze service host found."); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 106 | |
| 107 | setWindowless(true); |
| 108 | |
| 109 | mSensors = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 110 | 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 Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 114 | mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE); |
| 115 | mWakeLock = mPowerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, mTag); |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 116 | mWakeLock.setReferenceCounted(true); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 117 | mAlarmManager = (AlarmManager) mContext.getSystemService(Context.ALARM_SERVICE); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 118 | mDisplayStateSupported = mDozeParameters.getDisplayStateSupported(); |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 119 | turnDisplayOff(); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 120 | } |
| 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 Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 131 | |
| 132 | if (mHost == null) { |
| 133 | finish(); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | mPowerSaveActive = mHost.isPowerSaveActive(); |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 138 | if (DEBUG) Log.d(mTag, "onDreamingStarted canDoze=" + canDoze() + " mPowerSaveActive=" |
| 139 | + mPowerSaveActive); |
| 140 | if (mPowerSaveActive) { |
| 141 | finishToSavePower(); |
| 142 | return; |
| 143 | } |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 144 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 145 | mDreaming = true; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 146 | listenForPulseSignals(true); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 147 | rescheduleNotificationPulse(false /*predicate*/); // cancel any pending pulse alarms |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 148 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 149 | // 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 Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 157 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 158 | // 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 Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 164 | } |
| 165 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 166 | @Override |
| 167 | public void onDreamingStopped() { |
| 168 | if (DEBUG) Log.d(mTag, "onDreamingStopped isDozing=" + isDozing()); |
| 169 | super.onDreamingStopped(); |
| 170 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 171 | if (mHost == null) { |
| 172 | return; |
| 173 | } |
| 174 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 175 | mDreaming = false; |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 176 | listenForPulseSignals(false); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 177 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 178 | // Tell the host that it's over. |
| 179 | mHost.stopDozing(); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 180 | } |
| 181 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 182 | private void requestPulse() { |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 183 | 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 Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 202 | } |
Jeff Brown | a273924 | 2014-09-22 22:30:22 -0700 | [diff] [blame] | 203 | mWakeLock.release(); // needs to be unconditional to balance acquire |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 204 | } |
| 205 | }); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 209 | 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 Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 217 | } |
| 218 | |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 219 | private void finishToSavePower() { |
| 220 | Log.w(mTag, "Exiting ambient mode due to low power battery saver"); |
| 221 | finish(); |
| 222 | } |
| 223 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 224 | private void listenForPulseSignals(boolean listen) { |
| 225 | if (DEBUG) Log.d(mTag, "listenForPulseSignals: " + listen); |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 226 | mSigMotionSensor.setListening(listen); |
| 227 | mPickupSensor.setListening(listen); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 228 | listenForBroadcasts(listen); |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 229 | listenForNotifications(listen); |
| 230 | } |
| 231 | |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 232 | private void listenForBroadcasts(boolean listen) { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 233 | if (listen) { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 234 | final IntentFilter filter = new IntentFilter(PULSE_ACTION); |
| 235 | filter.addAction(NOTIFICATION_PULSE_ACTION); |
| 236 | mContext.registerReceiver(mBroadcastReceiver, filter); |
| 237 | mBroadcastReceiverRegistered = true; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 238 | } else { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 239 | if (mBroadcastReceiverRegistered) { |
| 240 | mContext.unregisterReceiver(mBroadcastReceiver); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 241 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 242 | mBroadcastReceiverRegistered = false; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 243 | } |
| 244 | } |
| 245 | |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 246 | private void listenForNotifications(boolean listen) { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 247 | if (listen) { |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 248 | resetNotificationResets(); |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 249 | mHost.addCallback(mHostCallback); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 250 | } else { |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 251 | mHost.removeCallback(mHostCallback); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 255 | 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 Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 266 | } |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 267 | 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 Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 278 | private PendingIntent notificationPulseIntent(long instance) { |
| 279 | return PendingIntent.getBroadcast(mContext, 0, |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 280 | new Intent(NOTIFICATION_PULSE_ACTION) |
| 281 | .setPackage(getPackageName()) |
| 282 | .putExtra(EXTRA_INSTANCE, instance) |
| 283 | .setFlags(Intent.FLAG_RECEIVER_FOREGROUND), |
John Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 284 | PendingIntent.FLAG_UPDATE_CURRENT); |
| 285 | } |
| 286 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 287 | private void rescheduleNotificationPulse(boolean predicate) { |
| 288 | if (DEBUG) Log.d(TAG, "rescheduleNotificationPulse predicate=" + predicate); |
John Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 289 | final PendingIntent notificationPulseIntent = notificationPulseIntent(0); |
| 290 | mAlarmManager.cancel(notificationPulseIntent); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 291 | 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 Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 311 | 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 Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 315 | } |
| 316 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 317 | 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 Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 330 | private final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 331 | @Override |
| 332 | public void onReceive(Context context, Intent intent) { |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 333 | if (PULSE_ACTION.equals(intent.getAction())) { |
| 334 | if (DEBUG) Log.d(mTag, "Received pulse intent"); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 335 | requestPulse(); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 336 | } |
| 337 | if (NOTIFICATION_PULSE_ACTION.equals(intent.getAction())) { |
John Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 338 | 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 Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 341 | requestPulse(); |
| 342 | rescheduleNotificationPulse(mNotificationLightOn); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 343 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 344 | } |
| 345 | }; |
| 346 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 347 | private final DozeHost.Callback mHostCallback = new DozeHost.Callback() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 348 | @Override |
| 349 | public void onNewNotifications() { |
| 350 | if (DEBUG) Log.d(mTag, "onNewNotifications"); |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 351 | // noop for now |
| 352 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 353 | |
John Spurlock | cad5768 | 2014-07-26 17:09:56 -0400 | [diff] [blame] | 354 | @Override |
| 355 | public void onBuzzBeepBlinked() { |
| 356 | if (DEBUG) Log.d(mTag, "onBuzzBeepBlinked"); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 357 | updateNotificationPulse(); |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 358 | } |
| 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 Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 365 | if (mNotificationLightOn) { |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 366 | updateNotificationPulse(); |
John Spurlock | d06aa57 | 2014-09-10 10:40:49 -0400 | [diff] [blame] | 367 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 368 | } |
John Spurlock | d96179e | 2014-08-21 16:43:45 -0400 | [diff] [blame] | 369 | |
| 370 | @Override |
| 371 | public void onPowerSaveChanged(boolean active) { |
| 372 | mPowerSaveActive = active; |
| 373 | if (mPowerSaveActive && mDreaming) { |
| 374 | finishToSavePower(); |
| 375 | } |
| 376 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 377 | }; |
| 378 | |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 379 | private class TriggerSensor extends TriggerEventListener { |
| 380 | private final Sensor mSensor; |
| 381 | private final boolean mConfigured; |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 382 | private final boolean mDebugVibrate; |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 383 | |
| 384 | private boolean mEnabled; |
| 385 | |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 386 | public TriggerSensor(int type, boolean configured, boolean debugVibrate) { |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 387 | mSensor = mSensors.getDefaultSensor(type); |
John Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 388 | mConfigured = configured; |
| 389 | mDebugVibrate = debugVibrate; |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 390 | } |
| 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 Spurlock | 190d026 | 2014-09-14 15:39:13 -0400 | [diff] [blame] | 405 | .append(mConfigured).append(", mDebugVibrate=").append(mDebugVibrate) |
| 406 | .append(", mSensor=").append(mSensor).append("}").toString(); |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 407 | } |
| 408 | |
| 409 | @Override |
| 410 | public void onTrigger(TriggerEvent event) { |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 411 | 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 Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 422 | } |
John Spurlock | 50a8ea6 | 2014-09-16 09:12:03 -0400 | [diff] [blame] | 423 | |
Jeff Brown | 4d69e22 | 2014-09-18 15:27:50 -0700 | [diff] [blame] | 424 | 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 Spurlock | 813552c | 2014-09-19 08:30:21 -0400 | [diff] [blame] | 442 | } |
John Spurlock | 559d959 | 2014-08-09 12:04:36 -0400 | [diff] [blame] | 443 | } |
| 444 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 445 | } |