mirror of https://github.com/tasks/tasks
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.8 KiB
Java
102 lines
3.8 KiB
Java
package com.todoroo.astrid.locale;
|
|
|
|
import android.app.Notification;
|
|
import android.app.PendingIntent;
|
|
import android.content.BroadcastReceiver;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.res.Resources;
|
|
import android.util.Log;
|
|
|
|
import com.timsu.astrid.R;
|
|
import com.todoroo.andlib.data.TodorooCursor;
|
|
import com.todoroo.andlib.service.Autowired;
|
|
import com.todoroo.andlib.service.DependencyInjectionService;
|
|
import com.todoroo.andlib.service.NotificationManager;
|
|
import com.todoroo.andlib.service.NotificationManager.AndroidNotificationManager;
|
|
import com.todoroo.andlib.utility.DateUtilities;
|
|
import com.todoroo.astrid.activity.ShortcutActivity;
|
|
import com.todoroo.astrid.api.Filter;
|
|
import com.todoroo.astrid.model.Task;
|
|
import com.todoroo.astrid.service.TaskService;
|
|
import com.todoroo.astrid.utility.Constants;
|
|
import com.todoroo.astrid.utility.Preferences;
|
|
|
|
/**
|
|
* Receiver is activated when Locale conditions are triggered
|
|
*
|
|
* @author timsu
|
|
*
|
|
*/
|
|
public class LocaleReceiver extends BroadcastReceiver {
|
|
|
|
@Autowired
|
|
private TaskService taskService;
|
|
|
|
/**
|
|
* Create a preference key for storing / retrieving last interval time
|
|
* @param filterTitle
|
|
* @param interval
|
|
* @return
|
|
*/
|
|
private String makePreferenceKey(String filterTitle, int interval) {
|
|
return "LOCALE:" + filterTitle + interval; //$NON-NLS-1$
|
|
}
|
|
|
|
@SuppressWarnings("nls")
|
|
@Override
|
|
/** Called when the system is started up */
|
|
public void onReceive(Context context, Intent intent) {
|
|
try {
|
|
if (LocaleEditAlerts.ACTION_LOCALE_ALERT.equals(intent.getAction())) {
|
|
final String title = intent.getStringExtra(LocaleEditAlerts.KEY_FILTER_TITLE);
|
|
final String sql = intent.getStringExtra(LocaleEditAlerts.KEY_SQL);
|
|
final int interval = intent.getIntExtra(LocaleEditAlerts.KEY_INTERVAL, 24*3600);
|
|
|
|
// check if we've already made a notification recently
|
|
String preferenceKey = makePreferenceKey(title, interval);
|
|
long lastNotifyTime = Preferences.getLong(preferenceKey, 0);
|
|
if(DateUtilities.now() - lastNotifyTime < interval * 1000L) {
|
|
Log.i("astrid-locale", title + ": Too soon, need " + (interval
|
|
- (DateUtilities.now() - lastNotifyTime)/1000) + " more seconds");
|
|
return;
|
|
}
|
|
|
|
// find out if we have active tasks with this tag
|
|
DependencyInjectionService.getInstance().inject(this);
|
|
Filter filter = new Filter(title, title, null, null);
|
|
filter.sqlQuery = sql;
|
|
TodorooCursor<Task> cursor = taskService.fetchFiltered(filter, Task.ID);
|
|
try {
|
|
if(cursor.getCount() == 0)
|
|
return;
|
|
Resources r = context.getResources();
|
|
String reminder = r.getString(R.string.locale_notification).
|
|
replace("$NUM", r.getQuantityString(R.plurals.Ntasks,
|
|
cursor.getCount(), cursor.getCount())).
|
|
replace("$FILTER", title);
|
|
|
|
// show a reminder
|
|
Intent notifyIntent = ShortcutActivity.createIntent(filter);
|
|
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
|
PendingIntent pendingIntent = PendingIntent.getActivity(context,
|
|
Constants.NOTIFICATION_TIMER, notifyIntent, 0);
|
|
Notification notification = new Notification(
|
|
R.drawable.timers_notification, reminder, System.currentTimeMillis());
|
|
notification.setLatestEventInfo(context, r.getString(R.string.locale_edit_alerts_title),
|
|
reminder, pendingIntent);
|
|
|
|
NotificationManager nm = new AndroidNotificationManager(context);
|
|
nm.notify(Constants.NOTIFICATION_TIMER, notification);
|
|
Preferences.setLong(preferenceKey, DateUtilities.now());
|
|
} finally {
|
|
cursor.close();
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
Log.e("astrid-locale-rx", "Error receiving intent", e);
|
|
}
|
|
}
|
|
|
|
}
|