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 3716af079e ("android: Avoid crash related to
TileService on Huawei devices").
This commit is contained in:
Tobias Brunner 2019-10-21 15:12:05 +02:00
parent 0ff939585e
commit 907a31db4c
1 changed files with 12 additions and 11 deletions

View 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());
}