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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20