1 |
package dk.thoerup.side9;
|
2 |
|
3 |
import java.net.URL;
|
4 |
import java.net.URLConnection;
|
5 |
|
6 |
import android.app.PendingIntent;
|
7 |
import android.appwidget.AppWidgetManager;
|
8 |
import android.appwidget.AppWidgetProvider;
|
9 |
import android.content.Context;
|
10 |
import android.content.Intent;
|
11 |
import android.graphics.Bitmap;
|
12 |
import android.graphics.BitmapFactory;
|
13 |
import android.net.ConnectivityManager;
|
14 |
import android.net.Uri;
|
15 |
import android.util.Log;
|
16 |
import android.widget.RemoteViews;
|
17 |
|
18 |
public class Side9WidgetProvider extends AppWidgetProvider {
|
19 |
|
20 |
|
21 |
//The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()
|
22 |
private static Side9Data usedData;
|
23 |
private static Bitmap usedBitmap;
|
24 |
private static long timestamp;
|
25 |
|
26 |
final static long UDPATESPAN = 6*60*60*1000;
|
27 |
|
28 |
|
29 |
static {
|
30 |
timestamp = 0L;
|
31 |
}
|
32 |
|
33 |
public Side9WidgetProvider() {
|
34 |
Log.i("Side9Pigen", "WidgetProvider constructor called");
|
35 |
}
|
36 |
|
37 |
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {
|
38 |
// Perform this loop procedure for each App Widget that belongs to this provider
|
39 |
final int N = appWidgetIds.length;
|
40 |
for (int i=0; i<N; i++) {
|
41 |
int appWidgetId = appWidgetIds[i];
|
42 |
|
43 |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);
|
44 |
|
45 |
if (image != null) {
|
46 |
views.setImageViewBitmap(R.id.side9picture, image);
|
47 |
} else {
|
48 |
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);
|
49 |
}
|
50 |
//views.setTextViewText(R.id.caption, newData.caption);
|
51 |
|
52 |
|
53 |
// Launch a browser when user clicks on the image
|
54 |
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://ekstrabladet.dk/side9/"));
|
55 |
viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
56 |
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
|
57 |
views.setOnClickPendingIntent(R.id.side9picture, pending);
|
58 |
|
59 |
// Tell the AppWidgetManager to perform an update on the current App Widget
|
60 |
appWidgetManager.updateAppWidget(appWidgetId, views);
|
61 |
|
62 |
Log.i("Side9Pigen", "done " + appWidgetId);
|
63 |
}
|
64 |
}
|
65 |
|
66 |
@Override
|
67 |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
68 |
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
69 |
|
70 |
Log.i("Side9Pigen", "onUpdate:");
|
71 |
|
72 |
if (usedBitmap == null) { //load default view
|
73 |
usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
|
74 |
}
|
75 |
|
76 |
|
77 |
|
78 |
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
79 |
if (connMgr.getBackgroundDataSetting() == false)
|
80 |
{
|
81 |
Log.i("Side9Pigen", "background data disabled");
|
82 |
return;
|
83 |
}
|
84 |
|
85 |
if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
|
86 |
Log.i("Side9Pigen", "time elapsed, force XML reload");
|
87 |
usedData = null;
|
88 |
}
|
89 |
|
90 |
try {
|
91 |
Side9Data newData = Side9Xml.loadXml();
|
92 |
|
93 |
if (! newData.equals(usedData)) {
|
94 |
|
95 |
|
96 |
Log.i("Side9Pigen", "(Re)loading image:" + newData.url);
|
97 |
|
98 |
URL imgUrl = new URL( newData.url );
|
99 |
|
100 |
URLConnection conn = imgUrl.openConnection();
|
101 |
conn.setConnectTimeout(2500);
|
102 |
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() );
|
103 |
|
104 |
usedData = newData; // if we made it to here without exceptions, save the new data
|
105 |
usedBitmap = image;
|
106 |
timestamp = System.currentTimeMillis();
|
107 |
|
108 |
} // endif
|
109 |
|
110 |
} catch (Exception e) {
|
111 |
Log.e("Side9Pigen", "update failed", e);
|
112 |
}
|
113 |
|
114 |
setImage(context,appWidgetManager,appWidgetIds, usedBitmap);
|
115 |
Log.i("Side9Pigen", "update completed");
|
116 |
}
|
117 |
|
118 |
|
119 |
//Called when the last widget is removed/disabled
|
120 |
@Override
|
121 |
public void onDisabled(Context context) {
|
122 |
super.onDisabled(context);
|
123 |
|
124 |
usedData = null; //free memory
|
125 |
usedBitmap = null;
|
126 |
}
|
127 |
|
128 |
}
|