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 19 import android.content.Context; 20 import android.content.res.Configuration; 21 import android.os.PowerManager; 22 import android.os.SystemClock; 23 import android.service.dreams.DreamService; 24 import android.util.Log; 25 26 import com.android.systemui.dagger.qualifiers.UiBackground; 27 import com.android.systemui.doze.dagger.DozeComponent; 28 import com.android.systemui.plugins.DozeServicePlugin; 29 import com.android.systemui.plugins.DozeServicePlugin.RequestDoze; 30 import com.android.systemui.plugins.PluginListener; 31 import com.android.systemui.plugins.PluginManager; 32 33 import java.io.FileDescriptor; 34 import java.io.PrintWriter; 35 import java.util.concurrent.Executor; 36 37 import javax.inject.Inject; 38 39 public class DozeService extends DreamService 40 implements DozeMachine.Service, RequestDoze, PluginListener<DozeServicePlugin> { 41 private static final String TAG = "DozeService"; 42 static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 43 private final DozeComponent.Builder mDozeComponentBuilder; 44 45 private DozeMachine mDozeMachine; 46 private DozeServicePlugin mDozePlugin; 47 private PluginManager mPluginManager; 48 private DozeLog mDozeLog; 49 private Executor mBgExecutor; 50 51 @Inject DozeService(DozeComponent.Builder dozeComponentBuilder, PluginManager pluginManager, DozeLog dozeLog, @UiBackground Executor bgExecutor)52 public DozeService(DozeComponent.Builder dozeComponentBuilder, PluginManager pluginManager, 53 DozeLog dozeLog, @UiBackground Executor bgExecutor) { 54 mDozeLog = dozeLog; 55 mBgExecutor = bgExecutor; 56 mDozeComponentBuilder = dozeComponentBuilder; 57 setDebug(DEBUG); 58 mPluginManager = pluginManager; 59 } 60 61 @Override onCreate()62 public void onCreate() { 63 super.onCreate(); 64 65 setWindowless(true); 66 67 mPluginManager.addPluginListener(this, DozeServicePlugin.class, false /* allowMultiple */); 68 DozeComponent dozeComponent = mDozeComponentBuilder.build(this); 69 mDozeMachine = dozeComponent.getDozeMachine(); 70 mDozeMachine.onConfigurationChanged(getResources().getConfiguration()); 71 } 72 73 @Override onDestroy()74 public void onDestroy() { 75 if (mPluginManager != null) { 76 mPluginManager.removePluginListener(this); 77 } 78 super.onDestroy(); 79 mDozeMachine.destroy(); 80 mDozeMachine = null; 81 } 82 83 @Override onPluginConnected(DozeServicePlugin plugin, Context pluginContext)84 public void onPluginConnected(DozeServicePlugin plugin, Context pluginContext) { 85 mDozePlugin = plugin; 86 mDozePlugin.setDozeRequester(this); 87 } 88 89 @Override onPluginDisconnected(DozeServicePlugin plugin)90 public void onPluginDisconnected(DozeServicePlugin plugin) { 91 if (mDozePlugin != null) { 92 mDozePlugin.onDreamingStopped(); 93 mDozePlugin = null; 94 } 95 } 96 97 @Override onDreamingStarted()98 public void onDreamingStarted() { 99 super.onDreamingStarted(); 100 mDozeMachine.requestState(DozeMachine.State.INITIALIZED); 101 startDozing(); 102 if (mDozePlugin != null) { 103 mDozePlugin.onDreamingStarted(); 104 } 105 } 106 107 @Override onDreamingStopped()108 public void onDreamingStopped() { 109 super.onDreamingStopped(); 110 mDozeMachine.requestState(DozeMachine.State.FINISH); 111 if (mDozePlugin != null) { 112 mDozePlugin.onDreamingStopped(); 113 } 114 } 115 116 @Override dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args)117 protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) { 118 super.dumpOnHandler(fd, pw, args); 119 if (mDozeMachine != null) { 120 mDozeMachine.dump(pw); 121 } 122 } 123 124 @Override requestWakeUp(@ozeLog.Reason int reason)125 public void requestWakeUp(@DozeLog.Reason int reason) { 126 final PowerManager pm = getSystemService(PowerManager.class); 127 pm.wakeUp(SystemClock.uptimeMillis(), DozeLog.getPowerManagerWakeReason(reason), 128 "com.android.systemui:NODOZE " + DozeLog.reasonToString(reason)); 129 } 130 131 @Override onRequestShowDoze()132 public void onRequestShowDoze() { 133 if (mDozeMachine != null) { 134 mDozeMachine.requestState(DozeMachine.State.DOZE_AOD); 135 } 136 } 137 138 @Override onConfigurationChanged(Configuration newConfig)139 public void onConfigurationChanged(Configuration newConfig) { 140 super.onConfigurationChanged(newConfig); 141 mDozeMachine.onConfigurationChanged(newConfig); 142 } 143 144 @Override onRequestHideDoze()145 public void onRequestHideDoze() { 146 if (mDozeMachine != null) { 147 mDozeMachine.requestState(DozeMachine.State.DOZE); 148 } 149 } 150 151 @Override setDozeScreenState(int state)152 public void setDozeScreenState(int state) { 153 mDozeLog.traceDisplayState(state, /* afterRequest */ false); 154 super.setDozeScreenState(state); 155 mDozeLog.traceDisplayState(state, /* afterRequest */ true); 156 if (mDozeMachine != null) { 157 mDozeMachine.onScreenState(state); 158 } 159 } 160 161 @Override setDozeScreenBrightness(int brightness)162 public void setDozeScreenBrightness(int brightness) { 163 mBgExecutor.execute(() -> { 164 mDozeLog.traceDozeScreenBrightness(brightness, /* afterRequest */ false); 165 super.setDozeScreenBrightness(brightness); 166 mDozeLog.traceDozeScreenBrightness(brightness, /* afterRequest */ true); 167 }); 168 } 169 170 @Override setDozeScreenBrightnessFloat(float brightness)171 public void setDozeScreenBrightnessFloat(float brightness) { 172 mBgExecutor.execute(() -> { 173 mDozeLog.traceDozeScreenBrightnessFloat(brightness, /* afterRequest */ false); 174 super.setDozeScreenBrightnessFloat(brightness); 175 mDozeLog.traceDozeScreenBrightnessFloat(brightness, /* afterRequest */ true); 176 }); 177 } 178 } 179