1 |
package dk.thoerup.side9;
|
2 |
|
3 |
import java.io.File;
|
4 |
import java.io.FileOutputStream;
|
5 |
import java.io.IOException;
|
6 |
|
7 |
import android.app.PendingIntent;
|
8 |
import android.appwidget.AppWidgetManager;
|
9 |
import android.appwidget.AppWidgetProvider;
|
10 |
import android.content.Context;
|
11 |
import android.content.Intent;
|
12 |
import android.content.SharedPreferences;
|
13 |
import android.graphics.Bitmap;
|
14 |
import android.graphics.BitmapFactory;
|
15 |
import android.net.ConnectivityManager;
|
16 |
import android.net.Uri;
|
17 |
import android.os.Environment;
|
18 |
import android.util.Log;
|
19 |
import android.widget.RemoteViews;
|
20 |
import dk.thoerup.androidutils.HttpUtil;
|
21 |
|
22 |
public class Side9WidgetProvider extends AppWidgetProvider {
|
23 |
|
24 |
public static final String TAG = "Side9Pigen";
|
25 |
|
26 |
//The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()
|
27 |
private static Side9Data usedData;
|
28 |
private static Bitmap usedBitmap;
|
29 |
private static long timestamp;
|
30 |
|
31 |
final static long UDPATESPAN = 3*60*60*1000;
|
32 |
|
33 |
final static String SAVEDIR = "/sdcard/Side9/";
|
34 |
|
35 |
|
36 |
static {
|
37 |
timestamp = 0L;
|
38 |
}
|
39 |
|
40 |
public Side9WidgetProvider() {
|
41 |
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) {
|
55 |
// Perform this loop procedure for each App Widget that belongs to this provider
|
56 |
final int N = appWidgetIds.length;
|
57 |
for (int i=0; i<N; i++) {
|
58 |
int appWidgetId = appWidgetIds[i];
|
59 |
|
60 |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);
|
61 |
|
62 |
if (image != null) {
|
63 |
views.setImageViewBitmap(R.id.side9picture, image);
|
64 |
} else {
|
65 |
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);
|
66 |
}
|
67 |
//views.setTextViewText(R.id.caption, newData.caption);
|
68 |
|
69 |
|
70 |
Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/"));
|
71 |
|
72 |
|
73 |
//View single file
|
74 |
// Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW);
|
75 |
// viewIntent.setDataAndType(Uri.parse("file:///sdcard/Side9/20100531_400.jpg"), "image/png");
|
76 |
|
77 |
//view single file
|
78 |
//Intent viewIntent = new Intent(Intent.ACTION_VIEW);
|
79 |
//Uri u = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "1" );
|
80 |
//viewIntent.setData(u);
|
81 |
|
82 |
//Intent viewIntent = new Intent();
|
83 |
//viewIntent.setClassName("com.android.camera", "com.android.camera.GalleryPicker");
|
84 |
|
85 |
|
86 |
/*Intent viewIntent = new Intent();
|
87 |
viewIntent.addCategory(Intent.CATEGORY_LAUNCHER);
|
88 |
viewIntent.setAction(Intent.ACTION_MAIN);
|
89 |
viewIntent.setComponent(new ComponentName("com.android.camera", ".GalleryPicker"));
|
90 |
viewIntent.setFlags(0x10200000);*/
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
|
96 |
views.setOnClickPendingIntent(R.id.side9picture, pending);
|
97 |
|
98 |
// Tell the AppWidgetManager to perform an update on the current App Widget
|
99 |
appWidgetManager.updateAppWidget(appWidgetId, views);
|
100 |
|
101 |
Log.i(TAG, "done " + appWidgetId);
|
102 |
}
|
103 |
}
|
104 |
|
105 |
@Override
|
106 |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
107 |
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
108 |
|
109 |
Log.i(TAG, "onUpdate:");
|
110 |
|
111 |
if (usedBitmap == null) { //load default view
|
112 |
usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
|
113 |
}
|
114 |
|
115 |
|
116 |
|
117 |
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
118 |
if (connMgr.getBackgroundDataSetting() == false)
|
119 |
{
|
120 |
Log.i(TAG, "background data disabled");
|
121 |
return;
|
122 |
}
|
123 |
|
124 |
if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
|
125 |
Log.i(TAG, "time elapsed, force XML reload");
|
126 |
usedData = null;
|
127 |
}
|
128 |
|
129 |
try {
|
130 |
Side9Data newData = Side9Xml.loadXml();
|
131 |
|
132 |
if (! newData.equals(usedData)) {
|
133 |
|
134 |
|
135 |
Log.i(TAG, "(Re)loading image:" + newData.url);
|
136 |
|
137 |
Bitmap image = getImageData(context, newData);
|
138 |
|
139 |
usedData = newData; // if we made it to here without exceptions, save the new data
|
140 |
usedBitmap = image;
|
141 |
timestamp = System.currentTimeMillis();
|
142 |
|
143 |
|
144 |
} // endif
|
145 |
|
146 |
} catch (Exception e) {
|
147 |
Log.e("Side9Pigen", "update failed", e);
|
148 |
}
|
149 |
|
150 |
setImage(context,appWidgetManager,appWidgetIds, usedBitmap);
|
151 |
Log.i("Side9Pigen", "update completed");
|
152 |
}
|
153 |
|
154 |
Bitmap getImageData(Context context, Side9Data data) throws IOException {
|
155 |
|
156 |
SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
|
157 |
boolean saveImage = prefs.getBoolean("saveimage", false);
|
158 |
|
159 |
File file = new File( SAVEDIR + data.getFilename() );
|
160 |
|
161 |
/* if the picture changes later on the day we do NOT want to use an old and invalid image
|
162 |
if (saveImage == true) {
|
163 |
if (file.exists()) {
|
164 |
return BitmapFactory.decodeFile(file.getAbsolutePath());
|
165 |
}
|
166 |
}*/
|
167 |
|
168 |
|
169 |
byte imageData[] = HttpUtil.getContent(data.url, 2500);
|
170 |
|
171 |
if (saveImage == true) {
|
172 |
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
|
173 |
File savedir = new File(SAVEDIR);
|
174 |
savedir.mkdirs();
|
175 |
|
176 |
if (file.exists()) {
|
177 |
file.delete();
|
178 |
}
|
179 |
|
180 |
FileOutputStream fos = new FileOutputStream(file);
|
181 |
fos.write(imageData);
|
182 |
fos.close();
|
183 |
} else {
|
184 |
Log.i(TAG, "sdcard is not mounted");
|
185 |
}
|
186 |
}
|
187 |
|
188 |
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
|
189 |
}
|
190 |
|
191 |
|
192 |
//Called when the last widget is removed/disabled
|
193 |
@Override
|
194 |
public void onDisabled(Context context) {
|
195 |
super.onDisabled(context);
|
196 |
Log.i(TAG, "onDisabled");
|
197 |
|
198 |
resetStatics();//free memory
|
199 |
}
|
200 |
|
201 |
@Override
|
202 |
public void onEnabled(Context context) {
|
203 |
super.onEnabled(context);
|
204 |
Log.i(TAG, "onEnabled");
|
205 |
|
206 |
resetStatics();//free memory
|
207 |
}
|
208 |
|
209 |
}
|