/[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 771 - (hide annotations) (download)
Mon May 31 14:14:59 2010 UTC (14 years ago) by torben
File size: 5360 byte(s)
Partial commit - first iteration of saving images to /sdcard/
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 632 import android.util.Log;
18     import android.widget.RemoteViews;
19 torben 771 import dk.thoerup.androidutils.HttpUtil;
20 torben 632
21     public class Side9WidgetProvider extends AppWidgetProvider {
22 torben 703
23 torben 771 public static final String TAG = "Side9Pigen";
24 torben 703
25 torben 635 //The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()
26     private static Side9Data usedData;
27 torben 689 private static Bitmap usedBitmap;
28 torben 703 private static long timestamp;
29    
30 torben 710 final static long UDPATESPAN = 3*60*60*1000;
31 torben 771
32     final static String SAVEDIR = "/sdcard/Side9/";
33 torben 703
34    
35     static {
36     timestamp = 0L;
37     }
38    
39 torben 649 public Side9WidgetProvider() {
40 torben 771 Log.i(TAG, "WidgetProvider constructor called");
41 torben 649 }
42 torben 703
43 torben 681 private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {
44 torben 703 // Perform this loop procedure for each App Widget that belongs to this provider
45 torben 681 final int N = appWidgetIds.length;
46 torben 703 for (int i=0; i<N; i++) {
47     int appWidgetId = appWidgetIds[i];
48    
49     RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);
50    
51     if (image != null) {
52     views.setImageViewBitmap(R.id.side9picture, image);
53     } else {
54     views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);
55     }
56     //views.setTextViewText(R.id.caption, newData.caption);
57    
58    
59 torben 771
60    
61     //View single file
62     // Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW);
63     // viewIntent.setDataAndType(Uri.parse("file:///sdcard/Side9/20100531_400.jpg"), "image/png");
64    
65     //view single file
66     //Intent viewIntent = new Intent(Intent.ACTION_VIEW);
67     //Uri u = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "1" );
68     //viewIntent.setData(u);
69    
70     //Intent viewIntent = new Intent();
71     //viewIntent.setClassName("com.android.camera", "com.android.camera.GalleryPicker");
72    
73    
74     Intent viewIntent = new Intent();
75     viewIntent.addCategory(Intent.CATEGORY_LAUNCHER);
76     viewIntent.setAction(Intent.ACTION_MAIN);
77     viewIntent.setComponent(new ComponentName("com.android.camera", ".GalleryPicker"));
78     viewIntent.setFlags(0x10200000);
79    
80    
81    
82    
83 torben 703 PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
84     views.setOnClickPendingIntent(R.id.side9picture, pending);
85    
86     // Tell the AppWidgetManager to perform an update on the current App Widget
87     appWidgetManager.updateAppWidget(appWidgetId, views);
88    
89 torben 771 Log.i(TAG, "done " + appWidgetId);
90 torben 703 }
91 torben 681 }
92 torben 703
93 torben 632 @Override
94     public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
95     super.onUpdate(context, appWidgetManager, appWidgetIds);
96 torben 703
97 torben 771 Log.i(TAG, "onUpdate:");
98 torben 703
99 torben 689 if (usedBitmap == null) { //load default view
100     usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
101     }
102 torben 703
103    
104    
105 torben 636 ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
106     if (connMgr.getBackgroundDataSetting() == false)
107     {
108 torben 771 Log.i(TAG, "background data disabled");
109 torben 636 return;
110     }
111 torben 703
112     if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
113 torben 771 Log.i(TAG, "time elapsed, force XML reload");
114 torben 703 usedData = null;
115     }
116    
117     try {
118     Side9Data newData = Side9Xml.loadXml();
119    
120     if (! newData.equals(usedData)) {
121    
122    
123 torben 771 Log.i(TAG, "(Re)loading image:" + newData.url);
124 torben 703
125 torben 771 Bitmap image = getImageData(context, newData);
126 torben 703
127     usedData = newData; // if we made it to here without exceptions, save the new data
128     usedBitmap = image;
129     timestamp = System.currentTimeMillis();
130 torben 771
131 torben 703
132     } // endif
133    
134     } catch (Exception e) {
135     Log.e("Side9Pigen", "update failed", e);
136     }
137    
138     setImage(context,appWidgetManager,appWidgetIds, usedBitmap);
139     Log.i("Side9Pigen", "update completed");
140 torben 632 }
141 torben 771
142     Bitmap getImageData(Context context, Side9Data data) throws IOException {
143    
144     SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
145     boolean saveImage = prefs.getBoolean("saveimage", false);
146    
147     File file = new File( SAVEDIR + data.getFilename() );
148    
149     if (saveImage == true) {
150     if (file.exists()) {
151     return BitmapFactory.decodeFile(file.getAbsolutePath());
152     }
153     }
154    
155     byte imageData[] = HttpUtil.getContent(data.url, 2500);
156    
157     if (saveImage == true) {
158     File savedir = new File(SAVEDIR);
159     savedir.mkdirs();
160    
161     FileOutputStream fos = new FileOutputStream(file);
162     fos.write(imageData);
163     fos.close();
164     }
165    
166     return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
167     }
168 torben 632
169 torben 703
170 torben 635 //Called when the last widget is removed/disabled
171     @Override
172     public void onDisabled(Context context) {
173     super.onDisabled(context);
174 torben 771 Log.i(TAG, "onDisabled");
175 torben 703
176 torben 635 usedData = null; //free memory
177 torben 689 usedBitmap = null;
178 torben 635 }
179    
180 torben 632 }

  ViewVC Help
Powered by ViewVC 1.1.20