/[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 788 - (show annotations) (download)
Thu Jun 3 06:55:59 2010 UTC (13 years, 11 months ago) by torben
File size: 6334 byte(s)
When image saved to sdcard, send out a MEDIA_MOUNTED broadcast

increased force-reload interval to 4 hours

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

  ViewVC Help
Powered by ViewVC 1.1.20