blob: 9e1514c31ab18a417193703639fb0ce183b0231d [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;
Dave Mankoff781ef7e2019-06-28 16:33:25 -040028import com.android.systemui.plugins.FalsingManager;
Mady Mellorf33a0972017-06-13 08:16:56 -070029import com.android.systemui.plugins.PluginListener;
Tony Wickham023cb192018-09-13 15:23:04 -070030import com.android.systemui.shared.plugins.PluginManager;
Lucas Dupin7517b5d2017-08-22 12:51:25 -070031
John Spurlock66127272014-06-28 11:27:17 -040032import java.io.FileDescriptor;
33import java.io.PrintWriter;
34
Dave Mankoff1a0e3822019-07-03 13:26:55 -040035import javax.inject.Inject;
36
Mady Mellorf33a0972017-06-13 08:16:56 -070037public class DozeService extends DreamService
38 implements DozeMachine.Service, RequestDoze, PluginListener<DozeServicePlugin> {
John Spurlocked69bd62014-07-23 11:09:02 -040039 private static final String TAG = "DozeService";
Adrian Roosea8d6ae2016-10-26 10:40:12 -070040 static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
John Spurlockbf370992014-06-17 13:58:31 -040041
Adrian Roosff2c4562016-11-03 12:13:36 -070042 private DozeMachine mDozeMachine;
Mady Mellorf33a0972017-06-13 08:16:56 -070043 private DozeServicePlugin mDozePlugin;
Lucas Dupinc8a01352018-11-16 11:31:33 -080044 private PluginManager mPluginManager;
Adrian Roosb7e4e102016-10-14 13:03:45 -070045
Dave Mankoff1a0e3822019-07-03 13:26:55 -040046 @Inject
John Spurlockbf370992014-06-17 13:58:31 -040047 public DozeService() {
John Spurlockbf370992014-06-17 13:58:31 -040048 setDebug(DEBUG);
49 }
50
51 @Override
52 public void onCreate() {
John Spurlockbf370992014-06-17 13:58:31 -040053 super.onCreate();
54
John Spurlockbf370992014-06-17 13:58:31 -040055 setWindowless(true);
56
Adrian Roos0b2e23a2017-01-03 12:13:49 -080057 if (DozeFactory.getHost(this) == null) {
58 finish();
59 return;
60 }
Lucas Dupinc8a01352018-11-16 11:31:33 -080061 mPluginManager = Dependency.get(PluginManager.class);
62 mPluginManager.addPluginListener(this, DozeServicePlugin.class, false /* allowMultiple */);
Dave Mankoff781ef7e2019-06-28 16:33:25 -040063 mDozeMachine = new DozeFactory().assembleMachine(
64 this, Dependency.get(FalsingManager.class));
John Spurlockbf370992014-06-17 13:58:31 -040065 }
66
67 @Override
Dan Sandlerc6dff3d2018-02-14 22:46:25 -050068 public void onDestroy() {
Lucas Dupine3823152019-03-18 16:15:28 -070069 if (mPluginManager != null) {
70 mPluginManager.removePluginListener(this);
71 }
Dan Sandlerc6dff3d2018-02-14 22:46:25 -050072 super.onDestroy();
Adrian Roos2f5a3852018-04-23 17:48:08 +020073 mDozeMachine = null;
Dan Sandlerc6dff3d2018-02-14 22:46:25 -050074 }
75
76 @Override
Mady Mellorf33a0972017-06-13 08:16:56 -070077 public void onPluginConnected(DozeServicePlugin plugin, Context pluginContext) {
78 mDozePlugin = plugin;
79 mDozePlugin.setDozeRequester(this);
80 }
81
82 @Override
83 public void onPluginDisconnected(DozeServicePlugin plugin) {
84 if (mDozePlugin != null) {
85 mDozePlugin.onDreamingStopped();
86 mDozePlugin = null;
87 }
88 }
89
90 @Override
John Spurlockbf370992014-06-17 13:58:31 -040091 public void onDreamingStarted() {
92 super.onDreamingStarted();
Adrian Roosff2c4562016-11-03 12:13:36 -070093 mDozeMachine.requestState(DozeMachine.State.INITIALIZED);
94 startDozing();
Mady Mellorf33a0972017-06-13 08:16:56 -070095 if (mDozePlugin != null) {
96 mDozePlugin.onDreamingStarted();
97 }
John Spurlocked69bd62014-07-23 11:09:02 -040098 }
99
John Spurlockbf370992014-06-17 13:58:31 -0400100 @Override
101 public void onDreamingStopped() {
John Spurlockbf370992014-06-17 13:58:31 -0400102 super.onDreamingStopped();
Adrian Roosff2c4562016-11-03 12:13:36 -0700103 mDozeMachine.requestState(DozeMachine.State.FINISH);
Mady Mellorf33a0972017-06-13 08:16:56 -0700104 if (mDozePlugin != null) {
105 mDozePlugin.onDreamingStopped();
106 }
John Spurlockcb566aa2014-08-03 22:58:28 -0400107 }
108
Adrian Roosea8d6ae2016-10-26 10:40:12 -0700109 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -0700110 protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) {
Lucas Dupin9bece0d2017-11-06 15:46:50 -0800111 super.dumpOnHandler(fd, pw, args);
Adrian Roos0b2e23a2017-01-03 12:13:49 -0800112 if (mDozeMachine != null) {
113 mDozeMachine.dump(pw);
114 }
John Spurlock92b8d412014-10-03 17:12:40 -0400115 }
Adrian Roos4fb1f512017-02-14 14:01:32 +0100116
117 @Override
118 public void requestWakeUp() {
119 PowerManager pm = getSystemService(PowerManager.class);
Michael Wrighte3001042019-02-05 00:13:14 +0000120 pm.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_GESTURE,
121 "com.android.systemui:NODOZE");
Adrian Roos4fb1f512017-02-14 14:01:32 +0100122 }
Mady Mellorf33a0972017-06-13 08:16:56 -0700123
124 @Override
125 public void onRequestShowDoze() {
126 if (mDozeMachine != null) {
127 mDozeMachine.requestState(DozeMachine.State.DOZE_AOD);
128 }
129 }
130
131 @Override
132 public void onRequestHideDoze() {
133 if (mDozeMachine != null) {
134 mDozeMachine.requestState(DozeMachine.State.DOZE);
135 }
136 }
John Spurlockbf370992014-06-17 13:58:31 -0400137}