Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a widget that allows the post code to be shown on the home screen #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
<category android:name="android.intent.category.LAUNCHER"></category>
</intent-filter>
</activity>
<receiver android:name="PostcodeAppWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/postcode_appwidget_info"/>
</receiver>
</application>
<uses-sdk android:minSdkVersion="11" />

Expand Down
117 changes: 117 additions & 0 deletions src/main/java/net/tevp/postcode/PostcodeAppWidgetProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package net.tevp.postcode;

import android.app.Activity;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.opengl.Visibility;
import android.os.Build;
import android.view.View;
import android.widget.RemoteViews;
import android.widget.Toast;

import java.text.DateFormat;

import static android.os.Build.VERSION.SDK_INT;

public class PostcodeAppWidgetProvider extends AppWidgetProvider implements PostcodeListener {

private static final String ACTION_UPDATE_AND_OPEN = "ACTION_UPDATE_AND_OPEN";
private static final String POSTCODE_PREFERENCE_NAME = "Postcode";
private static final String LAST_POSTCODE_PREFERENCE_KEY = "lastPostcode";

private final PostcodeBackend pb = new PostcodeBackend();
private long lastLocationUpdateTime;
private Context context;

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
pb.getPostcode(context, this, true);
this.context = context;
}

@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if(intent.getAction() == null) {
if(SDK_INT > Build.VERSION_CODES.HONEYCOMB) {
ClipboardManager manager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
if (manager != null) {
String lastPostcode = context.getSharedPreferences(POSTCODE_PREFERENCE_NAME, Activity.MODE_PRIVATE).getString(LAST_POSTCODE_PREFERENCE_KEY,"");
Toast.makeText(context, "Copied '"+lastPostcode+"' to clipboard ", Toast.LENGTH_SHORT).show();
manager.setPrimaryClip(ClipData.newPlainText("Postcode", lastPostcode));
}
}
} else if (intent.getAction().equals(ACTION_UPDATE_AND_OPEN)) {
this.context = context;
pb.getPostcode(context, this, true);
context.startActivity(new Intent(context,Postcode.class));
}
}

@Override
public void postcodeChange(final String postcode) {
final ComponentName thisWidget = new ComponentName(context, PostcodeAppWidgetProvider.class);
final AppWidgetManager manager = AppWidgetManager.getInstance(context);
final RemoteViews views = buildRemoteViews(context,postcode);
views.setOnClickPendingIntent(R.id.postcodewidget,createUpdatePendingIntent(manager.getAppWidgetIds(thisWidget), true));
views.setOnClickPendingIntent(R.id.refresh, createUpdatePendingIntent(manager.getAppWidgetIds(thisWidget), false));

if(SDK_INT > 11) {
views.setOnClickPendingIntent(R.id.copy, createCopyPendingIntent(manager.getAppWidgetIds(thisWidget)));
views.setViewVisibility(R.id.copy, View.VISIBLE);
}
views.setViewVisibility(R.id.refresh, View.VISIBLE);
manager.updateAppWidget(thisWidget, views);
SharedPreferences pref = context.getSharedPreferences(POSTCODE_PREFERENCE_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(LAST_POSTCODE_PREFERENCE_KEY, postcode);
editor.commit();
}

private PendingIntent createUpdatePendingIntent(final int[] widgetIds, boolean showPostcodeApp) {
final Intent update = new Intent(context, PostcodeAppWidgetProvider.class);
if(showPostcodeApp) {
update.setAction(ACTION_UPDATE_AND_OPEN);
}
else{
update.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
}

update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
return PendingIntent.getBroadcast(context,0,update,0);
}

private PendingIntent createCopyPendingIntent(final int[] widgetIds) {
final Intent update = new Intent(context, PostcodeAppWidgetProvider.class);
update.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds);
return PendingIntent.getBroadcast(context,0,update,0);
}

@Override
public void updatedLocation(final Location location) {
lastLocationUpdateTime = location.getTime();
}

@Override
public void locationFindFail() {
}

@Override
public void postcodeLookupFail() {
}

private RemoteViews buildRemoteViews(final Context context,final String postCode) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
views.setTextViewText(R.id.postcode, postCode);
views.setTextViewText(R.id.time, DateFormat.getTimeInstance(DateFormat.SHORT).format(lastLocationUpdateTime));
return views;
}
}
Binary file added src/main/res/drawable/copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/main/res/drawable/copy_button_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:tint="#000" android:src="@drawable/copy" />
</item>
</layer-list>
Binary file added src/main/res/drawable/preview.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/main/res/drawable/refresh_button_background.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap android:tint="#000" android:src="@android:drawable/stat_notify_sync_noanim" />
</item>
</layer-list>
72 changes: 72 additions & 0 deletions src/main/res/layout/appwidget.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/postcodewidget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="8dp">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
style="@style/widget_title_background"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#FFF"
android:padding="4dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:visibility="invisible"
android:id="@+id/copy"
android:layout_width="25dp"
android:layout_height="25dp"
android:gravity="center"
android:layout_marginRight="5dp"
android:background="@drawable/copy_button_background" />

<TextView
android:id="@+id/postcode"
style="@style/widget_postcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/locating"
android:textColor="@android:color/black" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:visibility="invisible"
android:id="@+id/refresh"
android:layout_width="25dp"
android:layout_height="25dp"
android:gravity="center"
android:layout_marginRight="5dp"
android:background="@drawable/refresh_button_background"/>

<TextView
android:id="@+id/time"
style="@style/widget_postcode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#444" />

</LinearLayout>
</LinearLayout>
</LinearLayout>
6 changes: 6 additions & 0 deletions src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="widget_padding">8dp</dimen>
<dimen name="button_width">50dp</dimen>
<dimen name="button_height">50dp</dimen>
</resources>
3 changes: 3 additions & 0 deletions src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
<resources>

<string name="app_name">Postcode</string>
<string name="locating">Locating</string>
<string name="updating">Updating</string>
<string name="lookup_fail">Lookup Failed</string>
</resources>
12 changes: 12 additions & 0 deletions src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="widget_postcode">
<item name="android:textSize">20sp</item>
</style>

<style name="widget_title_background">
<item name="android:background">#F00</item>
<item name="android:textColor">#FFF</item>
<item name="android:paddingLeft">8dp</item>
</style>
</resources>
10 changes: 10 additions & 0 deletions src/main/res/xml/postcode_appwidget_info.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="150dp"
android:minHeight="50dp"
android:updatePeriodMillis="1800000"
android:widgetCategory="home_screen|keyguard"
android:initialLayout="@layout/appwidget"
android:initialKeyguardLayout="@layout/appwidget"
android:previewImage="@drawable/preview"/>