1 /*
2  * Copyright (C) 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * 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  * @author Felix Arends (felixarends@gmail.com)
34  * @author Damon Kohler (damonkohler@gmail.com)
35  */
36 public class WakeLockFacade extends RpcReceiver {
37 
38     private final static String WAKE_LOCK_TAG =
39             "com.googlecode.android_scripting.facade.PowerManagerFacade";
40     private final PowerManager mmPowerManager;
41 
42     private enum WakeLockType {
43         FULL, PARTIAL, BRIGHT, DIM
44     }
45 
46     private class WakeLockManager {
47         private final Map<WakeLockType, WakeLock> mmLocks = new HashMap<WakeLockType, WakeLock>();
48 
WakeLockManager(PowerManager mmPowerManager)49         public WakeLockManager(PowerManager mmPowerManager) {
50             addWakeLock(WakeLockType.PARTIAL, PowerManager.PARTIAL_WAKE_LOCK);
51             addWakeLock(WakeLockType.FULL, PowerManager.FULL_WAKE_LOCK
52                     | PowerManager.ON_AFTER_RELEASE);
53             addWakeLock(WakeLockType.BRIGHT, PowerManager.SCREEN_BRIGHT_WAKE_LOCK
54                     | PowerManager.ON_AFTER_RELEASE);
55             addWakeLock(WakeLockType.DIM, PowerManager.SCREEN_DIM_WAKE_LOCK
56                     | PowerManager.ON_AFTER_RELEASE);
57         }
58 
addWakeLock(WakeLockType type, int flags)59         private void addWakeLock(WakeLockType type, int flags) {
60             WakeLock full = mmPowerManager.newWakeLock(flags, WAKE_LOCK_TAG);
61             full.setReferenceCounted(false);
62             mmLocks.put(type, full);
63         }
64 
acquire(WakeLockType type)65         public void acquire(WakeLockType type) {
66             mmLocks.get(type).acquire();
67             for (Entry<WakeLockType, WakeLock> entry : mmLocks.entrySet()) {
68                 if (entry.getKey() != type) {
69                     entry.getValue().release();
70                 }
71             }
72         }
73 
release()74         public void release() {
75             for (Entry<WakeLockType, WakeLock> entry : mmLocks.entrySet()) {
76                 entry.getValue().release();
77             }
78         }
79     }
80 
81     private final WakeLockManager mManager;
82 
WakeLockFacade(FacadeManager manager)83     public WakeLockFacade(FacadeManager manager) {
84         super(manager);
85         mmPowerManager = (PowerManager) manager.getService()
86                 .getSystemService(Context.POWER_SERVICE);
87         mManager = new WakeLockManager(mmPowerManager);
88     }
89 
90     @Rpc(description = "Acquires a full wake lock (CPU on, screen bright, keyboard bright).")
wakeLockAcquireFull()91     public void wakeLockAcquireFull() {
92         mManager.acquire(WakeLockType.FULL);
93     }
94 
95     @Rpc(description = "Acquires a partial wake lock (CPU on).")
wakeLockAcquirePartial()96     public void wakeLockAcquirePartial() {
97         mManager.acquire(WakeLockType.PARTIAL);
98     }
99 
100     @Rpc(description = "Acquires a bright wake lock (CPU on, screen bright).")
wakeLockAcquireBright()101     public void wakeLockAcquireBright() {
102         mManager.acquire(WakeLockType.BRIGHT);
103     }
104 
105     @Rpc(description = "Acquires a dim wake lock (CPU on, screen dim).")
wakeLockAcquireDim()106     public void wakeLockAcquireDim() {
107         mManager.acquire(WakeLockType.DIM);
108     }
109 
110     @Rpc(description = "Releases the wake lock.")
wakeLockRelease()111     public void wakeLockRelease() {
112         mManager.release();
113     }
114 
115     @Override
shutdown()116     public void shutdown() {
117         wakeLockRelease();
118     }
119 }
120