diff -r 37f16fd90039 AndroidManifest.xml
--- a/AndroidManifest.xml	Tue Oct 12 23:46:41 2010 +0200
+++ b/AndroidManifest.xml	Mon Jan 17 16:44:08 2011 +0100
@@ -76,6 +76,19 @@
 			</intent-filter>
 		</receiver>
 
+		<receiver android:name="de.schaeuffelhut.android.openvpn.widget.OpenVpnWidgetProvider" 
+			android:label="@string/app_name"
+			android:icon="@drawable/logo">
+			<intent-filter>
+				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
+				<action android:name="de.schaeuffelhut.android.openvpn.Intents.DAEMON_STATE_CHANGED" />
+			</intent-filter>
+			<meta-data android:name="android.appwidget.provider"
+				android:resource="@xml/appwidget_info" />
+		</receiver>
+		
+		<service android:name="de.schaeuffelhut.android.openvpn.widget.OpenVpnWidgetProvider$ToggleService" />
+		
 </application>
 
 <uses-permission android:name="android.permission.INTERNET"></uses-permission>
diff -r 37f16fd90039 res/drawable/widget_selector.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/drawable/widget_selector.xml	Mon Jan 17 16:44:08 2011 +0100
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 
+ Copyright 2011 Christian Erpelding
+ 
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ 
+ 	http://www.apache.org/licenses/LICENSE-2.0
+ 
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. 
+ -->
+ <selector xmlns:android="http://schemas.android.com/apk/res/android">
+
+    <item android:state_pressed="true"
+    	android:drawable="@drawable/yellow_trans"/>
+    <item android:state_selected="true"
+    	android:drawable="@drawable/yellow_trans"/>
+    <item android:state_checked="true"
+    	android:drawable="@drawable/yellow_trans"/>
+    <item
+    	android:drawable="@drawable/transparent"/>
+
+</selector>
diff -r 37f16fd90039 res/layout/appwidget.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/layout/appwidget.xml	Mon Jan 17 16:44:08 2011 +0100
@@ -0,0 +1,43 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 
+ Copyright 2011 Christian Erpelding
+ 
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ 
+ 	http://www.apache.org/licenses/LICENSE-2.0
+ 
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. 
+ -->
+ <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
+	android:layout_width="wrap_content"
+	android:layout_height="wrap_content"
+	>
+	<ImageButton 
+		android:src="@drawable/widget_selector"
+		android:id="@+id/toggle"
+		android:layout_width="wrap_content"
+		android:layout_height="wrap_content"
+		android:background="@drawable/logo"
+		android:layout_gravity="center"
+	/>
+	<TextView 
+		android:id="@+id/status"
+		android:layout_width="wrap_content"
+		android:layout_height="wrap_content"
+		android:layout_gravity="center"
+		android:gravity="center"
+		android:text="Status"
+		android:textColor="#000"
+		android:shadowRadius="3.0"
+		android:shadowColor="#fff"
+		android:textSize="10sp"
+		android:padding="5dip"
+		android:background="@drawable/white_trans"
+	/>
+</FrameLayout>
\ No newline at end of file
diff -r 37f16fd90039 res/values/colors.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/values/colors.xml	Mon Jan 17 16:44:08 2011 +0100
@@ -0,0 +1,5 @@
+<resources>
+	<drawable name="white_trans">#9fff</drawable>
+	<drawable name="yellow_trans">#9ff0</drawable>
+	<drawable name="transparent">#0000</drawable>
+</resources>
\ No newline at end of file
diff -r 37f16fd90039 res/xml/appwidget_info.xml
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/res/xml/appwidget_info.xml	Mon Jan 17 16:44:08 2011 +0100
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- 
+ Copyright 2011 Christian Erpelding
+ 
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+ 
+ 	http://www.apache.org/licenses/LICENSE-2.0
+ 
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License. 
+ -->
+ <appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
+	android:minWidth="72dip"
+	android:minHeight="72dip"
+	android:updatePeriodMillis="0"
+	android:initialLayout="@layout/appwidget" />
diff -r 37f16fd90039 src/de/schaeuffelhut/android/openvpn/DaemonEnabler.java
--- a/src/de/schaeuffelhut/android/openvpn/DaemonEnabler.java	Tue Oct 12 23:46:41 2010 +0200
+++ b/src/de/schaeuffelhut/android/openvpn/DaemonEnabler.java	Mon Jan 17 16:44:08 2011 +0100
@@ -278,7 +278,7 @@
 	}
 
 
-	private String getHumanReadableDaemonState(int daemonState) {
+	public static String getHumanReadableDaemonState(int daemonState) {
 		switch (daemonState) {
 		case Intents.DAEMON_STATE_STARTUP:
 			return "Startup";
@@ -293,7 +293,7 @@
 		}
 	}
 
-	private String getHumanReadableNetworkState(int networkState) {
+	public static String getHumanReadableNetworkState(int networkState) {
 		switch (networkState) {
 		case Intents.NETWORK_STATE_UNKNOWN:
 			return "Unknown";
diff -r 37f16fd90039 src/de/schaeuffelhut/android/openvpn/widget/OpenVpnWidgetProvider.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/de/schaeuffelhut/android/openvpn/widget/OpenVpnWidgetProvider.java	Mon Jan 17 16:44:08 2011 +0100
@@ -0,0 +1,107 @@
+/**
+ * Copyright 2011 Christian Erpelding
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License. 
+ */
+package de.schaeuffelhut.android.openvpn.widget;
+
+import android.app.PendingIntent;
+import android.app.Service;
+import android.appwidget.AppWidgetManager;
+import android.appwidget.AppWidgetProvider;
+import android.content.ComponentName;
+import android.content.Context;
+import android.content.Intent;
+import android.os.IBinder;
+import android.preference.PreferenceManager;
+import android.widget.RemoteViews;
+import de.schaeuffelhut.android.openvpn.DaemonEnabler;
+import de.schaeuffelhut.android.openvpn.Intents;
+import de.schaeuffelhut.android.openvpn.Preferences;
+import de.schaeuffelhut.android.openvpn.R;
+import de.schaeuffelhut.android.openvpn.service.OpenVpnService;
+
+public class OpenVpnWidgetProvider extends AppWidgetProvider {
+
+	@Override
+	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
+			int[] appWidgetIds) {
+		super.onUpdate(context, appWidgetManager, appWidgetIds);
+
+		final int N = appWidgetIds.length;
+
+		// Perform this loop procedure for each App Widget that belongs to this provider
+		for (int i=0; i<N; i++) {
+			int appWidgetId = appWidgetIds[i];
+
+			// Tell the AppWidgetManager to perform an update on the current App Widget
+			appWidgetManager.updateAppWidget(appWidgetId, getRemoteViewDaemonStateOnly(context, Intents.DAEMON_STATE_UNKNOWN));
+		}
+	}
+
+	private RemoteViews getRemoteViewDaemonStateOnly(Context context, int daemonState) {
+		// Create an Intent to toggle the VPN service
+		Intent intent = new Intent(context, ToggleService.class);
+		PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
+
+		// Get the layout for the App Widget and attach an on-click listener to the button
+		RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
+		views.setOnClickPendingIntent(R.id.toggle, pendingIntent);
+
+		boolean active = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(Preferences.KEY_OPENVPN_ENABLED, false);
+		if (daemonState == Intents.DAEMON_STATE_UNKNOWN) {
+			// DaemonState is unknown so use the state from the preferences 
+			if (active) {
+				daemonState = Intents.DAEMON_STATE_ENABLED;
+			} else {
+				daemonState = Intents.DAEMON_STATE_DISABLED;
+			}
+		}
+
+		views.setTextViewText(R.id.status, DaemonEnabler.getHumanReadableDaemonState(daemonState));
+		return views;
+	}
+
+	@Override
+	public void onReceive(Context context, Intent intent) {
+		super.onReceive(context, intent);
+		// Log.d("OpenVpnWidget", "onReceive: "+intent!=null?
+		// intent.getAction()+", "+intent.getIntExtra(Intents.EXTRA_DAEMON_STATE, 0)+", "+intent.getIntExtra(Intents.EXTRA_NETWORK_STATE, 0)
+		// :"n/a");
+		if (intent != null && Intents.DEAMON_STATE_CHANGED.equals(intent.getAction())) {
+			ComponentName me = new ComponentName(context, OpenVpnWidgetProvider.class);
+			AppWidgetManager mgr=AppWidgetManager.getInstance(context);
+			mgr.updateAppWidget(me, getRemoteViewDaemonStateOnly(context, intent.getIntExtra(Intents.EXTRA_DAEMON_STATE, Intents.DAEMON_STATE_UNKNOWN)));
+		}
+	}
+
+	public static class ToggleService extends Service {
+
+		@Override
+		public IBinder onBind(Intent intent) {
+			return null;
+		}
+
+		@Override
+		public void onStart(Intent intent, int startId) {
+			super.onStart(intent, startId);
+			boolean active = PreferenceManager.getDefaultSharedPreferences(this).getBoolean(Preferences.KEY_OPENVPN_ENABLED, false);
+			// Log.d("OpenVpnWidget", "ToggleService.onStart, a="+active);
+			if (!active) {
+				startService(new Intent(this, OpenVpnService.class));
+			} else {
+				stopService(new Intent(this, OpenVpnService.class));
+			}
+		}
+	}
+}

