--- android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/06/01 07:09:48 774 +++ android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java 2010/06/03 06:55:59 788 @@ -7,13 +7,14 @@ 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.os.Environment; import android.util.Log; import android.widget.RemoteViews; import dk.thoerup.androidutils.HttpUtil; @@ -27,10 +28,7 @@ private static Bitmap usedBitmap; private static long timestamp; - 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; @@ -39,6 +37,16 @@ public Side9WidgetProvider() { 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, Bitmap image) { // Perform this loop procedure for each App Widget that belongs to this provider @@ -56,6 +64,7 @@ //views.setTextViewText(R.id.caption, newData.caption); + Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/")); //View single file @@ -71,11 +80,11 @@ //viewIntent.setClassName("com.android.camera", "com.android.camera.GalleryPicker"); - Intent viewIntent = new Intent(); + /*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); + viewIntent.setFlags(0x10200000);*/ @@ -144,7 +153,9 @@ SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); boolean saveImage = prefs.getBoolean("saveimage", false); - File file = new File( SAVEDIR + data.getFilename() ); + final String savepath = Environment.getExternalStorageDirectory() + "/Side9"; + + 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) { @@ -152,20 +163,30 @@ return BitmapFactory.decodeFile(file.getAbsolutePath()); } }*/ + byte imageData[] = HttpUtil.getContent(data.url, 2500); if (saveImage == true) { - File savedir = new File(SAVEDIR); - savedir.mkdirs(); - - if (file.exists()) { - file.delete(); + if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + File savedir = new File(savepath); + savedir.mkdirs(); + + if (file.exists()) { + file.delete(); + } + + 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"); } - - FileOutputStream fos = new FileOutputStream(file); - fos.write(imageData); - fos.close(); } return BitmapFactory.decodeByteArray(imageData, 0, imageData.length); @@ -177,9 +198,16 @@ public void onDisabled(Context context) { super.onDisabled(context); Log.i(TAG, "onDisabled"); - - usedData = null; //free memory - usedBitmap = null; + + resetStatics();//free memory } + @Override + public void onEnabled(Context context) { + super.onEnabled(context); + Log.i(TAG, "onEnabled"); + + resetStatics();//free memory + } + }