/[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 649 by torben, Mon Apr 19 12:49:31 2010 UTC revision 788 by torben, Thu Jun 3 06:55: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.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;
29            private static long timestamp;
30    
31            final static long UDPATESPAN = 4*60*60*1000;
32    
33            static  {
34                    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) {
52                    // Perform this loop procedure for each App Widget that belongs to this provider
53                    final int N = appWidgetIds.length;
54                    for (int i=0; i<N; i++) {
55                            int appWidgetId = appWidgetIds[i];
56    
57                            RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);
58    
59                            if (image != null) {
60                                    views.setImageViewBitmap(R.id.side9picture, image);
61                            } else {
62                                    views.setImageViewResource(R.id.side9picture, R.drawable.side9logo);
63                            }
64                            //views.setTextViewText(R.id.caption, newData.caption);
65    
66    
67                            Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://www.ekstrabladet.dk/side9/"));                    
68    
69                            
70                            //View single file
71    //                      Intent viewIntent = new Intent(android.content.Intent.ACTION_VIEW);
72            //              viewIntent.setDataAndType(Uri.parse("file:///sdcard/Side9/20100531_400.jpg"), "image/png");
73                            
74                            //view single file
75                            //Intent viewIntent = new Intent(Intent.ACTION_VIEW);
76                            //Uri u = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "1" );
77                            //viewIntent.setData(u);
78                            
79                            //Intent viewIntent = new Intent();
80                            //viewIntent.setClassName("com.android.camera", "com.android.camera.GalleryPicker");
81                            
82    
83                            /*Intent viewIntent = new Intent();
84                            viewIntent.addCategory(Intent.CATEGORY_LAUNCHER);
85                            viewIntent.setAction(Intent.ACTION_MAIN);
86                            viewIntent.setComponent(new ComponentName("com.android.camera", ".GalleryPicker"));
87                            viewIntent.setFlags(0x10200000);*/
88    
89                            
90                            
91                            
92                            PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                          
93                            views.setOnClickPendingIntent(R.id.side9picture, pending);                          
94    
95                            // Tell the AppWidgetManager to perform an update on the current App Widget
96                            appWidgetManager.updateAppWidget(appWidgetId, views);
97    
98                            Log.i(TAG, "done " + appWidgetId);
99                    }      
100            }
101    
102          @Override          @Override
103          public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {          public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
104                  super.onUpdate(context, appWidgetManager, appWidgetIds);                  super.onUpdate(context, appWidgetManager, appWidgetIds);
105                    
106                  final int N = appWidgetIds.length; //                  Log.i(TAG, "onUpdate:");
107                    
108                  Log.e("Side9Pigen", "onUpdate:" + N);                  if (usedBitmap == null) { //load default view
109                                            usedBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.side9logo);
110                    }
111    
112    
113    
114                  ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);                  ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
115                  if (connMgr.getBackgroundDataSetting() == false)                  if (connMgr.getBackgroundDataSetting() == false)
116                  {                  {
117                          Log.i("Side9Pigen", "background data disabled");                          Log.i(TAG, "background data disabled");
118                          return;                          return;
119                  }                  }
120    
121                    if (System.currentTimeMillis() > (timestamp+UDPATESPAN) ) {
122                            Log.i(TAG, "time elapsed, force XML reload");
123                            usedData = null;
124                    }
125    
126                    try {
127                            Side9Data newData = Side9Xml.loadXml();
128    
129                            if (! newData.equals(usedData)) {
130    
131    
132                                    Log.i(TAG, "(Re)loading image:" + newData.url);
133    
134                                    Bitmap image = getImageData(context, newData);
135    
136                                    usedData = newData; // if we made it to here without exceptions, save the new data
137                                    usedBitmap = image;
138                                    timestamp = System.currentTimeMillis();
139                                    
140    
141                            } // endif
142    
143                    } catch (Exception e) {
144                            Log.e("Side9Pigen", "update failed", e);
145                    }
146    
147                    setImage(context,appWidgetManager,appWidgetIds, usedBitmap);
148                    Log.i("Side9Pigen", "update completed");
149            }
150            
151            Bitmap getImageData(Context context, Side9Data data) throws IOException {      
152                    
153                    SharedPreferences prefs = context.getSharedPreferences(TAG, Context.MODE_PRIVATE);
154                    boolean saveImage = prefs.getBoolean("saveimage", false);
155                                    
156          try {                  final String savepath = Environment.getExternalStorageDirectory() + "/Side9";
157                  Side9Data newData = Side9Xml.loadXml();                  
158                                    File file = new File( savepath + "/" + data.getFilename() );
159                  if (! newData.equals(usedData)) {                  
160                                            /* if the picture changes later on the day we do NOT want to use an old and invalid image              
161                                            if (saveImage == true) {                        
162                          Log.i("Side9Pigen", "(Re)loading image:" + newData.url);                          if (file.exists()) {
163                                                                                    return BitmapFactory.decodeFile(file.getAbsolutePath());
164                          URL imgUrl = new URL( newData.url );                                      }
165                                            }*/
166                          URLConnection conn = imgUrl.openConnection();                  
167                          Bitmap image = BitmapFactory.decodeStream( conn.getInputStream() );                                  
168                                            byte imageData[] = HttpUtil.getContent(data.url, 2500);
169                                            
170                                            if (saveImage == true) {
171                          usedData = newData; // if we made it to here without exceptions, save the new data                          if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
172                                                            File savedir = new File(savepath);
173                                                    savedir.mkdirs();
174                          // Perform this loop procedure for each App Widget that belongs to this provider                                  
175                          for (int i=0; i<N; i++) {                                  if (file.exists()) {
176                              int appWidgetId = appWidgetIds[i];                                          file.delete();
177                                                                                        }
178                              RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.side9widget);                                  
179                                                                                        FileOutputStream fos = new FileOutputStream(file);
180                              views.setImageViewBitmap(R.id.side9picture, image);                                  fos.write(imageData);
181                              views.setTextViewText(R.id.caption, newData.caption);                                  fos.close();
182                                
183                                                                Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) );
184                              // Launch a browser when user clicks on the image                                  rescan.putExtra("read-only", false);                                                            
185                              Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(Side9Xml.BASEURL));                                  context.sendBroadcast(rescan);
186                              viewIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);                                  
187                              PendingIntent pending = PendingIntent.getActivity(context, 0, viewIntent, Intent.FLAG_ACTIVITY_NEW_TASK);                                                } else {
188                              views.setOnClickPendingIntent(R.id.side9picture, pending);                                                        Log.i(TAG, "sdcard is not mounted");
189                                                        }
190                              // Tell the AppWidgetManager to perform an update on the current App Widget                  }
191                              appWidgetManager.updateAppWidget(appWidgetId, views);                  
192                                                return BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
                             Log.i("Side9Pigen", "done " + appWidgetId);  
                         }        
                 } //END if (data.equals)  
                   
         } catch (Exception e) {  
                 Log.e("Side9Pigen", "update failed", e);  
         }  
193          }          }
194    
195            
196          //Called when the last widget is removed/disabled          //Called when the last widget is removed/disabled
197          @Override          @Override
198          public void onDisabled(Context context) {          public void onDisabled(Context context) {
199                  super.onDisabled(context);                  super.onDisabled(context);
200                    Log.i(TAG, "onDisabled");
201                                    
202                  usedData = null; //free memory                  resetStatics();//free memory
203          }          }
204    
205            @Override
206            public void onEnabled(Context context) {
207                    super.onEnabled(context);
208                    Log.i(TAG, "onEnabled");
209                    
210                    resetStatics();//free memory
211            }
212            
213  }  }

Legend:
Removed from v.649  
changed lines
  Added in v.788

  ViewVC Help
Powered by ViewVC 1.1.20