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 * HSR Hochschule fuer Technik Rapperswil
* *
* This program is free software; you can redistribute it and/or modify it * 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) @TargetApi(Build.VERSION_CODES.N)
public class VpnTileService extends TileService implements VpnStateService.VpnStateListener public class VpnTileService extends TileService implements VpnStateService.VpnStateListener
{ {
private boolean mListening;
private VpnProfileDataSource mDataSource; private VpnProfileDataSource mDataSource;
private VpnStateService mService; private VpnStateService mService;
private final ServiceConnection mServiceConnection = new ServiceConnection() 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) public void onServiceConnected(ComponentName name, IBinder service)
{ {
mService = ((VpnStateService.LocalBinder)service).getService(); mService = ((VpnStateService.LocalBinder)service).getService();
if (mDataSource != null) if (mListening && mDataSource != null)
{ {
mService.registerListener(VpnTileService.this); mService.registerListener(VpnTileService.this);
updateTile(); updateTile();
@ -69,6 +70,9 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt
Context context = getApplicationContext(); Context context = getApplicationContext();
context.bindService(new Intent(context, VpnStateService.class), context.bindService(new Intent(context, VpnStateService.class),
mServiceConnection, Service.BIND_AUTO_CREATE); mServiceConnection, Service.BIND_AUTO_CREATE);
mDataSource = new VpnProfileDataSource(this);
mDataSource.open();
} }
@Override @Override
@ -80,15 +84,15 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt
{ {
getApplicationContext().unbindService(mServiceConnection); getApplicationContext().unbindService(mServiceConnection);
} }
mDataSource.close();
mDataSource = null;
} }
@Override @Override
public void onStartListening() public void onStartListening()
{ {
super.onStartListening(); super.onStartListening();
mListening = true;
mDataSource = new VpnProfileDataSource(this);
mDataSource.open();
if (mService != null) if (mService != null)
{ {
@ -101,14 +105,12 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt
public void onStopListening() public void onStopListening()
{ {
super.onStopListening(); super.onStopListening();
mListening = false;
if (mService != null) if (mService != null)
{ {
mService.unregisterListener(this); mService.unregisterListener(this);
} }
mDataSource.close();
mDataSource = null;
} }
private VpnProfile getProfile() private VpnProfile getProfile()
@ -119,8 +121,7 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt
{ {
uuid = pref.getString(Constants.PREF_MRU_VPN_PROFILE, null); uuid = pref.getString(Constants.PREF_MRU_VPN_PROFILE, null);
} }
return mDataSource != null ? mDataSource.getVpnProfile(uuid) : null;
return mDataSource.getVpnProfile(uuid);
} }
@Override @Override
@ -134,7 +135,7 @@ public class VpnTileService extends TileService implements VpnStateService.VpnSt
{ {
profile = getProfile(); profile = getProfile();
} }
else else if (mDataSource != null)
{ /* always get the plain profile without cached password */ { /* always get the plain profile without cached password */
profile = mDataSource.getVpnProfile(profile.getId()); profile = mDataSource.getVpnProfile(profile.getId());
} }