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 android.content.Context;
20import android.os.PowerManager;
21import android.support.annotation.VisibleForTesting;
22
23/** WakeLock wrapper for testability */
24public interface WakeLock {
25
26    /** @see android.os.PowerManager.WakeLock#acquire() */
27    void acquire();
28
29    /** @see android.os.PowerManager.WakeLock#release() */
30    void release();
31
32    /** @see android.os.PowerManager.WakeLock#wrap(Runnable) */
33    Runnable wrap(Runnable r);
34
35    static WakeLock createPartial(Context context, String tag) {
36        return wrap(createPartialInner(context, tag));
37    }
38
39    @VisibleForTesting
40    static PowerManager.WakeLock createPartialInner(Context context, String tag) {
41        return context.getSystemService(PowerManager.class)
42                    .newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, tag);
43    }
44
45    static WakeLock wrap(final PowerManager.WakeLock inner) {
46        return new WakeLock() {
47            /** @see PowerManager.WakeLock#acquire() */
48            public void acquire() {
49                inner.acquire();
50            }
51
52            /** @see PowerManager.WakeLock#release() */
53            public void release() {
54                inner.release();
55            }
56
57            /** @see PowerManager.WakeLock#wrap(Runnable) */
58            public Runnable wrap(Runnable runnable) {
59                return inner.wrap(runnable);
60            }
61        };
62    }
63}