blob: c61e10aa77ab3367de5e937fc826c99c99856af2 [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;
Adrian Roosb7e4e102016-10-14 13:03:45 -070041
John Spurlockbf370992014-06-17 13:58:31 -040042 public DozeService() {
John Spurlockbf370992014-06-17 13:58:31 -040043 setDebug(DEBUG);
44 }
45
46 @Override
47 public void onCreate() {
John Spurlockbf370992014-06-17 13:58:31 -040048 super.onCreate();
49
John Spurlockbf370992014-06-17 13:58:31 -040050 setWindowless(true);
51
Adrian Roos0b2e23a2017-01-03 12:13:49 -080052 if (DozeFactory.getHost(this) == null) {
53 finish();
54 return;
55 }
Mady Mellorf33a0972017-06-13 08:16:56 -070056 Dependency.get(PluginManager.class).addPluginListener(this,
57 DozeServicePlugin.class, false /* Allow multiple */);
Adrian Roos664c9d72017-04-28 15:52:24 -070058 mDozeMachine = new DozeFactory().assembleMachine(this);
John Spurlockbf370992014-06-17 13:58:31 -040059 }
60
61 @Override
Dan Sandlerc6dff3d2018-02-14 22:46:25 -050062 public void onDestroy() {
63 Dependency.get(PluginManager.class).removePluginListener(this);
64 super.onDestroy();
Adrian Roos2f5a3852018-04-23 17:48:08 +020065 mDozeMachine = null;
Dan Sandlerc6dff3d2018-02-14 22:46:25 -050066 }
67
68 @Override
Mady Mellorf33a0972017-06-13 08:16:56 -070069 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 Spurlockbf370992014-06-17 13:58:31 -040083 public void onDreamingStarted() {
84 super.onDreamingStarted();
Adrian Roosff2c4562016-11-03 12:13:36 -070085 mDozeMachine.requestState(DozeMachine.State.INITIALIZED);
86 startDozing();
Mady Mellorf33a0972017-06-13 08:16:56 -070087 if (mDozePlugin != null) {
88 mDozePlugin.onDreamingStarted();
89 }
John Spurlocked69bd62014-07-23 11:09:02 -040090 }
91
John Spurlockbf370992014-06-17 13:58:31 -040092 @Override
93 public void onDreamingStopped() {
John Spurlockbf370992014-06-17 13:58:31 -040094 super.onDreamingStopped();
Adrian Roosff2c4562016-11-03 12:13:36 -070095 mDozeMachine.requestState(DozeMachine.State.FINISH);
Mady Mellorf33a0972017-06-13 08:16:56 -070096 if (mDozePlugin != null) {
97 mDozePlugin.onDreamingStopped();
98 }
John Spurlockcb566aa2014-08-03 22:58:28 -040099 }
100
Adrian Roosea8d6ae2016-10-26 10:40:12 -0700101 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -0700102 protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) {
Lucas Dupin9bece0d2017-11-06 15:46:50 -0800103 super.dumpOnHandler(fd, pw, args);
Adrian Roos0b2e23a2017-01-03 12:13:49 -0800104 if (mDozeMachine != null) {
105 mDozeMachine.dump(pw);
106 }
John Spurlock92b8d412014-10-03 17:12:40 -0400107 }
Adrian Roos4fb1f512017-02-14 14:01:32 +0100108
109 @Override
110 public void requestWakeUp() {
111 PowerManager pm = getSystemService(PowerManager.class);
112 pm.wakeUp(SystemClock.uptimeMillis(), "com.android.systemui:NODOZE");
113 }
Mady Mellorf33a0972017-06-13 08:16:56 -0700114
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 Spurlockbf370992014-06-17 13:58:31 -0400128}