blob: 34d392861d7ca26001538b609eec4f49384b3801 [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;
Lucas Dupin7517b5d2017-08-22 12:51:25 -070029import com.android.systemui.plugins.PluginManager;
30
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
Mady Mellorf33a0972017-06-13 08:16:56 -070062 public void onPluginConnected(DozeServicePlugin plugin, Context pluginContext) {
63 mDozePlugin = plugin;
64 mDozePlugin.setDozeRequester(this);
65 }
66
67 @Override
68 public void onPluginDisconnected(DozeServicePlugin plugin) {
69 if (mDozePlugin != null) {
70 mDozePlugin.onDreamingStopped();
71 mDozePlugin = null;
72 }
73 }
74
75 @Override
John Spurlockbf370992014-06-17 13:58:31 -040076 public void onDreamingStarted() {
77 super.onDreamingStarted();
Adrian Roosff2c4562016-11-03 12:13:36 -070078 mDozeMachine.requestState(DozeMachine.State.INITIALIZED);
79 startDozing();
Mady Mellorf33a0972017-06-13 08:16:56 -070080 if (mDozePlugin != null) {
81 mDozePlugin.onDreamingStarted();
82 }
John Spurlocked69bd62014-07-23 11:09:02 -040083 }
84
John Spurlockbf370992014-06-17 13:58:31 -040085 @Override
86 public void onDreamingStopped() {
John Spurlockbf370992014-06-17 13:58:31 -040087 super.onDreamingStopped();
Adrian Roosff2c4562016-11-03 12:13:36 -070088 mDozeMachine.requestState(DozeMachine.State.FINISH);
Mady Mellorf33a0972017-06-13 08:16:56 -070089 if (mDozePlugin != null) {
90 mDozePlugin.onDreamingStopped();
91 }
John Spurlockcb566aa2014-08-03 22:58:28 -040092 }
93
Adrian Roosea8d6ae2016-10-26 10:40:12 -070094 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -070095 protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) {
Lucas Dupin9bece0d2017-11-06 15:46:50 -080096 super.dumpOnHandler(fd, pw, args);
Adrian Roos0b2e23a2017-01-03 12:13:49 -080097 if (mDozeMachine != null) {
98 mDozeMachine.dump(pw);
99 }
John Spurlock92b8d412014-10-03 17:12:40 -0400100 }
Adrian Roos4fb1f512017-02-14 14:01:32 +0100101
102 @Override
103 public void requestWakeUp() {
104 PowerManager pm = getSystemService(PowerManager.class);
105 pm.wakeUp(SystemClock.uptimeMillis(), "com.android.systemui:NODOZE");
106 }
Mady Mellorf33a0972017-06-13 08:16:56 -0700107
108 @Override
109 public void onRequestShowDoze() {
110 if (mDozeMachine != null) {
111 mDozeMachine.requestState(DozeMachine.State.DOZE_AOD);
112 }
113 }
114
115 @Override
116 public void onRequestHideDoze() {
117 if (mDozeMachine != null) {
118 mDozeMachine.requestState(DozeMachine.State.DOZE);
119 }
120 }
John Spurlockbf370992014-06-17 13:58:31 -0400121}