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

  ViewVC Help
Powered by ViewVC 1.1.20