1 /*
<lambda>null2 * Copyright 2018 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 @file:JvmName("WakeLocks")
17
18 package androidx.work.impl.utils
19
20 import android.content.Context
21 import android.os.PowerManager
22 import androidx.work.Logger
23 import java.util.WeakHashMap
24
25 private val TAG = Logger.tagWithPrefix("WakeLocks")
26
27 /**
28 * Creates and returns a new WakeLock.
29 *
30 * @param context The context from which to get the PowerManager
31 * @param tag A descriptive tag for the WakeLock; this method will prefix "WorkManager: " to it
32 * @return A new [android.os.PowerManager.WakeLock]
33 */
34 internal fun newWakeLock(context: Context, tag: String): PowerManager.WakeLock {
35 val powerManager =
36 context.applicationContext.getSystemService(Context.POWER_SERVICE) as PowerManager
37 val tagWithPrefix = "WorkManager: $tag"
38 val wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, tagWithPrefix)
39 // Wakelocks are created on the command processor thread, but we check if they are still
40 // being held on the main thread.
41 synchronized(WakeLocksHolder) { WakeLocksHolder.wakeLocks.put(wakeLock, tagWithPrefix) }
42 return wakeLock
43 }
44
45 /**
46 * Checks to see if there are any [PowerManager.WakeLock]s that
47 * [androidx.work.impl.background.systemalarm.SystemAlarmService] holds when all the pending
48 * commands have been drained in the command queue.
49 */
checkWakeLocksnull50 fun checkWakeLocks() {
51 // There is a small chance that while we are checking if all the commands in the queue are
52 // drained and wake locks are no longer being held, a new command comes along and we end up
53 // with a ConcurrentModificationException. The addition of commands happens on the command
54 // processor thread and this check is done on the main thread.
55 val wakeLocksCopy = mutableMapOf<PowerManager.WakeLock?, String>()
56 synchronized(WakeLocksHolder) {
57 // Copy the WakeLocks - otherwise we can get a ConcurrentModificationException if the
58 // garbage collector kicks in and ends up removing something from the master copy while
59 // we are iterating over it.
60 wakeLocksCopy.putAll(WakeLocksHolder.wakeLocks)
61 }
62
63 wakeLocksCopy.forEach { (wakeLock, tag) ->
64 if (wakeLock?.isHeld == true) Logger.get().warning(TAG, "WakeLock held for $tag")
65 }
66 }
67
68 private object WakeLocksHolder {
69 val wakeLocks = WeakHashMap<PowerManager.WakeLock, String>()
70 }
71