From e7908526fda0c4a17580ec304e717ef20f800617 Mon Sep 17 00:00:00 2001 From: Tobias Brunner Date: Thu, 9 Aug 2012 11:11:32 +0200 Subject: [PATCH] Add a fragment to MainActivity which will display the current VPN state The fragment is bound to the VpnStateService and registered as listener. --- .../android/res/drawable/vpn_state_background.xml | 21 +++++ src/frontends/android/res/layout/main.xml | 13 ++- .../android/res/layout/vpn_state_fragment.xml | 30 +++++++ .../strongswan/android/ui/VpnStateFragment.java | 93 ++++++++++++++++++++++ 4 files changed, 154 insertions(+), 3 deletions(-) create mode 100644 src/frontends/android/res/drawable/vpn_state_background.xml create mode 100644 src/frontends/android/res/layout/vpn_state_fragment.xml create mode 100644 src/frontends/android/src/org/strongswan/android/ui/VpnStateFragment.java diff --git a/src/frontends/android/res/drawable/vpn_state_background.xml b/src/frontends/android/res/drawable/vpn_state_background.xml new file mode 100644 index 0000000..24f469a --- /dev/null +++ b/src/frontends/android/res/drawable/vpn_state_background.xml @@ -0,0 +1,21 @@ + + + + + + + \ No newline at end of file diff --git a/src/frontends/android/res/layout/main.xml b/src/frontends/android/res/layout/main.xml index 104a26d..1c7973e 100644 --- a/src/frontends/android/res/layout/main.xml +++ b/src/frontends/android/res/layout/main.xml @@ -14,14 +14,21 @@ for more details. --> + + + android:layout_height="0dp" + android:layout_weight="1" /> diff --git a/src/frontends/android/res/layout/vpn_state_fragment.xml b/src/frontends/android/res/layout/vpn_state_fragment.xml new file mode 100644 index 0000000..c3adeae --- /dev/null +++ b/src/frontends/android/res/layout/vpn_state_fragment.xml @@ -0,0 +1,30 @@ + + + + + + + diff --git a/src/frontends/android/src/org/strongswan/android/ui/VpnStateFragment.java b/src/frontends/android/src/org/strongswan/android/ui/VpnStateFragment.java new file mode 100644 index 0000000..5c4ffdd --- /dev/null +++ b/src/frontends/android/src/org/strongswan/android/ui/VpnStateFragment.java @@ -0,0 +1,93 @@ +/* + * Copyright (C) 2012 Tobias Brunner + * Copyright (C) 2012 Giuliano Grassi + * Copyright (C) 2012 Ralf Sager + * Hochschule fuer Technik Rapperswil + * + * This program is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the + * Free Software Foundation; either version 2 of the License, or (at your + * option) any later version. See . + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +package org.strongswan.android.ui; + +import org.strongswan.android.R; +import org.strongswan.android.logic.VpnStateService; +import org.strongswan.android.logic.VpnStateService.VpnStateListener; + +import android.app.Fragment; +import android.app.Service; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.Bundle; +import android.os.IBinder; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +public class VpnStateFragment extends Fragment implements VpnStateListener +{ + private VpnStateService mService; + private final ServiceConnection mServiceConnection = new ServiceConnection() { + @Override + public void onServiceDisconnected(ComponentName name) + { + mService = null; + } + + @Override + public void onServiceConnected(ComponentName name, IBinder service) + { + mService = ((VpnStateService.LocalBinder)service).getService(); + } + }; + + @Override + public void onCreate(Bundle savedInstanceState) + { + super.onCreate(savedInstanceState); + + /* bind to the service only seems to work from the ApplicationContext */ + Context context = getActivity().getApplicationContext(); + context.bindService(new Intent(context, VpnStateService.class), + mServiceConnection, Service.BIND_AUTO_CREATE); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) + { + View view = inflater.inflate(R.layout.vpn_state_fragment, null); + return view; + } + + @Override + public void onStop() + { + super.onStop(); + } + + @Override + public void onDestroy() + { + super.onDestroy(); + if (mService != null) + { + mService.unregisterListener(this); + getActivity().getApplicationContext().unbindService(mServiceConnection); + } + } + + @Override + public void stateChanged() + { + } +} -- 2.7.4