blob: 2db73065fe1973441e857fd36b0651194fffe32d [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 Dupine3823152019-03-18 16:15:28 -070064 if (mPluginManager != null) {
65 mPluginManager.removePluginListener(this);
66 }
Dan Sandlerc6dff3d2018-02-14 22:46:25 -050067 super.onDestroy();
Adrian Roos2f5a3852018-04-23 17:48:08 +020068 mDozeMachine = null;
Dan Sandlerc6dff3d2018-02-14 22:46:25 -050069 }
70
71 @Override
Mady Mellorf33a0972017-06-13 08:16:56 -070072 public void onPluginConnected(DozeServicePlugin plugin, Context pluginContext) {
73 mDozePlugin = plugin;
74 mDozePlugin.setDozeRequester(this);
75 }
76
77 @Override
78 public void onPluginDisconnected(DozeServicePlugin plugin) {
79 if (mDozePlugin != null) {
80 mDozePlugin.onDreamingStopped();
81 mDozePlugin = null;
82 }
83 }
84
85 @Override
John Spurlockbf370992014-06-17 13:58:31 -040086 public void onDreamingStarted() {
87 super.onDreamingStarted();
Adrian Roosff2c4562016-11-03 12:13:36 -070088 mDozeMachine.requestState(DozeMachine.State.INITIALIZED);
89 startDozing();
Mady Mellorf33a0972017-06-13 08:16:56 -070090 if (mDozePlugin != null) {
91 mDozePlugin.onDreamingStarted();
92 }
John Spurlocked69bd62014-07-23 11:09:02 -040093 }
94
John Spurlockbf370992014-06-17 13:58:31 -040095 @Override
96 public void onDreamingStopped() {
John Spurlockbf370992014-06-17 13:58:31 -040097 super.onDreamingStopped();
Adrian Roosff2c4562016-11-03 12:13:36 -070098 mDozeMachine.requestState(DozeMachine.State.FINISH);
Mady Mellorf33a0972017-06-13 08:16:56 -070099 if (mDozePlugin != null) {
100 mDozePlugin.onDreamingStopped();
101 }
John Spurlockcb566aa2014-08-03 22:58:28 -0400102 }
103
Adrian Roosea8d6ae2016-10-26 10:40:12 -0700104 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -0700105 protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) {
Lucas Dupin9bece0d2017-11-06 15:46:50 -0800106 super.dumpOnHandler(fd, pw, args);
Adrian Roos0b2e23a2017-01-03 12:13:49 -0800107 if (mDozeMachine != null) {
108 mDozeMachine.dump(pw);
109 }
John Spurlock92b8d412014-10-03 17:12:40 -0400110 }
Adrian Roos4fb1f512017-02-14 14:01:32 +0100111
112 @Override
113 public void requestWakeUp() {
114 PowerManager pm = getSystemService(PowerManager.class);
Michael Wrighte3001042019-02-05 00:13:14 +0000115 pm.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_GESTURE,
116 "com.android.systemui:NODOZE");
Adrian Roos4fb1f512017-02-14 14:01:32 +0100117 }
Mady Mellorf33a0972017-06-13 08:16:56 -0700118
119 @Override
120 public void onRequestShowDoze() {
121 if (mDozeMachine != null) {
122 mDozeMachine.requestState(DozeMachine.State.DOZE_AOD);
123 }
124 }
125
126 @Override
127 public void onRequestHideDoze() {
128 if (mDozeMachine != null) {
129 mDozeMachine.requestState(DozeMachine.State.DOZE);
130 }
131 }
John Spurlockbf370992014-06-17 13:58:31 -0400132}