3 |
import java.net.URL; |
import java.net.URL; |
4 |
import java.net.URLConnection; |
import java.net.URLConnection; |
5 |
|
|
6 |
|
import android.app.PendingIntent; |
7 |
import android.appwidget.AppWidgetManager; |
import android.appwidget.AppWidgetManager; |
8 |
import android.appwidget.AppWidgetProvider; |
import android.appwidget.AppWidgetProvider; |
|
import android.appwidget.AppWidgetProviderInfo; |
|
9 |
import android.content.Context; |
import android.content.Context; |
10 |
|
import android.content.Intent; |
11 |
import android.graphics.Bitmap; |
import android.graphics.Bitmap; |
12 |
import android.graphics.BitmapFactory; |
import android.graphics.BitmapFactory; |
13 |
|
import android.net.ConnectivityManager; |
14 |
|
import android.net.Uri; |
15 |
import android.util.Log; |
import android.util.Log; |
16 |
import android.widget.RemoteViews; |
import android.widget.RemoteViews; |
17 |
|
|
18 |
public class Side9WidgetProvider extends AppWidgetProvider { |
public class Side9WidgetProvider extends AppWidgetProvider { |
19 |
|
|
20 |
private Side9Data usedData; |
|
21 |
|
//The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive() |
22 |
|
private static Side9Data usedData; |
23 |
|
|
24 |
|
public Side9WidgetProvider() { |
25 |
|
Log.i("Side9Pigen", "WidgetProvider constructor called"); |
26 |
|
} |
27 |
|
|
28 |
@Override |
@Override |
29 |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
31 |
|
|
32 |
final int N = appWidgetIds.length; // |
final int N = appWidgetIds.length; // |
33 |
|
|
34 |
Log.e("onUpdate", "onUpdate:" + N); |
Log.e("Side9Pigen", "onUpdate:" + N); |
35 |
|
|
36 |
|
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
37 |
|
if (connMgr.getBackgroundDataSetting() == false) |
38 |
|
{ |
39 |
|
Log.i("Side9Pigen", "background data disabled"); |
40 |
|
return; |
41 |
|
} |
42 |
|
|
43 |
try { |
try { |
44 |
Side9Data data = Side9Xml.loadXml(); |
Side9Data newData = Side9Xml.loadXml(); |
45 |
|
|
46 |
if (! data.equals(usedData)) { |
if (! newData.equals(usedData)) { |
47 |
usedData = data; // save the new data |
|
48 |
|
|
49 |
|
Log.i("Side9Pigen", "(Re)loading image:" + newData.url); |
50 |
|
|
51 |
URL imgUrl = new URL( data.url ); |
URL imgUrl = new URL( newData.url ); |
52 |
|
|
53 |
URLConnection conn = imgUrl.openConnection(); |
URLConnection conn = imgUrl.openConnection(); |
54 |
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); |
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
usedData = newData; // if we made it to here without exceptions, save the new data |
59 |
|
|
60 |
|
|
61 |
// Perform this loop procedure for each App Widget that belongs to this provider |
// Perform this loop procedure for each App Widget that belongs to this provider |
62 |
for (int i=0; i<N; i++) { |
for (int i=0; i<N; i++) { |
63 |
int appWidgetId = appWidgetIds[i]; |
int appWidgetId = appWidgetIds[i]; |
64 |
|
|
65 |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
66 |
|
|
|
|
|
67 |
views.setImageViewBitmap(R.id.side9picture, image); |
views.setImageViewBitmap(R.id.side9picture, image); |
68 |
|
views.setTextViewText(R.id.caption, newData.caption); |
69 |
|
|
70 |
|
|
71 |
|
// Launch a browser when user clicks on the image |
72 |
|
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Side9Xml.BASEURL)); |
73 |
|
viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
74 |
|
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
75 |
|
views.setOnClickPendingIntent(R.id.side9picture, pending); |
76 |
|
|
77 |
// Tell the AppWidgetManager to perform an update on the current App Widget |
// Tell the AppWidgetManager to perform an update on the current App Widget |
78 |
appWidgetManager.updateAppWidget(appWidgetId, views); |
appWidgetManager.updateAppWidget(appWidgetId, views); |
79 |
|
|
80 |
|
Log.i("Side9Pigen", "done " + appWidgetId); |
81 |
} |
} |
82 |
} //END if (data.equals) |
} //END if (data.equals) |
83 |
|
|
84 |
} catch (Exception e) { |
} catch (Exception e) { |
85 |
Log.e("UPDATE", "UPDATE", e); |
Log.e("Side9Pigen", "update failed", e); |
86 |
} |
} |
87 |
} |
} |
88 |
|
|
89 |
|
|
90 |
|
//Called when the last widget is removed/disabled |
91 |
|
@Override |
92 |
|
public void onDisabled(Context context) { |
93 |
|
super.onDisabled(context); |
94 |
|
|
95 |
|
usedData = null; //free memory |
96 |
|
} |
97 |
|
|
98 |
} |
} |