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