Skip to content

Commit

Permalink
move unregisterReceivers to methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Electric1447 committed Aug 30, 2021
1 parent a741d7f commit 4081e8a
Showing 1 changed file with 26 additions and 34 deletions.
60 changes: 26 additions & 34 deletions OpenPods/app/src/main/java/com/dosse/airpods/pods/PodsService.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,7 @@ public void onCreate () {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
startForeground(101, createBackgroundNotification());

try {
if (btReceiver != null) {
unregisterReceiver(btReceiver);
btReceiver = null;
}
} catch (Throwable t) {
Logger.error(t);
}
unregisterBtReceiver();

btReceiver = new BluetoothReceiver() {
@Override
Expand Down Expand Up @@ -215,14 +208,7 @@ public void onDisconnect () {
startAirPodsScanner(); // If BT is already on when the app is started, start the scanner without waiting for an event to happen

// Screen on/off listener to suspend scanning when the screen is off, to save battery
try {
if (screenReceiver != null) {
unregisterReceiver(screenReceiver);
screenReceiver = null;
}
} catch (Throwable t) {
Logger.error(t);
}
unregisterScreenReceiver();

if (isSavingBattery(getApplicationContext())) {
screenReceiver = new ScreenReceiver() {
Expand Down Expand Up @@ -267,24 +253,8 @@ private static boolean checkUUID (BluetoothDevice bluetoothDevice) {
@Override
public void onDestroy () {
super.onDestroy();

try {
if (btReceiver != null) {
unregisterReceiver(btReceiver);
btReceiver = null;
}
} catch (Throwable t) {
Logger.error(t);
}

try {
if (screenReceiver != null) {
unregisterReceiver(screenReceiver);
screenReceiver = null;
}
} catch (Throwable t) {
Logger.error(t);
}
unregisterBtReceiver();
unregisterScreenReceiver();
}

@Override
Expand Down Expand Up @@ -334,4 +304,26 @@ private Notification createBackgroundNotification () {
return builder.build();
}

private void unregisterBtReceiver () {
try {
if (btReceiver != null) {
unregisterReceiver(btReceiver);
btReceiver = null;
}
} catch (Throwable t) {
Logger.error(t);
}
}

private void unregisterScreenReceiver () {
try {
if (screenReceiver != null) {
unregisterReceiver(screenReceiver);
screenReceiver = null;
}
} catch (Throwable t) {
Logger.error(t);
}
}

}

0 comments on commit 4081e8a

Please sign in to comment.