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