/[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 921 by torben, Sat Jun 26 14:23:48 2010 UTC revision 1002 by torben, Mon Jul 26 10:10:38 2010 UTC
# Line 15  import android.graphics.BitmapFactory; Line 15  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;  import android.os.Environment;
18    import android.provider.Settings;
19  import android.util.DisplayMetrics;  import android.util.DisplayMetrics;
20  import android.util.Log;  import android.util.Log;
21  import android.view.View;  import android.view.View;
# Line 27  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;
35    
36          final static long UDPATESPAN = 4*60*60*1000;          final static long UDPATESPAN = 4*60*60*1000;
37    
38          static  {          static  {
39                  timestamp = 0L;                  mTimestamp = 0L;
40          }          }
41    
42          public Side9WidgetProvider() {          public Side9WidgetProvider() {
# Line 43  public class Side9WidgetProvider extends Line 45  public class Side9WidgetProvider extends
45                    
46          public void resetStatics() {          public void resetStatics() {
47                  Log.i(TAG, "resetStatics");                  Log.i(TAG, "resetStatics");
48                  usedData = null;                  mUsedData = null;
49                  usedBitmap = null;                                mUsedBitmap = null;            
50          }          }
51                    
52                    
# Line 59  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 (usedBitmap != null) {                          if (mUsedBitmap != null) {
65                                  views.setImageViewBitmap(R.id.side9picture, usedBitmap);                                  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                          }                          }
# Line 69  public class Side9WidgetProvider extends Line 71  public class Side9WidgetProvider extends
71                                          getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE).                                          getSharedPreferences(Side9WidgetProvider.TAG, Context.MODE_PRIVATE).
72                                          getBoolean(Side9Config.PREFS_SHOWCAPTION, false);                                          getBoolean(Side9Config.PREFS_SHOWCAPTION, false);
73                                                    
74                          if (showcaption == true && usedData != null) {                          if (showcaption == true && mUsedData != null) {
75                                  views.setTextViewText(R.id.caption, " " + usedData.caption + " ");                                  views.setTextViewText(R.id.caption, " " + mUsedData.caption + " ");
76                                  views.setViewVisibility(R.id.caption, View.VISIBLE);                                  views.setViewVisibility(R.id.caption, View.VISIBLE);
77                          } else {                          } else {
78                                  views.setViewVisibility(R.id.caption, View.GONE);                                  views.setViewVisibility(R.id.caption, View.GONE);
# Line 98  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 111  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 172  public class Side9WidgetProvider extends Line 179  public class Side9WidgetProvider extends
179                                  FileOutputStream fos = new FileOutputStream(file);                                  FileOutputStream fos = new FileOutputStream(file);
180                                  fos.write(imageData);                                  fos.write(imageData);
181                                  fos.close();                                  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()) );                                  Intent rescan = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()) );
192                                  rescan.putExtra("read-only", false);                                                                                              rescan.putExtra("read-only", false);                                                            

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

  ViewVC Help
Powered by ViewVC 1.1.20