/[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 1002 by torben, Mon Jul 26 10:10:38 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.provider.Settings;
19    import android.util.DisplayMetrics;
20  import android.util.Log;  import android.util.Log;
21    import android.view.View;
22    import android.view.WindowManager;
23  import android.widget.RemoteViews;  import android.widget.RemoteViews;
24  import dk.thoerup.androidutils.HttpUtil;  import dk.thoerup.androidutils.HttpUtil;
25    
# Line 23  public class Side9WidgetProvider extends Line 28  public class Side9WidgetProvider extends
28          public static final String TAG = "Side9Pigen";          public static final String TAG = "Side9Pigen";
29    
30          //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()
31          private static Side9Data usedData;          private static Side9Data mUsedData;
32          private static Bitmap usedBitmap;          private static Bitmap mUsedBitmap;
33          private static long timestamp;          private static long mTimestamp;
34            private static boolean mReloadData;
         final static long UDPATESPAN = 3*60*60*1000;  
           
         final static String SAVEDIR = "/sdcard/Side9/";  
35    
36            final static long UDPATESPAN = 4*60*60*1000;
37    
38          static  {          static  {
39                  timestamp = 0L;                  mTimestamp = 0L;
40          }          }
41    
42          public Side9WidgetProvider() {          public Side9WidgetProvider() {
43                  Log.i(TAG, "WidgetProvider constructor called");                  Log.i(TAG, "WidgetProvider constructor called");
44          }          }
45            
46            public void resetStatics() {
47                    Log.i(TAG, "resetStatics");
48                    mUsedData = null;
49                    mUsedBitmap = null;            
50            }
51            
52            
53    
54          private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds, Bitmap image) {  
55    
56            private void setImage(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
57                  // 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
58                  final int N = appWidgetIds.length;                  final int N = appWidgetIds.length;
59                  for (int i=0; i<N; i++) {                  for (int i=0; i<N; i++) {
# Line 48  public class Side9WidgetProvider extends Line 61  public class Side9WidgetProvider extends
61    
62                          RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);                          RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);
63    
64                          if (image != null) {                          if (mUsedBitmap != null) {
65                                  views.setImageViewBitmap(R.id.side9picture, image);                                  views.setImageViewBitmap(R.id.side9picture, mUsedBitmap);
66                          } else {                          } else {
67                                  views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);                                  views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);
68                          }                          }
                         //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");  
69                                                    
70                            boolean showcaption = context.
71                                            getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE).
72                                            getBoolean(Side9Config.PREFS_SHOWCAPTION, false);
73                            
74                            if (showcaption == true && mUsedData != null) {
75                                    views.setTextViewText(R.id.caption, " " + mUsedData.caption + " ");
76                                    views.setViewVisibility(R.id.caption, View.VISIBLE);
77                            } else {
78                                    views.setViewVisibility(R.id.caption, View.GONE);
79                            }
80    
                         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);  
81    
82                                                    //Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/"));                  
83                            
84                            Intent viewIntent = new Intent(context, dk.thoerup.side9.PictureOverview.class );
85                                                    
86                                                    
87                          PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                                                    PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                          
88                          views.setOnClickPendingIntent(R.id.side9picture, pending);                                                    views.setOnClickPendingIntent(R.id.side9picture, pending);                          
# Line 96  public class Side9WidgetProvider extends Line 100  public class Side9WidgetProvider extends
100    
101                  Log.i(TAG, "onUpdate:");                  Log.i(TAG, "onUpdate:");
102    
103                  if (usedBitmap == null) { //load default view                  if (mUsedBitmap == null) { //load default view
104                          usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);                          mUsedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
105                  }                  }
106    
107    
# Line 109  public class Side9WidgetProvider extends Line 113  public class Side9WidgetProvider extends
113                          return;                          return;
114                  }                  }
115    
116                  if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {                  //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
117                    //daily picture, and this is a (crude) way to ensure we have the latest data
118                    if (System.currentTimeMillis() > (mTimestamp+UDPATESPAN) ) {
119                          Log.i(TAG, "time elapsed, force XML reload");                          Log.i(TAG, "time elapsed, force XML reload");
120                          usedData = null;                          mReloadData = true;
121                  }                  }
122                    
123    
124                    String androidID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
125                    
126                  try {                  try {
127                          Side9Data newData = Side9Xml.loadXml();                          Side9Data newData = Side9Xml.loadXml(androidID);
128    
129                          if (! newData.equals(usedData)) {                          if (! newData.equals(mUsedData) || mReloadData == true) {
130    
131    
132                                  Log.i(TAG, "(Re)loading image:" + newData.url);                                  Log.i(TAG, "(Re)loading image:" + newData.url);
133    
134                                  Bitmap image = getImageData(context, newData);                                  Bitmap image = getImageData(context, newData);
135    
136                                  usedData = newData; // if we made it to here without exceptions, save the new data                                  mUsedData = newData; // if we made it to here without exceptions, save the new data
137                                  usedBitmap = image;                                  mUsedBitmap = image;
138                                  timestamp = System.currentTimeMillis();                                  mTimestamp = System.currentTimeMillis();
139                                                                    mReloadData = false;
140    
141                          } // endif                          } // endif
142    
# Line 135  public class Side9WidgetProvider extends Line 144  public class Side9WidgetProvider extends
144                          Log.e("Side9Pigen", "update failed", e);                          Log.e("Side9Pigen", "update failed", e);
145                  }                  }
146    
147                  setImage(context,appWidgetManager,appWidgetIds, usedBitmap);                  setImage(context,appWidgetManager,appWidgetIds);
148                  Log.i("Side9Pigen", "update completed");                  Log.i("Side9Pigen", "update completed");
149          }          }
150                    
151          Bitmap getImageData(Context context, Side9Data data) throws IOException {                Bitmap getImageData(Context context, Side9Data data) throws IOException {      
152                                    
153                  SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);                  SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
154                  boolean saveImage = prefs.getBoolean("saveimage", false);                  boolean saveImage = prefs.getBoolean(Side9Config.PREFS_SAVEIMAGE, false);
155                    
156                    final String savepath = Environment.getExternalStorageDirectory() + "/Side9";
157                                    
158                  File file = new File( SAVEDIR + data.getFilename() );                  File file = new File( savepath + "/" + data.getFilename() );
159                                    
160                  if (saveImage == true) {                                          /* if the picture changes later on the day we do NOT want to use an old and invalid image              
161                    if (saveImage == true) {                        
162                          if (file.exists()) {                          if (file.exists()) {
163                                  return BitmapFactory.decodeFile(file.getAbsolutePath());                                  return BitmapFactory.decodeFile(file.getAbsolutePath());
164                          }                          }
165                  }                  }*/
166                    
167                                                                    
168                  byte imageData[] = HttpUtil.getContent(data.url, 2500);                  byte imageData[] = HttpUtil.getContent(data.url, 2500);
169                                    
170                  if (saveImage == true) {                  if (saveImage == true) {
171                          File savedir = new File(SAVEDIR);                          if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
172                          savedir.mkdirs();                                  File savedir = new File(savepath);
173                                                            savedir.mkdirs();
174                          FileOutputStream fos = new FileOutputStream(file);                                  
175                          fos.write(imageData);                                  if (file.exists()) {
176                          fos.close();                                          file.delete();
177                                    }
178                                    
179                                    FileOutputStream fos = new FileOutputStream(file);
180                                    fos.write(imageData);
181                                    fos.close();
182                                    
183                                    File infoFile = new File( file.toString().replace(".jpg", ".txt"));
184                                    if (infoFile.exists()) {
185                                            infoFile.delete();
186                                    }
187                                    fos = new FileOutputStream(infoFile);
188                                    fos.write(data.caption.getBytes());
189                                    fos.close();
190    
191                                    Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) );
192                                    rescan.putExtra("read-only", false);                                                            
193                                    context.sendBroadcast(rescan);
194                                    
195                            } else {
196                                    Log.i(TAG, "sdcard is not mounted");
197                            }
198                  }                  }
199                                    
200                  return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);                   DisplayMetrics metrics = new DisplayMetrics();
201                     WindowManager wmgr = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
202                     wmgr.getDefaultDisplay().getMetrics(metrics);
203                     int w = metrics.widthPixels - (2*40);
204                     int h = getHeight(w);
205                    
206                     Log.e(TAG, "w=" + w + "   h=" + h);
207                    
208                     Bitmap full = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
209                     Bitmap scaled = Bitmap.createScaledBitmap(full, w, h, true);
210                     return scaled;
211          }          }
212    
213            private static int getHeight(int w) {
214                    double h = w * (650.0 / 450.0);
215                    return (int)h;
216            }
217            
218          //Called when the last widget is removed/disabled          //Called when the last widget is removed/disabled
219          @Override          @Override
220          public void onDisabled(Context context) {          public void onDisabled(Context context) {
221                  super.onDisabled(context);                  super.onDisabled(context);
222                  Log.i(TAG, "onDisabled");                  Log.i(TAG, "onDisabled");
223                    
224                  usedData = null; //free memory                  resetStatics();//free memory
                 usedBitmap = null;  
225          }          }
226    
227            @Override
228            public void onEnabled(Context context) {
229                    super.onEnabled(context);
230                    Log.i(TAG, "onEnabled");
231                    
232                    resetStatics();//free memory
233            }
234            
235  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.20