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 | |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame] | 19 | import android.content.Context; |
Adrian Roos | 4fb1f51 | 2017-02-14 14:01:32 +0100 | [diff] [blame] | 20 | import android.os.PowerManager; |
| 21 | import android.os.SystemClock; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 22 | import android.service.dreams.DreamService; |
| 23 | import android.util.Log; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 24 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 25 | import com.android.systemui.Dependency; |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame] | 26 | import com.android.systemui.plugins.DozeServicePlugin; |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame] | 27 | import com.android.systemui.plugins.DozeServicePlugin.RequestDoze; |
Dave Mankoff | 781ef7e | 2019-06-28 16:33:25 -0400 | [diff] [blame] | 28 | import com.android.systemui.plugins.FalsingManager; |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame] | 29 | import com.android.systemui.plugins.PluginListener; |
Tony Wickham | 023cb19 | 2018-09-13 15:23:04 -0700 | [diff] [blame] | 30 | import com.android.systemui.shared.plugins.PluginManager; |
Lucas Dupin | 7517b5d | 2017-08-22 12:51:25 -0700 | [diff] [blame] | 31 | |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 32 | import java.io.FileDescriptor; |
| 33 | import java.io.PrintWriter; |
| 34 | |
Dave Mankoff | 1a0e382 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 35 | import javax.inject.Inject; |
| 36 | |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame] | 37 | public class DozeService extends DreamService |
| 38 | implements DozeMachine.Service, RequestDoze, PluginListener<DozeServicePlugin> { |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 39 | private static final String TAG = "DozeService"; |
Adrian Roos | ea8d6ae | 2016-10-26 10:40:12 -0700 | [diff] [blame] | 40 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 41 | |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 42 | private DozeMachine mDozeMachine; |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame] | 43 | private DozeServicePlugin mDozePlugin; |
Lucas Dupin | c8a0135 | 2018-11-16 11:31:33 -0800 | [diff] [blame] | 44 | private PluginManager mPluginManager; |
Adrian Roos | b7e4e10 | 2016-10-14 13:03:45 -0700 | [diff] [blame] | 45 | |
Dave Mankoff | 1a0e382 | 2019-07-03 13:26:55 -0400 | [diff] [blame] | 46 | @Inject |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 47 | public DozeService() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 48 | setDebug(DEBUG); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public void onCreate() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 53 | super.onCreate(); |
| 54 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 55 | setWindowless(true); |
| 56 | |
Adrian Roos | 0b2e23a | 2017-01-03 12:13:49 -0800 | [diff] [blame] | 57 | if (DozeFactory.getHost(this) == null) { |
| 58 | finish(); |
| 59 | return; |
| 60 | } |
Lucas Dupin | c8a0135 | 2018-11-16 11:31:33 -0800 | [diff] [blame] | 61 | mPluginManager = Dependency.get(PluginManager.class); |
| 62 | mPluginManager.addPluginListener(this, DozeServicePlugin.class, false /* allowMultiple */); |
Dave Mankoff | 781ef7e | 2019-06-28 16:33:25 -0400 | [diff] [blame] | 63 | mDozeMachine = new DozeFactory().assembleMachine( |
| 64 | this, Dependency.get(FalsingManager.class)); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | @Override |
Dan Sandler | c6dff3d | 2018-02-14 22:46:25 -0500 | [diff] [blame] | 68 | public void onDestroy() { |
Lucas Dupin | e382315 | 2019-03-18 16:15:28 -0700 | [diff] [blame] | 69 | if (mPluginManager != null) { |
| 70 | mPluginManager.removePluginListener(this); |
| 71 | } |
Dan Sandler | c6dff3d | 2018-02-14 22:46:25 -0500 | [diff] [blame] | 72 | super.onDestroy(); |
Adrian Roos | 2f5a385 | 2018-04-23 17:48:08 +0200 | [diff] [blame] | 73 | mDozeMachine = null; |
Dan Sandler | c6dff3d | 2018-02-14 22:46:25 -0500 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame] | 77 | public void onPluginConnected(DozeServicePlugin plugin, Context pluginContext) { |
| 78 | mDozePlugin = plugin; |
| 79 | mDozePlugin.setDozeRequester(this); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public void onPluginDisconnected(DozeServicePlugin plugin) { |
| 84 | if (mDozePlugin != null) { |
| 85 | mDozePlugin.onDreamingStopped(); |
| 86 | mDozePlugin = null; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @Override |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 91 | public void onDreamingStarted() { |
| 92 | super.onDreamingStarted(); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 93 | mDozeMachine.requestState(DozeMachine.State.INITIALIZED); |
| 94 | startDozing(); |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame] | 95 | if (mDozePlugin != null) { |
| 96 | mDozePlugin.onDreamingStarted(); |
| 97 | } |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 98 | } |
| 99 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 100 | @Override |
| 101 | public void onDreamingStopped() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 102 | super.onDreamingStopped(); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 103 | mDozeMachine.requestState(DozeMachine.State.FINISH); |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame] | 104 | if (mDozePlugin != null) { |
| 105 | mDozePlugin.onDreamingStopped(); |
| 106 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 107 | } |
| 108 | |
Adrian Roos | ea8d6ae | 2016-10-26 10:40:12 -0700 | [diff] [blame] | 109 | @Override |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 110 | protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) { |
Lucas Dupin | 9bece0d | 2017-11-06 15:46:50 -0800 | [diff] [blame] | 111 | super.dumpOnHandler(fd, pw, args); |
Adrian Roos | 0b2e23a | 2017-01-03 12:13:49 -0800 | [diff] [blame] | 112 | if (mDozeMachine != null) { |
| 113 | mDozeMachine.dump(pw); |
| 114 | } |
John Spurlock | 92b8d41 | 2014-10-03 17:12:40 -0400 | [diff] [blame] | 115 | } |
Adrian Roos | 4fb1f51 | 2017-02-14 14:01:32 +0100 | [diff] [blame] | 116 | |
| 117 | @Override |
| 118 | public void requestWakeUp() { |
| 119 | PowerManager pm = getSystemService(PowerManager.class); |
Michael Wright | e300104 | 2019-02-05 00:13:14 +0000 | [diff] [blame] | 120 | pm.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_GESTURE, |
| 121 | "com.android.systemui:NODOZE"); |
Adrian Roos | 4fb1f51 | 2017-02-14 14:01:32 +0100 | [diff] [blame] | 122 | } |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame] | 123 | |
| 124 | @Override |
| 125 | public void onRequestShowDoze() { |
| 126 | if (mDozeMachine != null) { |
| 127 | mDozeMachine.requestState(DozeMachine.State.DOZE_AOD); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | @Override |
| 132 | public void onRequestHideDoze() { |
| 133 | if (mDozeMachine != null) { |
| 134 | mDozeMachine.requestState(DozeMachine.State.DOZE); |
| 135 | } |
| 136 | } |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 137 | } |