/[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 770 by torben, Wed May 5 11:52:27 2010 UTC revision 771 by torben, Mon May 31 14:14:59 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;
 import android.net.Uri;  
17  import android.util.Log;  import android.util.Log;
18  import android.widget.RemoteViews;  import android.widget.RemoteViews;
19    import dk.thoerup.androidutils.HttpUtil;
20    
21  public class Side9WidgetProvider extends AppWidgetProvider  {  public class Side9WidgetProvider extends AppWidgetProvider  {
22    
23            public static final String TAG = "Side9Pigen";
24    
25          //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()
26          private static Side9Data usedData;          private static Side9Data usedData;
# Line 24  public class Side9WidgetProvider extends Line 28  public class Side9WidgetProvider extends
28          private static long timestamp;          private static long timestamp;
29    
30          final static long UDPATESPAN = 3*60*60*1000;          final static long UDPATESPAN = 3*60*60*1000;
31            
32            final static String SAVEDIR = "/sdcard/Side9/";
33    
34    
35          static  {          static  {
# Line 31  public class Side9WidgetProvider extends Line 37  public class Side9WidgetProvider extends
37          }          }
38    
39          public Side9WidgetProvider() {          public Side9WidgetProvider() {
40                  Log.i("Side9Pigen", "WidgetProvider constructor called");                  Log.i(TAG, "WidgetProvider constructor called");
41          }          }
42    
43          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 56  public class Side9WidgetProvider extends
56                          //views.setTextViewText(R.id.caption, newData.caption);                          //views.setTextViewText(R.id.caption, newData.caption);
57    
58    
59                          // Launch a browser when user clicks on the image  
60                          Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://ekstrabladet.dk/side9/"));                          
61                          viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                          //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                          PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                                                    PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                          
84                          views.setOnClickPendingIntent(R.id.side9picture, pending);                                                    views.setOnClickPendingIntent(R.id.side9picture, pending);                          
85    
86                          // Tell the AppWidgetManager to perform an update on the current App Widget                          // Tell the AppWidgetManager to perform an update on the current App Widget
87                          appWidgetManager.updateAppWidget(appWidgetId, views);                          appWidgetManager.updateAppWidget(appWidgetId, views);
88    
89                          Log.i("Side9Pigen", "done " + appWidgetId);                          Log.i(TAG, "done " + appWidgetId);
90                  }                        }      
91          }          }
92    
# Line 67  public class Side9WidgetProvider extends Line 94  public class Side9WidgetProvider extends
94          public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {          public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
95                  super.onUpdate(context, appWidgetManager, appWidgetIds);                  super.onUpdate(context, appWidgetManager, appWidgetIds);
96    
97                  Log.i("Side9Pigen", "onUpdate:");                  Log.i(TAG, "onUpdate:");
98    
99                  if (usedBitmap == null) { //load default view                  if (usedBitmap == null) { //load default view
100                          usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);                          usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
# Line 78  public class Side9WidgetProvider extends Line 105  public class Side9WidgetProvider extends
105                  ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);                  ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
106                  if (connMgr.getBackgroundDataSetting() == false)                  if (connMgr.getBackgroundDataSetting() == false)
107                  {                  {
108                          Log.i("Side9Pigen", "background data disabled");                          Log.i(TAG, "background data disabled");
109                          return;                          return;
110                  }                  }
111    
112                  if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {                  if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
113                          Log.i("Side9Pigen", "time elapsed, force XML reload");                          Log.i(TAG, "time elapsed, force XML reload");
114                          usedData = null;                          usedData = null;
115                  }                  }
116    
# Line 93  public class Side9WidgetProvider extends Line 120  public class Side9WidgetProvider extends
120                          if (! newData.equals(usedData)) {                          if (! newData.equals(usedData)) {
121    
122    
123                                  Log.i("Side9Pigen", "(Re)loading image:" + newData.url);                                  Log.i(TAG, "(Re)loading image:" + newData.url);
   
                                 URL imgUrl = new URL( newData.url );              
124    
125                                  URLConnection conn = imgUrl.openConnection();                                  Bitmap image = getImageData(context, newData);
                                 conn.setConnectTimeout(2500);  
                                 Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() );  
126    
127                                  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
128                                  usedBitmap = image;                                  usedBitmap = image;
129                                  timestamp = System.currentTimeMillis();                                  timestamp = System.currentTimeMillis();
130                                    
131    
132                          } // endif                          } // endif
133    
# Line 114  public class Side9WidgetProvider extends Line 138  public class Side9WidgetProvider extends
138                  setImage(context,appWidgetManager,appWidgetIds, usedBitmap);                  setImage(context,appWidgetManager,appWidgetIds, usedBitmap);
139                  Log.i("Side9Pigen", "update completed");                  Log.i("Side9Pigen", "update completed");
140          }          }
141            
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    
169    
170          //Called when the last widget is removed/disabled          //Called when the last widget is removed/disabled
171          @Override          @Override
172          public void onDisabled(Context context) {          public void onDisabled(Context context) {
173                  super.onDisabled(context);                  super.onDisabled(context);
174                    Log.i(TAG, "onDisabled");
175    
176                  usedData = null; //free memory                  usedData = null; //free memory
177                  usedBitmap = null;                  usedBitmap = null;

Legend:
Removed from v.770  
changed lines
  Added in v.771

  ViewVC Help
Powered by ViewVC 1.1.20