From 3716af079e2133d04ba569d2d8e07838874d0414 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Fri, 16 Aug 2019 17:04:28 +0200 Subject: [PATCH] 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. --- .../strongswan/android/ui/VpnTileService.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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() -- 2.47.2