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.
111 lines
4.0 KiB
Java
111 lines
4.0 KiB
Java
/**
|
|
* Copyright (c) 2012 Todoroo Inc
|
|
*
|
|
* See the file "LICENSE" for the full license governing this code.
|
|
*/
|
|
package com.todoroo.astrid.widget;
|
|
|
|
import android.annotation.TargetApi;
|
|
import android.appwidget.AppWidgetManager;
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.os.Build;
|
|
import android.support.v4.app.TaskStackBuilder;
|
|
|
|
import com.todoroo.andlib.utility.DateUtilities;
|
|
import com.todoroo.astrid.activity.TaskEditFragment;
|
|
import com.todoroo.astrid.activity.TaskListFragment;
|
|
import com.todoroo.astrid.api.Filter;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.tasks.Broadcaster;
|
|
import org.tasks.R;
|
|
import org.tasks.injection.InjectingAppWidgetProvider;
|
|
import org.tasks.intents.TaskIntents;
|
|
import org.tasks.widget.WidgetHelper;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import static com.todoroo.andlib.utility.AndroidUtilities.preIceCreamSandwich;
|
|
import static com.todoroo.astrid.api.AstridApiConstants.BROADCAST_EVENT_REFRESH;
|
|
import static org.tasks.intents.TaskIntents.getEditTaskStack;
|
|
|
|
public class TasksWidget extends InjectingAppWidgetProvider {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(TasksWidget.class);
|
|
|
|
@Inject Broadcaster broadcaster;
|
|
@Inject WidgetHelper widgetHelper;
|
|
|
|
public static final String COMPLETE_TASK = "COMPLETE_TASK";
|
|
public static final String EDIT_TASK = "EDIT_TASK";
|
|
|
|
public static long suppressUpdateFlag = 0; // Timestamp--don't update widgets if this flag is non-zero and now() is within 5 minutes
|
|
private static final long SUPPRESS_TIME = DateUtilities.ONE_MINUTE * 5;
|
|
|
|
@Override
|
|
public void onReceive(Context context, Intent intent) {
|
|
super.onReceive(context, intent);
|
|
|
|
switch(intent.getAction()) {
|
|
case COMPLETE_TASK:
|
|
broadcaster.toggleCompletedState(intent.getLongExtra(TaskEditFragment.TOKEN_ID, 0));
|
|
break;
|
|
case EDIT_TASK:
|
|
getEditTaskStack(
|
|
context,
|
|
(Filter) intent.getParcelableExtra(TaskListFragment.TOKEN_FILTER),
|
|
intent.getLongExtra(TaskEditFragment.TOKEN_ID, 0))
|
|
.startActivities();
|
|
break;
|
|
case BROADCAST_EVENT_REFRESH:
|
|
updateWidgets(context);
|
|
break;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
|
|
try {
|
|
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
|
|
|
if (preIceCreamSandwich()) {
|
|
// Start in service to prevent Application Not Responding timeout
|
|
updateWidgets(context);
|
|
} else {
|
|
ComponentName thisWidget = new ComponentName(context, TasksWidget.class);
|
|
int[] ids = appWidgetManager.getAppWidgetIds(thisWidget);
|
|
for (int id : ids) {
|
|
appWidgetManager.updateAppWidget(id, widgetHelper.createScrollableWidget(context, id));
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
log.error(e.getMessage(), e);
|
|
}
|
|
}
|
|
|
|
public static void updateWidgets(Context context) {
|
|
if (suppressUpdateFlag > 0 && DateUtilities.now() - suppressUpdateFlag < SUPPRESS_TIME) {
|
|
return;
|
|
}
|
|
suppressUpdateFlag = 0;
|
|
|
|
if (preIceCreamSandwich()) {
|
|
context.startService(new Intent(context, WidgetUpdateService.class));
|
|
} else {
|
|
updateScrollableWidgets(context, null);
|
|
}
|
|
}
|
|
|
|
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
|
|
public static void updateScrollableWidgets(Context context, int[] widgetIds) {
|
|
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
|
if (widgetIds == null) {
|
|
widgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, TasksWidget.class));
|
|
}
|
|
appWidgetManager.notifyAppWidgetViewDataChanged(widgetIds, R.id.list_view);
|
|
}
|
|
}
|