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"
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
public void onServiceConnected(ComponentName name, IBinder service)
{
mService = ((VpnStateService.LocalBinder)service).getService();
+ mService.registerListener(VpnStateFragment.this);
+ updateView();
}
};
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
@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();
}
}