Commits

Micha Kops committed 08bf1da

initial import

Comments (0)

Files changed (15)

AndroidManifest.xml

+<?xml version="1.0" encoding="utf-8"?>
+<manifest xmlns:android="http://schemas.android.com/apk/res/android"
+	package="com.hascode.android.screenlock" android:versionCode="1"
+	android:versionName="1.0">
+	<uses-sdk android:minSdkVersion="8" />
+	<uses-permission android:name="android.permission.CHANGE_CONFIGURATION"></uses-permission>
+	<uses-permission android:name="android.permission.WRITE_APN_SETTINGS"></uses-permission>
+	<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
+
+	<application android:icon="@drawable/icon" android:label="@string/app_name">
+		<service android:enabled="true" android:name="LockControlService">
+		</service>
+		<receiver android:name="LockControlWidgetProvider">
+			<intent-filter>
+				<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
+			</intent-filter>
+			<meta-data android:name="android.appwidget.provider"
+				android:resource="@xml/lockwidget_provider" />
+		</receiver>
+
+	</application>
+</manifest>

default.properties

+# This file is automatically generated by Android Tools.
+# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
+#
+# This file must be checked in Version Control Systems.
+#
+# To customize properties used by the Ant build system use,
+# "build.properties", and override values to adapt the script to your
+# project structure.
+
+# Project target.
+target=android-8

gen/com/hascode/android/screenlock/R.java

+/* AUTO-GENERATED FILE.  DO NOT MODIFY.
+ *
+ * This class was automatically generated by the
+ * aapt tool from the resource data it found.  It
+ * should not be modified by hand.
+ */
+
+package com.hascode.android.screenlock;
+
+public final class R {
+    public static final class attr {
+    }
+    public static final class color {
+        public static final int green=0x7f050001;
+        public static final int red=0x7f050000;
+    }
+    public static final class dimen {
+        public static final int states_margin_top=0x7f060001;
+        public static final int text_margin_left=0x7f060000;
+    }
+    public static final class drawable {
+        public static final int background_2x2=0x7f020000;
+        public static final int icon=0x7f020001;
+    }
+    public static final class id {
+        public static final int tvTime=0x7f080001;
+        public static final int txtOff=0x7f080003;
+        public static final int txtOn=0x7f080002;
+        public static final int widget_root=0x7f080000;
+    }
+    public static final class layout {
+        public static final int main=0x7f030000;
+    }
+    public static final class string {
+        public static final int app_name=0x7f070001;
+        public static final int configError=0x7f070004;
+        public static final int hello=0x7f070000;
+        public static final int screenIsLocked=0x7f070002;
+        public static final int screenIsUnlocked=0x7f070003;
+    }
+    public static final class xml {
+        public static final int lockwidget_provider=0x7f040000;
+    }
+}
+-optimizationpasses 5
+-dontusemixedcaseclassnames
+-dontskipnonpubliclibraryclasses
+-dontpreverify
+-verbose
+-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
+
+-keep public class * extends android.app.Activity
+-keep public class * extends android.app.Application
+-keep public class * extends android.app.Service
+-keep public class * extends android.content.BroadcastReceiver
+-keep public class * extends android.content.ContentProvider
+-keep public class * extends android.app.backup.BackupAgentHelper
+-keep public class * extends android.preference.Preference
+-keep public class com.android.vending.licensing.ILicensingService
+
+-keepclasseswithmembernames class * {
+    native <methods>;
+}
+
+-keepclasseswithmembers class * {
+    public <init>(android.content.Context, android.util.AttributeSet);
+}
+
+-keepclasseswithmembers class * {
+    public <init>(android.content.Context, android.util.AttributeSet, int);
+}
+
+-keepclassmembers class * extends android.app.Activity {
+   public void *(android.view.View);
+}
+
+-keepclassmembers enum * {
+    public static **[] values();
+    public static ** valueOf(java.lang.String);
+}
+
+-keep class * implements android.os.Parcelable {
+  public static final android.os.Parcelable$Creator *;
+}

res/drawable-hdpi/icon.png

Added
New image

res/drawable-ldpi/icon.png

Added
New image

res/drawable-mdpi/icon.png

Added
New image

res/drawable/background_2x2.png

Added
New image

res/layout/main.xml

+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+	android:id="@+id/widget_root" android:layout_height="200dp"
+	android:layout_width="160dp" android:background="@drawable/background_2x2"
+	android:orientation="vertical">
+	<TextView android:id="@+id/tvTime" android:layout_width="match_parent"
+		android:gravity="top|center_horizontal" android:paddingTop="30dp"
+		android:textColor="@android:color/black" android:text="Loading"
+		android:textSize="12pt" android:layout_height="wrap_content" />
+	<TextView android:text="On" android:id="@+id/txtOn"
+		android:textColor="@color/green" android:clickable="true"
+		android:layout_width="fill_parent" android:textSize="8pt"
+		android:layout_height="wrap_content" android:layout_marginLeft="@dimen/text_margin_left" android:layout_marginTop="@dimen/states_margin_top"/>
+	<TextView android:text="Off" android:id="@+id/txtOff"
+		android:textColor="@color/red" android:clickable="true"
+		android:layout_width="fill_parent" android:textSize="8pt"
+		android:layout_height="wrap_content" android:layout_marginLeft="@dimen/text_margin_left" android:layout_marginTop="@dimen/states_margin_top"/>
+</LinearLayout>

res/values/colors.xml

+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <color name="red">#ff0000</color>
+    <color name="green">#00ff00</color>
+    
+</resources>

res/values/dimens.xml

+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <dimen name="text_margin_left">20dp</dimen>
+    <dimen name="states_margin_top">20dp</dimen>
+</resources>

res/values/strings.xml

+<?xml version="1.0" encoding="utf-8"?>
+<resources>
+    <string name="hello">Hello World!</string>
+    <string name="app_name">Screen Lock Control Widget</string>
+    <string name="screenIsLocked">Lock On</string>
+    <string name="screenIsUnlocked">Lock Off</string>
+    <string name="configError">Error</string>
+</resources>

res/xml/lockwidget_provider.xml

+<?xml version="1.0" encoding="utf-8" ?>
+<appwidget-provider
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:minWidth="146dp"
+    android:initialLayout="@layout/main"
+    android:updatePeriodMillis="180000"
+    android:minHeight="144dp"/>

src/com/hascode/android/screenlock/LockControlService.java

+package com.hascode.android.screenlock;
+
+import android.app.Service;
+import android.appwidget.AppWidgetManager;
+import android.content.Context;
+import android.content.Intent;
+import android.os.IBinder;
+import android.provider.Settings;
+import android.provider.Settings.SettingNotFoundException;
+import android.util.Log;
+import android.widget.RemoteViews;
+
+public class LockControlService extends Service {
+	public static final String EXTRA_LOCK_ACTIVATED = "com.hascode.android.lockwidget.activated";
+	public static final String ACTION_LOCK = "com.hascode.android.lockwidget.lock";
+	public static final String ACTION_UNLOCK = "com.hascode.android.lockwidget.unlock";
+	public static final String ACTION_STATUS = "com.hascode.android.lockwidget.status";
+
+	private static final int DEFAULT_TIMEOUT_MILLIS = 6000;
+	private static final String APP_TAG = "com.hascode.android.lockwidget";
+
+	@Override
+	public void onStart(Intent intent, int startId) {
+		AppWidgetManager widgetManager = AppWidgetManager.getInstance(this
+				.getApplicationContext());
+
+		// fetch widgets to be updated
+		int[] widgetIds = intent
+				.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
+		if (widgetIds.length > 0) {
+			// fetching timeout, setting status
+			for (int widgetId : widgetIds) {
+				RemoteViews remoteViews = new RemoteViews(getPackageName(),
+						R.layout.main);
+				if (intent.hasExtra(EXTRA_LOCK_ACTIVATED)) {
+					Log.d(APP_TAG, "intent has extra enableLock");
+					if (intent.getBooleanExtra(EXTRA_LOCK_ACTIVATED, true)) {
+						lockOn();
+					} else {
+						lockOff();
+					}
+				} else {
+					Log.d(APP_TAG, "intent has no extra enableLock");
+				}
+				printStatus(getApplicationContext(), remoteViews);
+				widgetManager.updateAppWidget(widgetId, remoteViews);
+			}
+			stopSelf();
+		}
+		super.onStart(intent, startId);
+	}
+
+	private void lockOff() {
+		Log.d(APP_TAG, "lock settings from intent: enable=true");
+		Settings.System.putInt(getContentResolver(),
+				Settings.System.SCREEN_OFF_TIMEOUT, -1);
+	}
+
+	private void lockOn() {
+		Log.d(APP_TAG, "lock settings from intent: enable=false");
+		Settings.System.putInt(getContentResolver(),
+				Settings.System.SCREEN_OFF_TIMEOUT, DEFAULT_TIMEOUT_MILLIS);
+	}
+
+	@Override
+	public IBinder onBind(Intent intent) {
+		return null;
+	}
+
+	private void printStatus(final Context context,
+			final RemoteViews remoteViews) {
+		int screenTimeoutMillis = 0;
+		CharSequence status = getText(R.string.configError);
+		try {
+			screenTimeoutMillis = android.provider.Settings.System.getInt(
+					context.getContentResolver(),
+					Settings.System.SCREEN_OFF_TIMEOUT);
+		} catch (SettingNotFoundException e) {
+			Log.e(APP_TAG, "reading settings failed");
+		}
+
+		if (-1 == screenTimeoutMillis) {
+			status = getText(R.string.screenIsUnlocked);
+		} else {
+			status = getText(R.string.screenIsLocked);
+		}
+		remoteViews.setTextViewText(R.id.tvTime, status);
+	}
+}

src/com/hascode/android/screenlock/LockControlWidgetProvider.java

+package com.hascode.android.screenlock;
+
+import android.app.PendingIntent;
+import android.appwidget.AppWidgetManager;
+import android.appwidget.AppWidgetProvider;
+import android.content.Context;
+import android.content.Intent;
+import android.widget.RemoteViews;
+
+public class LockControlWidgetProvider extends AppWidgetProvider {
+	@Override
+	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
+			int[] appWidgetIds) {
+
+		// fetching our remote views
+		RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
+				R.layout.main);
+
+		// unlock intent
+		final Intent unlockIntent = createIntent(context, appWidgetIds);
+		unlockIntent.putExtra(LockControlService.EXTRA_LOCK_ACTIVATED, false);
+		unlockIntent.setAction(LockControlService.ACTION_UNLOCK);
+		final PendingIntent pendingUnlockIntent = createPendingIntent(context,
+				unlockIntent);
+
+		// lock intent
+		final Intent lockIntent = createIntent(context, appWidgetIds);
+		lockIntent.putExtra(LockControlService.EXTRA_LOCK_ACTIVATED, true);
+		unlockIntent.setAction(LockControlService.ACTION_LOCK);
+		final PendingIntent pendingLockIntent = createPendingIntent(context,
+				lockIntent);
+
+		// status intent
+		final Intent statusIntent = createIntent(context, appWidgetIds);
+		statusIntent.setAction(LockControlService.ACTION_LOCK);
+		final PendingIntent pendingStatusIntent = createPendingIntent(context,
+				statusIntent);
+
+		// bind click events to the pending intents
+		remoteViews.setOnClickPendingIntent(R.id.txtOn, pendingLockIntent);
+		remoteViews.setOnClickPendingIntent(R.id.txtOff, pendingUnlockIntent);
+		remoteViews.setOnClickPendingIntent(R.id.widget_root,
+				pendingStatusIntent);
+
+		appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
+		context.startService(statusIntent);
+	}
+
+	private Intent createIntent(Context context, int[] appWidgetIds) {
+		Intent updateIntent = new Intent(context.getApplicationContext(),
+				LockControlService.class);
+		updateIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
+				appWidgetIds);
+		return updateIntent;
+	}
+
+	private PendingIntent createPendingIntent(Context context,
+			Intent updateIntent) {
+		PendingIntent pendingIntent = PendingIntent.getService(
+				context.getApplicationContext(), 0, updateIntent,
+				PendingIntent.FLAG_UPDATE_CURRENT);
+		return pendingIntent;
+	}
+
+}