/[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 788 - (hide 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 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 632 import android.util.Log;
19     import android.widget.RemoteViews;
20 torben 771 import dk.thoerup.androidutils.HttpUtil;
21 torben 632
22     public class Side9WidgetProvider extends AppWidgetProvider {
23 torben 703
24 torben 771 public static final String TAG = "Side9Pigen";
25 torben 703
26 torben 635 //The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()
27     private static Side9Data usedData;
28 torben 689 private static Bitmap usedBitmap;
29 torben 703 private static long timestamp;
30    
31 torben 788 final static long UDPATESPAN = 4*60*60*1000;
32 torben 703
33     static {
34     timestamp = 0L;
35     }
36    
37 torben 649 public Side9WidgetProvider() {
38 torben 771 Log.i(TAG, "WidgetProvider constructor called");
39 torben 649 }
40 torben 778
41     public void resetStatics() {
42     Log.i(TAG, "resetStatics");
43     usedData = null;
44     usedBitmap = null;
45     }
46    
47    
48 torben 703
49 torben 778
50    
51 torben 681 private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {
52 torben 703 // Perform this loop procedure for each App Widget that belongs to this provider
53 torben 681 final int N = appWidgetIds.length;
54 torben 703 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 torben 776 Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/"));
68 torben 771
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 torben 776 /*Intent viewIntent = new Intent();
84 torben 771 viewIntent.addCategory(Intent.CATEGORY_LAUNCHER);
85     viewIntent.setAction(Intent.ACTION_MAIN);
86     viewIntent.setComponent(new ComponentName("com.android.camera", ".GalleryPicker"));
87 torben 776 viewIntent.setFlags(0x10200000);*/
88 torben 771
89    
90    
91    
92 torben 703 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 torben 771 Log.i(TAG, "done " + appWidgetId);
99 torben 703 }
100 torben 681 }
101 torben 703
102 torben 632 @Override
103     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
104     super.onUpdate(context, appWidgetManager, appWidgetIds);
105 torben 703
106 torben 771 Log.i(TAG, "onUpdate:");
107 torben 703
108 torben 689 if (usedBitmap == null) { //load default view
109     usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
110     }
111 torben 703
112    
113    
114 torben 636 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
115     if (connMgr.getBackgroundDataSetting() == false)
116     {
117 torben 771 Log.i(TAG, "background data disabled");
118 torben 636 return;
119     }
120 torben 703
121     if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
122 torben 771 Log.i(TAG, "time elapsed, force XML reload");
123 torben 703 usedData = null;
124     }
125    
126     try {
127     Side9Data newData = Side9Xml.loadXml();
128    
129     if (! newData.equals(usedData)) {
130    
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     usedData = newData; // if we made it to here without exceptions, save the new data
137     usedBitmap = image;
138     timestamp = System.currentTimeMillis();
139 torben 771
140 torben 703
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 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     boolean saveImage = prefs.getBoolean("saveimage", false);
155    
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 788
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 torben 776 } else {
188     Log.i(TAG, "sdcard is not mounted");
189 torben 774 }
190 torben 771 }
191    
192     return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
193     }
194 torben 632
195 torben 703
196 torben 635 //Called when the last widget is removed/disabled
197     @Override
198     public void onDisabled(Context context) {
199     super.onDisabled(context);
200 torben 771 Log.i(TAG, "onDisabled");
201 torben 778
202     resetStatics();//free memory
203     }
204 torben 703
205 torben 778 @Override
206     public void onEnabled(Context context) {
207     super.onEnabled(context);
208     Log.i(TAG, "onEnabled");
209    
210     resetStatics();//free memory
211 torben 635 }
212 torben 778
213 torben 632 }

  ViewVC Help
Powered by ViewVC 1.1.20