Home | History | Annotate | Download | only in facade
      1 /*
      2  * Copyright (C) 2017 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.googlecode.android_scripting.facade;
     18 
     19 import android.content.Context;
     20 import android.os.PowerManager;
     21 import android.os.PowerManager.WakeLock;
     22 
     23 import com.googlecode.android_scripting.jsonrpc.RpcReceiver;
     24 import com.googlecode.android_scripting.rpc.Rpc;
     25 
     26 import java.util.HashMap;
     27 import java.util.Map;
     28 import java.util.Map.Entry;
     29 
     30 /**
     31  * A facade exposing some of the functionality of the PowerManager, in particular wake locks.
     32  *
     33  */
     34 public class WakeLockFacade extends RpcReceiver {
     35 
     36     private final static String WAKE_LOCK_TAG =
     37             "com.googlecode.android_scripting.facade.PowerManagerFacade";
     38     private final PowerManager mmPowerManager;
     39 
     40     private enum WakeLockType {
     41         FULL, PARTIAL, BRIGHT, DIM
     42     }
     43 
     44     private class WakeLockManager {
     45         private final Map<WakeLockType, WakeLock> mmLocks = new HashMap<WakeLockType, WakeLock>();
     46 
     47         public WakeLockManager(PowerManager mmPowerManager) {
     48             addWakeLock(WakeLockType.PARTIAL, PowerManager.PARTIAL_WAKE_LOCK);
     49             addWakeLock(WakeLockType.FULL, PowerManager.FULL_WAKE_LOCK
     50                     | PowerManager.ON_AFTER_RELEASE);
     51             addWakeLock(WakeLockType.BRIGHT, PowerManager.SCREEN_BRIGHT_WAKE_LOCK
     52                     | PowerManager.ON_AFTER_RELEASE);
     53             addWakeLock(WakeLockType.DIM, PowerManager.SCREEN_DIM_WAKE_LOCK
     54                     | PowerManager.ON_AFTER_RELEASE);
     55         }
     56 
     57         private void addWakeLock(WakeLockType type, int flags) {
     58             WakeLock full = mmPowerManager.newWakeLock(flags, WAKE_LOCK_TAG);
     59             full.setReferenceCounted(false);
     60             mmLocks.put(type, full);
     61         }
     62 
     63         public void acquire(WakeLockType type) {
     64             mmLocks.get(type).acquire();
     65             for (Entry<WakeLockType, WakeLock> entry : mmLocks.entrySet()) {
     66                 if (entry.getKey() != type) {
     67                     entry.getValue().release();
     68                 }
     69             }
     70         }
     71 
     72         public void release() {
     73             for (Entry<WakeLockType, WakeLock> entry : mmLocks.entrySet()) {
     74                 entry.getValue().release();
     75             }
     76         }
     77     }
     78 
     79     private final WakeLockManager mManager;
     80 
     81     public WakeLockFacade(FacadeManager manager) {
     82         super(manager);
     83         mmPowerManager = (PowerManager) manager.getService()
     84                 .getSystemService(Context.POWER_SERVICE);
     85         mManager = new WakeLockManager(mmPowerManager);
     86     }
     87 
     88     @Rpc(description = "Acquires a full wake lock (CPU on, screen bright, keyboard bright).")
     89     public void wakeLockAcquireFull() {
     90         mManager.acquire(WakeLockType.FULL);
     91     }
     92 
     93     @Rpc(description = "Acquires a partial wake lock (CPU on).")
     94     public void wakeLockAcquirePartial() {
     95         mManager.acquire(WakeLockType.PARTIAL);
     96     }
     97 
     98     @Rpc(description = "Acquires a bright wake lock (CPU on, screen bright).")
     99     public void wakeLockAcquireBright() {
    100         mManager.acquire(WakeLockType.BRIGHT);
    101     }
    102 
    103     @Rpc(description = "Acquires a dim wake lock (CPU on, screen dim).")
    104     public void wakeLockAcquireDim() {
    105         mManager.acquire(WakeLockType.DIM);
    106     }
    107 
    108     @Rpc(description = "Releases the wake lock.")
    109     public void wakeLockRelease() {
    110         mManager.release();
    111     }
    112 
    113     @Override
    114     public void shutdown() {
    115         wakeLockRelease();
    116     }
    117 }
    118