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