/[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 778 - (hide annotations) (download)
Tue Jun 1 09:14:19 2010 UTC (14 years ago) by torben
File size: 6113 byte(s)
Some more force reload when creating or removing the widget
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 torben 771 import android.content.ComponentName;
11 torben 632 import android.content.Context;
12 torben 635 import android.content.Intent;
13 torben 771 import android.content.SharedPreferences;
14 torben 632 import android.graphics.Bitmap;
15     import android.graphics.BitmapFactory;
16 torben 636 import android.net.ConnectivityManager;
17 torben 776 import android.net.Uri;
18 torben 775 import android.os.Environment;
19 torben 632 import android.util.Log;
20     import android.widget.RemoteViews;
21 torben 771 import dk.thoerup.androidutils.HttpUtil;
22 torben 632
23     public class Side9WidgetProvider extends AppWidgetProvider {
24 torben 703
25 torben 771 public static final String TAG = "Side9Pigen";
26 torben 703
27 torben 635 //The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()
28     private static Side9Data usedData;
29 torben 689 private static Bitmap usedBitmap;
30 torben 703 private static long timestamp;
31    
32 torben 710 final static long UDPATESPAN = 3*60*60*1000;
33 torben 771
34     final static String SAVEDIR = "/sdcard/Side9/";
35 torben 703
36    
37     static {
38     timestamp = 0L;
39     }
40    
41 torben 649 public Side9WidgetProvider() {
42 torben 771 Log.i(TAG, "WidgetProvider constructor called");
43 torben 649 }
44 torben 778
45     public void resetStatics() {
46     Log.i(TAG, "resetStatics");
47     usedData = null;
48     usedBitmap = null;
49     }
50    
51    
52 torben 703
53 torben 778
54    
55 torben 681 private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {
56 torben 703 // Perform this loop procedure for each App Widget that belongs to this provider
57 torben 681 final int N = appWidgetIds.length;
58 torben 703 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 torben 776 Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/"));
72 torben 771
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 torben 776 /*Intent viewIntent = new Intent();
88 torben 771 viewIntent.addCategory(Intent.CATEGORY_LAUNCHER);
89     viewIntent.setAction(Intent.ACTION_MAIN);
90     viewIntent.setComponent(new ComponentName("com.android.camera", ".GalleryPicker"));
91 torben 776 viewIntent.setFlags(0x10200000);*/
92 torben 771
93    
94    
95    
96 torben 703 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 torben 771 Log.i(TAG, "done " + appWidgetId);
103 torben 703 }
104 torben 681 }
105 torben 703
106 torben 632 @Override
107     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
108     super.onUpdate(context, appWidgetManager, appWidgetIds);
109 torben 703
110 torben 771 Log.i(TAG, "onUpdate:");
111 torben 703
112 torben 689 if (usedBitmap == null) { //load default view
113     usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
114     }
115 torben 703
116    
117    
118 torben 636 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
119     if (connMgr.getBackgroundDataSetting() == false)
120     {
121 torben 771 Log.i(TAG, "background data disabled");
122 torben 636 return;
123     }
124 torben 703
125     if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
126 torben 771 Log.i(TAG, "time elapsed, force XML reload");
127 torben 703 usedData = null;
128     }
129    
130     try {
131     Side9Data newData = Side9Xml.loadXml();
132    
133     if (! newData.equals(usedData)) {
134    
135    
136 torben 771 Log.i(TAG, "(Re)loading image:" + newData.url);
137 torben 703
138 torben 771 Bitmap image = getImageData(context, newData);
139 torben 703
140     usedData = newData; // if we made it to here without exceptions, save the new data
141     usedBitmap = image;
142     timestamp = System.currentTimeMillis();
143 torben 771
144 torben 703
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 torben 632 }
154 torben 771
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 torben 774 /* if the picture changes later on the day we do NOT want to use an old and invalid image
163     if (saveImage == true) {
164 torben 771 if (file.exists()) {
165     return BitmapFactory.decodeFile(file.getAbsolutePath());
166     }
167 torben 774 }*/
168 torben 775
169 torben 771
170     byte imageData[] = HttpUtil.getContent(data.url, 2500);
171    
172     if (saveImage == true) {
173 torben 775 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 torben 776 } else {
185     Log.i(TAG, "sdcard is not mounted");
186 torben 774 }
187 torben 771 }
188    
189     return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
190     }
191 torben 632
192 torben 703
193 torben 635 //Called when the last widget is removed/disabled
194     @Override
195     public void onDisabled(Context context) {
196     super.onDisabled(context);
197 torben 771 Log.i(TAG, "onDisabled");
198 torben 778
199     resetStatics();//free memory
200     }
201 torben 703
202 torben 778 @Override
203     public void onEnabled(Context context) {
204     super.onEnabled(context);
205     Log.i(TAG, "onEnabled");
206    
207     resetStatics();//free memory
208 torben 635 }
209 torben 778
210 torben 632 }

  ViewVC Help
Powered by ViewVC 1.1.20