/[projects]/android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java
ViewVC logotype

Contents of /android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 921 - (show annotations) (download)
Sat Jun 26 14:23:48 2010 UTC (13 years, 10 months ago) by torben
File size: 6576 byte(s)
Only try to set caption if we have some saved Data
1 package dk.thoerup.side9;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6
7 import android.app.PendingIntent;
8 import android.appwidget.AppWidgetManager;
9 import android.appwidget.AppWidgetProvider;
10 import android.content.Context;
11 import android.content.Intent;
12 import android.content.SharedPreferences;
13 import android.graphics.Bitmap;
14 import android.graphics.BitmapFactory;
15 import android.net.ConnectivityManager;
16 import android.net.Uri;
17 import android.os.Environment;
18 import android.util.DisplayMetrics;
19 import android.util.Log;
20 import android.view.View;
21 import android.view.WindowManager;
22 import android.widget.RemoteViews;
23 import dk.thoerup.androidutils.HttpUtil;
24
25 public class Side9WidgetProvider extends AppWidgetProvider {
26
27 public static final String TAG = "Side9Pigen";
28
29 //The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()
30 private static Side9Data usedData;
31 private static Bitmap usedBitmap;
32 private static long timestamp;
33
34 final static long UDPATESPAN = 4*60*60*1000;
35
36 static {
37 timestamp = 0L;
38 }
39
40 public Side9WidgetProvider() {
41 Log.i(TAG, "WidgetProvider constructor called");
42 }
43
44 public void resetStatics() {
45 Log.i(TAG, "resetStatics");
46 usedData = null;
47 usedBitmap = null;
48 }
49
50
51
52
53
54 private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
55 // Perform this loop procedure for each App Widget that belongs to this provider
56 final int N = appWidgetIds.length;
57 for (int i=0; i<N; i++) {
58 int appWidgetId = appWidgetIds[i];
59
60 RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);
61
62 if (usedBitmap != null) {
63 views.setImageViewBitmap(R.id.side9picture, usedBitmap);
64 } else {
65 views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);
66 }
67
68 boolean showcaption = context.
69 getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE).
70 getBoolean(Side9Config.PREFS_SHOWCAPTION, false);
71
72 if (showcaption == true && usedData != null) {
73 views.setTextViewText(R.id.caption, " " + usedData.caption + " ");
74 views.setViewVisibility(R.id.caption, View.VISIBLE);
75 } else {
76 views.setViewVisibility(R.id.caption, View.GONE);
77 }
78
79
80 //Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/"));
81
82 Intent viewIntent = new Intent(context, dk.thoerup.side9.PictureOverview.class );
83
84
85 PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
86 views.setOnClickPendingIntent(R.id.side9picture, pending);
87
88 // Tell the AppWidgetManager to perform an update on the current App Widget
89 appWidgetManager.updateAppWidget(appWidgetId, views);
90
91 Log.i(TAG, "done " + appWidgetId);
92 }
93 }
94
95 @Override
96 public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
97 super.onUpdate(context, appWidgetManager, appWidgetIds);
98
99 Log.i(TAG, "onUpdate:");
100
101 if (usedBitmap == null) { //load default view
102 usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
103 }
104
105
106
107 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
108 if (connMgr.getBackgroundDataSetting() == false)
109 {
110 Log.i(TAG, "background data disabled");
111 return;
112 }
113
114 if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
115 Log.i(TAG, "time elapsed, force XML reload");
116 usedData = null;
117 }
118
119 try {
120 Side9Data newData = Side9Xml.loadXml();
121
122 if (! newData.equals(usedData)) {
123
124
125 Log.i(TAG, "(Re)loading image:" + newData.url);
126
127 Bitmap image = getImageData(context, newData);
128
129 usedData = newData; // if we made it to here without exceptions, save the new data
130 usedBitmap = image;
131 timestamp = System.currentTimeMillis();
132
133
134 } // endif
135
136 } catch (Exception e) {
137 Log.e("Side9Pigen", "update failed", e);
138 }
139
140 setImage(context,appWidgetManager,appWidgetIds);
141 Log.i("Side9Pigen", "update completed");
142 }
143
144 Bitmap getImageData(Context context, Side9Data data) throws IOException {
145
146 SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
147 boolean saveImage = prefs.getBoolean(Side9Config.PREFS_SAVEIMAGE, false);
148
149 final String savepath = Environment.getExternalStorageDirectory() + "/Side9";
150
151 File file = new File( savepath + "/" + data.getFilename() );
152
153 /* if the picture changes later on the day we do NOT want to use an old and invalid image
154 if (saveImage == true) {
155 if (file.exists()) {
156 return BitmapFactory.decodeFile(file.getAbsolutePath());
157 }
158 }*/
159
160
161 byte imageData[] = HttpUtil.getContent(data.url, 2500);
162
163 if (saveImage == true) {
164 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
165 File savedir = new File(savepath);
166 savedir.mkdirs();
167
168 if (file.exists()) {
169 file.delete();
170 }
171
172 FileOutputStream fos = new FileOutputStream(file);
173 fos.write(imageData);
174 fos.close();
175
176 Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) );
177 rescan.putExtra("read-only", false);
178 context.sendBroadcast(rescan);
179
180 } else {
181 Log.i(TAG, "sdcard is not mounted");
182 }
183 }
184
185 DisplayMetrics metrics = new DisplayMetrics();
186 WindowManager wmgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
187 wmgr.getDefaultDisplay().getMetrics(metrics);
188 int w = metrics.widthPixels - (2*40);
189 int h = getHeight(w);
190
191 Log.e(TAG, "w=" + w + " h=" + h);
192
193 Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
194 Bitmap scaled = Bitmap.createScaledBitmap(full, w, h, true);
195 return scaled;
196 }
197
198 private static int getHeight(int w) {
199 double h = w * (650.0 / 450.0);
200 return (int)h;
201 }
202
203 //Called when the last widget is removed/disabled
204 @Override
205 public void onDisabled(Context context) {
206 super.onDisabled(context);
207 Log.i(TAG, "onDisabled");
208
209 resetStatics();//free memory
210 }
211
212 @Override
213 public void onEnabled(Context context) {
214 super.onEnabled(context);
215 Log.i(TAG, "onEnabled");
216
217 resetStatics();//free memory
218 }
219
220 }

  ViewVC Help
Powered by ViewVC 1.1.20