/[projects]/android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java
ViewVC logotype

Diff of /android/Side9/src/dk/thoerup/side9/Side9WidgetProvider.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 704 by torben, Mon May 3 21:15:52 2010 UTC revision 775 by torben, Tue Jun 1 07:46:12 2010 UTC
# Line 1  Line 1 
1  package dk.thoerup.side9;  package dk.thoerup.side9;
2    
3  import java.net.URL;  import java.io.File;
4  import java.net.URLConnection;  import java.io.FileOutputStream;
5    import java.io.IOException;
6    
7  import android.app.PendingIntent;  import android.app.PendingIntent;
8  import android.appwidget.AppWidgetManager;  import android.appwidget.AppWidgetManager;
9  import android.appwidget.AppWidgetProvider;  import android.appwidget.AppWidgetProvider;
10    import android.content.ComponentName;
11  import android.content.Context;  import android.content.Context;
12  import android.content.Intent;  import android.content.Intent;
13    import android.content.SharedPreferences;
14  import android.graphics.Bitmap;  import android.graphics.Bitmap;
15  import android.graphics.BitmapFactory;  import android.graphics.BitmapFactory;
16  import android.net.ConnectivityManager;  import android.net.ConnectivityManager;
17  import android.net.Uri;  import android.os.Environment;
18  import android.util.Log;  import android.util.Log;
19  import android.widget.RemoteViews;  import android.widget.RemoteViews;
20    import dk.thoerup.androidutils.HttpUtil;
21    
22  public class Side9WidgetProvider extends AppWidgetProvider  {  public class Side9WidgetProvider extends AppWidgetProvider  {
23    
24            public static final String TAG = "Side9Pigen";
25    
26          //The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()          //The data needs to be static, since BroadcastReceivers (which WidgetProviders extends) are only valid during onReceive()
27          private static Side9Data usedData;          private static Side9Data usedData;
28          private static Bitmap usedBitmap;          private static Bitmap usedBitmap;
29          private static long timestamp;          private static long timestamp;
30    
31          final static long UDPATESPAN = 4*60*60*1000;          final static long UDPATESPAN = 3*60*60*1000;
32            
33            final static String SAVEDIR = "/sdcard/Side9/";
34    
35    
36          static  {          static  {
# Line 31  public class Side9WidgetProvider extends Line 38  public class Side9WidgetProvider extends
38          }          }
39    
40          public Side9WidgetProvider() {          public Side9WidgetProvider() {
41                  Log.i("Side9Pigen", "WidgetProvider constructor called");                  Log.i(TAG, "WidgetProvider constructor called");
42          }          }
43    
44          private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {          private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {
# Line 50  public class Side9WidgetProvider extends Line 57  public class Side9WidgetProvider extends
57                          //views.setTextViewText(R.id.caption, newData.caption);                          //views.setTextViewText(R.id.caption, newData.caption);
58    
59    
60                          // Launch a browser when user clicks on the image  
61                          Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://ekstrabladet.dk/side9/"));                          
62                          viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                          //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                          PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                                                    PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                          
85                          views.setOnClickPendingIntent(R.id.side9picture, pending);                                                    views.setOnClickPendingIntent(R.id.side9picture, pending);                          
86    
87                          // Tell the AppWidgetManager to perform an update on the current App Widget                          // Tell the AppWidgetManager to perform an update on the current App Widget
88                          appWidgetManager.updateAppWidget(appWidgetId, views);                          appWidgetManager.updateAppWidget(appWidgetId, views);
89    
90                          Log.i("Side9Pigen", "done " + appWidgetId);                          Log.i(TAG, "done " + appWidgetId);
91                  }                        }      
92          }          }
93    
# Line 67  public class Side9WidgetProvider extends Line 95  public class Side9WidgetProvider extends
95          public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {          public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
96                  super.onUpdate(context, appWidgetManager, appWidgetIds);                  super.onUpdate(context, appWidgetManager, appWidgetIds);
97    
98                  Log.i("Side9Pigen", "onUpdate:");                  Log.i(TAG, "onUpdate:");
99    
100                  if (usedBitmap == null) { //load default view                  if (usedBitmap == null) { //load default view
101                          usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);                          usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
# Line 78  public class Side9WidgetProvider extends Line 106  public class Side9WidgetProvider extends
106                  ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);                  ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
107                  if (connMgr.getBackgroundDataSetting() == false)                  if (connMgr.getBackgroundDataSetting() == false)
108                  {                  {
109                          Log.i("Side9Pigen", "background data disabled");                          Log.i(TAG, "background data disabled");
110                          return;                          return;
111                  }                  }
112    
113                  if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {                  if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
114                          Log.i("Side9Pigen", "time elapsed, force XML reload");                          Log.i(TAG, "time elapsed, force XML reload");
115                          usedData = null;                          usedData = null;
116                  }                  }
117    
# Line 93  public class Side9WidgetProvider extends Line 121  public class Side9WidgetProvider extends
121                          if (! newData.equals(usedData)) {                          if (! newData.equals(usedData)) {
122    
123    
124                                  Log.i("Side9Pigen", "(Re)loading image:" + newData.url);                                  Log.i(TAG, "(Re)loading image:" + newData.url);
   
                                 URL imgUrl = new URL( newData.url );              
125    
126                                  URLConnection conn = imgUrl.openConnection();                                  Bitmap image = getImageData(context, newData);
                                 conn.setConnectTimeout(2500);  
                                 Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() );  
127    
128                                  usedData = newData; // if we made it to here without exceptions, save the new data                                  usedData = newData; // if we made it to here without exceptions, save the new data
129                                  usedBitmap = image;                                  usedBitmap = image;
130                                  timestamp = System.currentTimeMillis();                                  timestamp = System.currentTimeMillis();
131                                    
132    
133                          } // endif                          } // endif
134    
# Line 114  public class Side9WidgetProvider extends Line 139  public class Side9WidgetProvider extends
139                  setImage(context,appWidgetManager,appWidgetIds, usedBitmap);                  setImage(context,appWidgetManager,appWidgetIds, usedBitmap);
140                  Log.i("Side9Pigen", "update completed");                  Log.i("Side9Pigen", "update completed");
141          }          }
142            
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                    /* if the picture changes later on the day we do NOT want to use an old and invalid image              
151                    if (saveImage == true) {                        
152                            if (file.exists()) {
153                                    return BitmapFactory.decodeFile(file.getAbsolutePath());
154                            }
155                    }*/
156                    
157                                    
158                    byte imageData[] = HttpUtil.getContent(data.url, 2500);
159                    
160                    if (saveImage == true) {
161                            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                            }
173                    }
174                    
175                    return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
176            }
177    
178    
179          //Called when the last widget is removed/disabled          //Called when the last widget is removed/disabled
180          @Override          @Override
181          public void onDisabled(Context context) {          public void onDisabled(Context context) {
182                  super.onDisabled(context);                  super.onDisabled(context);
183                    Log.i(TAG, "onDisabled");
184    
185                  usedData = null; //free memory                  usedData = null; //free memory
186                  usedBitmap = null;                  usedBitmap = null;

Legend:
Removed from v.704  
changed lines
  Added in v.775

  ViewVC Help
Powered by ViewVC 1.1.20