android: Simplify error handling in VPN state fragment
Always reset the error state when disconnecting via state service. This way the error state is also cleared when the connection is terminated directly via control activity.
This commit is contained in:
+3
@@ -262,7 +262,10 @@ public class VpnStateService extends Service
|
|||||||
*/
|
*/
|
||||||
public void disconnect()
|
public void disconnect()
|
||||||
{
|
{
|
||||||
|
/* reset any potential retry timer and error state */
|
||||||
resetRetryTimer();
|
resetRetryTimer();
|
||||||
|
setError(ErrorState.NO_ERROR);
|
||||||
|
|
||||||
/* as soon as the TUN device is created by calling establish() on the
|
/* as soon as the TUN device is created by calling establish() on the
|
||||||
* VpnService.Builder object the system binds to the service and keeps
|
* VpnService.Builder object the system binds to the service and keeps
|
||||||
* bound until the file descriptor of the TUN device is closed. thus
|
* bound until the file descriptor of the TUN device is closed. thus
|
||||||
|
|||||||
+6
-43
@@ -28,7 +28,6 @@ import android.support.v4.app.Fragment;
|
|||||||
import android.support.v4.content.ContextCompat;
|
import android.support.v4.content.ContextCompat;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.view.View.OnClickListener;
|
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.LinearLayout;
|
import android.widget.LinearLayout;
|
||||||
@@ -44,8 +43,6 @@ import org.strongswan.android.logic.VpnStateService.VpnStateListener;
|
|||||||
|
|
||||||
public class VpnStateFragment extends Fragment implements VpnStateListener
|
public class VpnStateFragment extends Fragment implements VpnStateListener
|
||||||
{
|
{
|
||||||
private static final String KEY_ERROR_CONNECTION_ID = "error_connection_id";
|
|
||||||
|
|
||||||
private boolean mVisible;
|
private boolean mVisible;
|
||||||
private TextView mProfileNameView;
|
private TextView mProfileNameView;
|
||||||
private TextView mProfileView;
|
private TextView mProfileView;
|
||||||
@@ -59,7 +56,6 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
|
|||||||
private TextView mErrorText;
|
private TextView mErrorText;
|
||||||
private Button mErrorRetry;
|
private Button mErrorRetry;
|
||||||
private Button mShowLog;
|
private Button mShowLog;
|
||||||
private long mErrorConnectionID;
|
|
||||||
private VpnStateService mService;
|
private VpnStateService mService;
|
||||||
private final ServiceConnection mServiceConnection = new ServiceConnection()
|
private final ServiceConnection mServiceConnection = new ServiceConnection()
|
||||||
{
|
{
|
||||||
@@ -80,17 +76,6 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
private OnClickListener mDisconnectListener = new OnClickListener()
|
|
||||||
{
|
|
||||||
@Override
|
|
||||||
public void onClick(View v)
|
|
||||||
{
|
|
||||||
if (mService != null)
|
|
||||||
{
|
|
||||||
mService.disconnect();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState)
|
public void onCreate(Bundle savedInstanceState)
|
||||||
@@ -104,20 +89,6 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
|
|||||||
Context context = getActivity().getApplicationContext();
|
Context context = getActivity().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);
|
||||||
|
|
||||||
mErrorConnectionID = 0;
|
|
||||||
if (savedInstanceState != null && savedInstanceState.containsKey(KEY_ERROR_CONNECTION_ID))
|
|
||||||
{
|
|
||||||
mErrorConnectionID = (Long)savedInstanceState.getSerializable(KEY_ERROR_CONNECTION_ID);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSaveInstanceState(Bundle outState)
|
|
||||||
{
|
|
||||||
super.onSaveInstanceState(outState);
|
|
||||||
|
|
||||||
outState.putSerializable(KEY_ERROR_CONNECTION_ID, mErrorConnectionID);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -127,7 +98,12 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
|
|||||||
View view = inflater.inflate(R.layout.vpn_state_fragment, null);
|
View view = inflater.inflate(R.layout.vpn_state_fragment, null);
|
||||||
|
|
||||||
mActionButton = (Button)view.findViewById(R.id.action);
|
mActionButton = (Button)view.findViewById(R.id.action);
|
||||||
mActionButton.setOnClickListener(v -> clearError());
|
mActionButton.setOnClickListener(v -> {
|
||||||
|
if (mService != null)
|
||||||
|
{
|
||||||
|
mService.disconnect();
|
||||||
|
}
|
||||||
|
});
|
||||||
enableActionButton(null);
|
enableActionButton(null);
|
||||||
|
|
||||||
mErrorView = view.findViewById(R.id.vpn_error);
|
mErrorView = view.findViewById(R.id.vpn_error);
|
||||||
@@ -259,7 +235,6 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
|
|||||||
mErrorView.setVisibility(View.GONE);
|
mErrorView.setVisibility(View.GONE);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
mErrorConnectionID = connectionID;
|
|
||||||
mProfileNameView.setText(name);
|
mProfileNameView.setText(name);
|
||||||
showProfile(true);
|
showProfile(true);
|
||||||
mStateView.setText(R.string.state_error);
|
mStateView.setText(R.string.state_error);
|
||||||
@@ -298,16 +273,4 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
|
|||||||
mActionButton.setEnabled(text != null);
|
mActionButton.setEnabled(text != null);
|
||||||
mActionButton.setVisibility(text != null ? View.VISIBLE : View.GONE);
|
mActionButton.setVisibility(text != null ? View.VISIBLE : View.GONE);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void clearError()
|
|
||||||
{
|
|
||||||
if (mService != null)
|
|
||||||
{
|
|
||||||
mService.disconnect();
|
|
||||||
if (mService.getConnectionID() == mErrorConnectionID)
|
|
||||||
{
|
|
||||||
mService.setError(ErrorState.NO_ERROR);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user