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