/[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 775 - (hide annotations) (download)
Tue Jun 1 07:46:12 2010 UTC (14 years ago) by torben
File size: 5649 byte(s)
only try to save the image IF the sd-card is mounted

... and added new icon

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

  ViewVC Help
Powered by ViewVC 1.1.20