CS 491 Lecture 16 – Wevents Part V
Agenda
Post-haste
I never got notes written when this post was first published. These are being written two months later. They won’t flow.
Flushing remote changes
When a user changes a calendar event, we always commit it to the local database first. This caching scheme is called writeback; we make our changes to the cache and then asynchronously push the cache changes to long-term storage. Here we demonstrate the asynchronous flush of the local changes to the remote database. We get a list of the dirty events, ship them off (one at a time, bad idea, let’s use a JSON array next time), and reset the dirty flag on the local event record (‘cuz it’s not dirty anymore).
private void flush(Intent intent) {
synchronized (lock) {
Event[] dirties = db.getDirtyEvents();
Log.d("WEVENTS", "need flushed: " + java.util.Arrays.toString(dirties));
for (Event event : db.getDirtyEvents()) {
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("username", intent.getStringExtra("username")));
postParameters.add(new BasicNameValuePair("password", intent.getStringExtra("password")));
postParameters.add(new BasicNameValuePair("id", event.getID() + ""));
postParameters.add(new BasicNameValuePair("title", event.getTitle()));
postParameters.add(new BasicNameValuePair("location", event.getLocation()));
Bundle bundle = post("http://www.twodee.org/wevents2/edit.php", postParameters);
if (bundle != null) {
Log.d("WEVENTS", "flushed " + event);
event.isLocal(false);
db.update(event);
}
}
Log.d("WEVENTS", "done flushing: " + java.util.Arrays.toString(dirties));
}
}
Triggering a flush
Okay, we’ve seen how to flush, but when do we flush? Not until we get a network connection back up. Rather than poll for such an event, we can register a BroadcastReceiver for network state changes. Our receiver will get an extra that tells us if the network is up or down given the change. We only want it operating while the app is foregrounded, so we birth and kill it in the lifecycle callbacks.
networkEar = new BroadcastReceiver() {
@Override
public void onReceive(Context context,
Intent intent) {
if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
boolean hasConnection = !intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
Log.d("WEVENTS", "connection: " + hasConnection);
if (hasConnection) {
((WeventsActivity) context).onNetworkReconnected();
}
}
}
};
@Override
public void onResume() {
super.onResume();
IntentFilter filter = new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE");
registerReceiver(networkEar, filter);
}
@Override
public void onPause() {
super.onPause();
unregisterReceiver(networkEar);
}
In onNetworkReconnected, we can issue the flush.
Scheduling flushes
It may also be useful to trigger a flush when a device boots. Perhaps the battery died or the device was powered off before the local edits were committed. When the devices turns on again, we don’t want to wait for the app to be run before letting the other family members know the edits.
For this, we can write another BroadcastReceiver, one that gets run on boot. We can also define our own custom flush signal. Before we write the class, let’s declare it in the manifest:
<receiver android:name=".FlushReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"/>
<action android:name="org.twodee.monthster.ACTION_FLUSH"/>
</intent-filter>
</receiver>
And now, the class itself. It’ll simply ask the service to perform the flush. But then, let’s also schedule a future flush. For this, we can use the AlarmManager, which allows us to schedule future intents, and a PendingIntent, which wraps around the Intent we wish to dispatch at a later time.
public class FlushReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context,
Intent intent) {
Log.d("WEVENTS", " WE WANT A FLUSH -------- ");
Intent flushIntent = new Intent(WeventsApplication.ACTION_FLUSH, null, context, WeventsService.class);
flushIntent.putExtra("username", "****");
flushIntent.putExtra("password", "****");
context.startService(flushIntent);
AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
Intent scheduleIntent = new Intent(WeventsApplication.ACTION_FLUSH);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, scheduleIntent, PendingIntent.FLAG_UPDATE_CURRENT);
manager.set(AlarmManager.RTC, System.currentTimeMillis() + 30 * 60 * 1000, pendingIntent);
}
}
A desktop view
Suppose we’d like to provide a little desktop widget to show what events are happening soon. We can use an AppWidget for this.
public class WeventsAppWidgetProvider extends AppWidgetProvider {
public void onUpdate(Context context,
AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget);
views.setTextViewText(R.id.box, "foo");
appWidgetManager.updateAppWidget(appWidgetIds, views);
Intent intent = new Intent(WeventsApplication.ACTION_WIDGET, null, context, WeventsService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[0]);
context.startService(intent);
}
}
Let’s declare it in XML:
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/appwidget"
android:minHeight="72dp"
android:minWidth="294dp"
android:updatePeriodMillis="1000">
</appwidget-provider>
An AppWidgetProvider is a BroadcastReceiver which gets a special APPWIDGET_UPDATE signal periodically from the OS. We must declare it in the manifest:
<receiver android:name="WeventsAppWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/wevents_appwidget_info"/>
</receiver>
Haiku
made one to show the Home screen
overflowed the stack