Show current VPN state and profile name

Show modal dialogs while connecting and disconnecting the VPN.
This commit is contained in:
Tobias Brunner
2012-08-13 11:18:51 +02:00
parent e7908526fd
commit a43bdf9a37
3 changed files with 189 additions and 0 deletions
@@ -22,6 +22,56 @@
android:background="@drawable/vpn_state_background"
android:orientation="vertical" >
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:columnCount="2"
android:rowCount="2" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="top"
android:text="@string/state_label"
android:textColor="?android:textColorPrimary"
android:textSize="20sp" />
<TextView
android:id="@+id/vpn_state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:text="@string/state_disabled"
android:textColor="?android:textColorSecondary"
android:textSize="20sp" />
<TextView
android:id="@+id/vpn_profile_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:gravity="top"
android:text="@string/profile_label"
android:textColor="?android:textColorPrimary"
android:textSize="20sp"
android:visibility="gone" >
</TextView>
<TextView
android:id="@+id/vpn_profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="top"
android:textSize="20sp"
android:visibility="gone" >
</TextView>
</GridLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
@@ -50,8 +50,18 @@
<string name="alert_text_nocertfound_title">No CA certificate selected</string>
<string name="alert_text_nocertfound">Please select one or activate <i>Select automatically</i></string>
<!-- VPN state fragment -->
<string name="state_label">Status:</string>
<string name="profile_label">Profile:</string>
<string name="state_connecting">Connecting&#8230;</string>
<string name="state_connected">Connected</string>
<string name="state_disconnecting">Disconnecting&#8230;</string>
<string name="state_disabled">No active VPN</string>
<!-- Dialogs -->
<string name="login_title">Enter password to connect</string>
<string name="login_confirm">Connect</string>
<string name="connecting_title">Connecting: %1$s</string>
<string name="connecting_message">Establishing VPN with \""%1$s\".</string>
</resources>
@@ -18,23 +18,35 @@
package org.strongswan.android.ui;
import org.strongswan.android.R;
import org.strongswan.android.data.VpnProfile;
import org.strongswan.android.logic.VpnStateService;
import org.strongswan.android.logic.VpnStateService.State;
import org.strongswan.android.logic.VpnStateService.VpnStateListener;
import android.app.Fragment;
import android.app.ProgressDialog;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class VpnStateFragment extends Fragment implements VpnStateListener
{
private TextView mProfileNameView;
private TextView mProfileView;
private TextView mStateView;
private int stateBaseColor;
private ProgressDialog mProgressDialog;
private State mState;
private VpnStateService mService;
private final ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
@@ -47,6 +59,8 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
public void onServiceConnected(ComponentName name, IBinder service)
{
mService = ((VpnStateService.LocalBinder)service).getService();
mService.registerListener(VpnStateFragment.this);
updateView();
}
};
@@ -66,13 +80,30 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.vpn_state_fragment, null);
mStateView = (TextView)view.findViewById(R.id.vpn_state);
stateBaseColor = mStateView.getCurrentTextColor();
mProfileView = (TextView)view.findViewById(R.id.vpn_profile_label);
mProfileNameView = (TextView)view.findViewById(R.id.vpn_profile_name);
return view;
}
@Override
public void onStart()
{
super.onStart();
if (mService != null)
{
updateView();
}
}
@Override
public void onStop()
{
super.onStop();
hideProgressDialog();
}
@Override
@@ -89,5 +120,103 @@ public class VpnStateFragment extends Fragment implements VpnStateListener
@Override
public void stateChanged()
{
updateView();
}
public void updateView()
{
State state = mService.getState();
String name = "", gateway = "";
if (state != State.DISABLED)
{
VpnProfile profile = mService.getProfile();
if (profile != null)
{
name = profile.getName();
gateway = profile.getGateway();
}
}
if (state == mState)
{ /* avoid unnecessary updates */
return;
}
hideProgressDialog();
mProfileNameView.setText(name);
mState = state;
switch (state)
{
case DISABLED:
showProfile(false);
mStateView.setText(R.string.state_disabled);
mStateView.setTextColor(stateBaseColor);
break;
case CONNECTING:
showProfile(true);
showConnectDialog(name, gateway);
mStateView.setText(R.string.state_connecting);
mStateView.setTextColor(stateBaseColor);
break;
case CONNECTED:
showProfile(true);
mStateView.setText(R.string.state_connected);
mStateView.setTextColor(Color.GREEN);
break;
case DISCONNECTING:
showProfile(true);
showDisconnectDialog(name);
mStateView.setText(R.string.state_disconnecting);
mStateView.setTextColor(stateBaseColor);
break;
}
}
private void showProfile(boolean show)
{
mProfileView.setVisibility(show ? View.VISIBLE : View.GONE);
mProfileNameView.setVisibility(show ? View.VISIBLE : View.GONE);
}
private void hideProgressDialog()
{
if (mProgressDialog != null)
{
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
private void showConnectDialog(String profile, String gateway)
{
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setTitle(String.format(getString(R.string.connecting_title), profile));
mProgressDialog.setMessage(String.format(getString(R.string.connecting_message), gateway));
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.setButton(getString(android.R.string.cancel),
new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
if (mService != null)
{
mService.disconnect();
}
}
});
mProgressDialog.show();
}
private void showDisconnectDialog(String profile)
{
mProgressDialog = new ProgressDialog(getActivity());
mProgressDialog.setMessage(getString(R.string.state_disconnecting));
mProgressDialog.setIndeterminate(true);
mProgressDialog.setCancelable(false);
mProgressDialog.show();
}
}