--- android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/06/01 11:42:06 780 +++ android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/06/26 18:04:07 923 @@ -15,7 +15,10 @@ import android.net.ConnectivityManager; import android.net.Uri; import android.os.Environment; +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; @@ -27,11 +30,9 @@ private static Side9Data usedData; private static Bitmap usedBitmap; private static long timestamp; + private static boolean reloadData; - final static long UDPATESPAN = 3*60*60*1000; - - final static String SAVEDIR = "/sdcard/Side9/"; - + final static long UDPATESPAN = 4*60*60*1000; static { timestamp = 0L; @@ -51,7 +52,7 @@ - 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(TAG, "time elapsed, force XML reload"); - usedData = null; + reloadData = true; } try { Side9Data newData = Side9Xml.loadXml(); - if (! newData.equals(usedData)) { + if (! newData.equals(usedData) || reloadData == true) { Log.i(TAG, "(Re)loading image:" + newData.url); @@ -139,7 +130,7 @@ usedData = newData; // if we made it to here without exceptions, save the new data usedBitmap = image; timestamp = System.currentTimeMillis(); - + reloadData = false; } // endif @@ -147,16 +138,18 @@ 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("saveimage", false); + boolean saveImage = prefs.getBoolean(Side9Config.PREFS_SAVEIMAGE, false); + + final String savepath = Environment.getExternalStorageDirectory() + "/Side9"; - File file = new File( SAVEDIR + data.getFilename() ); + 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) { @@ -170,7 +163,7 @@ if (saveImage == true) { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { - File savedir = new File(SAVEDIR); + File savedir = new File(savepath); savedir.mkdirs(); if (file.exists()) { @@ -180,15 +173,34 @@ FileOutputStream fos = new FileOutputStream(file); fos.write(imageData); 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"); } } - return BitmapFactory.decodeByteArray(imageData, 0, imageData.length); + 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) {