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