Avoid unnecessary getSystemService call on WifiLockManager and WakeLockManager if not enabled

This commit is contained in:
Colin Kho 2024-06-23 00:07:46 -07:00 committed by Ian Baker
parent be2d68c2b3
commit 2ee69cb1f9
2 changed files with 9 additions and 8 deletions

View File

@ -32,15 +32,13 @@ import androidx.media3.common.util.Log;
private static final String TAG = "WakeLockManager"; private static final String TAG = "WakeLockManager";
private static final String WAKE_LOCK_TAG = "ExoPlayer:WakeLockManager"; private static final String WAKE_LOCK_TAG = "ExoPlayer:WakeLockManager";
@Nullable private final PowerManager powerManager;
@Nullable private WakeLock wakeLock; @Nullable private WakeLock wakeLock;
private boolean enabled; private boolean enabled;
private boolean stayAwake; private boolean stayAwake;
private final Context applicationContext;
public WakeLockManager(Context context) { public WakeLockManager(Context context) {
powerManager = applicationContext = context.getApplicationContext();
(PowerManager) context.getApplicationContext().getSystemService(Context.POWER_SERVICE);
} }
/** /**
@ -56,6 +54,8 @@ import androidx.media3.common.util.Log;
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
if (enabled) { if (enabled) {
if (wakeLock == null) { if (wakeLock == null) {
final PowerManager powerManager =
(PowerManager) applicationContext.getSystemService(Context.POWER_SERVICE);
if (powerManager == null) { if (powerManager == null) {
Log.w(TAG, "PowerManager is null, therefore not creating the WakeLock."); Log.w(TAG, "PowerManager is null, therefore not creating the WakeLock.");
return; return;

View File

@ -31,15 +31,13 @@ import androidx.media3.common.util.Log;
private static final String TAG = "WifiLockManager"; private static final String TAG = "WifiLockManager";
private static final String WIFI_LOCK_TAG = "ExoPlayer:WifiLockManager"; private static final String WIFI_LOCK_TAG = "ExoPlayer:WifiLockManager";
@Nullable private final WifiManager wifiManager;
@Nullable private WifiLock wifiLock; @Nullable private WifiLock wifiLock;
private boolean enabled; private boolean enabled;
private boolean stayAwake; private boolean stayAwake;
private Context applicationContext;
public WifiLockManager(Context context) { public WifiLockManager(Context context) {
wifiManager = applicationContext = context.getApplicationContext();
(WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
} }
/** /**
@ -54,6 +52,9 @@ import androidx.media3.common.util.Log;
*/ */
public void setEnabled(boolean enabled) { public void setEnabled(boolean enabled) {
if (enabled && wifiLock == null) { if (enabled && wifiLock == null) {
final WifiManager wifiManager =
(WifiManager) applicationContext.getApplicationContext()
.getSystemService(Context.WIFI_SERVICE);
if (wifiManager == null) { if (wifiManager == null) {
Log.w(TAG, "WifiManager is null, therefore not creating the WifiLock."); Log.w(TAG, "WifiManager is null, therefore not creating the WifiLock.");
return; return;