From 853bfc23e4b3b6c5a53978b98f55946136132f00 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 22 Oct 2014 01:49:40 +0200 Subject: [PATCH] WeatherInformation: update widgets when new location --- .../java/de/example/exampletdd/MapActivity.java | 6 ++++-- .../fragment/current/CurrentFragment.java | 25 +++++++++++++++++++--- .../example/exampletdd/model/WeatherLocation.java | 10 +++++++++ .../exampletdd/model/WeatherLocationContract.java | 2 ++ .../exampletdd/model/WeatherLocationDbHelper.java | 3 ++- .../exampletdd/model/WeatherLocationDbQueries.java | 12 ++++++++--- .../example/exampletdd/widget/WidgetProvider.java | 3 ++- 7 files changed, 51 insertions(+), 10 deletions(-) diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/MapActivity.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/MapActivity.java index 01a3c41..4bfa6a2 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/MapActivity.java +++ b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/MapActivity.java @@ -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); } } diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java index d3a3d1e..e0423d5 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java +++ b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/fragment/current/CurrentFragment.java @@ -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 { diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocation.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocation.java index f20b0b6..13ef635 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocation.java +++ b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocation.java @@ -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; + } } diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationContract.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationContract.java index 91e3472..ebfa8a8 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationContract.java +++ b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationContract.java @@ -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"; } } diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationDbHelper.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationDbHelper.java index 8083516..eed2145 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationDbHelper.java +++ b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationDbHelper.java @@ -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 " + ");"); } diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationDbQueries.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationDbQueries.java index fa97ca2..a4f3f56 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationDbQueries.java +++ b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/model/WeatherLocationDbQueries.java @@ -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); } diff --git a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java index b5126f6..91263f2 100644 --- a/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java +++ b/Android/WeatherInformation/app/src/main/java/de/example/exampletdd/widget/WidgetProvider.java @@ -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); } } -- 2.1.4