7 |
import android.app.PendingIntent; |
import android.app.PendingIntent; |
8 |
import android.appwidget.AppWidgetManager; |
import android.appwidget.AppWidgetManager; |
9 |
import android.appwidget.AppWidgetProvider; |
import android.appwidget.AppWidgetProvider; |
|
import android.content.ComponentName; |
|
10 |
import android.content.Context; |
import android.content.Context; |
11 |
import android.content.Intent; |
import android.content.Intent; |
12 |
import android.content.SharedPreferences; |
import android.content.SharedPreferences; |
13 |
import android.graphics.Bitmap; |
import android.graphics.Bitmap; |
14 |
import android.graphics.BitmapFactory; |
import android.graphics.BitmapFactory; |
15 |
import android.net.ConnectivityManager; |
import android.net.ConnectivityManager; |
16 |
|
import android.net.Uri; |
17 |
|
import android.os.Environment; |
18 |
import android.util.Log; |
import android.util.Log; |
19 |
import android.widget.RemoteViews; |
import android.widget.RemoteViews; |
20 |
import dk.thoerup.androidutils.HttpUtil; |
import dk.thoerup.androidutils.HttpUtil; |
28 |
private static Bitmap usedBitmap; |
private static Bitmap usedBitmap; |
29 |
private static long timestamp; |
private static long timestamp; |
30 |
|
|
31 |
final static long UDPATESPAN = 3*60*60*1000; |
final static long UDPATESPAN = 4*60*60*1000; |
|
|
|
|
final static String SAVEDIR = "/sdcard/Side9/"; |
|
|
|
|
32 |
|
|
33 |
static { |
static { |
34 |
timestamp = 0L; |
timestamp = 0L; |
37 |
public Side9WidgetProvider() { |
public Side9WidgetProvider() { |
38 |
Log.i(TAG, "WidgetProvider constructor called"); |
Log.i(TAG, "WidgetProvider constructor called"); |
39 |
} |
} |
40 |
|
|
41 |
|
public void resetStatics() { |
42 |
|
Log.i(TAG, "resetStatics"); |
43 |
|
usedData = null; |
44 |
|
usedBitmap = null; |
45 |
|
} |
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) { |
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) { |
52 |
// Perform this loop procedure for each App Widget that belongs to this provider |
// Perform this loop procedure for each App Widget that belongs to this provider |
64 |
//views.setTextViewText(R.id.caption, newData.caption); |
//views.setTextViewText(R.id.caption, newData.caption); |
65 |
|
|
66 |
|
|
67 |
|
//Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/")); |
68 |
|
|
69 |
|
Intent viewIntent = new Intent(context, dk.thoerup.side9.PictureOverview.class ); |
70 |
//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); |
|
|
|
|
|
|
|
|
|
|
71 |
|
|
72 |
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
73 |
views.setOnClickPendingIntent(R.id.side9picture, pending); |
views.setOnClickPendingIntent(R.id.side9picture, pending); |
133 |
SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); |
SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); |
134 |
boolean saveImage = prefs.getBoolean("saveimage", false); |
boolean saveImage = prefs.getBoolean("saveimage", false); |
135 |
|
|
136 |
File file = new File( SAVEDIR + data.getFilename() ); |
final String savepath = Environment.getExternalStorageDirectory() + "/Side9"; |
137 |
|
|
138 |
if (saveImage == true) { |
File file = new File( savepath + "/" + data.getFilename() ); |
139 |
|
|
140 |
|
/* if the picture changes later on the day we do NOT want to use an old and invalid image |
141 |
|
if (saveImage == true) { |
142 |
if (file.exists()) { |
if (file.exists()) { |
143 |
return BitmapFactory.decodeFile(file.getAbsolutePath()); |
return BitmapFactory.decodeFile(file.getAbsolutePath()); |
144 |
} |
} |
145 |
} |
}*/ |
146 |
|
|
147 |
|
|
148 |
byte imageData[] = HttpUtil.getContent(data.url, 2500); |
byte imageData[] = HttpUtil.getContent(data.url, 2500); |
149 |
|
|
150 |
if (saveImage == true) { |
if (saveImage == true) { |
151 |
File savedir = new File(SAVEDIR); |
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
152 |
savedir.mkdirs(); |
File savedir = new File(savepath); |
153 |
|
savedir.mkdirs(); |
154 |
FileOutputStream fos = new FileOutputStream(file); |
|
155 |
fos.write(imageData); |
if (file.exists()) { |
156 |
fos.close(); |
file.delete(); |
157 |
|
} |
158 |
|
|
159 |
|
FileOutputStream fos = new FileOutputStream(file); |
160 |
|
fos.write(imageData); |
161 |
|
fos.close(); |
162 |
|
|
163 |
|
Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) ); |
164 |
|
rescan.putExtra("read-only", false); |
165 |
|
context.sendBroadcast(rescan); |
166 |
|
|
167 |
|
} else { |
168 |
|
Log.i(TAG, "sdcard is not mounted"); |
169 |
|
} |
170 |
} |
} |
171 |
|
|
172 |
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
178 |
public void onDisabled(Context context) { |
public void onDisabled(Context context) { |
179 |
super.onDisabled(context); |
super.onDisabled(context); |
180 |
Log.i(TAG, "onDisabled"); |
Log.i(TAG, "onDisabled"); |
181 |
|
|
182 |
usedData = null; //free memory |
resetStatics();//free memory |
|
usedBitmap = null; |
|
183 |
} |
} |
184 |
|
|
185 |
|
@Override |
186 |
|
public void onEnabled(Context context) { |
187 |
|
super.onEnabled(context); |
188 |
|
Log.i(TAG, "onEnabled"); |
189 |
|
|
190 |
|
resetStatics();//free memory |
191 |
|
} |
192 |
|
|
193 |
} |
} |