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; |
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 |
|
|
27 |
public static final String TAG = "Side9Pigen"; |
public static final String TAG = "Side9Pigen"; |
28 |
|
|
29 |
//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() |
30 |
private static Side9Data usedData; |
private static Side9Data mUsedData; |
31 |
private static Bitmap usedBitmap; |
private static Bitmap mUsedBitmap; |
32 |
private static long timestamp; |
private static long mTimestamp; |
33 |
|
private static boolean mReloadData; |
34 |
|
|
35 |
final static long UDPATESPAN = 4*60*60*1000; |
final static long UDPATESPAN = 4*60*60*1000; |
36 |
|
|
37 |
static { |
static { |
38 |
timestamp = 0L; |
mTimestamp = 0L; |
39 |
} |
} |
40 |
|
|
41 |
public Side9WidgetProvider() { |
public Side9WidgetProvider() { |
44 |
|
|
45 |
public void resetStatics() { |
public void resetStatics() { |
46 |
Log.i(TAG, "resetStatics"); |
Log.i(TAG, "resetStatics"); |
47 |
usedData = null; |
mUsedData = null; |
48 |
usedBitmap = null; |
mUsedBitmap = null; |
49 |
} |
} |
50 |
|
|
51 |
|
|
60 |
|
|
61 |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget); |
62 |
|
|
63 |
if (usedBitmap != null) { |
if (mUsedBitmap != null) { |
64 |
views.setImageViewBitmap(R.id.side9picture, usedBitmap); |
views.setImageViewBitmap(R.id.side9picture, mUsedBitmap); |
65 |
} else { |
} else { |
66 |
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); |
views.setImageViewResource(R.id.side9picture, R.drawable.side9logo); |
67 |
} |
} |
70 |
getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE). |
getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE). |
71 |
getBoolean(Side9Config.PREFS_SHOWCAPTION, false); |
getBoolean(Side9Config.PREFS_SHOWCAPTION, false); |
72 |
|
|
73 |
if (showcaption == true) { |
if (showcaption == true && mUsedData != null) { |
74 |
views.setTextViewText(R.id.caption, " " + usedData.caption + " "); |
views.setTextViewText(R.id.caption, " " + mUsedData.caption + " "); |
75 |
views.setViewVisibility(R.id.caption, View.VISIBLE); |
views.setViewVisibility(R.id.caption, View.VISIBLE); |
76 |
} else { |
} else { |
77 |
views.setViewVisibility(R.id.caption, View.GONE); |
views.setViewVisibility(R.id.caption, View.GONE); |
99 |
|
|
100 |
Log.i(TAG, "onUpdate:"); |
Log.i(TAG, "onUpdate:"); |
101 |
|
|
102 |
if (usedBitmap == null) { //load default view |
if (mUsedBitmap == null) { //load default view |
103 |
usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo); |
mUsedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo); |
104 |
} |
} |
105 |
|
|
106 |
|
|
112 |
return; |
return; |
113 |
} |
} |
114 |
|
|
115 |
if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) { |
//we need to do a full reload of all data now and then because eb.dk sometimes are a bit slow with publishing the new |
116 |
|
//daily picture, and this is a (crude) way to ensure we have the latest data |
117 |
|
if (System.currentTimeMillis() > (mTimestamp+UDPATESPAN) ) { |
118 |
Log.i(TAG, "time elapsed, force XML reload"); |
Log.i(TAG, "time elapsed, force XML reload"); |
119 |
usedData = null; |
mReloadData = true; |
120 |
} |
} |
121 |
|
|
122 |
try { |
try { |
123 |
Side9Data newData = Side9Xml.loadXml(); |
Side9Data newData = Side9Xml.loadXml(); |
124 |
|
|
125 |
if (! newData.equals(usedData)) { |
if (! newData.equals(mUsedData) || mReloadData == true) { |
126 |
|
|
127 |
|
|
128 |
Log.i(TAG, "(Re)loading image:" + newData.url); |
Log.i(TAG, "(Re)loading image:" + newData.url); |
129 |
|
|
130 |
Bitmap image = getImageData(context, newData); |
Bitmap image = getImageData(context, newData); |
131 |
|
|
132 |
usedData = newData; // if we made it to here without exceptions, save the new data |
mUsedData = newData; // if we made it to here without exceptions, save the new data |
133 |
usedBitmap = image; |
mUsedBitmap = image; |
134 |
timestamp = System.currentTimeMillis(); |
mTimestamp = System.currentTimeMillis(); |
135 |
|
mReloadData = false; |
136 |
|
|
137 |
} // endif |
} // endif |
138 |
|
|
175 |
FileOutputStream fos = new FileOutputStream(file); |
FileOutputStream fos = new FileOutputStream(file); |
176 |
fos.write(imageData); |
fos.write(imageData); |
177 |
fos.close(); |
fos.close(); |
178 |
|
|
179 |
|
File infoFile = new File( file.toString().replace(".jpg", ".txt")); |
180 |
|
if (infoFile.exists()) { |
181 |
|
infoFile.delete(); |
182 |
|
} |
183 |
|
fos = new FileOutputStream(infoFile); |
184 |
|
fos.write(data.caption.getBytes()); |
185 |
|
fos.close(); |
186 |
|
|
187 |
Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) ); |
Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) ); |
188 |
rescan.putExtra("read-only", false); |
rescan.putExtra("read-only", false); |
193 |
} |
} |
194 |
} |
} |
195 |
|
|
196 |
Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
DisplayMetrics metrics = new DisplayMetrics(); |
197 |
Bitmap scaled = Bitmap.createScaledBitmap(full, 400, 577, true); |
WindowManager wmgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); |
198 |
return scaled; |
wmgr.getDefaultDisplay().getMetrics(metrics); |
199 |
|
int w = metrics.widthPixels - (2*40); |
200 |
|
int h = getHeight(w); |
201 |
|
|
202 |
|
Log.e(TAG, "w=" + w + " h=" + h); |
203 |
|
|
204 |
|
Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); |
205 |
|
Bitmap scaled = Bitmap.createScaledBitmap(full, w, h, true); |
206 |
|
return scaled; |
207 |
|
} |
208 |
|
|
209 |
|
private static int getHeight(int w) { |
210 |
|
double h = w * (650.0 / 450.0); |
211 |
|
return (int)h; |
212 |
} |
} |
213 |
|
|
|
|
|
214 |
//Called when the last widget is removed/disabled |
//Called when the last widget is removed/disabled |
215 |
@Override |
@Override |
216 |
public void onDisabled(Context context) { |
public void onDisabled(Context context) { |