15 |
import android.net.ConnectivityManager; |
import android.net.ConnectivityManager; |
16 |
import android.net.Uri; |
import android.net.Uri; |
17 |
import android.os.Environment; |
import android.os.Environment; |
18 |
|
import android.util.DisplayMetrics; |
19 |
import android.util.Log; |
import android.util.Log; |
20 |
|
import android.view.View; |
21 |
|
import android.view.WindowManager; |
22 |
import android.widget.RemoteViews; |
import android.widget.RemoteViews; |
23 |
import dk.thoerup.androidutils.HttpUtil; |
import dk.thoerup.androidutils.HttpUtil; |
24 |
|
|
30 |
private static Side9Data usedData; |
private static Side9Data usedData; |
31 |
private static Bitmap usedBitmap; |
private static Bitmap usedBitmap; |
32 |
private static long timestamp; |
private static long timestamp; |
33 |
|
private static boolean reloadData; |
34 |
|
|
35 |
final static long UDPATESPAN = 4*60*60*1000; |
final static long UDPATESPAN = 4*60*60*1000; |
36 |
|
|
52 |
|
|
53 |
|
|
54 |
|
|
55 |
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) { |
private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { |
56 |
// 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 |
57 |
final int N = appWidgetIds.length; |
final int N = appWidgetIds.length; |
58 |
for (int i=0; i<N; i++) { |
for (int i=0; i<N; i++) { |
60 |
|
|
61 |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
62 |
|
|
63 |
if (image != null) { |
if (usedBitmap != null) { |
64 |
views.setImageViewBitmap(R.id.side9picture, image); |
views.setImageViewBitmap(R.id.side9picture, usedBitmap); |
65 |
} else { |
} else { |
66 |
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); |
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); |
67 |
} |
} |
68 |
//views.setTextViewText(R.id.caption, newData.caption); |
|
69 |
|
boolean showcaption = context. |
70 |
|
getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE). |
71 |
|
getBoolean(Side9Config.PREFS_SHOWCAPTION, false); |
72 |
|
|
73 |
|
if (showcaption == true && usedData != null) { |
74 |
|
views.setTextViewText(R.id.caption, " " + usedData.caption + " "); |
75 |
|
views.setViewVisibility(R.id.caption, View.VISIBLE); |
76 |
|
} else { |
77 |
|
views.setViewVisibility(R.id.caption, View.GONE); |
78 |
|
} |
79 |
|
|
80 |
|
|
81 |
//Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/")); |
//Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/")); |
114 |
|
|
115 |
if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) { |
if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) { |
116 |
Log.i(TAG, "time elapsed, force XML reload"); |
Log.i(TAG, "time elapsed, force XML reload"); |
117 |
usedData = null; |
reloadData = true; |
118 |
} |
} |
119 |
|
|
120 |
try { |
try { |
121 |
Side9Data newData = Side9Xml.loadXml(); |
Side9Data newData = Side9Xml.loadXml(); |
122 |
|
|
123 |
if (! newData.equals(usedData)) { |
if (! newData.equals(usedData) || reloadData == true) { |
124 |
|
|
125 |
|
|
126 |
Log.i(TAG, "(Re)loading image:" + newData.url); |
Log.i(TAG, "(Re)loading image:" + newData.url); |
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 |
|
reloadData = false; |
134 |
|
|
135 |
} // endif |
} // endif |
136 |
|
|
138 |
Log.e("Side9Pigen", "update failed", e); |
Log.e("Side9Pigen", "update failed", e); |
139 |
} |
} |
140 |
|
|
141 |
setImage(context,appWidgetManager,appWidgetIds, usedBitmap); |
setImage(context,appWidgetManager,appWidgetIds); |
142 |
Log.i("Side9Pigen", "update completed"); |
Log.i("Side9Pigen", "update completed"); |
143 |
} |
} |
144 |
|
|
145 |
Bitmap getImageData(Context context, Side9Data data) throws IOException { |
Bitmap getImageData(Context context, Side9Data data) throws IOException { |
146 |
|
|
147 |
SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); |
SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE); |
148 |
boolean saveImage = prefs.getBoolean("saveimage", false); |
boolean saveImage = prefs.getBoolean(Side9Config.PREFS_SAVEIMAGE, false); |
149 |
|
|
150 |
final String savepath = Environment.getExternalStorageDirectory() + "/Side9"; |
final String savepath = Environment.getExternalStorageDirectory() + "/Side9"; |
151 |
|
|
183 |
} |
} |
184 |
} |
} |
185 |
|
|
186 |
Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
DisplayMetrics metrics = new DisplayMetrics(); |
187 |
Bitmap scaled = Bitmap.createScaledBitmap(full, 400, 577, true); |
WindowManager wmgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
188 |
return scaled; |
wmgr.getDefaultDisplay().getMetrics(metrics); |
189 |
|
int w = metrics.widthPixels - (2*40); |
190 |
|
int h = getHeight(w); |
191 |
|
|
192 |
|
Log.e(TAG, "w=" + w + " h=" + h); |
193 |
|
|
194 |
|
Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
195 |
|
Bitmap scaled = Bitmap.createScaledBitmap(full, w, h, true); |
196 |
|
return scaled; |
197 |
|
} |
198 |
|
|
199 |
|
private static int getHeight(int w) { |
200 |
|
double h = w * (650.0 / 450.0); |
201 |
|
return (int)h; |
202 |
} |
} |
203 |
|
|
|
|
|
204 |
//Called when the last widget is removed/disabled |
//Called when the last widget is removed/disabled |
205 |
@Override |
@Override |
206 |
public void onDisabled(Context context) { |
public void onDisabled(Context context) { |