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