/[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 703 by torben, Mon May 3 14:54:30 2010 UTC revision 793 by torben, Thu Jun 3 09:39:56 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.Context;  import android.content.Context;
11  import android.content.Intent;  import android.content.Intent;
12    import android.content.SharedPreferences;
13  import android.graphics.Bitmap;  import android.graphics.Bitmap;
14  import android.graphics.BitmapFactory;  import android.graphics.BitmapFactory;
15  import android.net.ConnectivityManager;  import android.net.ConnectivityManager;
16  import android.net.Uri;  import android.net.Uri;
17    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 = 6*60*60*1000;          final static long UDPATESPAN = 4*60*60*1000;
   
32    
33          static  {          static  {
34                  timestamp = 0L;                  timestamp = 0L;
35          }          }
36    
37          public Side9WidgetProvider() {          public Side9WidgetProvider() {
38                  Log.i("Side9Pigen", "WidgetProvider constructor called");                  Log.i(TAG, "WidgetProvider constructor called");
39            }
40            
41            public void resetStatics() {
42                    Log.i(TAG, "resetStatics");
43                    usedData = null;
44                    usedBitmap = null;              
45          }          }
46            
47            
48    
49    
50    
51          private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {          private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {
52                  // Perform this loop procedure for each App Widget that belongs to this provider                  // Perform this loop procedure for each App Widget that belongs to this provider
# Line 50  public class Side9WidgetProvider extends Line 64  public class Side9WidgetProvider extends
64                          //views.setTextViewText(R.id.caption, newData.caption);                          //views.setTextViewText(R.id.caption, newData.caption);
65    
66    
67                          // Launch a browser when user clicks on the image                          //Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/"));                  
68                          Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://ekstrabladet.dk/side9/"));  
69                          viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                          Intent viewIntent = new Intent(context, dk.thoerup.side9.PictureOverview.class );
70                                                    
71                            
72                          PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                                                    PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                          
73                          views.setOnClickPendingIntent(R.id.side9picture, pending);                                                    views.setOnClickPendingIntent(R.id.side9picture, pending);                          
74    
75                          // Tell the AppWidgetManager to perform an update on the current App Widget                          // Tell the AppWidgetManager to perform an update on the current App Widget
76                          appWidgetManager.updateAppWidget(appWidgetId, views);                          appWidgetManager.updateAppWidget(appWidgetId, views);
77    
78                          Log.i("Side9Pigen", "done " + appWidgetId);                          Log.i(TAG, "done " + appWidgetId);
79                  }                        }      
80          }          }
81    
# Line 67  public class Side9WidgetProvider extends Line 83  public class Side9WidgetProvider extends
83          public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {          public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
84                  super.onUpdate(context, appWidgetManager, appWidgetIds);                  super.onUpdate(context, appWidgetManager, appWidgetIds);
85    
86                  Log.i("Side9Pigen", "onUpdate:");                  Log.i(TAG, "onUpdate:");
87    
88                  if (usedBitmap == null) { //load default view                  if (usedBitmap == null) { //load default view
89                          usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);                          usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
# Line 78  public class Side9WidgetProvider extends Line 94  public class Side9WidgetProvider extends
94                  ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);                  ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
95                  if (connMgr.getBackgroundDataSetting() == false)                  if (connMgr.getBackgroundDataSetting() == false)
96                  {                  {
97                          Log.i("Side9Pigen", "background data disabled");                          Log.i(TAG, "background data disabled");
98                          return;                          return;
99                  }                  }
100    
101                  if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {                  if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
102                          Log.i("Side9Pigen", "time elapsed, force XML reload");                          Log.i(TAG, "time elapsed, force XML reload");
103                          usedData = null;                          usedData = null;
104                  }                  }
105    
# Line 93  public class Side9WidgetProvider extends Line 109  public class Side9WidgetProvider extends
109                          if (! newData.equals(usedData)) {                          if (! newData.equals(usedData)) {
110    
111    
112                                  Log.i("Side9Pigen", "(Re)loading image:" + newData.url);                                  Log.i(TAG, "(Re)loading image:" + newData.url);
   
                                 URL imgUrl = new URL( newData.url );              
113    
114                                  URLConnection conn = imgUrl.openConnection();                                  Bitmap image = getImageData(context, newData);
                                 conn.setConnectTimeout(2500);  
                                 Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() );  
115    
116                                  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
117                                  usedBitmap = image;                                  usedBitmap = image;
118                                  timestamp = System.currentTimeMillis();                                  timestamp = System.currentTimeMillis();
119                                    
120    
121                          } // endif                          } // endif
122    
# Line 114  public class Side9WidgetProvider extends Line 127  public class Side9WidgetProvider extends
127                  setImage(context,appWidgetManager,appWidgetIds, usedBitmap);                  setImage(context,appWidgetManager,appWidgetIds, usedBitmap);
128                  Log.i("Side9Pigen", "update completed");                  Log.i("Side9Pigen", "update completed");
129          }          }
130            
131            Bitmap getImageData(Context context, Side9Data data) throws IOException {      
132                    
133                    SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
134                    boolean saveImage = prefs.getBoolean("saveimage", false);
135                    
136                    final String savepath = Environment.getExternalStorageDirectory() + "/Side9";
137                    
138                    File file = new File( savepath + "/" + data.getFilename() );
139                    
140                    /* if the picture changes later on the day we do NOT want to use an old and invalid image              
141                    if (saveImage == true) {                        
142                            if (file.exists()) {
143                                    return BitmapFactory.decodeFile(file.getAbsolutePath());
144                            }
145                    }*/
146                    
147                                    
148                    byte imageData[] = HttpUtil.getContent(data.url, 2500);
149                    
150                    if (saveImage == true) {
151                            if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
152                                    File savedir = new File(savepath);
153                                    savedir.mkdirs();
154                                    
155                                    if (file.exists()) {
156                                            file.delete();
157                                    }
158                                    
159                                    FileOutputStream fos = new FileOutputStream(file);
160                                    fos.write(imageData);
161                                    fos.close();
162    
163                                    Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) );
164                                    rescan.putExtra("read-only", false);                                                            
165                                    context.sendBroadcast(rescan);
166                                    
167                            } else {
168                                    Log.i(TAG, "sdcard is not mounted");
169                            }
170                    }
171                    
172                    Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
173                    Bitmap scaled = Bitmap.createScaledBitmap(full, 400, 577, true);
174                    return scaled;
175            }
176    
177    
178          //Called when the last widget is removed/disabled          //Called when the last widget is removed/disabled
179          @Override          @Override
180          public void onDisabled(Context context) {          public void onDisabled(Context context) {
181                  super.onDisabled(context);                  super.onDisabled(context);
182                    Log.i(TAG, "onDisabled");
183                  usedData = null; //free memory                  
184                  usedBitmap = null;                  resetStatics();//free memory
185          }          }
186    
187            @Override
188            public void onEnabled(Context context) {
189                    super.onEnabled(context);
190                    Log.i(TAG, "onEnabled");
191                    
192                    resetStatics();//free memory
193            }
194            
195  }  }

Legend:
Removed from v.703  
changed lines
  Added in v.793

  ViewVC Help
Powered by ViewVC 1.1.20