blob: 4557b4de4901d1bc0a678e533db4a805ea42c101 [file] [log] [blame]
John Spurlockbf370992014-06-17 13:58:31 -04001/*
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
17package com.android.systemui.doze;
18
Mady Mellorf33a0972017-06-13 08:16:56 -070019import android.content.Context;
Adrian Roos4fb1f512017-02-14 14:01:32 +010020import android.os.PowerManager;
21import android.os.SystemClock;
John Spurlockbf370992014-06-17 13:58:31 -040022import android.service.dreams.DreamService;
23import android.util.Log;
John Spurlockbf370992014-06-17 13:58:31 -040024
Jason Monkde850bb2017-02-01 19:26:30 -050025import com.android.systemui.Dependency;
Mady Mellorf33a0972017-06-13 08:16:56 -070026import com.android.systemui.plugins.DozeServicePlugin;
Mady Mellorf33a0972017-06-13 08:16:56 -070027import com.android.systemui.plugins.DozeServicePlugin.RequestDoze;
28import com.android.systemui.plugins.PluginListener;
Tony Wickham023cb192018-09-13 15:23:04 -070029import com.android.systemui.shared.plugins.PluginManager;
Lucas Dupin7517b5d2017-08-22 12:51:25 -070030
John Spurlock66127272014-06-28 11:27:17 -040031import java.io.FileDescriptor;
32import java.io.PrintWriter;
33
Mady Mellorf33a0972017-06-13 08:16:56 -070034public class DozeService extends DreamService
35 implements DozeMachine.Service, RequestDoze, PluginListener<DozeServicePlugin> {
John Spurlocked69bd62014-07-23 11:09:02 -040036 private static final String TAG = "DozeService";
Adrian Roosea8d6ae2016-10-26 10:40:12 -070037 static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
John Spurlockbf370992014-06-17 13:58:31 -040038
Adrian Roosff2c4562016-11-03 12:13:36 -070039 private DozeMachine mDozeMachine;
Mady Mellorf33a0972017-06-13 08:16:56 -070040 private DozeServicePlugin mDozePlugin;
Lucas Dupinc8a01352018-11-16 11:31:33 -080041 private PluginManager mPluginManager;
Adrian Roosb7e4e102016-10-14 13:03:45 -070042
John Spurlockbf370992014-06-17 13:58:31 -040043 public DozeService() {
John Spurlockbf370992014-06-17 13:58:31 -040044 setDebug(DEBUG);
45 }
46
47 @Override
48 public void onCreate() {
John Spurlockbf370992014-06-17 13:58:31 -040049 super.onCreate();
50
John Spurlockbf370992014-06-17 13:58:31 -040051 setWindowless(true);
52
Adrian Roos0b2e23a2017-01-03 12:13:49 -080053 if (DozeFactory.getHost(this) == null) {
54 finish();
55 return;
56 }
Lucas Dupinc8a01352018-11-16 11:31:33 -080057 mPluginManager = Dependency.get(PluginManager.class);
58 mPluginManager.addPluginListener(this, DozeServicePlugin.class, false /* allowMultiple */);
Adrian Roos664c9d72017-04-28 15:52:24 -070059 mDozeMachine = new DozeFactory().assembleMachine(this);
John Spurlockbf370992014-06-17 13:58:31 -040060 }
61
62 @Override
Dan Sandlerc6dff3d2018-02-14 22:46:25 -050063 public void onDestroy() {
Lucas Dupinc8a01352018-11-16 11:31:33 -080064 mPluginManager.removePluginListener(this);
Dan Sandlerc6dff3d2018-02-14 22:46:25 -050065 super.onDestroy();
Adrian Roos2f5a3852018-04-23 17:48:08 +020066 mDozeMachine = null;
Dan Sandlerc6dff3d2018-02-14 22:46:25 -050067 }
68
69 @Override
Mady Mellorf33a0972017-06-13 08:16:56 -070070 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 Spurlockbf370992014-06-17 13:58:31 -040084 public void onDreamingStarted() {
85 super.onDreamingStarted();
Adrian Roosff2c4562016-11-03 12:13:36 -070086 mDozeMachine.requestState(DozeMachine.State.INITIALIZED);
87 startDozing();
Mady Mellorf33a0972017-06-13 08:16:56 -070088 if (mDozePlugin != null) {
89 mDozePlugin.onDreamingStarted();
90 }
John Spurlocked69bd62014-07-23 11:09:02 -040091 }
92
John Spurlockbf370992014-06-17 13:58:31 -040093 @Override
94 public void onDreamingStopped() {
John Spurlockbf370992014-06-17 13:58:31 -040095 super.onDreamingStopped();
Adrian Roosff2c4562016-11-03 12:13:36 -070096 mDozeMachine.requestState(DozeMachine.State.FINISH);
Mady Mellorf33a0972017-06-13 08:16:56 -070097 if (mDozePlugin != null) {
98 mDozePlugin.onDreamingStopped();
99 }
John Spurlockcb566aa2014-08-03 22:58:28 -0400100 }
101
Adrian Roosea8d6ae2016-10-26 10:40:12 -0700102 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -0700103 protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) {
Lucas Dupin9bece0d2017-11-06 15:46:50 -0800104 super.dumpOnHandler(fd, pw, args);
Adrian Roos0b2e23a2017-01-03 12:13:49 -0800105 if (mDozeMachine != null) {
106 mDozeMachine.dump(pw);
107 }
John Spurlock92b8d412014-10-03 17:12:40 -0400108 }
Adrian Roos4fb1f512017-02-14 14:01:32 +0100109
110 @Override
111 public void requestWakeUp() {
112 PowerManager pm = getSystemService(PowerManager.class);
113 pm.wakeUp(SystemClock.uptimeMillis(), "com.android.systemui:NODOZE");
114 }
Mady Mellorf33a0972017-06-13 08:16:56 -0700115
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 Spurlockbf370992014-06-17 13:58:31 -0400129}