1 /* 2 * Copyright (C) 2020 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.deskclock 18 19 import android.content.Context 20 import android.os.PowerManager 21 import android.os.PowerManager.WakeLock 22 23 /** 24 * Utility class to hold wake lock in app. 25 */ 26 object AlarmAlertWakeLock { 27 private const val TAG = "AlarmAlertWakeLock" 28 29 private var sCpuWakeLock: WakeLock? = null 30 31 @JvmStatic createPartialWakeLocknull32 fun createPartialWakeLock(context: Context): WakeLock { 33 val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager 34 return pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG) 35 } 36 37 @JvmStatic acquireCpuWakeLocknull38 fun acquireCpuWakeLock(context: Context) { 39 if (sCpuWakeLock != null) { 40 return 41 } 42 43 sCpuWakeLock = createPartialWakeLock(context) 44 sCpuWakeLock!!.acquire() 45 } 46 47 @JvmStatic acquireScreenCpuWakeLocknull48 fun acquireScreenCpuWakeLock(context: Context) { 49 if (sCpuWakeLock != null) { 50 return 51 } 52 val pm = context.getSystemService(Context.POWER_SERVICE) as PowerManager 53 sCpuWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK 54 or PowerManager.ACQUIRE_CAUSES_WAKEUP or PowerManager.ON_AFTER_RELEASE, TAG) 55 sCpuWakeLock!!.acquire() 56 } 57 58 @JvmStatic releaseCpuLocknull59 fun releaseCpuLock() { 60 if (sCpuWakeLock != null) { 61 sCpuWakeLock!!.release() 62 sCpuWakeLock = null 63 } 64 } 65 }