Local broadcast receiver used as observer in loader

This commit is contained in:
Phillip Hsu
2016-07-06 00:40:52 -07:00
parent 9fb4727051
commit cf46160b1e
6 changed files with 92 additions and 45 deletions
@@ -9,6 +9,7 @@ import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import com.philliphsu.clock2.Alarm;
import com.philliphsu.clock2.util.LocalBroadcastHelper;
import static com.philliphsu.clock2.DaysOfWeek.FRIDAY;
import static com.philliphsu.clock2.DaysOfWeek.MONDAY;
@@ -65,8 +66,13 @@ public class AlarmDatabaseHelper extends SQLiteOpenHelper {
private static final String SORT_ORDER =
COLUMN_RING_TIME_MILLIS + " ASC, " + COLUMN_ID + " ASC";
private final Context mAppContext;
public AlarmDatabaseHelper(Context context) {
super(context, DB_NAME, null, VERSION_1);
// Since DatabaseManager calls this with the application
// context, we can safely hold onto this context.
mAppContext = context;
}
@Override
@@ -105,6 +111,7 @@ public class AlarmDatabaseHelper extends SQLiteOpenHelper {
long id = getWritableDatabase().insert(TABLE_ALARMS,
null, toContentValues(alarm));
alarm.setId(id);
notifyContentChanged();
return id;
}
@@ -115,26 +122,32 @@ public class AlarmDatabaseHelper extends SQLiteOpenHelper {
public int updateAlarm(Alarm oldAlarm, Alarm newAlarm) {
newAlarm.setId(oldAlarm.id());
SQLiteDatabase db = getWritableDatabase();
return db.update(TABLE_ALARMS,
int rowsUpdated = db.update(TABLE_ALARMS,
toContentValues(newAlarm),
COLUMN_ID + " = " + newAlarm.id(),
null);
notifyContentChanged();
return rowsUpdated;
}
public int updateAlarm(long id, Alarm newAlarm) {
newAlarm.setId(id);
SQLiteDatabase db = getWritableDatabase();
return db.update(TABLE_ALARMS,
int rowsUpdated = db.update(TABLE_ALARMS,
toContentValues(newAlarm),
COLUMN_ID + " = " + id,
null);
notifyContentChanged();
return rowsUpdated;
}
public int deleteAlarm(Alarm alarm) {
SQLiteDatabase db = getWritableDatabase();
return db.delete(TABLE_ALARMS,
int rowsDeleted = db.delete(TABLE_ALARMS,
COLUMN_ID + " = " + alarm.id(),
null);
notifyContentChanged();
return rowsDeleted;
}
public AlarmCursor queryAlarm(long id) {
@@ -184,6 +197,12 @@ public class AlarmDatabaseHelper extends SQLiteOpenHelper {
return values;
}
private void notifyContentChanged() {
Log.d(TAG, "notifyContentChanged()");
LocalBroadcastHelper.sendBroadcast(mAppContext,
SQLiteCursorLoader.ACTION_CHANGE_CONTENT);
}
// An alternative method to creating an Alarm from a cursor is to
// make an Alarm constructor that takes an Cursor param. However,
// this method has the advantage of keeping all the constants
@@ -1,8 +1,13 @@
package com.philliphsu.clock2.model;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.support.v4.content.AsyncTaskLoader;
import android.util.Log;
import com.philliphsu.clock2.util.LocalBroadcastHelper;
/**
* Created by Phillip Hsu on 6/28/2016.
@@ -12,7 +17,10 @@ import android.support.v4.content.AsyncTaskLoader;
public abstract class SQLiteCursorLoader extends AsyncTaskLoader<Cursor> {
private static final String TAG = "SQLiteCursorLoader";
public static final String ACTION_CHANGE_CONTENT = "com.philliphsu.clock2.model.action.CHANGE_CONTENT";
private Cursor mCursor;
private OnContentChangeReceiver mOnContentChangeReceiver;
public SQLiteCursorLoader(Context context) {
super(context);
@@ -66,30 +74,54 @@ public abstract class SQLiteCursorLoader extends AsyncTaskLoader<Cursor> {
if (mCursor != null) {
deliverResult(mCursor);
}
if (mOnContentChangeReceiver == null) {
mOnContentChangeReceiver = new OnContentChangeReceiver();
LocalBroadcastHelper.registerReceiver(getContext(),
mOnContentChangeReceiver, ACTION_CHANGE_CONTENT);
}
if (takeContentChanged() || mCursor == null) {
forceLoad();
}
}
@Override
protected void onStopLoading() {
// Attempt to cancel the current load task if possible.
cancelLoad();
}
@Override
public void onCanceled(Cursor cursor) {
if (cursor != null && !cursor.isClosed()) {
cursor.close();
}
}
@Override
protected void onReset() {
super.onReset();
// Ensure the loader is stopped
onStopLoading();
if (mCursor != null && !mCursor.isClosed()) {
mCursor.close();
}
mCursor = null;
if (mOnContentChangeReceiver != null) {
LocalBroadcastHelper.unregisterReceiver(getContext(),
mOnContentChangeReceiver);
mOnContentChangeReceiver = null;
}
}
private final class OnContentChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "Received content change event");
onContentChanged();
}
}
}