diff --git a/OpenPods/app/src/main/java/com/dosse/airpods/pods/PodsService.java b/OpenPods/app/src/main/java/com/dosse/airpods/pods/PodsService.java index 2d08f6a..dc4802c 100644 --- a/OpenPods/app/src/main/java/com/dosse/airpods/pods/PodsService.java +++ b/OpenPods/app/src/main/java/com/dosse/airpods/pods/PodsService.java @@ -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 @@ -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() { @@ -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 @@ -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); + } + } + }