--- android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/06/26 18:04:07 923 +++ android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/07/26 10:10:38 1002 @@ -15,6 +15,7 @@ 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; @@ -27,15 +28,15 @@ 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 boolean reloadData; + 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() { @@ -44,8 +45,8 @@ public void resetStatics() { Log.i(TAG, "resetStatics"); - usedData = null; - usedBitmap = null; + mUsedData = null; + mUsedBitmap = null; } @@ -60,8 +61,8 @@ RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); - if (usedBitmap != null) { - views.setImageViewBitmap(R.id.side9picture, usedBitmap); + if (mUsedBitmap != null) { + views.setImageViewBitmap(R.id.side9picture, mUsedBitmap); } else { views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); } @@ -70,8 +71,8 @@ getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE). getBoolean(Side9Config.PREFS_SHOWCAPTION, false); - if (showcaption == true && usedData != null) { - views.setTextViewText(R.id.caption, " " + usedData.caption + " "); + if (showcaption == true && mUsedData != null) { + views.setTextViewText(R.id.caption, " " + mUsedData.caption + " "); views.setViewVisibility(R.id.caption, View.VISIBLE); } else { views.setViewVisibility(R.id.caption, View.GONE); @@ -99,8 +100,8 @@ Log.i(TAG, "onUpdate:"); - if (usedBitmap == null) { //load default view - usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo); + if (mUsedBitmap == null) { //load default view + mUsedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo); } @@ -112,25 +113,30 @@ return; } - if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) { + //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"); - reloadData = true; + 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) || reloadData == true) { + if (! newData.equals(mUsedData) || mReloadData == true) { Log.i(TAG, "(Re)loading image:" + newData.url); Bitmap image = getImageData(context, newData); - usedData = newData; // if we made it to here without exceptions, save the new data - usedBitmap = image; - timestamp = System.currentTimeMillis(); - reloadData = false; + mUsedData = newData; // if we made it to here without exceptions, save the new data + mUsedBitmap = image; + mTimestamp = System.currentTimeMillis(); + mReloadData = false; } // endif @@ -173,6 +179,14 @@ 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);