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