1 |
package dk.thoerup.side9; |
package dk.thoerup.side9; |
2 |
|
|
3 |
|
import java.io.File; |
4 |
|
import java.io.FileOutputStream; |
5 |
import java.io.IOException; |
import java.io.IOException; |
|
import java.net.URL; |
|
|
import java.net.URLConnection; |
|
6 |
|
|
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; |
10 |
import android.content.Context; |
import android.content.Context; |
11 |
import android.content.Intent; |
import android.content.Intent; |
12 |
|
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; |
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; |
24 |
|
|
25 |
public class Side9WidgetProvider extends AppWidgetProvider { |
public class Side9WidgetProvider extends AppWidgetProvider { |
26 |
|
|
27 |
|
public static final String TAG = "Side9Pigen"; |
28 |
|
|
29 |
//The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive() |
//The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive() |
30 |
private static Side9Data usedData; |
private static Side9Data mUsedData; |
31 |
|
private static Bitmap mUsedBitmap; |
32 |
|
private static long mTimestamp; |
33 |
|
private static boolean mReloadData; |
34 |
|
|
35 |
|
final static long UDPATESPAN = 4*60*60*1000; |
36 |
|
|
37 |
|
static { |
38 |
|
mTimestamp = 0L; |
39 |
|
} |
40 |
|
|
41 |
public Side9WidgetProvider() { |
public Side9WidgetProvider() { |
42 |
Log.i("Side9Pigen", "WidgetProvider constructor called"); |
Log.i(TAG, "WidgetProvider constructor called"); |
43 |
|
} |
44 |
|
|
45 |
|
public void resetStatics() { |
46 |
|
Log.i(TAG, "resetStatics"); |
47 |
|
mUsedData = null; |
48 |
|
mUsedBitmap = null; |
49 |
} |
} |
50 |
|
|
51 |
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 |
|
53 |
|
|
54 |
|
|
55 |
|
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
56 |
|
// Perform this loop procedure for each App Widget that belongs to this provider |
57 |
final int N = appWidgetIds.length; |
final int N = appWidgetIds.length; |
58 |
for (int i=0; i<N; i++) { |
for (int i=0; i<N; i++) { |
59 |
int appWidgetId = appWidgetIds[i]; |
int appWidgetId = appWidgetIds[i]; |
60 |
|
|
61 |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
62 |
|
|
63 |
if (image != null) { |
if (mUsedBitmap != null) { |
64 |
views.setImageViewBitmap(R.id.side9picture, image); |
views.setImageViewBitmap(R.id.side9picture, mUsedBitmap); |
65 |
} else { |
} else { |
66 |
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); |
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); |
67 |
} |
} |
68 |
//views.setTextViewText(R.id.caption, newData.caption); |
|
69 |
|
boolean showcaption = context. |
70 |
|
getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE). |
71 |
// Launch a browser when user clicks on the image |
getBoolean(Side9Config.PREFS_SHOWCAPTION, false); |
72 |
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Side9Xml.BASEURL)); |
|
73 |
viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
if (showcaption == true && mUsedData != null) { |
74 |
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
views.setTextViewText(R.id.caption, " " + mUsedData.caption + " "); |
75 |
views.setOnClickPendingIntent(R.id.side9picture, pending); |
views.setViewVisibility(R.id.caption, View.VISIBLE); |
76 |
|
} else { |
77 |
// Tell the AppWidgetManager to perform an update on the current App Widget |
views.setViewVisibility(R.id.caption, View.GONE); |
78 |
appWidgetManager.updateAppWidget(appWidgetId, views); |
} |
79 |
|
|
80 |
Log.i("Side9Pigen", "done " + appWidgetId); |
|
81 |
} |
//Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/")); |
82 |
|
|
83 |
|
Intent viewIntent = new Intent(context, dk.thoerup.side9.PictureOverview.class ); |
84 |
|
|
85 |
|
|
86 |
|
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
87 |
|
views.setOnClickPendingIntent(R.id.side9picture, pending); |
88 |
|
|
89 |
|
// Tell the AppWidgetManager to perform an update on the current App Widget |
90 |
|
appWidgetManager.updateAppWidget(appWidgetId, views); |
91 |
|
|
92 |
|
Log.i(TAG, "done " + appWidgetId); |
93 |
|
} |
94 |
} |
} |
95 |
|
|
96 |
@Override |
@Override |
97 |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
98 |
super.onUpdate(context, appWidgetManager, appWidgetIds); |
super.onUpdate(context, appWidgetManager, appWidgetIds); |
99 |
|
|
100 |
|
Log.i(TAG, "onUpdate:"); |
101 |
|
|
102 |
Log.e("Side9Pigen", "onUpdate:"); |
if (mUsedBitmap == null) { //load default view |
103 |
|
mUsedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo); |
104 |
|
} |
105 |
|
|
106 |
|
|
107 |
|
|
108 |
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
109 |
if (connMgr.getBackgroundDataSetting() == false) |
if (connMgr.getBackgroundDataSetting() == false) |
110 |
{ |
{ |
111 |
Log.i("Side9Pigen", "background data disabled"); |
Log.i(TAG, "background data disabled"); |
112 |
return; |
return; |
113 |
} |
} |
114 |
|
|
115 |
|
//we need to do a full reload of all data now and then because eb.dk sometimes are a bit slow with publishing the new |
116 |
|
//daily picture, and this is a (crude) way to ensure we have the latest data |
117 |
|
if (System.currentTimeMillis() > (mTimestamp+UDPATESPAN) ) { |
118 |
|
Log.i(TAG, "time elapsed, force XML reload"); |
119 |
|
mReloadData = true; |
120 |
|
} |
121 |
|
|
122 |
|
try { |
123 |
|
Side9Data newData = Side9Xml.loadXml(); |
124 |
|
|
125 |
|
if (! newData.equals(mUsedData) || mReloadData == true) { |
126 |
|
|
127 |
|
|
128 |
|
Log.i(TAG, "(Re)loading image:" + newData.url); |
129 |
|
|
130 |
|
Bitmap image = getImageData(context, newData); |
131 |
|
|
132 |
|
mUsedData = newData; // if we made it to here without exceptions, save the new data |
133 |
|
mUsedBitmap = image; |
134 |
|
mTimestamp = System.currentTimeMillis(); |
135 |
|
mReloadData = false; |
136 |
|
|
137 |
|
} // endif |
138 |
|
|
139 |
|
} catch (Exception e) { |
140 |
|
Log.e("Side9Pigen", "update failed", e); |
141 |
|
} |
142 |
|
|
143 |
|
setImage(context,appWidgetManager,appWidgetIds); |
144 |
|
Log.i("Side9Pigen", "update completed"); |
145 |
|
} |
146 |
|
|
147 |
|
Bitmap getImageData(Context context, Side9Data data) throws IOException { |
148 |
|
|
149 |
|
SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); |
150 |
|
boolean saveImage = prefs.getBoolean(Side9Config.PREFS_SAVEIMAGE, false); |
151 |
|
|
152 |
try { |
final String savepath = Environment.getExternalStorageDirectory() + "/Side9"; |
153 |
Side9Data newData = Side9Xml.loadXml(); |
|
154 |
|
File file = new File( savepath + "/" + data.getFilename() ); |
155 |
if (! newData.equals(usedData)) { |
|
156 |
|
/* if the picture changes later on the day we do NOT want to use an old and invalid image |
157 |
|
if (saveImage == true) { |
158 |
Log.i("Side9Pigen", "(Re)loading image:" + newData.url); |
if (file.exists()) { |
159 |
|
return BitmapFactory.decodeFile(file.getAbsolutePath()); |
160 |
URL imgUrl = new URL( newData.url ); |
} |
161 |
|
}*/ |
162 |
URLConnection conn = imgUrl.openConnection(); |
|
163 |
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); |
|
164 |
|
byte imageData[] = HttpUtil.getContent(data.url, 2500); |
165 |
setImage(context,appWidgetManager,appWidgetIds, image); |
|
166 |
|
if (saveImage == true) { |
167 |
usedData = newData; // if we made it to here without exceptions, save the new data |
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
168 |
|
File savedir = new File(savepath); |
169 |
} // endif |
savedir.mkdirs(); |
170 |
|
|
171 |
|
if (file.exists()) { |
172 |
} catch (Exception e) { |
file.delete(); |
173 |
Log.e("Side9Pigen", "update failed", e); |
} |
174 |
|
|
175 |
if (usedData == null) { |
FileOutputStream fos = new FileOutputStream(file); |
176 |
setImage(context,appWidgetManager,appWidgetIds, null); |
fos.write(imageData); |
177 |
} |
fos.close(); |
178 |
} |
|
179 |
|
File infoFile = new File( file.toString().replace(".jpg", ".txt")); |
180 |
|
if (infoFile.exists()) { |
181 |
|
infoFile.delete(); |
182 |
|
} |
183 |
|
fos = new FileOutputStream(infoFile); |
184 |
|
fos.write(data.caption.getBytes()); |
185 |
|
fos.close(); |
186 |
|
|
187 |
|
Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) ); |
188 |
|
rescan.putExtra("read-only", false); |
189 |
|
context.sendBroadcast(rescan); |
190 |
|
|
191 |
|
} else { |
192 |
|
Log.i(TAG, "sdcard is not mounted"); |
193 |
|
} |
194 |
|
} |
195 |
|
|
196 |
|
DisplayMetrics metrics = new DisplayMetrics(); |
197 |
|
WindowManager wmgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
198 |
|
wmgr.getDefaultDisplay().getMetrics(metrics); |
199 |
|
int w = metrics.widthPixels - (2*40); |
200 |
|
int h = getHeight(w); |
201 |
|
|
202 |
|
Log.e(TAG, "w=" + w + " h=" + h); |
203 |
|
|
204 |
|
Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
205 |
|
Bitmap scaled = Bitmap.createScaledBitmap(full, w, h, true); |
206 |
|
return scaled; |
207 |
} |
} |
208 |
|
|
209 |
|
private static int getHeight(int w) { |
210 |
|
double h = w * (650.0 / 450.0); |
211 |
|
return (int)h; |
212 |
|
} |
213 |
|
|
214 |
//Called when the last widget is removed/disabled |
//Called when the last widget is removed/disabled |
215 |
@Override |
@Override |
216 |
public void onDisabled(Context context) { |
public void onDisabled(Context context) { |
217 |
super.onDisabled(context); |
super.onDisabled(context); |
218 |
|
Log.i(TAG, "onDisabled"); |
219 |
|
|
220 |
usedData = null; //free memory |
resetStatics();//free memory |
221 |
} |
} |
222 |
|
|
223 |
|
@Override |
224 |
|
public void onEnabled(Context context) { |
225 |
|
super.onEnabled(context); |
226 |
|
Log.i(TAG, "onEnabled"); |
227 |
|
|
228 |
|
resetStatics();//free memory |
229 |
|
} |
230 |
|
|
231 |
} |
} |