• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 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.cellbroadcastreceiver;
18 
19 import android.content.Context;
20 import android.os.PowerManager;
21 import android.os.PowerManager.WakeLock;
22 import android.util.Log;
23 
24 /**
25  * Manage wakelocks that are used by Cell broadcast receiver various services.
26  */
27 class CellBroadcastAlertWakeLock {
28     private static final String TAG = "CellBroadcastAlertWakeLock";
29 
30     private static final long MAX_PARTIAL_WAKELOCK_DURATION = 1000;                  // 1 sec
31     private static final long MAX_SCREEN_BRIGHT_WAKELOCK_DURATION = 1000 * 60 * 5;   // 5 minutes
32 
33     private static WakeLock sPartialWakeLock;
34     private static WakeLock sScreenBrightWakeLock;
35 
CellBroadcastAlertWakeLock()36     private CellBroadcastAlertWakeLock() {}
37 
acquirePartialWakeLock(Context context)38     static void acquirePartialWakeLock(Context context) {
39         // Make sure we don't acquire the partial lock for more than 1 second. This lock
40         // is currently used to make sure the alert reminder tone and vibration could be played
41         // properly in timely manner.
42         acquirePartialWakeLock(context, MAX_PARTIAL_WAKELOCK_DURATION);
43     }
44 
acquirePartialWakeLock(Context context, long timeout)45     static void acquirePartialWakeLock(Context context, long timeout) {
46         if (sPartialWakeLock == null) {
47             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
48             sPartialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
49         }
50 
51         if (!sPartialWakeLock.isHeld()) {
52             sPartialWakeLock.acquire(timeout);
53             Log.d(TAG, "acquired partial wakelock");
54         }
55     }
56 
releasePartialWakeLock()57     static void releasePartialWakeLock() {
58         if (sPartialWakeLock != null && sPartialWakeLock.isHeld()) {
59             sPartialWakeLock.release();
60             Log.d(TAG, "released partial wakelock");
61         }
62     }
63 
acquireScreenBrightWakeLock(Context context)64     static void acquireScreenBrightWakeLock(Context context) {
65         // Make sure we don't acquire the full lock for more than 5 minutes. This lock
66         // is currently used by the main alert tone playing. Normally we hold the lock while
67         // the audio is playing for about 10 ~ 20 seconds.
68         acquireScreenBrightWakeLock(context, MAX_SCREEN_BRIGHT_WAKELOCK_DURATION);
69     }
70 
acquireScreenBrightWakeLock(Context context, long timeout)71     static void acquireScreenBrightWakeLock(Context context, long timeout) {
72         if (sScreenBrightWakeLock == null) {
73             PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
74             sScreenBrightWakeLock = pm.newWakeLock(
75                     PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG);
76         }
77 
78         if (!sScreenBrightWakeLock.isHeld()) {
79             sScreenBrightWakeLock.acquire(timeout);
80             Log.d(TAG, "acquired screen bright wakelock");
81         }
82     }
83 
releaseScreenBrightWakeLock()84     static void releaseScreenBrightWakeLock() {
85         if (sScreenBrightWakeLock != null && sScreenBrightWakeLock.isHeld()) {
86             sScreenBrightWakeLock.release();
87             Log.d(TAG, "released screen bright wakelock");
88         }
89     }
90 }
91