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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20