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