--- android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/05/03 21:15:52 704 +++ android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2011/01/07 13:16:34 1213 @@ -1,40 +1,59 @@ package dk.thoerup.side9; -import java.net.URL; -import java.net.URLConnection; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.Context; import android.content.Intent; +import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.ConnectivityManager; import android.net.Uri; +import android.os.Environment; +import android.provider.Settings; +import android.util.DisplayMetrics; import android.util.Log; +import android.view.View; +import android.view.WindowManager; import android.widget.RemoteViews; +import dk.thoerup.androidutils.HttpUtil; public class Side9WidgetProvider extends AppWidgetProvider { + public static final String TAG = "Side9Pigen"; //The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive() - private static Side9Data usedData; - private static Bitmap usedBitmap; - private static long timestamp; + private static Side9Data mUsedData; + private static Bitmap mUsedBitmap; + private static long mTimestamp; + private static boolean mReloadData; final static long UDPATESPAN = 4*60*60*1000; - static { - timestamp = 0L; + mTimestamp = 0L; } public Side9WidgetProvider() { - Log.i("Side9Pigen", "WidgetProvider constructor called"); + Log.i(TAG, "WidgetProvider constructor called"); + } + + public void resetStatics() { + Log.i(TAG, "resetStatics"); + mUsedData = null; + mUsedBitmap = null; } + + + - private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) { + + private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // Perform this loop procedure for each App Widget that belongs to this provider final int N = appWidgetIds.length; for (int i=0; i (timestamp+UDPATESPAN) ) { - Log.i("Side9Pigen", "time elapsed, force XML reload"); - usedData = null; + //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 + //daily picture, and this is a (crude) way to ensure we have the latest data + if (System.currentTimeMillis() > (mTimestamp+UDPATESPAN) ) { + Log.i(TAG, "time elapsed, force XML reload"); + mReloadData = true; } + + String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID); + try { - Side9Data newData = Side9Xml.loadXml(); + Side9Data newData = Side9Xml.loadXml(androidID); - if (! newData.equals(usedData)) { + if (! newData.equals(mUsedData) || mReloadData == true) { - Log.i("Side9Pigen", "(Re)loading image:" + newData.url); + Log.i(TAG, "(Re)loading image:" + newData.url); - URL imgUrl = new URL( newData.url ); + Bitmap image = getImageData(context, newData); - URLConnection conn = imgUrl.openConnection(); - conn.setConnectTimeout(2500); - Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); - - usedData = newData; // if we made it to here without exceptions, save the new data - usedBitmap = image; - timestamp = System.currentTimeMillis(); + mUsedData = newData; // if we made it to here without exceptions, save the new data + mUsedBitmap = image; + mTimestamp = System.currentTimeMillis(); + mReloadData = false; } // endif + } catch (IOException ioe) { + Log.e("Side9Pigen", "update failed, IOException: " + ioe.getMessage() ); } catch (Exception e) { Log.e("Side9Pigen", "update failed", e); } - setImage(context,appWidgetManager,appWidgetIds, usedBitmap); + setImage(context,appWidgetManager,appWidgetIds); Log.i("Side9Pigen", "update completed"); } + + Bitmap getImageData(Context context, Side9Data data) throws IOException { + + SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); + boolean saveImage = prefs.getBoolean(Side9Config.PREFS_SAVEIMAGE, false); + + final String savepath = Environment.getExternalStorageDirectory() + "/Side9"; + + File file = new File( savepath + "/" + data.getFilename() ); + + /* if the picture changes later on the day we do NOT want to use an old and invalid image + if (saveImage == true) { + if (file.exists()) { + return BitmapFactory.decodeFile(file.getAbsolutePath()); + } + }*/ + + + byte imageData[] = HttpUtil.getContent(data.url, 2500); + + if (saveImage == true) { + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + File savedir = new File(savepath); + savedir.mkdirs(); + + if (file.exists()) { + file.delete(); + } + + FileOutputStream fos = new FileOutputStream(file); + fos.write(imageData); + fos.close(); + + File infoFile = new File( file.toString().replace(".jpg", ".txt")); + if (infoFile.exists()) { + infoFile.delete(); + } + fos = new FileOutputStream(infoFile); + fos.write(data.caption.getBytes()); + fos.close(); + + Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) ); + rescan.putExtra("read-only", false); + context.sendBroadcast(rescan); + + } else { + Log.i(TAG, "sdcard is not mounted"); + } + } + + DisplayMetrics metrics = new DisplayMetrics(); + WindowManager wmgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); + wmgr.getDefaultDisplay().getMetrics(metrics); + int w = metrics.widthPixels - (2*40); + int h = getHeight(w); + + Log.e(TAG, "w=" + w + " h=" + h); + + Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); + Bitmap scaled = Bitmap.createScaledBitmap(full, w, h, true); + return scaled; + } - + private static int getHeight(int w) { + double h = w * (650.0 / 450.0); + return (int)h; + } + //Called when the last widget is removed/disabled @Override public void onDisabled(Context context) { super.onDisabled(context); - - usedData = null; //free memory - usedBitmap = null; + Log.i(TAG, "onDisabled"); + + resetStatics();//free memory } + @Override + public void onEnabled(Context context) { + super.onEnabled(context); + Log.i(TAG, "onEnabled"); + + resetStatics();//free memory + } + }