Updates to the locale receiver so that notification only occurs for tags with visible and active tasks, and the last notification time is recorded.

Adding "synchronized" to the onOpen calls for all the controllers to avoid a race condition where the database would get opened from two different threads, get created in one, and while in the process of creation, get queried from the other thread.
pull/14/head
Tim Su 17 years ago
parent 2c501a35b7
commit 349e50c6c4

@ -140,7 +140,7 @@ public class SyncDataController extends AbstractController {
* @throws SQLException if the database could be neither opened or created
*/
@Override
public void open() throws SQLException {
public synchronized void open() throws SQLException {
SQLiteOpenHelper helper = new SyncMappingDatabaseHelper(context,
SYNC_TABLE_NAME, SYNC_TABLE_NAME);
syncDatabase = helper.getWritableDatabase();

@ -292,7 +292,7 @@ public class TagController extends AbstractController {
* @throws SQLException if the database could be neither opened or created
*/
@Override
public void open() throws SQLException {
public synchronized void open() throws SQLException {
tagToTaskMapDatabase = new TagToTaskMappingDatabaseHelper(context,
TAG_TASK_MAP_NAME, TAG_TASK_MAP_NAME).getWritableDatabase();
tagDatabase = new TagModelDatabaseHelper(context,

@ -143,12 +143,10 @@ public class TaskController extends AbstractController {
return list;
}
/** Get identifiers for all tasks */
public HashSet<TaskIdentifier> getAllTaskIdentifiers() {
/** Helper method to take a cursor pointing to a list of id's and generate
* a hashset */
private HashSet<TaskIdentifier> createTaskIdentifierSet(Cursor cursor) {
HashSet<TaskIdentifier> list = new HashSet<TaskIdentifier>();
Cursor cursor = database.query(TASK_TABLE_NAME, new String[] { KEY_ROWID },
null, null, null, null, null, null);
try {
if(cursor.getCount() == 0)
return list;
@ -165,29 +163,32 @@ public class TaskController extends AbstractController {
}
}
/** Get identifiers for all tasks */
public HashSet<TaskIdentifier> getAllTaskIdentifiers() {
Cursor cursor = database.query(TASK_TABLE_NAME, new String[] { KEY_ROWID },
null, null, null, null, null, null);
return createTaskIdentifierSet(cursor);
}
/** Get identifiers for all non-completed tasks */
public HashSet<TaskIdentifier> getActiveTaskIdentifiers() {
HashSet<TaskIdentifier> list = new HashSet<TaskIdentifier>();
Cursor cursor = database.query(TASK_TABLE_NAME, new String[] { KEY_ROWID },
AbstractTaskModel.PROGRESS_PERCENTAGE + " < " +
AbstractTaskModel.COMPLETE_PERCENTAGE, null, null, null, null, null);
return createTaskIdentifierSet(cursor);
}
try {
if(cursor.getCount() == 0)
return list;
do {
cursor.moveToNext();
list.add(new TaskIdentifier(cursor.getInt(
cursor.getColumnIndexOrThrow(KEY_ROWID))));
} while(!cursor.isLast());
return list;
} finally {
cursor.close();
}
/** Get identifiers for all non-completed, non-hidden tasks */
public HashSet<TaskIdentifier> getActiveVisibleTaskIdentifiers() {
Cursor cursor = database.query(TASK_TABLE_NAME, new String[] { KEY_ROWID },
AbstractTaskModel.PROGRESS_PERCENTAGE + " < " +
AbstractTaskModel.COMPLETE_PERCENTAGE + " AND " +
AbstractTaskModel.HIDDEN_UNTIL + " < " +
System.currentTimeMillis(), null, null, null, null, null);
return createTaskIdentifierSet(cursor);
}
/** Create a weighted list of tasks from the db cursor given */
public Cursor getTaskListCursorById(List<TaskIdentifier> idList) {
@ -521,7 +522,7 @@ public class TaskController extends AbstractController {
* @throws SQLException if the database could be neither opened or created
*/
@Override
public void open() throws SQLException {
public synchronized void open() throws SQLException {
SQLiteOpenHelper databaseHelper = new TaskModelDatabaseHelper(
context, TASK_TABLE_NAME, TASK_TABLE_NAME);
database = databaseHelper.getWritableDatabase();

@ -1,6 +1,5 @@
package com.timsu.astrid.utilities;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
@ -29,10 +28,6 @@ public class LocaleReceiver extends BroadcastReceiver {
/** minimum amount of time between two notifications */
private static final long MIN_NOTIFY_INTERVAL = 8*3600*1000L;
/** hash map of times we've notified */
private static final HashMap<Long, Long> notifyTimes =
new HashMap<Long, Long>();
@Override
/** Called when the system is started up */
public void onReceive(Context context, Intent intent) {
@ -46,10 +41,9 @@ public class LocaleReceiver extends BroadcastReceiver {
}
// check if we've already made a notification recently
if(notifyTimes.containsKey(tagId)) {
if(System.currentTimeMillis() - notifyTimes.get(tagId) <
MIN_NOTIFY_INTERVAL)
return;
if(System.currentTimeMillis() - Preferences.
getLocaleLastAlertTime(context, tagId) < MIN_NOTIFY_INTERVAL) {
return;
}
// find out if we have active tasks with this tag
@ -58,7 +52,7 @@ public class LocaleReceiver extends BroadcastReceiver {
TagController tagController = new TagController(context);
tagController.open();
try {
HashSet<TaskIdentifier> activeTasks = taskController.getActiveTaskIdentifiers();
HashSet<TaskIdentifier> activeTasks = taskController.getActiveVisibleTaskIdentifiers();
LinkedList<TaskIdentifier> tasks = tagController.getTaggedTasks(
new TagIdentifier(tagId));
int count = TagListSubActivity.countActiveTasks(activeTasks, tasks);
@ -67,7 +61,9 @@ public class LocaleReceiver extends BroadcastReceiver {
String reminder = r.getString(R.string.notif_tagNotification,
r.getQuantityString(R.plurals.Ntasks, count, count), tagName);
Notifications.showTagNotification(context, tagId, reminder);
notifyTimes.put(tagId, System.currentTimeMillis());
Preferences.setLocaleLastAlertTime(context, tagId,
System.currentTimeMillis());
}
} finally {
taskController.close();

@ -21,6 +21,7 @@ public class Preferences {
private static final String P_SYNC_RTM_LAST_SYNC = "rtmlastsync";
private static final String P_SYNC_LAST_SYNC = "lastsync";
private static final String P_SYNC_LAST_SYNC_ATTEMPT = "lastsyncattempt";
private static final String P_LOCALE_LAST_NOTIFY = "locnot";
// pref values
public static final int ICON_SET_PINK = 0;
@ -315,6 +316,18 @@ public class Preferences {
editor.commit();
}
// --- locale
public static void setLocaleLastAlertTime(Context context, long tag, long time) {
Editor editor = getPrefs(context).edit();
editor.putLong(P_LOCALE_LAST_NOTIFY + tag, time);
editor.commit();
}
public static long getLocaleLastAlertTime(Context context, long tag) {
return getPrefs(context).getLong(P_LOCALE_LAST_NOTIFY + tag, 0);
}
// --- helper methods
/** Clear the given preference */

Loading…
Cancel
Save