1 |
package dk.thoerup.side9; |
package dk.thoerup.side9; |
2 |
|
|
3 |
|
import java.io.IOException; |
4 |
import java.net.URL; |
import java.net.URL; |
5 |
import java.net.URLConnection; |
import java.net.URLConnection; |
6 |
|
|
7 |
|
import android.app.PendingIntent; |
8 |
import android.appwidget.AppWidgetManager; |
import android.appwidget.AppWidgetManager; |
9 |
import android.appwidget.AppWidgetProvider; |
import android.appwidget.AppWidgetProvider; |
10 |
import android.content.Context; |
import android.content.Context; |
11 |
|
import android.content.Intent; |
12 |
import android.graphics.Bitmap; |
import android.graphics.Bitmap; |
13 |
import android.graphics.BitmapFactory; |
import android.graphics.BitmapFactory; |
14 |
|
import android.net.ConnectivityManager; |
15 |
|
import android.net.Uri; |
16 |
import android.util.Log; |
import android.util.Log; |
17 |
import android.widget.RemoteViews; |
import android.widget.RemoteViews; |
18 |
|
|
19 |
public class Side9WidgetProvider extends AppWidgetProvider { |
public class Side9WidgetProvider extends AppWidgetProvider { |
20 |
|
|
21 |
|
|
22 |
|
//The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive() |
23 |
|
private static Side9Data usedData; |
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 |
|
|
|
final int N = appWidgetIds.length; // |
|
62 |
|
|
|
Log.e("onUpdate", "onUpdate"); |
|
63 |
|
|
64 |
|
Log.e("Side9Pigen", "onUpdate:"); |
65 |
|
|
66 |
|
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
67 |
|
if (connMgr.getBackgroundDataSetting() == false) |
68 |
|
{ |
69 |
|
Log.i("Side9Pigen", "background data disabled"); |
70 |
|
return; |
71 |
|
} |
72 |
|
|
73 |
try { |
try { |
74 |
Side9Xml dataSource = new Side9Xml(); |
Side9Data newData = Side9Xml.loadXml(); |
|
Side9Data data = dataSource.loadXml(); |
|
75 |
|
|
76 |
|
if (! newData.equals(usedData)) { |
77 |
|
|
78 |
|
|
79 |
|
Log.i("Side9Pigen", "(Re)loading image:" + newData.url); |
80 |
|
|
81 |
|
URL imgUrl = new URL( newData.url ); |
82 |
|
|
83 |
|
URLConnection conn = imgUrl.openConnection(); |
84 |
|
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); |
85 |
|
if (true) |
86 |
|
throw new IOException("assdasd"); |
87 |
|
|
88 |
|
setImage(context,appWidgetManager,appWidgetIds, image); |
89 |
|
|
90 |
|
usedData = newData; // if we made it to here without exceptions, save the new data |
91 |
|
|
92 |
|
} // endif |
93 |
|
|
94 |
|
|
|
URL imgUrl = new URL( data.url ); |
|
|
|
|
|
URLConnection conn = imgUrl.openConnection(); |
|
|
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); |
|
|
|
|
|
// 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); |
|
|
|
|
|
// Tell the AppWidgetManager to perform an update on the current App Widget |
|
|
appWidgetManager.updateAppWidget(appWidgetId, views); |
|
|
|
|
|
} |
|
95 |
} catch (Exception e) { |
} catch (Exception e) { |
96 |
Log.e("UPDATE", "UPDATE", e); |
Log.e("Side9Pigen", "update failed", e); |
97 |
|
|
98 |
|
if (usedData == null) { |
99 |
|
setImage(context,appWidgetManager,appWidgetIds, null); |
100 |
|
} |
101 |
} |
} |
102 |
} |
} |
103 |
|
|
104 |
|
|
105 |
|
//Called when the last widget is removed/disabled |
106 |
|
@Override |
107 |
|
public void onDisabled(Context context) { |
108 |
|
super.onDisabled(context); |
109 |
|
|
110 |
|
usedData = null; //free memory |
111 |
|
} |
112 |
|
|
113 |
} |
} |