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; |
8 |
import android.appwidget.AppWidgetManager; |
import android.appwidget.AppWidgetManager; |
9 |
import android.appwidget.AppWidgetProvider; |
import android.appwidget.AppWidgetProvider; |
|
import android.appwidget.AppWidgetProviderInfo; |
|
10 |
import android.content.Context; |
import android.content.Context; |
11 |
|
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; |
16 |
|
import android.net.Uri; |
17 |
|
import android.os.Environment; |
18 |
import android.util.Log; |
import android.util.Log; |
19 |
import android.widget.RemoteViews; |
import android.widget.RemoteViews; |
20 |
|
import dk.thoerup.androidutils.HttpUtil; |
21 |
|
|
22 |
public class Side9WidgetProvider extends AppWidgetProvider { |
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 = 4*60*60*1000; |
32 |
|
|
33 |
|
static { |
34 |
|
timestamp = 0L; |
35 |
|
} |
36 |
|
|
37 |
|
public Side9WidgetProvider() { |
38 |
|
Log.i(TAG, "WidgetProvider constructor called"); |
39 |
|
} |
40 |
|
|
41 |
|
public void resetStatics() { |
42 |
|
Log.i(TAG, "resetStatics"); |
43 |
|
usedData = null; |
44 |
|
usedBitmap = null; |
45 |
|
} |
46 |
|
|
47 |
|
|
48 |
|
|
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 |
|
final int N = appWidgetIds.length; |
54 |
|
for (int i=0; i<N; i++) { |
55 |
|
int appWidgetId = appWidgetIds[i]; |
56 |
|
|
57 |
|
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
58 |
|
|
59 |
|
if (image != null) { |
60 |
|
views.setImageViewBitmap(R.id.side9picture, image); |
61 |
|
} else { |
62 |
|
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); |
63 |
|
} |
64 |
|
//views.setTextViewText(R.id.caption, newData.caption); |
65 |
|
|
66 |
|
|
67 |
|
//Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/")); |
68 |
|
|
69 |
|
Intent viewIntent = new Intent(context, dk.thoerup.side9.PictureOverview.class ); |
70 |
|
|
71 |
|
|
72 |
|
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
73 |
|
views.setOnClickPendingIntent(R.id.side9picture, pending); |
74 |
|
|
75 |
|
// Tell the AppWidgetManager to perform an update on the current App Widget |
76 |
|
appWidgetManager.updateAppWidget(appWidgetId, views); |
77 |
|
|
78 |
|
Log.i(TAG, "done " + appWidgetId); |
79 |
|
} |
80 |
|
} |
81 |
|
|
82 |
@Override |
@Override |
83 |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
84 |
super.onUpdate(context, appWidgetManager, appWidgetIds); |
super.onUpdate(context, appWidgetManager, appWidgetIds); |
85 |
|
|
86 |
|
Log.i(TAG, "onUpdate:"); |
87 |
|
|
88 |
|
if (usedBitmap == null) { //load default view |
89 |
|
usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo); |
90 |
|
} |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
95 |
|
if (connMgr.getBackgroundDataSetting() == false) |
96 |
|
{ |
97 |
|
Log.i(TAG, "background data disabled"); |
98 |
|
return; |
99 |
|
} |
100 |
|
|
101 |
|
if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) { |
102 |
|
Log.i(TAG, "time elapsed, force XML reload"); |
103 |
|
usedData = null; |
104 |
|
} |
105 |
|
|
106 |
|
try { |
107 |
|
Side9Data newData = Side9Xml.loadXml(); |
108 |
|
|
109 |
|
if (! newData.equals(usedData)) { |
110 |
|
|
111 |
|
|
112 |
|
Log.i(TAG, "(Re)loading image:" + newData.url); |
113 |
|
|
114 |
|
Bitmap image = getImageData(context, newData); |
115 |
|
|
116 |
|
usedData = newData; // if we made it to here without exceptions, save the new data |
117 |
|
usedBitmap = image; |
118 |
|
timestamp = System.currentTimeMillis(); |
119 |
|
|
120 |
|
|
121 |
|
} // endif |
122 |
|
|
123 |
|
} catch (Exception e) { |
124 |
|
Log.e("Side9Pigen", "update failed", e); |
125 |
|
} |
126 |
|
|
127 |
|
setImage(context,appWidgetManager,appWidgetIds, usedBitmap); |
128 |
|
Log.i("Side9Pigen", "update completed"); |
129 |
|
} |
130 |
|
|
131 |
|
Bitmap getImageData(Context context, Side9Data data) throws IOException { |
132 |
|
|
133 |
final int N = appWidgetIds.length; // |
SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); |
134 |
|
boolean saveImage = prefs.getBoolean("saveimage", false); |
135 |
|
|
136 |
Log.e("onUpdate", "onUpdate:" + N); |
final String savepath = Environment.getExternalStorageDirectory() + "/Side9"; |
137 |
|
|
138 |
|
File file = new File( savepath + "/" + data.getFilename() ); |
139 |
|
|
140 |
try { |
/* if the picture changes later on the day we do NOT want to use an old and invalid image |
141 |
Side9Xml dataSource = new Side9Xml(); |
if (saveImage == true) { |
142 |
Side9Data data = dataSource.loadXml(); |
if (file.exists()) { |
143 |
|
return BitmapFactory.decodeFile(file.getAbsolutePath()); |
144 |
|
} |
145 |
URL imgUrl = new URL( data.url ); |
}*/ |
146 |
|
|
147 |
URLConnection conn = imgUrl.openConnection(); |
|
148 |
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); |
byte imageData[] = HttpUtil.getContent(data.url, 2500); |
149 |
|
|
150 |
|
if (saveImage == true) { |
151 |
// Perform this loop procedure for each App Widget that belongs to this provider |
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
152 |
for (int i=0; i<N; i++) { |
File savedir = new File(savepath); |
153 |
int appWidgetId = appWidgetIds[i]; |
savedir.mkdirs(); |
154 |
|
|
155 |
|
if (file.exists()) { |
156 |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
file.delete(); |
157 |
|
} |
158 |
|
|
159 |
views.setImageViewBitmap(R.id.side9picture, image); |
FileOutputStream fos = new FileOutputStream(file); |
160 |
|
fos.write(imageData); |
161 |
|
fos.close(); |
162 |
// Tell the AppWidgetManager to perform an update on the current App Widget |
|
163 |
appWidgetManager.updateAppWidget(appWidgetId, views); |
Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) ); |
164 |
|
rescan.putExtra("read-only", false); |
165 |
|
context.sendBroadcast(rescan); |
166 |
} |
|
167 |
} catch (Exception e) { |
} else { |
168 |
Log.e("UPDATE", "UPDATE", e); |
Log.i(TAG, "sdcard is not mounted"); |
169 |
} |
} |
170 |
|
} |
171 |
|
|
172 |
|
Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
173 |
|
Bitmap scaled = Bitmap.createScaledBitmap(full, 400, 577, true); |
174 |
|
return scaled; |
175 |
} |
} |
176 |
|
|
177 |
|
|
178 |
|
//Called when the last widget is removed/disabled |
179 |
|
@Override |
180 |
|
public void onDisabled(Context context) { |
181 |
|
super.onDisabled(context); |
182 |
|
Log.i(TAG, "onDisabled"); |
183 |
|
|
184 |
|
resetStatics();//free memory |
185 |
|
} |
186 |
|
|
187 |
|
@Override |
188 |
|
public void onEnabled(Context context) { |
189 |
|
super.onEnabled(context); |
190 |
|
Log.i(TAG, "onEnabled"); |
191 |
|
|
192 |
|
resetStatics();//free memory |
193 |
|
} |
194 |
|
|
195 |
} |
} |