/[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 771 by torben, Mon May 31 14:14:59 2010 UTC revision 924 by torben, Sat Jun 26 18:16:01 2010 UTC
# Line 7  import java.io.IOException; Line 7  import java.io.IOException;
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;
 import android.content.ComponentName;  
10  import android.content.Context;  import android.content.Context;
11  import android.content.Intent;  import android.content.Intent;
12  import android.content.SharedPreferences;  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;
17    import android.os.Environment;
18    import android.util.DisplayMetrics;
19  import android.util.Log;  import android.util.Log;
20    import android.view.View;
21    import android.view.WindowManager;
22  import android.widget.RemoteViews;  import android.widget.RemoteViews;
23  import dk.thoerup.androidutils.HttpUtil;  import dk.thoerup.androidutils.HttpUtil;
24    
# Line 26  public class Side9WidgetProvider extends Line 30  public class Side9WidgetProvider extends
30          private static Side9Data usedData;          private static Side9Data usedData;
31          private static Bitmap usedBitmap;          private static Bitmap usedBitmap;
32          private static long timestamp;          private static long timestamp;
33            private static boolean reloadData;
34    
35          final static long UDPATESPAN = 3*60*60*1000;          final static long UDPATESPAN = 4*60*60*1000;
           
         final static String SAVEDIR = "/sdcard/Side9/";  
   
36    
37          static  {          static  {
38                  timestamp = 0L;                  timestamp = 0L;
# Line 39  public class Side9WidgetProvider extends Line 41  public class Side9WidgetProvider extends
41          public Side9WidgetProvider() {          public Side9WidgetProvider() {
42                  Log.i(TAG, "WidgetProvider constructor called");                  Log.i(TAG, "WidgetProvider constructor called");
43          }          }
44            
45            public void resetStatics() {
46                    Log.i(TAG, "resetStatics");
47                    usedData = null;
48                    usedBitmap = null;              
49            }
50            
51            
52    
53    
54    
55          private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {          private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
56                  // 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
57                  final int N = appWidgetIds.length;                  final int N = appWidgetIds.length;
58                  for (int i=0; i<N; i++) {                  for (int i=0; i<N; i++) {
# Line 48  public class Side9WidgetProvider extends Line 60  public class Side9WidgetProvider extends
60    
61                          RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);                          RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);
62    
63                          if (image != null) {                          if (usedBitmap != null) {
64                                  views.setImageViewBitmap(R.id.side9picture, image);                                  views.setImageViewBitmap(R.id.side9picture, usedBitmap);
65                          } else {                          } else {
66                                  views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);                                  views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);
67                          }                          }
                         //views.setTextViewText(R.id.caption, newData.caption);  
   
   
   
                           
                         //View single file  
 //                      Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW);  
         //              viewIntent.setDataAndType(Uri.parse("file:///sdcard/Side9/20100531_400.jpg"), "image/png");  
                           
                         //view single file  
                         //Intent viewIntent = new Intent(Intent.ACTION_VIEW);  
                         //Uri u = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "1" );  
                         //viewIntent.setData(u);  
                           
                         //Intent viewIntent = new Intent();  
                         //viewIntent.setClassName("com.android.camera", "com.android.camera.GalleryPicker");  
68                                                    
69                            boolean showcaption = context.
70                                            getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE).
71                                            getBoolean(Side9Config.PREFS_SHOWCAPTION, false);
72                            
73                            if (showcaption == true && usedData != null) {
74                                    views.setTextViewText(R.id.caption, " " + usedData.caption + " ");
75                                    views.setViewVisibility(R.id.caption, View.VISIBLE);
76                            } else {
77                                    views.setViewVisibility(R.id.caption, View.GONE);
78                            }
79    
                         Intent viewIntent = new Intent();  
                         viewIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
                         viewIntent.setAction(Intent.ACTION_MAIN);  
                         viewIntent.setComponent(new ComponentName("com.android.camera", ".GalleryPicker"));  
                         viewIntent.setFlags(0x10200000);  
80    
81                                                    //Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/"));                  
82                            
83                            Intent viewIntent = new Intent(context, dk.thoerup.side9.PictureOverview.class );
84                                                    
85                                                    
86                          PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                                                    PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                          
87                          views.setOnClickPendingIntent(R.id.side9picture, pending);                                                    views.setOnClickPendingIntent(R.id.side9picture, pending);                          
# Line 109  public class Side9WidgetProvider extends Line 112  public class Side9WidgetProvider extends
112                          return;                          return;
113                  }                  }
114    
115                    //we need to do a full reload of all data now and then because eb.dk sometimes are a bit slow with publishing the new
116                    //daily picture, and this is a (crude) way to ensure we have the latest data
117                  if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {                  if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
118                          Log.i(TAG, "time elapsed, force XML reload");                          Log.i(TAG, "time elapsed, force XML reload");
119                          usedData = null;                          reloadData = true;
120                  }                  }
121    
122                  try {                  try {
123                          Side9Data newData = Side9Xml.loadXml();                          Side9Data newData = Side9Xml.loadXml();
124    
125                          if (! newData.equals(usedData)) {                          if (! newData.equals(usedData) || reloadData == true) {
126    
127    
128                                  Log.i(TAG, "(Re)loading image:" + newData.url);                                  Log.i(TAG, "(Re)loading image:" + newData.url);
# Line 127  public class Side9WidgetProvider extends Line 132  public class Side9WidgetProvider extends
132                                  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
133                                  usedBitmap = image;                                  usedBitmap = image;
134                                  timestamp = System.currentTimeMillis();                                  timestamp = System.currentTimeMillis();
135                                                                    reloadData = false;
136    
137                          } // endif                          } // endif
138    
# Line 135  public class Side9WidgetProvider extends Line 140  public class Side9WidgetProvider extends
140                          Log.e("Side9Pigen", "update failed", e);                          Log.e("Side9Pigen", "update failed", e);
141                  }                  }
142    
143                  setImage(context,appWidgetManager,appWidgetIds, usedBitmap);                  setImage(context,appWidgetManager,appWidgetIds);
144                  Log.i("Side9Pigen", "update completed");                  Log.i("Side9Pigen", "update completed");
145          }          }
146                    
147          Bitmap getImageData(Context context, Side9Data data) throws IOException {                Bitmap getImageData(Context context, Side9Data data) throws IOException {      
148                                    
149                  SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);                  SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
150                  boolean saveImage = prefs.getBoolean("saveimage", false);                  boolean saveImage = prefs.getBoolean(Side9Config.PREFS_SAVEIMAGE, false);
151                                    
152                  File file = new File( SAVEDIR + data.getFilename() );                  final String savepath = Environment.getExternalStorageDirectory() + "/Side9";
153                                    
154                  if (saveImage == true) {                                          File file = new File( savepath + "/" + data.getFilename() );
155                    
156                    /* if the picture changes later on the day we do NOT want to use an old and invalid image              
157                    if (saveImage == true) {                        
158                          if (file.exists()) {                          if (file.exists()) {
159                                  return BitmapFactory.decodeFile(file.getAbsolutePath());                                  return BitmapFactory.decodeFile(file.getAbsolutePath());
160                          }                          }
161                  }                  }*/
162                    
163                                                                    
164                  byte imageData[] = HttpUtil.getContent(data.url, 2500);                  byte imageData[] = HttpUtil.getContent(data.url, 2500);
165                                    
166                  if (saveImage == true) {                  if (saveImage == true) {
167                          File savedir = new File(SAVEDIR);                          if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
168                          savedir.mkdirs();                                  File savedir = new File(savepath);
169                                                            savedir.mkdirs();
170                          FileOutputStream fos = new FileOutputStream(file);                                  
171                          fos.write(imageData);                                  if (file.exists()) {
172                          fos.close();                                          file.delete();
173                                    }
174                                    
175                                    FileOutputStream fos = new FileOutputStream(file);
176                                    fos.write(imageData);
177                                    fos.close();
178    
179                                    Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) );
180                                    rescan.putExtra("read-only", false);                                                            
181                                    context.sendBroadcast(rescan);
182                                    
183                            } else {
184                                    Log.i(TAG, "sdcard is not mounted");
185                            }
186                  }                  }
187                                    
188                  return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);                   DisplayMetrics metrics = new DisplayMetrics();
189                     WindowManager wmgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
190                     wmgr.getDefaultDisplay().getMetrics(metrics);
191                     int w = metrics.widthPixels - (2*40);
192                     int h = getHeight(w);
193                    
194                     Log.e(TAG, "w=" + w + "   h=" + h);
195                    
196                     Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
197                     Bitmap scaled = Bitmap.createScaledBitmap(full, w, h, true);
198                     return scaled;
199          }          }
200    
201            private static int getHeight(int w) {
202                    double h = w * (650.0 / 450.0);
203                    return (int)h;
204            }
205            
206          //Called when the last widget is removed/disabled          //Called when the last widget is removed/disabled
207          @Override          @Override
208          public void onDisabled(Context context) {          public void onDisabled(Context context) {
209                  super.onDisabled(context);                  super.onDisabled(context);
210                  Log.i(TAG, "onDisabled");                  Log.i(TAG, "onDisabled");
211                    
212                  usedData = null; //free memory                  resetStatics();//free memory
                 usedBitmap = null;  
213          }          }
214    
215            @Override
216            public void onEnabled(Context context) {
217                    super.onEnabled(context);
218                    Log.i(TAG, "onEnabled");
219                    
220                    resetStatics();//free memory
221            }
222            
223  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20