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.DisplayMetrics; |
19 |
import android.util.Log; |
import android.util.Log; |
20 |
|
import android.view.View; |
21 |
|
import android.view.WindowManager; |
22 |
import android.widget.RemoteViews; |
import android.widget.RemoteViews; |
23 |
import dk.thoerup.androidutils.HttpUtil; |
import dk.thoerup.androidutils.HttpUtil; |
24 |
|
|
31 |
private static Bitmap usedBitmap; |
private static Bitmap usedBitmap; |
32 |
private static long timestamp; |
private static long timestamp; |
33 |
|
|
34 |
final static long UDPATESPAN = 3*60*60*1000; |
final static long UDPATESPAN = 4*60*60*1000; |
|
|
|
|
final static String SAVEDIR = "/sdcard/Side9/"; |
|
|
|
|
35 |
|
|
36 |
static { |
static { |
37 |
timestamp = 0L; |
timestamp = 0L; |
40 |
public Side9WidgetProvider() { |
public Side9WidgetProvider() { |
41 |
Log.i(TAG, "WidgetProvider constructor called"); |
Log.i(TAG, "WidgetProvider constructor called"); |
42 |
} |
} |
43 |
|
|
44 |
|
public void resetStatics() { |
45 |
|
Log.i(TAG, "resetStatics"); |
46 |
|
usedData = null; |
47 |
|
usedBitmap = null; |
48 |
|
} |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
|
54 |
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) { |
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
55 |
// 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 |
56 |
final int N = appWidgetIds.length; |
final int N = appWidgetIds.length; |
57 |
for (int i=0; i<N; i++) { |
for (int i=0; i<N; i++) { |
59 |
|
|
60 |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
61 |
|
|
62 |
if (image != null) { |
if (usedBitmap != null) { |
63 |
views.setImageViewBitmap(R.id.side9picture, image); |
views.setImageViewBitmap(R.id.side9picture, usedBitmap); |
64 |
} else { |
} else { |
65 |
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); |
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); |
66 |
} |
} |
|
//views.setTextViewText(R.id.caption, newData.caption); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//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"); |
|
67 |
|
|
68 |
|
boolean showcaption = context. |
69 |
|
getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE). |
70 |
|
getBoolean(Side9Config.PREFS_SHOWCAPTION, false); |
71 |
|
|
72 |
|
if (showcaption == true && usedData != null) { |
73 |
|
views.setTextViewText(R.id.caption, " " + usedData.caption + " "); |
74 |
|
views.setViewVisibility(R.id.caption, View.VISIBLE); |
75 |
|
} else { |
76 |
|
views.setViewVisibility(R.id.caption, View.GONE); |
77 |
|
} |
78 |
|
|
|
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); |
|
79 |
|
|
80 |
|
//Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/")); |
81 |
|
|
82 |
|
Intent viewIntent = new Intent(context, dk.thoerup.side9.PictureOverview.class ); |
83 |
|
|
84 |
|
|
85 |
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
86 |
views.setOnClickPendingIntent(R.id.side9picture, pending); |
views.setOnClickPendingIntent(R.id.side9picture, pending); |
137 |
Log.e("Side9Pigen", "update failed", e); |
Log.e("Side9Pigen", "update failed", e); |
138 |
} |
} |
139 |
|
|
140 |
setImage(context,appWidgetManager,appWidgetIds, usedBitmap); |
setImage(context,appWidgetManager,appWidgetIds); |
141 |
Log.i("Side9Pigen", "update completed"); |
Log.i("Side9Pigen", "update completed"); |
142 |
} |
} |
143 |
|
|
144 |
Bitmap getImageData(Context context, Side9Data data) throws IOException { |
Bitmap getImageData(Context context, Side9Data data) throws IOException { |
145 |
|
|
146 |
SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); |
SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); |
147 |
boolean saveImage = prefs.getBoolean("saveimage", false); |
boolean saveImage = prefs.getBoolean(Side9Config.PREFS_SAVEIMAGE, false); |
148 |
|
|
149 |
File file = new File( SAVEDIR + data.getFilename() ); |
final String savepath = Environment.getExternalStorageDirectory() + "/Side9"; |
150 |
|
|
151 |
if (saveImage == true) { |
File file = new File( savepath + "/" + data.getFilename() ); |
152 |
|
|
153 |
|
/* if the picture changes later on the day we do NOT want to use an old and invalid image |
154 |
|
if (saveImage == true) { |
155 |
if (file.exists()) { |
if (file.exists()) { |
156 |
return BitmapFactory.decodeFile(file.getAbsolutePath()); |
return BitmapFactory.decodeFile(file.getAbsolutePath()); |
157 |
} |
} |
158 |
} |
}*/ |
159 |
|
|
160 |
|
|
161 |
byte imageData[] = HttpUtil.getContent(data.url, 2500); |
byte imageData[] = HttpUtil.getContent(data.url, 2500); |
162 |
|
|
163 |
if (saveImage == true) { |
if (saveImage == true) { |
164 |
File savedir = new File(SAVEDIR); |
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
165 |
savedir.mkdirs(); |
File savedir = new File(savepath); |
166 |
|
savedir.mkdirs(); |
167 |
FileOutputStream fos = new FileOutputStream(file); |
|
168 |
fos.write(imageData); |
if (file.exists()) { |
169 |
fos.close(); |
file.delete(); |
170 |
|
} |
171 |
|
|
172 |
|
FileOutputStream fos = new FileOutputStream(file); |
173 |
|
fos.write(imageData); |
174 |
|
fos.close(); |
175 |
|
|
176 |
|
Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) ); |
177 |
|
rescan.putExtra("read-only", false); |
178 |
|
context.sendBroadcast(rescan); |
179 |
|
|
180 |
|
} else { |
181 |
|
Log.i(TAG, "sdcard is not mounted"); |
182 |
|
} |
183 |
} |
} |
184 |
|
|
185 |
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
DisplayMetrics metrics = new DisplayMetrics(); |
186 |
|
WindowManager wmgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
187 |
|
wmgr.getDefaultDisplay().getMetrics(metrics); |
188 |
|
int w = metrics.widthPixels - (2*40); |
189 |
|
int h = getHeight(w); |
190 |
|
|
191 |
|
Log.e(TAG, "w=" + w + " h=" + h); |
192 |
|
|
193 |
|
Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
194 |
|
Bitmap scaled = Bitmap.createScaledBitmap(full, w, h, true); |
195 |
|
return scaled; |
196 |
} |
} |
197 |
|
|
198 |
|
private static int getHeight(int w) { |
199 |
|
double h = w * (650.0 / 450.0); |
200 |
|
return (int)h; |
201 |
|
} |
202 |
|
|
203 |
//Called when the last widget is removed/disabled |
//Called when the last widget is removed/disabled |
204 |
@Override |
@Override |
205 |
public void onDisabled(Context context) { |
public void onDisabled(Context context) { |
206 |
super.onDisabled(context); |
super.onDisabled(context); |
207 |
Log.i(TAG, "onDisabled"); |
Log.i(TAG, "onDisabled"); |
208 |
|
|
209 |
usedData = null; //free memory |
resetStatics();//free memory |
|
usedBitmap = null; |
|
210 |
} |
} |
211 |
|
|
212 |
|
@Override |
213 |
|
public void onEnabled(Context context) { |
214 |
|
super.onEnabled(context); |
215 |
|
Log.i(TAG, "onEnabled"); |
216 |
|
|
217 |
|
resetStatics();//free memory |
218 |
|
} |
219 |
|
|
220 |
} |
} |