--- android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/06/16 07:51:40 857 +++ android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/06/26 18:16:01 924 @@ -15,8 +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; @@ -28,6 +30,7 @@ private static Side9Data usedData; private static Bitmap usedBitmap; private static long timestamp; + private static boolean reloadData; final static long UDPATESPAN = 4*60*60*1000; @@ -67,8 +70,9 @@ getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE). getBoolean(Side9Config.PREFS_SHOWCAPTION, false); - if (showcaption == true) { + if (showcaption == true && usedData != null) { views.setTextViewText(R.id.caption, " " + usedData.caption + " "); + views.setViewVisibility(R.id.caption, View.VISIBLE); } else { views.setViewVisibility(R.id.caption, View.GONE); } @@ -108,15 +112,17 @@ return; } + //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() > (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); @@ -126,7 +132,7 @@ usedData = newData; // if we made it to here without exceptions, save the new data usedBitmap = image; timestamp = System.currentTimeMillis(); - + reloadData = false; } // endif @@ -179,12 +185,24 @@ } } - Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); - Bitmap scaled = Bitmap.createScaledBitmap(full, 400, 577, true); - return scaled; + 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) {