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