• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
21 import com.android.internal.util.Preconditions;
22 
23 public class WakeLockFake implements WakeLock {
24 
25     private int mAcquired = 0;
26 
27     @Override
acquire(String why)28     public void acquire(String why) {
29         mAcquired++;
30     }
31 
32     @Override
release(String why)33     public void release(String why) {
34         Preconditions.checkState(mAcquired > 0);
35         mAcquired--;
36     }
37 
38     @Override
wrap(Runnable runnable)39     public Runnable wrap(Runnable runnable) {
40         acquire(WakeLockFake.class.getSimpleName());
41         return () -> {
42             try {
43                 runnable.run();
44             } finally {
45                 release(WakeLockFake.class.getSimpleName());
46             }
47         };
48     }
49 
50     public boolean isHeld() {
51         return mAcquired > 0;
52     }
53 
54     public static class Builder extends WakeLock.Builder {
55         private WakeLock mWakeLock;
56 
57         public Builder(Context context) {
58             super(context, null);
59         }
60 
61         public void setWakeLock(WakeLock wakeLock) {
62             mWakeLock = wakeLock;
63         }
64 
65         public WakeLock build() {
66             if (mWakeLock != null) {
67                 return mWakeLock;
68             }
69 
70             return super.build();
71         }
72     }
73 }
74