blob: d9fb087a6041b74ca778e8528a2103a7d1525119 [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;
Jason Monk59d86ed2017-01-04 15:13:11 -050027import com.android.systemui.plugins.PluginManager;
Mady Mellorf33a0972017-06-13 08:16:56 -070028import com.android.systemui.plugins.DozeServicePlugin.RequestDoze;
29import com.android.systemui.plugins.PluginListener;
John Spurlock66127272014-06-28 11:27:17 -040030import java.io.FileDescriptor;
31import java.io.PrintWriter;
32
Mady Mellorf33a0972017-06-13 08:16:56 -070033public class DozeService extends DreamService
34 implements DozeMachine.Service, RequestDoze, PluginListener<DozeServicePlugin> {
John Spurlocked69bd62014-07-23 11:09:02 -040035 private static final String TAG = "DozeService";
Adrian Roosea8d6ae2016-10-26 10:40:12 -070036 static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
John Spurlockbf370992014-06-17 13:58:31 -040037
Adrian Roosff2c4562016-11-03 12:13:36 -070038 private DozeMachine mDozeMachine;
Mady Mellorf33a0972017-06-13 08:16:56 -070039 private DozeServicePlugin mDozePlugin;
Adrian Roosb7e4e102016-10-14 13:03:45 -070040
John Spurlockbf370992014-06-17 13:58:31 -040041 public DozeService() {
John Spurlockbf370992014-06-17 13:58:31 -040042 setDebug(DEBUG);
43 }
44
45 @Override
46 public void onCreate() {
John Spurlockbf370992014-06-17 13:58:31 -040047 super.onCreate();
48
John Spurlockbf370992014-06-17 13:58:31 -040049 setWindowless(true);
50
Adrian Roos0b2e23a2017-01-03 12:13:49 -080051 if (DozeFactory.getHost(this) == null) {
52 finish();
53 return;
54 }
Mady Mellorf33a0972017-06-13 08:16:56 -070055 Dependency.get(PluginManager.class).addPluginListener(this,
56 DozeServicePlugin.class, false /* Allow multiple */);
Adrian Roos664c9d72017-04-28 15:52:24 -070057 mDozeMachine = new DozeFactory().assembleMachine(this);
John Spurlockbf370992014-06-17 13:58:31 -040058 }
59
60 @Override
Mady Mellorf33a0972017-06-13 08:16:56 -070061 public void onPluginConnected(DozeServicePlugin plugin, Context pluginContext) {
62 mDozePlugin = plugin;
63 mDozePlugin.setDozeRequester(this);
64 }
65
66 @Override
67 public void onPluginDisconnected(DozeServicePlugin plugin) {
68 if (mDozePlugin != null) {
69 mDozePlugin.onDreamingStopped();
70 mDozePlugin = null;
71 }
72 }
73
74 @Override
John Spurlockbf370992014-06-17 13:58:31 -040075 public void onDreamingStarted() {
76 super.onDreamingStarted();
Adrian Roosff2c4562016-11-03 12:13:36 -070077 mDozeMachine.requestState(DozeMachine.State.INITIALIZED);
78 startDozing();
Adrian Roos2324d852017-04-27 15:50:14 -070079 setDozeScreenBrightness(getResources().getInteger(
80 com.android.internal.R.integer.config_screenBrightnessDoze));
Mady Mellorf33a0972017-06-13 08:16:56 -070081 if (mDozePlugin != null) {
82 mDozePlugin.onDreamingStarted();
83 }
John Spurlocked69bd62014-07-23 11:09:02 -040084 }
85
John Spurlockbf370992014-06-17 13:58:31 -040086 @Override
87 public void onDreamingStopped() {
John Spurlockbf370992014-06-17 13:58:31 -040088 super.onDreamingStopped();
Adrian Roosff2c4562016-11-03 12:13:36 -070089 mDozeMachine.requestState(DozeMachine.State.FINISH);
Mady Mellorf33a0972017-06-13 08:16:56 -070090 if (mDozePlugin != null) {
91 mDozePlugin.onDreamingStopped();
92 }
John Spurlockcb566aa2014-08-03 22:58:28 -040093 }
94
Adrian Roosea8d6ae2016-10-26 10:40:12 -070095 @Override
Adrian Roosff2c4562016-11-03 12:13:36 -070096 protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] 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}