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