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