--- android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/04/19 12:49:31 649 +++ android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/06/16 07:40:59 854 @@ -1,98 +1,203 @@ 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.util.Log; +import android.view.View; 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; + + final static long UDPATESPAN = 4*60*60*1000; + + static { + timestamp = 0L; + } + public Side9WidgetProvider() { - Log.i("Side9Pigen", "WidgetProvider constructor called"); + Log.i(TAG, "WidgetProvider constructor called"); } - + + public void resetStatics() { + Log.i(TAG, "resetStatics"); + usedData = null; + usedBitmap = null; + } + + + + + + 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; + } + + try { + Side9Data newData = Side9Xml.loadXml(); + + if (! newData.equals(usedData)) { + + + 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(); + + + } // endif + + } catch (Exception e) { + Log.e("Side9Pigen", "update failed", e); + } + + 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); - try { - Side9Data newData = Side9Xml.loadXml(); - - if (! newData.equals(usedData)) { - - - Log.i("Side9Pigen", "(Re)loading image:" + newData.url); - - URL imgUrl = new URL( newData.url ); - - URLConnection conn = imgUrl.openConnection(); - Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); - - - - usedData = newData; // if we made it to here without exceptions, save the new data - - - // Perform this loop procedure for each App Widget that belongs to this provider - for (int i=0; i