--- android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/05/31 07:21:46 770 +++ android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/05/31 14:14:59 771 @@ -1,22 +1,26 @@ 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.ComponentName; 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.util.Log; 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; @@ -24,6 +28,8 @@ private static long timestamp; final static long UDPATESPAN = 3*60*60*1000; + + final static String SAVEDIR = "/sdcard/Side9/"; static { @@ -31,7 +37,7 @@ } public Side9WidgetProvider() { - Log.i("Side9Pigen", "WidgetProvider constructor called"); + Log.i(TAG, "WidgetProvider constructor called"); } private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) { @@ -50,16 +56,37 @@ //views.setTextViewText(R.id.caption, newData.caption); - // Launch a browser when user clicks on the image - Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://ekstrabladet.dk/side9/")); - viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + + + //View single file +// Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW); + // viewIntent.setDataAndType(Uri.parse("file:///sdcard/Side9/20100531_400.jpg"), "image/png"); + + //view single file + //Intent viewIntent = new Intent(Intent.ACTION_VIEW); + //Uri u = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "1" ); + //viewIntent.setData(u); + + //Intent viewIntent = new Intent(); + //viewIntent.setClassName("com.android.camera", "com.android.camera.GalleryPicker"); + + + Intent viewIntent = new Intent(); + viewIntent.addCategory(Intent.CATEGORY_LAUNCHER); + viewIntent.setAction(Intent.ACTION_MAIN); + viewIntent.setComponent(new ComponentName("com.android.camera", ".GalleryPicker")); + viewIntent.setFlags(0x10200000); + + + + PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); views.setOnClickPendingIntent(R.id.side9picture, pending); // Tell the AppWidgetManager to perform an update on the current App Widget appWidgetManager.updateAppWidget(appWidgetId, views); - Log.i("Side9Pigen", "done " + appWidgetId); + Log.i(TAG, "done " + appWidgetId); } } @@ -67,7 +94,7 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { super.onUpdate(context, appWidgetManager, appWidgetIds); - Log.i("Side9Pigen", "onUpdate:"); + Log.i(TAG, "onUpdate:"); if (usedBitmap == null) { //load default view usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo); @@ -78,12 +105,12 @@ ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); if (connMgr.getBackgroundDataSetting() == false) { - Log.i("Side9Pigen", "background data disabled"); + Log.i(TAG, "background data disabled"); return; } if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) { - Log.i("Side9Pigen", "time elapsed, force XML reload"); + Log.i(TAG, "time elapsed, force XML reload"); usedData = null; } @@ -93,17 +120,14 @@ if (! newData.equals(usedData)) { - Log.i("Side9Pigen", "(Re)loading image:" + newData.url); - - URL imgUrl = new URL( newData.url ); + Log.i(TAG, "(Re)loading image:" + newData.url); - URLConnection conn = imgUrl.openConnection(); - conn.setConnectTimeout(2500); - Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); + 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 @@ -114,12 +138,40 @@ setImage(context,appWidgetManager,appWidgetIds, usedBitmap); 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); + + File file = new File( SAVEDIR + data.getFilename() ); + + if (saveImage == true) { + if (file.exists()) { + return BitmapFactory.decodeFile(file.getAbsolutePath()); + } + } + + byte imageData[] = HttpUtil.getContent(data.url, 2500); + + if (saveImage == true) { + File savedir = new File(SAVEDIR); + savedir.mkdirs(); + + FileOutputStream fos = new FileOutputStream(file); + fos.write(imageData); + fos.close(); + } + + return BitmapFactory.decodeByteArray(imageData, 0, imageData.length); + } //Called when the last widget is removed/disabled @Override public void onDisabled(Context context) { super.onDisabled(context); + Log.i(TAG, "onDisabled"); usedData = null; //free memory usedBitmap = null;