Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [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.os.Handler; |
| 20 | |
| 21 | /** |
| 22 | * A wake lock that has a built in delay when releasing to give the framebuffer time to update. |
| 23 | */ |
| 24 | public class DelayedWakeLock implements WakeLock { |
| 25 | |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 26 | private static final String TO_STRING_PREFIX = "[DelayedWakeLock] "; |
Lucas Dupin | a11298b | 2018-06-26 15:09:08 -0700 | [diff] [blame] | 27 | private static final long RELEASE_DELAY_MS = 100; |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 28 | |
| 29 | private final Handler mHandler; |
| 30 | private final WakeLock mInner; |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 31 | |
| 32 | public DelayedWakeLock(Handler h, WakeLock inner) { |
| 33 | mHandler = h; |
| 34 | mInner = inner; |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | @Override |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 38 | public void acquire(String why) { |
| 39 | mInner.acquire(why); |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | @Override |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 43 | public void release(String why) { |
| 44 | mHandler.postDelayed(() -> mInner.release(why), RELEASE_DELAY_MS); |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | @Override |
| 48 | public Runnable wrap(Runnable r) { |
| 49 | return WakeLock.wrapImpl(this, r); |
| 50 | } |
Lucas Dupin | ee4c9b7 | 2019-02-18 17:04:58 -0800 | [diff] [blame] | 51 | |
| 52 | @Override |
| 53 | public String toString() { |
| 54 | return TO_STRING_PREFIX + mInner; |
| 55 | } |
Adrian Roos | 0fb55ae | 2017-04-14 14:49:11 -0700 | [diff] [blame] | 56 | } |