20 |
|
|
21 |
//The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive() |
//The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive() |
22 |
private static Side9Data usedData; |
private static Side9Data usedData; |
23 |
|
private static Bitmap usedBitmap; |
24 |
|
|
25 |
|
public Side9WidgetProvider() { |
26 |
|
Log.i("Side9Pigen", "WidgetProvider constructor called"); |
27 |
|
} |
28 |
|
|
29 |
|
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) { |
30 |
|
// Perform this loop procedure for each App Widget that belongs to this provider |
31 |
|
final int N = appWidgetIds.length; |
32 |
|
for (int i=0; i<N; i++) { |
33 |
|
int appWidgetId = appWidgetIds[i]; |
34 |
|
|
35 |
|
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
36 |
|
|
37 |
|
if (image != null) { |
38 |
|
views.setImageViewBitmap(R.id.side9picture, image); |
39 |
|
} else { |
40 |
|
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); |
41 |
|
} |
42 |
|
//views.setTextViewText(R.id.caption, newData.caption); |
43 |
|
|
44 |
|
|
45 |
|
// Launch a browser when user clicks on the image |
46 |
|
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Side9Xml.BASEURL)); |
47 |
|
viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
48 |
|
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
49 |
|
views.setOnClickPendingIntent(R.id.side9picture, pending); |
50 |
|
|
51 |
|
// Tell the AppWidgetManager to perform an update on the current App Widget |
52 |
|
appWidgetManager.updateAppWidget(appWidgetId, views); |
53 |
|
|
54 |
|
Log.i("Side9Pigen", "done " + appWidgetId); |
55 |
|
} |
56 |
|
} |
57 |
|
|
58 |
@Override |
@Override |
59 |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
60 |
super.onUpdate(context, appWidgetManager, appWidgetIds); |
super.onUpdate(context, appWidgetManager, appWidgetIds); |
61 |
|
|
62 |
|
Log.i("Side9Pigen", "onUpdate:"); |
63 |
|
|
64 |
final int N = appWidgetIds.length; // |
if (usedBitmap == null) { //load default view |
65 |
|
usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo); |
66 |
Log.e("Side9Pigen", "onUpdate:" + N); |
} |
67 |
|
|
68 |
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
69 |
if (connMgr.getBackgroundDataSetting() == false) |
if (connMgr.getBackgroundDataSetting() == false) |
70 |
{ |
{ |
71 |
Log.e("Side9Pigen", "background data disabled"); |
Log.i("Side9Pigen", "background data disabled"); |
72 |
return; |
return; |
73 |
} |
} |
74 |
|
|
78 |
if (! newData.equals(usedData)) { |
if (! newData.equals(usedData)) { |
79 |
|
|
80 |
|
|
81 |
Log.e("Side9Pigen", "(Re)loading image:" + newData.url); |
Log.i("Side9Pigen", "(Re)loading image:" + newData.url); |
82 |
|
|
83 |
URL imgUrl = new URL( newData.url ); |
URL imgUrl = new URL( newData.url ); |
84 |
|
|
85 |
URLConnection conn = imgUrl.openConnection(); |
URLConnection conn = imgUrl.openConnection(); |
86 |
|
conn.setConnectTimeout(2000); |
87 |
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); |
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); |
88 |
|
|
|
|
|
89 |
usedData = newData; // if we made it to here without exceptions, save the new data |
usedData = newData; // if we made it to here without exceptions, save the new data |
90 |
|
usedBitmap = image; |
91 |
|
|
92 |
|
} // endif |
93 |
// Perform this loop procedure for each App Widget that belongs to this provider |
|
|
for (int i=0; i<N; i++) { |
|
|
int appWidgetId = appWidgetIds[i]; |
|
|
|
|
|
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
|
|
|
|
|
views.setImageViewBitmap(R.id.side9picture, image); |
|
|
|
|
|
// Launch a browser when user clicks on the image |
|
|
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Side9Xml.BASEURL)); |
|
|
viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
|
|
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
|
|
views.setOnClickPendingIntent(R.id.side9picture, pending); |
|
|
|
|
|
// Tell the AppWidgetManager to perform an update on the current App Widget |
|
|
appWidgetManager.updateAppWidget(appWidgetId, views); |
|
|
} |
|
|
} //END if (data.equals) |
|
|
|
|
94 |
} catch (Exception e) { |
} catch (Exception e) { |
95 |
Log.e("Side9Pigen", "update failed", e); |
Log.e("Side9Pigen", "update failed", e); |
96 |
} |
} |
97 |
|
|
98 |
|
setImage(context,appWidgetManager,appWidgetIds, usedBitmap); |
99 |
|
Log.i("Side9Pigen", "update completed"); |
100 |
} |
} |
101 |
|
|
102 |
|
|
106 |
super.onDisabled(context); |
super.onDisabled(context); |
107 |
|
|
108 |
usedData = null; //free memory |
usedData = null; //free memory |
109 |
|
usedBitmap = null; |
110 |
} |
} |
111 |
|
|
112 |
} |
} |