]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
android: Again change how data source is handled in TileService
authorTobias Brunner <tobias@strongswan.org>
Mon, 21 Oct 2019 13:12:05 +0000 (15:12 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 2 Jun 2020 11:57:37 +0000 (13:57 +0200)
Evidently, onClick() may be called either before onStartListening() or
after onStopListening() has been called, which causes a crash when
trying to load a VpnProfile via mDataSource.

This partially reverts 3716af079e21 ("android: Avoid crash related to
TileService on Huawei devices").

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

index e754fe15116dd2d8be51450bcce2ba448a82798b..6016ebfbb25529276f9f5557ebe0a1f55a4fd6d9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2018 Tobias Brunner
+ * Copyright (C) 2018-2019 Tobias Brunner
  * HSR Hochschule fuer Technik Rapperswil
  *
  * This program is free software; you can redistribute it and/or modify it
@@ -39,6 +39,7 @@ 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()
@@ -53,7 +54,7 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt
                public void onServiceConnected(ComponentName name, IBinder service)
                {
                        mService = ((VpnStateService.LocalBinder)service).getService();
-                       if (mDataSource != null)
+                       if (mListening && mDataSource != null)
                        {
                                mService.registerListener(VpnTileService.this);
                                updateTile();
@@ -69,6 +70,9 @@ 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
@@ -80,15 +84,15 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt
                {
                        getApplicationContext().unbindService(mServiceConnection);
                }
+               mDataSource.close();
+               mDataSource = null;
        }
 
        @Override
        public void onStartListening()
        {
                super.onStartListening();
-
-               mDataSource = new VpnProfileDataSource(this);
-               mDataSource.open();
+               mListening = true;
 
                if (mService != null)
                {
@@ -101,14 +105,12 @@ 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()
@@ -119,8 +121,7 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt
                {
                        uuid = pref.getString(Constants.PREF_MRU_VPN_PROFILE, null);
                }
-
-               return mDataSource.getVpnProfile(uuid);
+               return mDataSource != null ? mDataSource.getVpnProfile(uuid) : null;
        }
 
        @Override
@@ -134,7 +135,7 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt
                        {
                                profile = getProfile();
                        }
-                       else
+                       else if (mDataSource != null)
                        {   /* always get the plain profile without cached password */
                                profile = mDataSource.getVpnProfile(profile.getId());
                        }