1/*
2 * Copyright (C) 2015 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.server.telecom;
18
19import android.content.Context;
20import android.os.PowerManager;
21
22/**
23 * Container for PowerManager / PowerManager.WakeLock access in telecom to facilitate unit testing.
24 */
25public class TelecomWakeLock {
26
27    public class WakeLockAdapter {
28
29        private PowerManager.WakeLock mWakeLock;
30
31        public WakeLockAdapter(PowerManager.WakeLock wakeLock) {
32            mWakeLock = wakeLock;
33        }
34
35        public void acquire() {
36            mWakeLock.acquire();
37        }
38
39        public boolean isHeld() {
40            return mWakeLock.isHeld();
41        }
42
43        public void release(int flags) {
44            mWakeLock.release(flags);
45        }
46
47        public void setReferenceCounted(boolean isReferencedCounted){
48            mWakeLock.setReferenceCounted(isReferencedCounted);
49        }
50
51    }
52
53    private static final String TAG = "TelecomWakeLock";
54
55    private Context mContext;
56    private int mWakeLockLevel;
57    private String mWakeLockTag;
58    private WakeLockAdapter mWakeLock;
59
60    public TelecomWakeLock(Context context, int wakeLockLevel, String wakeLockTag) {
61        mContext = context;
62        mWakeLockLevel = wakeLockLevel;
63        mWakeLockTag = wakeLockTag;
64        mWakeLock = getWakeLockFromPowerManager();
65    }
66
67    // Used For Testing
68    public TelecomWakeLock(Context context, WakeLockAdapter wakeLockAdapter, int wakeLockLevel,
69            String wakeLockTag) {
70        mContext = context;
71        mWakeLockLevel = wakeLockLevel;
72        mWakeLockTag = wakeLockTag;
73        mWakeLock = wakeLockAdapter;
74    }
75
76    private WakeLockAdapter getWakeLockFromPowerManager() {
77        PowerManager powerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
78        WakeLockAdapter adapter = null;
79        if(powerManager.isWakeLockLevelSupported(mWakeLockLevel)) {
80            PowerManager.WakeLock wakeLock = powerManager.newWakeLock(mWakeLockLevel, mWakeLockTag);
81            adapter = new WakeLockAdapter(wakeLock);
82        }
83        return adapter;
84    }
85
86    public boolean isHeld() {
87        return mWakeLock != null && mWakeLock.isHeld();
88    }
89
90    public void acquire() {
91        if(mWakeLock == null) {
92            Log.i(TAG, "Can not acquire WakeLock (not supported) with level: " + mWakeLockLevel);
93            return;
94        }
95
96        if (!isHeld()) {
97            mWakeLock.acquire();
98            Log.i(TAG, "Acquiring WakeLock with id: " + mWakeLockLevel);
99        } else {
100            Log.i(TAG, "WakeLock already acquired for id: " + mWakeLockLevel);
101        }
102    }
103
104    public void release(int flags) {
105        if (mWakeLock == null) {
106            Log.i(TAG, "Can not release WakeLock (not supported) with id: " + mWakeLockLevel);
107            return;
108        }
109
110        if (isHeld()) {
111            mWakeLock.release(flags);
112            Log.i(TAG, "Releasing WakeLock with id: " + mWakeLockLevel);
113        } else {
114            Log.i(TAG, "WakeLock already released with id: " + mWakeLockLevel);
115        }
116    }
117
118    public void setReferenceCounted(boolean isReferencedCounted) {
119        if (mWakeLock == null) {
120            return;
121        }
122        mWakeLock.setReferenceCounted(isReferencedCounted);
123    }
124
125    @Override
126    public String toString() {
127        if(mWakeLock != null) {
128            return mWakeLock.toString();
129        } else {
130            return "null";
131        }
132    }
133}
134