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

  ViewVC Help
Powered by ViewVC 1.1.20