Skip to content

Commit

Permalink
Fix a NPE crash when the service has been destroyed but the controlle…
Browse files Browse the repository at this point in the history
…rs are still in the process of shutting down
  • Loading branch information
christianrowlands committed Mar 13, 2024
1 parent 2f53781 commit 36c3106
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ public void onLogFileTypePreferenceChanged()
*/
public void refreshScanRate()
{
if (surveyService == null) return;

bluetoothScanRateMs = PreferenceUtils.getScanRatePreferenceMs(NetworkSurveyConstants.PROPERTY_BLUETOOTH_SCAN_INTERVAL_SECONDS,
NetworkSurveyConstants.DEFAULT_BLUETOOTH_SCAN_INTERVAL_SECONDS, surveyService.getApplicationContext());
}
Expand All @@ -160,6 +162,8 @@ public Boolean toggleLogging(boolean enable)
final boolean originalLoggingState = bluetoothLoggingEnabled.get();
if (originalLoggingState == enable) return originalLoggingState;

if (surveyService == null) return null;

Timber.i("Toggling Bluetooth logging to %s", enable);

boolean successful = false;
Expand Down Expand Up @@ -215,6 +219,8 @@ public Boolean toggleLogging(boolean enable)
*/
public void initializeBtScanningResources()
{
if (surveyService == null) return;

final BluetoothManager bluetoothManager = (BluetoothManager) surveyService.getSystemService(Context.BLUETOOTH_SERVICE);

if (bluetoothManager == null)
Expand Down Expand Up @@ -337,6 +343,8 @@ private boolean hasBtConnectPermission()
@SuppressLint("MissingPermission")
public void startBluetoothRecordScanning()
{
if (surveyService == null) return;

synchronized (bluetoothLoggingEnabled)
{
if (!hasBtScanPermission())
Expand Down Expand Up @@ -421,6 +429,8 @@ public void run()
@SuppressLint("MissingPermission") // Permissions are checked in the first part of the method
public void stopBluetoothRecordScanning()
{
if (surveyService == null) return;

synchronized (bluetoothLoggingEnabled)
{
bluetoothScanningActive.set(false);
Expand Down Expand Up @@ -466,6 +476,8 @@ public void stopAllLogging()

private void toggleBtConfig(boolean enable, LogTypeState types)
{
if (surveyService == null) return;

bluetoothLoggingEnabled.set(enable);
if (enable)
{
Expand Down Expand Up @@ -507,6 +519,8 @@ private void toggleBtConfig(boolean enable, LogTypeState types)
// Permissions are checked in the first part of this method call
private boolean isBluetoothEnabledAndPermissionsGranted(boolean promptEnable)
{
if (surveyService == null) return false;

if (!hasBtConnectPermission() || !hasBtScanPermission())
{
Timber.i("Missing a Bluetooth permission, can't enable BT scanning");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ public void onLogFileTypePreferenceChanged()
*/
public void refreshScanRate()
{
if (surveyService == null) return;

cellularScanRateMs = PreferenceUtils.getScanRatePreferenceMs(NetworkSurveyConstants.PROPERTY_CELLULAR_SCAN_INTERVAL_SECONDS,
NetworkSurveyConstants.DEFAULT_CELLULAR_SCAN_INTERVAL_SECONDS, surveyService.getApplicationContext());
}
Expand All @@ -213,6 +215,8 @@ public Boolean toggleLogging(boolean enable)
final boolean originalLoggingState = cellularLoggingEnabled.get();
if (originalLoggingState == enable) return originalLoggingState;

if (surveyService == null) return null;

Timber.i("Toggling cellular logging to %s", enable);

boolean successful = false;
Expand Down Expand Up @@ -307,6 +311,8 @@ public void startPhoneStateListener()
// The onServiceStateChanged required API level 29.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
{
if (surveyService == null) return;

// Add a listener for the Service State information if we have access to the Telephony Manager
final TelephonyManager telephonyManager = (TelephonyManager) surveyService.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null && surveyService.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY))
Expand Down Expand Up @@ -342,7 +348,7 @@ public void onRegistrationFailed(@NonNull CellIdentity cellIdentity, @NonNull St

public void stopPhoneStateListener()
{
if (phoneStateListener != null)
if (phoneStateListener != null && surveyService != null)
{
final TelephonyManager telephonyManager = (TelephonyManager) surveyService.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager != null && surveyService.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY))
Expand Down Expand Up @@ -453,6 +459,8 @@ public void onError(int errorCode, @Nullable Throwable detail)
*/
private void registerSimStateChangeReceiver()
{
if (surveyService == null) return;

simBroadcastReceiver = new BroadcastReceiver()
{
@Override
Expand All @@ -477,6 +485,8 @@ public void onReceive(Context context, Intent intent)
*/
public synchronized void runSingleScan()
{
if (surveyService == null) return;

final TelephonyManager telephonyManager = (TelephonyManager) surveyService.getSystemService(Context.TELEPHONY_SERVICE);

if (telephonyManager == null || !surveyService.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY))
Expand Down Expand Up @@ -537,6 +547,8 @@ public synchronized void runSingleScan()
*/
public void startCellularRecordScanning()
{
if (surveyService == null) return;

synchronized (cellularLoggingEnabled)
{
if (cellularScanningActive.getAndSet(true)) return;
Expand Down Expand Up @@ -647,6 +659,8 @@ public void stopAllLogging()
*/
private void toggleCellularConfig(boolean enable, LogTypeState types)
{
if (surveyService == null) return;

cellularLoggingEnabled.set(enable);
if (enable)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ public void onLogFileTypePreferenceChanged()
*/
public void refreshScanRate()
{
if (surveyService == null) return;

gnssScanRateMs = PreferenceUtils.getScanRatePreferenceMs(NetworkSurveyConstants.PROPERTY_GNSS_SCAN_INTERVAL_SECONDS,
NetworkSurveyConstants.DEFAULT_GNSS_SCAN_INTERVAL_SECONDS, surveyService.getApplicationContext());
}
Expand All @@ -164,6 +166,8 @@ public void refreshScanRate()
*/
public Boolean toggleLogging(boolean enable)
{
if (surveyService == null) return;

synchronized (gnssLoggingEnabled)
{
final boolean originalLoggingState = gnssLoggingEnabled.get();
Expand Down Expand Up @@ -243,6 +247,8 @@ public void onGnssMeasurementsReceived(GnssMeasurementsEvent event)
*/
public boolean startGnssRecordScanning()
{
if (surveyService == null) return false;

// Using gnssLoggingEnabled as the lock object because it is also used in the toggleLogging method
synchronized (gnssLoggingEnabled)
{
Expand Down Expand Up @@ -332,6 +338,8 @@ public void run()
*/
public void stopGnssRecordScanning()
{
if (surveyService == null) return;

// Using gnssLoggingEnabled as the lock object because it is also used in the toggleLogging method
synchronized (gnssLoggingEnabled)
{
Expand Down Expand Up @@ -368,6 +376,8 @@ public void stopAllLogging()

private void toggleGnssConfig(boolean enable, LogTypeState types)
{
if (surveyService == null) return;

gnssLoggingEnabled.set(enable);
if (enable)
{
Expand Down Expand Up @@ -416,6 +426,8 @@ public void clearGnssFailureListener()
*/
private void checkForGnssTimeout()
{
if (surveyService == null) return;

if (!gnssRawSupportKnown && !hasGnssRawFailureNagLaunched)
{
if (firstGpsAcqTime < 0L)
Expand Down Expand Up @@ -447,6 +459,8 @@ private void checkForGnssTimeout()
*/
private void runOneMeasurement()
{
if (surveyService == null) return;

synchronized (gnssLoggingEnabled)
{
boolean hasPermissions = ContextCompat.checkSelfPermission(surveyService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ public void onLogFileTypePreferenceChanged()
*/
public void refreshScanRate()
{
if (surveyService == null) return;

wifiScanRateMs = PreferenceUtils.getScanRatePreferenceMs(NetworkSurveyConstants.PROPERTY_WIFI_SCAN_INTERVAL_SECONDS,
NetworkSurveyConstants.DEFAULT_WIFI_SCAN_INTERVAL_SECONDS, surveyService.getApplicationContext());
}
Expand All @@ -144,6 +146,8 @@ public void refreshScanRate()
*/
public Boolean toggleLogging(boolean enable)
{
if (surveyService == null) return null;

synchronized (wifiLoggingEnabled)
{
final boolean originalLoggingState = wifiLoggingEnabled.get();
Expand Down Expand Up @@ -204,6 +208,8 @@ public Boolean toggleLogging(boolean enable)
*/
public void initializeWifiScanningResources()
{
if (surveyService == null) return;

final WifiManager wifiManager = (WifiManager) surveyService.getSystemService(Context.WIFI_SERVICE);

if (wifiManager == null)
Expand Down Expand Up @@ -249,6 +255,8 @@ public void onReceive(Context c, Intent intent)
*/
public void startWifiRecordScanning()
{
if (surveyService == null) return;

// Using wifiLoggingEnabled as the lock object because it is also used in the toggleLogging method
synchronized (wifiLoggingEnabled)
{
Expand Down Expand Up @@ -303,6 +311,8 @@ public void run()
*/
public void stopWifiRecordScanning()
{
if (surveyService == null) return;

// Using wifiLoggingEnabled as the lock object because it is also used in the toggleLogging method
synchronized (wifiLoggingEnabled)
{
Expand Down Expand Up @@ -335,6 +345,8 @@ public void stopAllLogging()

private void toggleWifiConfig(boolean enable, LogTypeState types)
{
if (surveyService == null) return;

wifiLoggingEnabled.set(enable);
if (enable)
{
Expand Down Expand Up @@ -371,6 +383,8 @@ private void toggleWifiConfig(boolean enable, LogTypeState types)
*/
private boolean isWifiEnabled(boolean promptEnable)
{
if (surveyService == null) return false;

boolean isEnabled = true;

final WifiManager wifiManager = (WifiManager) surveyService.getSystemService(Context.WIFI_SERVICE);
Expand Down

0 comments on commit 36c3106

Please sign in to comment.