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

  ViewVC Help
Powered by ViewVC 1.1.20