/[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 790 - (hide annotations) (download)
Thu Jun 3 09:11:07 2010 UTC (13 years, 11 months ago) by torben
File size: 5606 byte(s)
First version with built-in image gallery
1 torben 632 package dk.thoerup.side9;
2    
3 torben 771 import java.io.File;
4     import java.io.FileOutputStream;
5     import java.io.IOException;
6 torben 632
7 torben 635 import android.app.PendingIntent;
8 torben 632 import android.appwidget.AppWidgetManager;
9     import android.appwidget.AppWidgetProvider;
10     import android.content.Context;
11 torben 635 import android.content.Intent;
12 torben 771 import android.content.SharedPreferences;
13 torben 632 import android.graphics.Bitmap;
14     import android.graphics.BitmapFactory;
15 torben 636 import android.net.ConnectivityManager;
16 torben 776 import android.net.Uri;
17 torben 775 import android.os.Environment;
18 torben 632 import android.util.Log;
19     import android.widget.RemoteViews;
20 torben 771 import dk.thoerup.androidutils.HttpUtil;
21 torben 632
22     public class Side9WidgetProvider extends AppWidgetProvider {
23 torben 703
24 torben 771 public static final String TAG = "Side9Pigen";
25 torben 703
26 torben 635 //The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()
27     private static Side9Data usedData;
28 torben 689 private static Bitmap usedBitmap;
29 torben 703 private static long timestamp;
30    
31 torben 788 final static long UDPATESPAN = 4*60*60*1000;
32 torben 703
33     static {
34     timestamp = 0L;
35     }
36    
37 torben 649 public Side9WidgetProvider() {
38 torben 771 Log.i(TAG, "WidgetProvider constructor called");
39 torben 649 }
40 torben 778
41     public void resetStatics() {
42     Log.i(TAG, "resetStatics");
43     usedData = null;
44     usedBitmap = null;
45     }
46    
47    
48 torben 703
49 torben 778
50    
51 torben 681 private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {
52 torben 703 // Perform this loop procedure for each App Widget that belongs to this provider
53 torben 681 final int N = appWidgetIds.length;
54 torben 703 for (int i=0; i<N; i++) {
55     int appWidgetId = appWidgetIds[i];
56    
57     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);
58    
59     if (image != null) {
60     views.setImageViewBitmap(R.id.side9picture, image);
61     } else {
62     views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);
63     }
64     //views.setTextViewText(R.id.caption, newData.caption);
65    
66    
67 torben 790 //Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/"));
68 torben 771
69 torben 790 Intent viewIntent = new Intent(context, dk.thoerup.side9.PictureOverview.class );
70    
71 torben 771
72 torben 703 PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
73     views.setOnClickPendingIntent(R.id.side9picture, pending);
74    
75     // Tell the AppWidgetManager to perform an update on the current App Widget
76     appWidgetManager.updateAppWidget(appWidgetId, views);
77    
78 torben 771 Log.i(TAG, "done " + appWidgetId);
79 torben 703 }
80 torben 681 }
81 torben 703
82 torben 632 @Override
83     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
84     super.onUpdate(context, appWidgetManager, appWidgetIds);
85 torben 703
86 torben 771 Log.i(TAG, "onUpdate:");
87 torben 703
88 torben 689 if (usedBitmap == null) { //load default view
89     usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
90     }
91 torben 703
92    
93    
94 torben 636 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
95     if (connMgr.getBackgroundDataSetting() == false)
96     {
97 torben 771 Log.i(TAG, "background data disabled");
98 torben 636 return;
99     }
100 torben 703
101     if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
102 torben 771 Log.i(TAG, "time elapsed, force XML reload");
103 torben 703 usedData = null;
104     }
105    
106     try {
107     Side9Data newData = Side9Xml.loadXml();
108    
109     if (! newData.equals(usedData)) {
110    
111    
112 torben 771 Log.i(TAG, "(Re)loading image:" + newData.url);
113 torben 703
114 torben 771 Bitmap image = getImageData(context, newData);
115 torben 703
116     usedData = newData; // if we made it to here without exceptions, save the new data
117     usedBitmap = image;
118     timestamp = System.currentTimeMillis();
119 torben 771
120 torben 703
121     } // endif
122    
123     } catch (Exception e) {
124     Log.e("Side9Pigen", "update failed", e);
125     }
126    
127     setImage(context,appWidgetManager,appWidgetIds, usedBitmap);
128     Log.i("Side9Pigen", "update completed");
129 torben 632 }
130 torben 771
131     Bitmap getImageData(Context context, Side9Data data) throws IOException {
132    
133     SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
134     boolean saveImage = prefs.getBoolean("saveimage", false);
135    
136 torben 788 final String savepath = Environment.getExternalStorageDirectory() + "/Side9";
137 torben 771
138 torben 788 File file = new File( savepath + "/" + data.getFilename() );
139    
140 torben 774 /* if the picture changes later on the day we do NOT want to use an old and invalid image
141     if (saveImage == true) {
142 torben 771 if (file.exists()) {
143     return BitmapFactory.decodeFile(file.getAbsolutePath());
144     }
145 torben 774 }*/
146 torben 775
147 torben 771
148     byte imageData[] = HttpUtil.getContent(data.url, 2500);
149    
150     if (saveImage == true) {
151 torben 775 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
152 torben 788 File savedir = new File(savepath);
153 torben 775 savedir.mkdirs();
154    
155     if (file.exists()) {
156     file.delete();
157     }
158    
159     FileOutputStream fos = new FileOutputStream(file);
160     fos.write(imageData);
161     fos.close();
162 torben 788
163     Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) );
164     rescan.putExtra("read-only", false);
165     context.sendBroadcast(rescan);
166    
167 torben 776 } else {
168     Log.i(TAG, "sdcard is not mounted");
169 torben 774 }
170 torben 771 }
171    
172     return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
173     }
174 torben 632
175 torben 703
176 torben 635 //Called when the last widget is removed/disabled
177     @Override
178     public void onDisabled(Context context) {
179     super.onDisabled(context);
180 torben 771 Log.i(TAG, "onDisabled");
181 torben 778
182     resetStatics();//free memory
183     }
184 torben 703
185 torben 778 @Override
186     public void onEnabled(Context context) {
187     super.onEnabled(context);
188     Log.i(TAG, "onEnabled");
189    
190     resetStatics();//free memory
191 torben 635 }
192 torben 778
193 torben 632 }

  ViewVC Help
Powered by ViewVC 1.1.20