.setLatitude(position.latitude)
.setLongitude(position.longitude)
.setLastCurrentUIUpdate(null)
- .setLastForecastUIUpdate(null);
+ .setLastForecastUIUpdate(null)
+ .setIsNew(true);
query.updateDataBase(weatherLocation);
} else {
final WeatherLocation location = new WeatherLocation()
.setCountry(countryString)
.setIsSelected(true)
.setLatitude(position.latitude)
- .setLongitude(position.longitude);
+ .setLongitude(position.longitude)
+ .setIsNew(true);
query.insertIntoDataBase(location);
}
}
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;
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";
// 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 {
private double longitude;
private Date lastCurrentUIUpdate;
private Date lastForecastUIUpdate;
+ private boolean isNew;
public WeatherLocation setId(int id) {
this.id = id;
return this;
}
+ public WeatherLocation setIsNew(final boolean isNew) {
+ this.isNew = isNew;
+ return this;
+ }
+
public int getId() {
return this.id;
}
public Date getLastForecastUIUpdate() {
return this.lastForecastUIUpdate;
}
+
+ public boolean getIsNew() {
+ return this.isNew;
+ }
}
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";
}
}
+ 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 "
+ ");");
}
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
};
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))) {
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()
.setLastCurrentUIUpdate(lastCurrentUIUpdate)
.setLastForecastUIUpdate(lasForecastUIUpdate)
.setLatitude(latitude)
- .setLongitude(longitude);
+ .setLongitude(longitude)
+ .setIsNew(isNew);
}
};
}
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);
}
}
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);
}
// 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);
}
}
final Intent intent = new Intent(context.getApplicationContext(), WidgetIntentService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
+ intent.putExtra("updateByApp", true);
context.startService(intent);
}
}