Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 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.android.systemui.util.wakelock; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.PowerManager; |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 21 | import android.util.Log; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 22 | |
Aurimas Liutikas | fd52c14 | 2018-04-17 09:50:46 -0700 | [diff] [blame] | 23 | import androidx.annotation.VisibleForTesting; |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 24 | |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 25 | import java.util.HashMap; |
| 26 | |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 27 | /** WakeLock wrapper for testability */ |
Adrian Roos | 664c9d7 | 2017-04-28 15:52:24 -0700 | [diff] [blame] | 28 | public interface WakeLock { |
| 29 | |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 30 | static final String TAG = "WakeLock"; |
| 31 | static final String REASON_WRAP = "wrap"; |
Adrian Roos | 664c9d7 | 2017-04-28 15:52:24 -0700 | [diff] [blame] | 32 | |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 33 | /** |
Lucas Dupin | 924010d | 2019-06-26 13:23:22 -0700 | [diff] [blame] | 34 | * Default wake-lock timeout, to avoid battery regressions. |
| 35 | */ |
| 36 | long DEFAULT_MAX_TIMEOUT = 20000; |
| 37 | |
| 38 | /** |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 39 | * @param why A tag that will be saved for sysui dumps. |
| 40 | * @see android.os.PowerManager.WakeLock#acquire() |
| 41 | **/ |
| 42 | void acquire(String why); |
| 43 | |
| 44 | /** |
| 45 | * @param why Same tag used in {@link #acquire(String)} |
| 46 | * @see android.os.PowerManager.WakeLock#release() |
| 47 | **/ |
| 48 | void release(String why); |
Adrian Roos | 664c9d7 | 2017-04-28 15:52:24 -0700 | [diff] [blame] | 49 | |
| 50 | /** @see android.os.PowerManager.WakeLock#wrap(Runnable) */ |
| 51 | Runnable wrap(Runnable r); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 52 | |
| 53 | static WakeLock createPartial(Context context, String tag) { |
Lucas Dupin | 924010d | 2019-06-26 13:23:22 -0700 | [diff] [blame] | 54 | return createPartial(context, tag, DEFAULT_MAX_TIMEOUT); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Creates a {@link WakeLock} that has a default release timeout. |
| 59 | * @see android.os.PowerManager.WakeLock#acquire(long) */ |
| 60 | static WakeLock createPartial(Context context, String tag, long maxTimeout) { |
| 61 | return wrap(createPartialInner(context, tag), maxTimeout); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @VisibleForTesting |
| 65 | static PowerManager.WakeLock createPartialInner(Context context, String tag) { |
| 66 | return context.getSystemService(PowerManager.class) |
| 67 | .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, tag); |
| 68 | } |
| 69 | |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 70 | static Runnable wrapImpl(WakeLock w, Runnable r) { |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 71 | w.acquire(REASON_WRAP); |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 72 | return () -> { |
| 73 | try { |
| 74 | r.run(); |
| 75 | } finally { |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 76 | w.release(REASON_WRAP); |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 77 | } |
| 78 | }; |
| 79 | } |
| 80 | |
Lucas Dupin | 924010d | 2019-06-26 13:23:22 -0700 | [diff] [blame] | 81 | /** |
| 82 | * Create a {@link WakeLock} containing a {@link PowerManager.WakeLock}. |
| 83 | * @param inner To be wrapped. |
| 84 | * @param maxTimeout When to expire. |
| 85 | * @return The new wake lock. |
| 86 | */ |
| 87 | @VisibleForTesting |
| 88 | static WakeLock wrap(final PowerManager.WakeLock inner, long maxTimeout) { |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 89 | return new WakeLock() { |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 90 | private final HashMap<String, Integer> mActiveClients = new HashMap<>(); |
| 91 | |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 92 | /** @see PowerManager.WakeLock#acquire() */ |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 93 | public void acquire(String why) { |
| 94 | mActiveClients.putIfAbsent(why, 0); |
| 95 | mActiveClients.put(why, mActiveClients.get(why) + 1); |
Lucas Dupin | 924010d | 2019-06-26 13:23:22 -0700 | [diff] [blame] | 96 | inner.acquire(maxTimeout); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /** @see PowerManager.WakeLock#release() */ |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 100 | public void release(String why) { |
| 101 | Integer count = mActiveClients.get(why); |
| 102 | if (count == null) { |
| 103 | Log.wtf(TAG, "Releasing WakeLock with invalid reason: " + why, |
| 104 | new Throwable()); |
| 105 | } else if (count == 1) { |
| 106 | mActiveClients.remove(why); |
| 107 | } else { |
| 108 | mActiveClients.put(why, count - 1); |
| 109 | } |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 110 | inner.release(); |
| 111 | } |
| 112 | |
| 113 | /** @see PowerManager.WakeLock#wrap(Runnable) */ |
| 114 | public Runnable wrap(Runnable runnable) { |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 115 | return wrapImpl(this, runnable); |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 116 | } |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 117 | |
| 118 | @Override |
| 119 | public String toString() { |
| 120 | return "active clients= " + mActiveClients.toString(); |
| 121 | } |
Adrian Roos | c1b5032 | 2017-02-27 21:07:58 +0100 | [diff] [blame] | 122 | }; |
| 123 | } |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 124 | } |