WeatherInformation: update widgets when new location
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 21 Oct 2014 23:49:40 +0000 (01:49 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Tue, 21 Oct 2014 23:49:40 +0000 (01:49 +0200)
app/src/main/java/de/example/exampletdd/MapActivity.java
app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java
app/src/main/java/de/example/exampletdd/model/WeatherLocation.java
app/src/main/java/de/example/exampletdd/model/WeatherLocationContract.java
app/src/main/java/de/example/exampletdd/model/WeatherLocationDbHelper.java
app/src/main/java/de/example/exampletdd/model/WeatherLocationDbQueries.java
app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java

index 01a3c41..4bfa6a2 100644 (file)
@@ -188,7 +188,8 @@ public class MapActivity extends FragmentActivity implements
                .setLatitude(position.latitude)
                .setLongitude(position.longitude)
                .setLastCurrentUIUpdate(null)
-               .setLastForecastUIUpdate(null);
+               .setLastForecastUIUpdate(null)
+                .setIsNew(true);
                query.updateDataBase(weatherLocation);
             } else {
                final WeatherLocation location = new WeatherLocation()
@@ -196,7 +197,8 @@ public class MapActivity extends FragmentActivity implements
                        .setCountry(countryString)
                        .setIsSelected(true)
                        .setLatitude(position.latitude)
-                       .setLongitude(position.longitude);
+                       .setLongitude(position.longitude)
+                    .setIsNew(true);
                query.insertIntoDataBase(location);
             }
        }
index d3a3d1e..e0423d5 100644 (file)
@@ -13,7 +13,9 @@ import java.util.Locale;
 
 import org.apache.http.client.ClientProtocolException;
 
+import android.appwidget.AppWidgetManager;
 import android.content.BroadcastReceiver;
+import android.content.ComponentName;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentFilter;
@@ -46,6 +48,7 @@ import de.example.exampletdd.parser.JPOSWeatherParser;
 import de.example.exampletdd.service.IconsList;
 import de.example.exampletdd.service.PermanentStorage;
 import de.example.exampletdd.service.ServiceParser;
+import de.example.exampletdd.widget.WidgetProvider;
 
 public class CurrentFragment extends Fragment {
     private static final String TAG = "CurrentFragment";
@@ -112,10 +115,26 @@ public class CurrentFragment extends Fragment {
                                        // 2. Update UI.
                                        CurrentFragment.this.updateUI(currentRemote);
 
-                                       // 3. Update Data.
+                                       // 3. Update current data.
                                                        store.saveCurrent(currentRemote);
-                                           weatherLocation.setLastCurrentUIUpdate(new Date());
-                                           query.updateDataBase(weatherLocation);
+
+                            // 4. If is new data (new location) update widgets.
+                            if (weatherLocation.getIsNew()) {
+                                final ComponentName widgets = new ComponentName(context.getApplicationContext(), WidgetProvider.class);
+                                final AppWidgetManager manager = AppWidgetManager.getInstance(context.getApplicationContext());
+                                final int[] appWidgetIds = manager.getAppWidgetIds(widgets);
+                                for (final int appWidgetId : appWidgetIds) {
+                                    final Intent intentWidget = new Intent(context.getApplicationContext(), WidgetIntentService.class);
+                                    intentWidget.putExtra("updateByApp", true);
+                                    intentWidget.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+                                    context.getApplicationContext().startService(intentWidget);
+                                }
+                            }
+
+                            // 5. Update location data.
+                            weatherLocation.setIsNew(false);
+                            weatherLocation.setLastCurrentUIUpdate(new Date());
+                            query.updateDataBase(weatherLocation);
                                    }
 
                                        } else {
index f20b0b6..13ef635 100644 (file)
@@ -14,6 +14,7 @@ public class WeatherLocation implements Serializable {
     private double longitude;
     private Date lastCurrentUIUpdate;
     private Date lastForecastUIUpdate;
+    private boolean isNew;
 
     public WeatherLocation setId(int id) {
                this.id = id;
@@ -55,6 +56,11 @@ public class WeatherLocation implements Serializable {
                return this;
        }
 
+    public WeatherLocation setIsNew(final boolean isNew) {
+        this.isNew = isNew;
+        return this;
+    }
+
        public int getId() {
        return this.id;
     }
@@ -86,4 +92,8 @@ public class WeatherLocation implements Serializable {
     public Date getLastForecastUIUpdate() {
        return this.lastForecastUIUpdate;
     }
+
+    public boolean getIsNew() {
+        return this.isNew;
+    }
 }
index 91e3472..ebfa8a8 100644 (file)
@@ -27,6 +27,8 @@ public class WeatherLocationContract {
                public static final String COLUMN_NAME_LAST_FORECAST_UI_UPDATE = "lastForecastUpdate";
                
                public static final String COLUMN_NAME_LAST_CURRENT_UI_UPDATE = "lastCurrentUpdate";
+
+        public static final String COLUMN_NAME_IS_NEW = "isNew";
        }
 
 }
index 8083516..eed2145 100644 (file)
@@ -24,7 +24,8 @@ public class WeatherLocationDbHelper extends SQLiteOpenHelper {
                                + WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE + " INTEGER, "
                                + WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE + " INTEGER, "
                                + WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE + " REAL" + " NOT NULL, "
-                               + WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE + " REAL" + " NOT NULL "
+                               + WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE + " REAL" + " NOT NULL, "
+                + WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW + " INTEGER" + " NOT NULL "
                                + ");");
        }
 
index fa97ca2..a4f3f56 100644 (file)
@@ -31,7 +31,8 @@ public class WeatherLocationDbQueries {
                        WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE,
                        WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_FORECAST_UI_UPDATE,
                        WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE,
-                       WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE
+                       WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE,
+                WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW
                    };
         
 
@@ -45,7 +46,7 @@ public class WeatherLocationDbQueries {
                        final String country = cursor.getString(cursor.
                                        getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_COUNTRY));
                        final boolean isSelected = (cursor.getInt(cursor
-                                       .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED)) == 0) ? false : true;  
+                        .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_SELECTED)) != 0);
                        Date lastCurrentUIUpdate = null;
                        if (!cursor.isNull(cursor
                                        .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LAST_CURRENT_UI_UPDATE))) {
@@ -64,6 +65,8 @@ public class WeatherLocationDbQueries {
                                        getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE));
                        final double longitude = cursor.getDouble(cursor.
                                        getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE));
+                final boolean isNew = (cursor.getInt(cursor
+                        .getColumnIndexOrThrow(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW)) != 0);
                        
                        
                        return new WeatherLocation()
@@ -74,7 +77,8 @@ public class WeatherLocationDbQueries {
                                        .setLastCurrentUIUpdate(lastCurrentUIUpdate)
                                        .setLastForecastUIUpdate(lasForecastUIUpdate)
                                        .setLatitude(latitude)
-                                       .setLongitude(longitude);
+                                       .setLongitude(longitude)
+                        .setIsNew(isNew);
                }
         };
 
@@ -99,6 +103,7 @@ public class WeatherLocationDbQueries {
                }
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
+        values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW, weatherLocation.getIsNew());
                
                return this.insertIntoDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, values);
        }
@@ -125,6 +130,7 @@ public class WeatherLocationDbQueries {
                }
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LATITUDE, weatherLocation.getLatitude());
                values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_LONGITUDE, weatherLocation.getLongitude());
+        values.put(WeatherLocationContract.WeatherLocation.COLUMN_NAME_IS_NEW, weatherLocation.getIsNew());
                
                this.updateDataBase(WeatherLocationContract.WeatherLocation.TABLE_NAME, selectionArgs, selection, values);
        }
index b5126f6..91263f2 100644 (file)
@@ -23,7 +23,7 @@ public class WidgetProvider extends AppWidgetProvider {
             // To prevent any ANR timeouts, we perform the update in a service
                final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
                intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
-               intent.putExtra("updateByApp", false);
+            intent.putExtra("updateByApp", false);
             context.startService(intent);
         }
     }
@@ -59,6 +59,7 @@ public class WidgetProvider extends AppWidgetProvider {
         
         final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+        intent.putExtra("updateByApp", true);
         context.startService(intent);
     }
 }