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 17package com.android.systemui.util.wakelock; 18 19import com.android.internal.util.Preconditions; 20 21public class WakeLockFake implements WakeLock { 22 23 private int mAcquired = 0; 24 25 @Override 26 public void acquire() { 27 mAcquired++; 28 } 29 30 @Override 31 public void release() { 32 Preconditions.checkState(mAcquired > 0); 33 mAcquired--; 34 } 35 36 @Override 37 public Runnable wrap(Runnable runnable) { 38 acquire(); 39 return () -> { 40 try { 41 runnable.run(); 42 } finally { 43 release(); 44 } 45 }; 46 } 47 48 public boolean isHeld() { 49 return mAcquired > 0; 50 } 51} 52