From 907a31db4cf36dcd76fa8de3ae98c295d6c2f0e0 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Mon, 21 Oct 2019 15:12:05 +0200 Subject: [PATCH] android: Again change how data source is handled in TileService 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"). --- .../strongswan/android/ui/VpnTileService.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 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 e754fe1511..6016ebfbb2 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 @@ -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()); } -- 2.47.2