2017-10-28 19:08:01 +02:00
|
|
|
package org.schabi.newpipe.player.helper;
|
2017-10-27 04:59:25 +02:00
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.wifi.WifiManager;
|
|
|
|
import android.os.PowerManager;
|
|
|
|
import android.util.Log;
|
|
|
|
|
2020-09-13 13:50:29 +02:00
|
|
|
import androidx.core.content.ContextCompat;
|
2017-10-27 04:59:25 +02:00
|
|
|
|
|
|
|
public class LockManager {
|
|
|
|
private final String TAG = "LockManager@" + hashCode();
|
|
|
|
|
|
|
|
private final PowerManager powerManager;
|
|
|
|
private final WifiManager wifiManager;
|
|
|
|
|
|
|
|
private PowerManager.WakeLock wakeLock;
|
|
|
|
private WifiManager.WifiLock wifiLock;
|
|
|
|
|
|
|
|
public LockManager(final Context context) {
|
2020-09-13 13:50:29 +02:00
|
|
|
powerManager = ContextCompat.getSystemService(context.getApplicationContext(),
|
|
|
|
PowerManager.class);
|
|
|
|
wifiManager = ContextCompat.getSystemService(context, WifiManager.class);
|
2017-10-27 04:59:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void acquireWifiAndCpu() {
|
|
|
|
Log.d(TAG, "acquireWifiAndCpu() called");
|
2020-03-31 19:20:15 +02:00
|
|
|
if (wakeLock != null && wakeLock.isHeld() && wifiLock != null && wifiLock.isHeld()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-27 04:59:25 +02:00
|
|
|
|
|
|
|
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
|
|
|
|
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL, TAG);
|
|
|
|
|
2020-03-31 19:20:15 +02:00
|
|
|
if (wakeLock != null) {
|
|
|
|
wakeLock.acquire();
|
|
|
|
}
|
|
|
|
if (wifiLock != null) {
|
|
|
|
wifiLock.acquire();
|
|
|
|
}
|
2017-10-27 04:59:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void releaseWifiAndCpu() {
|
|
|
|
Log.d(TAG, "releaseWifiAndCpu() called");
|
2020-03-31 19:20:15 +02:00
|
|
|
if (wakeLock != null && wakeLock.isHeld()) {
|
|
|
|
wakeLock.release();
|
|
|
|
}
|
|
|
|
if (wifiLock != null && wifiLock.isHeld()) {
|
|
|
|
wifiLock.release();
|
|
|
|
}
|
2017-10-27 04:59:25 +02:00
|
|
|
|
|
|
|
wakeLock = null;
|
|
|
|
wifiLock = null;
|
|
|
|
}
|
|
|
|
}
|