/[projects]/android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java
ViewVC logotype

Contents of /android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 635 - (show annotations) (download)
Wed Mar 24 08:57:30 2010 UTC (14 years, 1 month ago) by torben
File size: 2908 byte(s)
First semi-functional version
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.Uri;
14 import android.util.Log;
15 import android.widget.RemoteViews;
16
17 public class Side9WidgetProvider extends AppWidgetProvider {
18
19
20 //The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()
21 private static Side9Data usedData;
22
23 @Override
24 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
25 super.onUpdate(context, appWidgetManager, appWidgetIds);
26
27 final int N = appWidgetIds.length; //
28
29 Log.e("Side9Pigen", "onUpdate:" + N);
30
31
32 try {
33 Side9Data newData = Side9Xml.loadXml();
34
35 if (! newData.equals(usedData)) {
36
37
38 Log.e("Side9Pigen", "(Re)loading image:" + newData.url);
39
40 URL imgUrl = new URL( newData.url );
41
42 URLConnection conn = imgUrl.openConnection();
43 Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() );
44
45
46 usedData = newData; // if we made it to here without exceptions, save the new data
47
48
49 // Perform this loop procedure for each App Widget that belongs to this provider
50 for (int i=0; i<N; i++) {
51 int appWidgetId = appWidgetIds[i];
52
53 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);
54
55 views.setImageViewBitmap(R.id.side9picture, image);
56
57 // Launch a browser when user clicks on the image
58 Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Side9Xml.BASEURL));
59 viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
60 PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
61 views.setOnClickPendingIntent(R.id.side9picture, pending);
62
63 // Tell the AppWidgetManager to perform an update on the current App Widget
64 appWidgetManager.updateAppWidget(appWidgetId, views);
65 }
66 } //END if (data.equals)
67
68 } catch (Exception e) {
69 Log.e("Side9Pigen", "update failed", e);
70 }
71 }
72
73
74 //Called when the last widget is removed/disabled
75 @Override
76 public void onDisabled(Context context) {
77 super.onDisabled(context);
78
79 usedData = null; //free memory
80 }
81
82 }

  ViewVC Help
Powered by ViewVC 1.1.20