/[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 1354 - (show annotations) (download)
Wed Apr 20 18:57:18 2011 UTC (13 years ago) by torben
File size: 7459 byte(s)
more Use GenericJavaUtils
1 package dk.thoerup.side9;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6
7 import android.app.PendingIntent;
8 import android.appwidget.AppWidgetManager;
9 import android.appwidget.AppWidgetProvider;
10 import android.content.Context;
11 import android.content.Intent;
12 import android.content.SharedPreferences;
13 import android.graphics.Bitmap;
14 import android.graphics.BitmapFactory;
15 import android.net.ConnectivityManager;
16 import android.net.Uri;
17 import android.os.Environment;
18 import android.provider.Settings;
19 import android.util.DisplayMetrics;
20 import android.util.Log;
21 import android.view.View;
22 import android.view.WindowManager;
23 import android.widget.RemoteViews;
24 import dk.thoerup.genericjavautils.HttpUtil;
25
26 public class Side9WidgetProvider extends AppWidgetProvider {
27
28 public static final String TAG = "Side9Pigen";
29
30 //The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()
31 private static Side9Data mUsedData;
32 private static Bitmap mUsedBitmap;
33 private static long mTimestamp;
34
35 final static long UDPATESPAN = 4*60*60*1000;
36
37 static {
38 mTimestamp = 0L;
39 }
40
41 public Side9WidgetProvider() {
42 Log.i(TAG, "WidgetProvider constructor called");
43 }
44
45 public void resetStatics() {
46 Log.i(TAG, "resetStatics");
47 mUsedData = null;
48 mUsedBitmap = null;
49 }
50
51
52
53
54
55 private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
56 // Perform this loop procedure for each App Widget that belongs to this provider
57 final int N = appWidgetIds.length;
58 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 if (mUsedBitmap != null) {
64 views.setImageViewBitmap(R.id.side9picture, mUsedBitmap);
65 } else {
66 views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);
67 }
68
69 boolean showcaption = context.
70 getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE).
71 getBoolean(Side9Config.PREFS_SHOWCAPTION, false);
72
73 if (showcaption == true && mUsedData != null) {
74 views.setTextViewText(R.id.caption, " " + mUsedData.caption + " ");
75 views.setViewVisibility(R.id.caption, View.VISIBLE);
76 } else {
77 views.setViewVisibility(R.id.caption, View.GONE);
78 }
79
80
81 //Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/"));
82
83 Intent viewIntent = new Intent(context, dk.thoerup.side9.PictureOverview.class );
84
85
86 PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, 0);
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 Log.i(TAG, "done " + appWidgetId);
93 }
94 }
95
96 @Override
97 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
98 super.onUpdate(context, appWidgetManager, appWidgetIds);
99
100 Log.i(TAG, "onUpdate:");
101
102 if (mUsedBitmap == null) { //load default view
103 mUsedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
104 }
105
106
107
108 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
109 if (connMgr.getBackgroundDataSetting() == false)
110 {
111 Log.i(TAG, "background data disabled");
112 return;
113 }
114
115 //we need to do a full reload of all data now and then because eb.dk sometimes are a bit slow with publishing the new
116 //daily picture, and this is a (crude) way to ensure we have the latest data
117 boolean reloadData = false;
118 if (System.currentTimeMillis() > (mTimestamp+UDPATESPAN) ) {
119 Log.i(TAG, "time elapsed, force XML reload");
120 reloadData = true;
121 }
122
123
124 String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
125
126 try {
127 Side9Data newData = Side9Xml.loadXml(androidID);
128
129 if (! newData.equals(mUsedData) || reloadData == true) {
130
131
132 Log.i(TAG, "(Re)loading image:" + newData.url);
133
134 Bitmap image = getImageData(context, newData);
135
136 mUsedData = newData; // if we made it to here without exceptions, save the new data
137 mUsedBitmap = image;
138 mTimestamp = System.currentTimeMillis();
139
140 } // endif
141
142 } catch (IOException ioe) {
143 Log.e("Side9Pigen", "update failed, IOException: " + ioe.getMessage() );
144 } catch (Exception e) {
145 Log.e("Side9Pigen", "update failed", e);
146 }
147
148 setImage(context,appWidgetManager,appWidgetIds);
149 Log.i("Side9Pigen", "update completed");
150 }
151
152 Bitmap getImageData(Context context, Side9Data data) throws IOException {
153
154 SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
155 boolean saveImage = prefs.getBoolean(Side9Config.PREFS_SAVEIMAGE, false);
156
157 final String savepath = Environment.getExternalStorageDirectory() + "/Side9";
158
159
160
161 File imageFile = new File( savepath + "/" + HttpUtil.getLastPart(data.url) );
162
163 /* if the picture changes later on the day we do NOT want to use an old and invalid image
164 if (saveImage == true) {
165 if (file.exists()) {
166 return BitmapFactory.decodeFile(file.getAbsolutePath());
167 }
168 }*/
169
170
171 byte imageData[] = HttpUtil.getContent(data.url, 2500);
172
173 if (saveImage == true) {
174 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
175 File savedir = new File(savepath);
176 savedir.mkdirs();
177
178 if (imageFile.exists()) {
179 imageFile.delete();
180 }
181
182 FileOutputStream fos = new FileOutputStream(imageFile);
183 fos.write(imageData);
184 fos.close();
185
186 File infoFile = new File( imageFile.toString().replace(".jpg", ".txt"));
187 if (infoFile.exists()) {
188 infoFile.delete();
189 }
190 fos = new FileOutputStream(infoFile);
191 fos.write(data.caption.getBytes());
192 fos.close();
193
194 //save a thumb image
195 ImageAdapter.generateAndLoadThumb(imageFile);
196
197 Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) );
198 rescan.putExtra("read-only", false);
199 context.sendBroadcast(rescan);
200
201 } else {
202 Log.i(TAG, "sdcard is not mounted");
203 }
204 }
205
206 DisplayMetrics metrics = new DisplayMetrics();
207 WindowManager wmgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
208 wmgr.getDefaultDisplay().getMetrics(metrics);
209 int w = metrics.widthPixels - (2*40);
210 int h = getHeight(w);
211
212 Log.e(TAG, "w=" + w + " h=" + h);
213
214 Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
215 Bitmap scaled = Bitmap.createScaledBitmap(full, w, h, true);
216 return scaled;
217 }
218
219 private static int getHeight(int w) {
220 double h = w * (650.0 / 450.0);
221 return (int)h;
222 }
223
224 //Called when the last widget is removed/disabled
225 @Override
226 public void onDisabled(Context context) {
227 super.onDisabled(context);
228 Log.i(TAG, "onDisabled");
229
230 resetStatics();//free memory
231 }
232
233 @Override
234 public void onEnabled(Context context) {
235 super.onEnabled(context);
236 Log.i(TAG, "onEnabled");
237
238 resetStatics();//free memory
239 }
240
241 }

  ViewVC Help
Powered by ViewVC 1.1.20