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 |
|
|
25 |
public Side9WidgetProvider() { |
public Side9WidgetProvider() { |
26 |
Log.i("Side9Pigen", "WidgetProvider constructor called"); |
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 |
|
|
63 |
Log.e("Side9Pigen", "onUpdate:" + N); |
|
64 |
|
Log.e("Side9Pigen", "onUpdate:"); |
65 |
|
|
66 |
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
67 |
if (connMgr.getBackgroundDataSetting() == false) |
if (connMgr.getBackgroundDataSetting() == false) |
82 |
|
|
83 |
URLConnection conn = imgUrl.openConnection(); |
URLConnection conn = imgUrl.openConnection(); |
84 |
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); |
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 |
usedData = newData; // if we made it to here without exceptions, save the new data |
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); |
|
|
views.setTextViewText(R.id.caption, newData.caption); |
|
|
|
|
|
|
|
|
// 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); |
|
|
|
|
|
Log.i("Side9Pigen", "done " + appWidgetId); |
|
|
} |
|
|
} //END if (data.equals) |
|
94 |
|
|
95 |
} catch (Exception e) { |
} catch (Exception e) { |
96 |
Log.e("Side9Pigen", "update failed", e); |
Log.e("Side9Pigen", "update failed", e); |
97 |
|
|
98 |
|
if (usedData == null) { |
99 |
|
setImage(context,appWidgetManager,appWidgetIds, null); |
100 |
|
} |
101 |
} |
} |
102 |
} |
} |
103 |
|
|