From: Tobias Brunner Date: Fri, 16 Aug 2019 15:04:28 +0000 (+0200) Subject: android: Avoid crash related to TileService on Huawei devices X-Git-Tag: 5.8.1rc1~1^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=3716af079e21;p=thirdparty%2Fstrongswan.git android: Avoid crash related to TileService on Huawei devices No idea when exactly this happens but on many Huawei devices (and only on them) it seems that onStartListening is sometimes called after onDestroy i.e. when the database was already closed. This caused an InvalidStateException in getProfile via updateTile when retrieving the current profile. It's possible that it happens during shutdown (there have been similar reports related to TileService implementations) so users might not even notice, but it pollutes the Play Console, so this workaround now makes sure the database is open when updateTile is called. --- diff --git a/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnTileService.java b/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnTileService.java index 623a80f225..e754fe1511 100644 --- a/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnTileService.java +++ b/src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnTileService.java @@ -39,7 +39,6 @@ import org.strongswan.android.utils.Constants; @TargetApi(Build.VERSION_CODES.N) public class VpnTileService extends TileService implements VpnStateService.VpnStateListener { - private boolean mListening; private VpnProfileDataSource mDataSource; private VpnStateService mService; private final ServiceConnection mServiceConnection = new ServiceConnection() @@ -54,7 +53,7 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt public void onServiceConnected(ComponentName name, IBinder service) { mService = ((VpnStateService.LocalBinder)service).getService(); - if (mListening) + if (mDataSource != null) { mService.registerListener(VpnTileService.this); updateTile(); @@ -70,27 +69,27 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt Context context = getApplicationContext(); context.bindService(new Intent(context, VpnStateService.class), mServiceConnection, Service.BIND_AUTO_CREATE); - - mDataSource = new VpnProfileDataSource(this); - mDataSource.open(); } @Override public void onDestroy() { super.onDestroy(); + if (mService != null) { getApplicationContext().unbindService(mServiceConnection); } - mDataSource.close(); } @Override public void onStartListening() { super.onStartListening(); - mListening = true; + + mDataSource = new VpnProfileDataSource(this); + mDataSource.open(); + if (mService != null) { mService.registerListener(this); @@ -102,11 +101,14 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt public void onStopListening() { super.onStopListening(); - mListening = false; + if (mService != null) { mService.unregisterListener(this); } + + mDataSource.close(); + mDataSource = null; } private VpnProfile getProfile()