android: Avoid crash related to TileService on Huawei devices
authorTobias Brunner <tobias@strongswan.org>
Fri, 16 Aug 2019 15:04:28 +0000 (17:04 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 26 Aug 2019 09:28:16 +0000 (11:28 +0200)
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.

src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnTileService.java

index 623a80f..e754fe1 100644 (file)
@@ -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()