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.ComponentName; |
11 |
import android.content.Context; |
import android.content.Context; |
12 |
import android.content.Intent; |
import android.content.Intent; |
13 |
|
import android.content.SharedPreferences; |
14 |
import android.graphics.Bitmap; |
import android.graphics.Bitmap; |
15 |
import android.graphics.BitmapFactory; |
import android.graphics.BitmapFactory; |
16 |
import android.net.ConnectivityManager; |
import android.net.ConnectivityManager; |
17 |
import android.net.Uri; |
import android.net.Uri; |
18 |
|
import android.os.Environment; |
19 |
import android.util.Log; |
import android.util.Log; |
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 = 4*60*60*1000; |
final static long UDPATESPAN = 3*60*60*1000; |
33 |
|
|
34 |
|
final static String SAVEDIR = "/sdcard/Side9/"; |
35 |
|
|
36 |
|
|
37 |
static { |
static { |
39 |
} |
} |
40 |
|
|
41 |
public Side9WidgetProvider() { |
public Side9WidgetProvider() { |
42 |
Log.i("Side9Pigen", "WidgetProvider constructor called"); |
Log.i(TAG, "WidgetProvider constructor called"); |
43 |
} |
} |
44 |
|
|
45 |
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) { |
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) { |
58 |
//views.setTextViewText(R.id.caption, newData.caption); |
//views.setTextViewText(R.id.caption, newData.caption); |
59 |
|
|
60 |
|
|
61 |
// 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/")); |
62 |
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://ekstrabladet.dk/side9/")); |
|
63 |
viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
|
64 |
|
//View single file |
65 |
|
// Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW); |
66 |
|
// viewIntent.setDataAndType(Uri.parse("file:///sdcard/Side9/20100531_400.jpg"), "image/png"); |
67 |
|
|
68 |
|
//view single file |
69 |
|
//Intent viewIntent = new Intent(Intent.ACTION_VIEW); |
70 |
|
//Uri u = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "1" ); |
71 |
|
//viewIntent.setData(u); |
72 |
|
|
73 |
|
//Intent viewIntent = new Intent(); |
74 |
|
//viewIntent.setClassName("com.android.camera", "com.android.camera.GalleryPicker"); |
75 |
|
|
76 |
|
|
77 |
|
/*Intent viewIntent = new Intent(); |
78 |
|
viewIntent.addCategory(Intent.CATEGORY_LAUNCHER); |
79 |
|
viewIntent.setAction(Intent.ACTION_MAIN); |
80 |
|
viewIntent.setComponent(new ComponentName("com.android.camera", ".GalleryPicker")); |
81 |
|
viewIntent.setFlags(0x10200000);*/ |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK); |
87 |
views.setOnClickPendingIntent(R.id.side9picture, pending); |
views.setOnClickPendingIntent(R.id.side9picture, pending); |
88 |
|
|
89 |
// Tell the AppWidgetManager to perform an update on the current App Widget |
// Tell the AppWidgetManager to perform an update on the current App Widget |
90 |
appWidgetManager.updateAppWidget(appWidgetId, views); |
appWidgetManager.updateAppWidget(appWidgetId, views); |
91 |
|
|
92 |
Log.i("Side9Pigen", "done " + appWidgetId); |
Log.i(TAG, "done " + appWidgetId); |
93 |
} |
} |
94 |
} |
} |
95 |
|
|
97 |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
98 |
super.onUpdate(context, appWidgetManager, appWidgetIds); |
super.onUpdate(context, appWidgetManager, appWidgetIds); |
99 |
|
|
100 |
Log.i("Side9Pigen", "onUpdate:"); |
Log.i(TAG, "onUpdate:"); |
101 |
|
|
102 |
if (usedBitmap == null) { //load default view |
if (usedBitmap == null) { //load default view |
103 |
usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo); |
usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo); |
108 |
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
109 |
if (connMgr.getBackgroundDataSetting() == false) |
if (connMgr.getBackgroundDataSetting() == false) |
110 |
{ |
{ |
111 |
Log.i("Side9Pigen", "background data disabled"); |
Log.i(TAG, "background data disabled"); |
112 |
return; |
return; |
113 |
} |
} |
114 |
|
|
115 |
if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) { |
if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) { |
116 |
Log.i("Side9Pigen", "time elapsed, force XML reload"); |
Log.i(TAG, "time elapsed, force XML reload"); |
117 |
usedData = null; |
usedData = null; |
118 |
} |
} |
119 |
|
|
123 |
if (! newData.equals(usedData)) { |
if (! newData.equals(usedData)) { |
124 |
|
|
125 |
|
|
126 |
Log.i("Side9Pigen", "(Re)loading image:" + newData.url); |
Log.i(TAG, "(Re)loading image:" + newData.url); |
|
|
|
|
URL imgUrl = new URL( newData.url ); |
|
127 |
|
|
128 |
URLConnection conn = imgUrl.openConnection(); |
Bitmap image = getImageData(context, newData); |
|
conn.setConnectTimeout(2500); |
|
|
Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() ); |
|
129 |
|
|
130 |
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 |
131 |
usedBitmap = image; |
usedBitmap = image; |
132 |
timestamp = System.currentTimeMillis(); |
timestamp = System.currentTimeMillis(); |
133 |
|
|
134 |
|
|
135 |
} // endif |
} // endif |
136 |
|
|
141 |
setImage(context,appWidgetManager,appWidgetIds, usedBitmap); |
setImage(context,appWidgetManager,appWidgetIds, usedBitmap); |
142 |
Log.i("Side9Pigen", "update completed"); |
Log.i("Side9Pigen", "update completed"); |
143 |
} |
} |
144 |
|
|
145 |
|
Bitmap getImageData(Context context, Side9Data data) throws IOException { |
146 |
|
|
147 |
|
SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); |
148 |
|
boolean saveImage = prefs.getBoolean("saveimage", false); |
149 |
|
|
150 |
|
File file = new File( SAVEDIR + data.getFilename() ); |
151 |
|
|
152 |
|
/* if the picture changes later on the day we do NOT want to use an old and invalid image |
153 |
|
if (saveImage == true) { |
154 |
|
if (file.exists()) { |
155 |
|
return BitmapFactory.decodeFile(file.getAbsolutePath()); |
156 |
|
} |
157 |
|
}*/ |
158 |
|
|
159 |
|
|
160 |
|
byte imageData[] = HttpUtil.getContent(data.url, 2500); |
161 |
|
|
162 |
|
if (saveImage == true) { |
163 |
|
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
164 |
|
File savedir = new File(SAVEDIR); |
165 |
|
savedir.mkdirs(); |
166 |
|
|
167 |
|
if (file.exists()) { |
168 |
|
file.delete(); |
169 |
|
} |
170 |
|
|
171 |
|
FileOutputStream fos = new FileOutputStream(file); |
172 |
|
fos.write(imageData); |
173 |
|
fos.close(); |
174 |
|
} else { |
175 |
|
Log.i(TAG, "sdcard is not mounted"); |
176 |
|
} |
177 |
|
} |
178 |
|
|
179 |
|
return BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
180 |
|
} |
181 |
|
|
182 |
|
|
183 |
//Called when the last widget is removed/disabled |
//Called when the last widget is removed/disabled |
184 |
@Override |
@Override |
185 |
public void onDisabled(Context context) { |
public void onDisabled(Context context) { |
186 |
super.onDisabled(context); |
super.onDisabled(context); |
187 |
|
Log.i(TAG, "onDisabled"); |
188 |
|
|
189 |
usedData = null; //free memory |
usedData = null; //free memory |
190 |
usedBitmap = null; |
usedBitmap = null; |