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

  ViewVC Help
Powered by ViewVC 1.1.20