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.os.PowerManager; 21 import android.os.SystemClock; 22 import android.service.dreams.DreamService; 23 import android.util.Log; 24 25 import com.android.systemui.doze.dagger.DozeComponent; 26 import com.android.systemui.plugins.DozeServicePlugin; 27 import com.android.systemui.plugins.DozeServicePlugin.RequestDoze; 28 import com.android.systemui.plugins.PluginListener; 29 import com.android.systemui.shared.plugins.PluginManager; 30 31 import java.io.FileDescriptor; 32 import java.io.PrintWriter; 33 34 import javax.inject.Inject; 35 36 public class DozeService extends DreamService 37 implements DozeMachine.Service, RequestDoze, PluginListener<DozeServicePlugin> { 38 private static final String TAG = "DozeService"; 39 static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); 40 private final DozeComponent.Builder mDozeComponentBuilder; 41 42 private DozeMachine mDozeMachine; 43 private DozeServicePlugin mDozePlugin; 44 private PluginManager mPluginManager; 45 46 @Inject DozeService(DozeComponent.Builder dozeComponentBuilder, PluginManager pluginManager)47 public DozeService(DozeComponent.Builder dozeComponentBuilder, PluginManager pluginManager) { 48 mDozeComponentBuilder = dozeComponentBuilder; 49 setDebug(DEBUG); 50 mPluginManager = pluginManager; 51 } 52 53 @Override onCreate()54 public void onCreate() { 55 super.onCreate(); 56 57 setWindowless(true); 58 59 mPluginManager.addPluginListener(this, DozeServicePlugin.class, false /* allowMultiple */); 60 DozeComponent dozeComponent = mDozeComponentBuilder.build(this); 61 mDozeMachine = dozeComponent.getDozeMachine(); 62 } 63 64 @Override onDestroy()65 public void onDestroy() { 66 if (mPluginManager != null) { 67 mPluginManager.removePluginListener(this); 68 } 69 super.onDestroy(); 70 mDozeMachine.destroy(); 71 mDozeMachine = null; 72 } 73 74 @Override onPluginConnected(DozeServicePlugin plugin, Context pluginContext)75 public void onPluginConnected(DozeServicePlugin plugin, Context pluginContext) { 76 mDozePlugin = plugin; 77 mDozePlugin.setDozeRequester(this); 78 } 79 80 @Override onPluginDisconnected(DozeServicePlugin plugin)81 public void onPluginDisconnected(DozeServicePlugin plugin) { 82 if (mDozePlugin != null) { 83 mDozePlugin.onDreamingStopped(); 84 mDozePlugin = null; 85 } 86 } 87 88 @Override onDreamingStarted()89 public void onDreamingStarted() { 90 super.onDreamingStarted(); 91 mDozeMachine.requestState(DozeMachine.State.INITIALIZED); 92 startDozing(); 93 if (mDozePlugin != null) { 94 mDozePlugin.onDreamingStarted(); 95 } 96 } 97 98 @Override onDreamingStopped()99 public void onDreamingStopped() { 100 super.onDreamingStopped(); 101 mDozeMachine.requestState(DozeMachine.State.FINISH); 102 if (mDozePlugin != null) { 103 mDozePlugin.onDreamingStopped(); 104 } 105 } 106 107 @Override dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args)108 protected void dumpOnHandler(FileDescriptor fd, PrintWriter pw, String[] args) { 109 super.dumpOnHandler(fd, pw, args); 110 if (mDozeMachine != null) { 111 mDozeMachine.dump(pw); 112 } 113 } 114 115 @Override requestWakeUp()116 public void requestWakeUp() { 117 PowerManager pm = getSystemService(PowerManager.class); 118 pm.wakeUp(SystemClock.uptimeMillis(), PowerManager.WAKE_REASON_GESTURE, 119 "com.android.systemui:NODOZE"); 120 } 121 122 @Override onRequestShowDoze()123 public void onRequestShowDoze() { 124 if (mDozeMachine != null) { 125 mDozeMachine.requestState(DozeMachine.State.DOZE_AOD); 126 } 127 } 128 129 @Override onRequestHideDoze()130 public void onRequestHideDoze() { 131 if (mDozeMachine != null) { 132 mDozeMachine.requestState(DozeMachine.State.DOZE); 133 } 134 } 135 136 @Override setDozeScreenState(int state)137 public void setDozeScreenState(int state) { 138 super.setDozeScreenState(state); 139 mDozeMachine.onScreenState(state); 140 } 141 } 142