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

  ViewVC Help
Powered by ViewVC 1.1.20