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