android: Avoid crash related to TileService on Huawei devices

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.
This commit is contained in:
Tobias Brunner
2019-08-26 11:28:16 +02:00
parent 8af50736e1
commit 3716af079e
@@ -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()