John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.doze; |
| 18 | |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame^] | 19 | import android.content.Context; |
Adrian Roos | 4fb1f51 | 2017-02-14 14:01:32 +0100 | [diff] [blame] | 20 | import android.os.PowerManager; |
| 21 | import android.os.SystemClock; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 22 | import android.service.dreams.DreamService; |
| 23 | import android.util.Log; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 24 | |
Jason Monk | de850bb | 2017-02-01 19:26:30 -0500 | [diff] [blame] | 25 | import com.android.systemui.Dependency; |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame^] | 26 | import com.android.systemui.plugins.DozeServicePlugin; |
Jason Monk | 59d86ed | 2017-01-04 15:13:11 -0500 | [diff] [blame] | 27 | import com.android.systemui.plugins.PluginManager; |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame^] | 28 | import com.android.systemui.plugins.DozeServicePlugin.RequestDoze; |
| 29 | import com.android.systemui.plugins.PluginListener; |
John Spurlock | 6612727 | 2014-06-28 11:27:17 -0400 | [diff] [blame] | 30 | import java.io.FileDescriptor; |
| 31 | import java.io.PrintWriter; |
| 32 | |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame^] | 33 | public class DozeService extends DreamService |
| 34 | implements DozeMachine.Service, RequestDoze, PluginListener<DozeServicePlugin> { |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 35 | private static final String TAG = "DozeService"; |
Adrian Roos | ea8d6ae | 2016-10-26 10:40:12 -0700 | [diff] [blame] | 36 | static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 37 | |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 38 | private DozeMachine mDozeMachine; |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame^] | 39 | private DozeServicePlugin mDozePlugin; |
Adrian Roos | b7e4e10 | 2016-10-14 13:03:45 -0700 | [diff] [blame] | 40 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 41 | public DozeService() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 42 | setDebug(DEBUG); |
| 43 | } |
| 44 | |
| 45 | @Override |
| 46 | public void onCreate() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 47 | super.onCreate(); |
| 48 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 49 | setWindowless(true); |
| 50 | |
Adrian Roos | 0b2e23a | 2017-01-03 12:13:49 -0800 | [diff] [blame] | 51 | if (DozeFactory.getHost(this) == null) { |
| 52 | finish(); |
| 53 | return; |
| 54 | } |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame^] | 55 | Dependency.get(PluginManager.class).addPluginListener(this, |
| 56 | DozeServicePlugin.class, false /* Allow multiple */); |
Adrian Roos | 664c9d7 | 2017-04-28 15:52:24 -0700 | [diff] [blame] | 57 | mDozeMachine = new DozeFactory().assembleMachine(this); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @Override |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame^] | 61 | 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 Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 75 | public void onDreamingStarted() { |
| 76 | super.onDreamingStarted(); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 77 | mDozeMachine.requestState(DozeMachine.State.INITIALIZED); |
| 78 | startDozing(); |
Adrian Roos | 2324d85 | 2017-04-27 15:50:14 -0700 | [diff] [blame] | 79 | setDozeScreenBrightness(getResources().getInteger( |
| 80 | com.android.internal.R.integer.config_screenBrightnessDoze)); |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame^] | 81 | if (mDozePlugin != null) { |
| 82 | mDozePlugin.onDreamingStarted(); |
| 83 | } |
John Spurlock | ed69bd6 | 2014-07-23 11:09:02 -0400 | [diff] [blame] | 84 | } |
| 85 | |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 86 | @Override |
| 87 | public void onDreamingStopped() { |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 88 | super.onDreamingStopped(); |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 89 | mDozeMachine.requestState(DozeMachine.State.FINISH); |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame^] | 90 | if (mDozePlugin != null) { |
| 91 | mDozePlugin.onDreamingStopped(); |
| 92 | } |
John Spurlock | cb566aa | 2014-08-03 22:58:28 -0400 | [diff] [blame] | 93 | } |
| 94 | |
Adrian Roos | ea8d6ae | 2016-10-26 10:40:12 -0700 | [diff] [blame] | 95 | @Override |
Adrian Roos | ff2c456 | 2016-11-03 12:13:36 -0700 | [diff] [blame] | 96 | protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) { |
Adrian Roos | 0b2e23a | 2017-01-03 12:13:49 -0800 | [diff] [blame] | 97 | if (mDozeMachine != null) { |
| 98 | mDozeMachine.dump(pw); |
| 99 | } |
John Spurlock | 92b8d41 | 2014-10-03 17:12:40 -0400 | [diff] [blame] | 100 | } |
Adrian Roos | 4fb1f51 | 2017-02-14 14:01:32 +0100 | [diff] [blame] | 101 | |
| 102 | @Override |
| 103 | public void requestWakeUp() { |
| 104 | PowerManager pm = getSystemService(PowerManager.class); |
| 105 | pm.wakeUp(SystemClock.uptimeMillis(), "com.android.systemui:NODOZE"); |
| 106 | } |
Mady Mellor | f33a097 | 2017-06-13 08:16:56 -0700 | [diff] [blame^] | 107 | |
| 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 Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 121 | } |