Remove WidgetHelper

pull/384/head
Alex Baker 9 years ago
parent eb58a70269
commit 434df72403

@ -77,6 +77,7 @@ import com.todoroo.astrid.timers.TimerPlugin;
import com.todoroo.astrid.voice.VoiceInputAssistant;
import com.todoroo.astrid.widget.TasksWidget;
import org.tasks.Broadcaster;
import org.tasks.R;
import org.tasks.activities.SortActivity;
import org.tasks.dialogs.DialogBuilder;
@ -145,6 +146,7 @@ public class TaskListFragment extends InjectingListFragment implements SwipeRefr
@Inject VoiceInputAssistant voiceInputAssistant;
@Inject TaskCreator taskCreator;
@Inject TagDataDao tagDataDao;
@Inject Broadcaster broadcaster;
@Bind(R.id.swipe_layout) SwipeRefreshLayout swipeRefreshLayout;
@Bind(R.id.swipe_layout_empty) SwipeRefreshLayout emptyView;
@ -352,13 +354,13 @@ public class TaskListFragment extends InjectingListFragment implements SwipeRefr
item.setChecked(!item.isChecked());
preferences.setBoolean(R.string.p_show_hidden_tasks, item.isChecked());
reconstructCursor();
TasksWidget.updateWidgets(getActivity());
broadcaster.refresh();
return true;
case R.id.menu_show_completed:
item.setChecked(!item.isChecked());
preferences.setBoolean(R.string.p_show_completed_tasks, item.isChecked());
reconstructCursor();
TasksWidget.updateWidgets(getActivity());
broadcaster.refresh();
return true;
case R.id.menu_filter_settings:
startActivityForResult(new Intent(getActivity(), FilterSettingsActivity.class) {{
@ -791,7 +793,7 @@ public class TaskListFragment extends InjectingListFragment implements SwipeRefr
}
} else if (requestCode == REQUEST_SORT) {
if (resultCode == Activity.RESULT_OK) {
TasksWidget.updateWidgets(getActivity());
broadcaster.refresh();
((TaskListActivity) getActivity()).onFilterItemClicked(filter);
}
} else {

@ -5,14 +5,10 @@
*/
package com.todoroo.astrid.service;
import android.content.Context;
import com.todoroo.astrid.gtasks.GtasksList;
import com.todoroo.astrid.gtasks.sync.GtasksSyncV2Provider;
import com.todoroo.astrid.sync.SyncResultCallback;
import org.tasks.injection.ForApplication;
import javax.inject.Inject;
import javax.inject.Singleton;
@ -32,11 +28,9 @@ public class SyncV2Service {
* for responding to sync requests through this new API.
*/
private final GtasksSyncV2Provider provider;
private final Context context;
@Inject
public SyncV2Service(@ForApplication Context context, GtasksSyncV2Provider gtasksSyncV2Provider) {
this.context = context;
public SyncV2Service(GtasksSyncV2Provider gtasksSyncV2Provider) {
provider = gtasksSyncV2Provider;
}
@ -52,7 +46,7 @@ public class SyncV2Service {
*/
public boolean synchronizeActiveTasks(SyncResultCallback callback) {
if (provider.isActive()) {
provider.synchronizeActiveTasks(new WidgetUpdatingCallbackWrapper(context, callback));
provider.synchronizeActiveTasks(callback);
return true;
}
return false;
@ -66,7 +60,7 @@ public class SyncV2Service {
*/
public void synchronizeList(GtasksList list, SyncResultCallback callback) {
if(provider.isActive()) {
provider.synchronizeList(list, new WidgetUpdatingCallbackWrapper(context, callback));
provider.synchronizeList(list, callback);
}
}
}

@ -1,31 +0,0 @@
package com.todoroo.astrid.service;
import android.content.Context;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.astrid.sync.SyncResultCallback;
import com.todoroo.astrid.widget.TasksWidget;
public class WidgetUpdatingCallbackWrapper implements SyncResultCallback {
private final Context context;
private SyncResultCallback wrap;
public WidgetUpdatingCallbackWrapper(Context context, SyncResultCallback wrap) {
this.context = context;
this.wrap = wrap;
}
@Override
public void started() {
wrap.started();
TasksWidget.suppressUpdateFlag = DateUtilities.now();
}
@Override
public void finished() {
wrap.finished();
TasksWidget.suppressUpdateFlag = 0L;
TasksWidget.updateWidgets(context);
}
}

@ -5,30 +5,48 @@
*/
package com.todoroo.astrid.widget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import com.todoroo.andlib.utility.DateUtilities;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.FilterWithCustomIntent;
import com.todoroo.astrid.core.BuiltInFilterExposer;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.tags.TagFilterExposer;
import org.tasks.Broadcaster;
import org.tasks.R;
import org.tasks.injection.InjectingAppWidgetProvider;
import org.tasks.widget.WidgetHelper;
import org.tasks.intents.TaskIntents;
import org.tasks.preferences.Preferences;
import org.tasks.widget.ScrollableWidgetUpdateService;
import javax.inject.Inject;
import timber.log.Timber;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
import static com.todoroo.astrid.api.AstridApiConstants.BROADCAST_EVENT_REFRESH;
import static org.tasks.intents.TaskIntents.getEditTaskStack;
public class TasksWidget extends InjectingAppWidgetProvider {
private static int flags = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP;
@Inject Broadcaster broadcaster;
@Inject WidgetHelper widgetHelper;
@Inject TagDataDao tagDataDao;
@Inject Preferences preferences;
public static final String COMPLETE_TASK = "COMPLETE_TASK";
public static final String EDIT_TASK = "EDIT_TASK";
@ -36,9 +54,6 @@ public class TasksWidget extends InjectingAppWidgetProvider {
public static final String EXTRA_FILTER = "extra_filter";
public static final String EXTRA_ID = "extra_id"; //$NON-NLS-1$
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);
@ -53,7 +68,9 @@ public class TasksWidget extends InjectingAppWidgetProvider {
getEditTaskStack(context, filter, taskId).startActivities();
break;
case BROADCAST_EVENT_REFRESH:
updateWidgets(context);
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
int[] widgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, TasksWidget.class));
appWidgetManager.notifyAppWidgetViewDataChanged(widgetIds, R.id.list_view);
break;
}
}
@ -66,27 +83,113 @@ public class TasksWidget extends InjectingAppWidgetProvider {
ComponentName thisWidget = new ComponentName(context, TasksWidget.class);
int[] ids = appWidgetManager.getAppWidgetIds(thisWidget);
for (int id : ids) {
appWidgetManager.updateAppWidget(id, widgetHelper.createScrollableWidget(context, id));
appWidgetManager.updateAppWidget(id, createScrollableWidget(context, id));
}
} catch (Exception e) {
Timber.e(e, e.getMessage());
}
}
public static void updateWidgets(Context context) {
if (suppressUpdateFlag > 0 && DateUtilities.now() - suppressUpdateFlag < SUPPRESS_TIME) {
return;
private RemoteViews createScrollableWidget(Context context, int id) {
Filter filter = getFilter(context, id);
Intent rvIntent = new Intent(context, ScrollableWidgetUpdateService.class);
Bundle filterBundle = new Bundle(com.todoroo.astrid.api.Filter.class.getClassLoader());
filterBundle.putParcelable(ScrollableWidgetUpdateService.FILTER, filter);
rvIntent.putExtra(ScrollableWidgetUpdateService.FILTER, filterBundle);
rvIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
rvIntent.setData(Uri.parse(rvIntent.toUri(Intent.URI_INTENT_SCHEME)));
boolean darkTheme = preferences.useDarkWidgetTheme(id);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), darkTheme ? R.layout.scrollable_widget_dark : R.layout.scrollable_widget_light);
if (preferences.getBoolean(WidgetConfigActivity.PREF_HIDE_HEADER + id, false)) {
remoteViews.setViewVisibility(R.id.widget_header, View.GONE);
}
if (preferences.getBoolean(WidgetConfigActivity.PREF_WIDGET_TRANSPARENT + id, false)) {
remoteViews.setInt(R.id.widget_header, "setBackgroundColor", android.R.color.transparent);
remoteViews.setInt(R.id.list_view, "setBackgroundColor", android.R.color.transparent);
remoteViews.setInt(R.id.empty_view, "setBackgroundColor", android.R.color.transparent);
}
suppressUpdateFlag = 0;
remoteViews.setTextViewText(R.id.widget_title, filter.listingTitle);
remoteViews.setRemoteAdapter(R.id.list_view, rvIntent);
remoteViews.setEmptyView(R.id.list_view, R.id.empty_view);
remoteViews.setOnClickPendingIntent(R.id.widget_title, getOpenListIntent(context, filter, id));
remoteViews.setOnClickPendingIntent(R.id.widget_button, getNewTaskIntent(context, filter, id));
remoteViews.setPendingIntentTemplate(R.id.list_view, getPendingIntentTemplate(context));
return remoteViews;
}
updateScrollableWidgets(context, null);
private PendingIntent getPendingIntentTemplate(Context context) {
Intent intent = new Intent(context, TasksWidget.class);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void updateScrollableWidgets(Context context, int[] widgetIds) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
if (widgetIds == null) {
widgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, TasksWidget.class));
private PendingIntent getOpenListIntent(Context context, Filter filter, int widgetId) {
Intent intent = TaskIntents.getTaskListIntent(context, filter);
intent.setFlags(flags);
return PendingIntent.getActivity(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private PendingIntent getNewTaskIntent(Context context, Filter filter, int widgetId) {
Intent intent = TaskIntents.getNewTaskIntent(context, filter);
intent.setFlags(flags);
return PendingIntent.getActivity(context, -widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
private Filter getFilter(Context context, int widgetId) {
// base our filter off the inbox filter, replace stuff if we have it
Filter filter = BuiltInFilterExposer.getMyTasksFilter(context.getResources());
String sql = preferences.getStringValue(WidgetConfigActivity.PREF_SQL + widgetId);
if (sql != null) {
sql = sql.replace("tasks.userId=0", "1"); // TODO: replace dirty hack for missing column
filter.setSqlQuery(sql);
}
String title = preferences.getStringValue(WidgetConfigActivity.PREF_TITLE + widgetId);
if (title != null) {
filter.listingTitle = title;
}
appWidgetManager.notifyAppWidgetViewDataChanged(widgetIds, R.id.list_view);
String contentValues = preferences.getStringValue(WidgetConfigActivity.PREF_VALUES + widgetId);
if (contentValues != null) {
filter.valuesForNewTasks = AndroidUtilities.contentValuesFromSerializedString(contentValues);
}
String customComponent = preferences.getStringValue(WidgetConfigActivity.PREF_CUSTOM_INTENT
+ widgetId);
if (customComponent != null) {
ComponentName component = ComponentName.unflattenFromString(customComponent);
filter = new FilterWithCustomIntent(filter.listingTitle, filter.getSqlQuery(), filter.valuesForNewTasks);
((FilterWithCustomIntent) filter).customTaskList = component;
String serializedExtras = preferences.getStringValue(WidgetConfigActivity.PREF_CUSTOM_EXTRAS
+ widgetId);
((FilterWithCustomIntent) filter).customExtras = AndroidUtilities.bundleFromSerializedString(serializedExtras);
}
// Validate tagData
long id = preferences.getLong(WidgetConfigActivity.PREF_TAG_ID + widgetId, 0);
TagData tagData;
if (id > 0) {
tagData = tagDataDao.fetch(id, TagData.ID, TagData.NAME, TagData.UUID);
if (tagData != null && !tagData.getName().equals(filter.listingTitle)) { // Tag has been renamed; rebuild filter
filter = TagFilterExposer.filterFromTagData(context, tagData);
preferences.setString(WidgetConfigActivity.PREF_SQL + widgetId, filter.getSqlQuery());
preferences.setString(WidgetConfigActivity.PREF_TITLE + widgetId, filter.listingTitle);
ContentValues newTaskValues = filter.valuesForNewTasks;
String contentValuesString = null;
if (newTaskValues != null) {
contentValuesString = AndroidUtilities.contentValuesToSerializedString(newTaskValues);
}
preferences.setString(WidgetConfigActivity.PREF_VALUES + widgetId, contentValuesString);
String flattenedExtras = AndroidUtilities.bundleToSerializedString(((FilterWithCustomIntent) filter).customExtras);
if (flattenedExtras != null) {
preferences.setString(WidgetConfigActivity.PREF_CUSTOM_EXTRAS + widgetId,
flattenedExtras);
}
}
} else {
tagData = tagDataDao.getTagByName(filter.listingTitle, TagData.ID);
if (tagData != null) {
preferences.setLong(WidgetConfigActivity.PREF_TAG_ID + widgetId, tagData.getId());
}
}
return filter;
}
}

@ -26,7 +26,6 @@ import org.tasks.filters.FilterCounter;
import org.tasks.filters.FilterProvider;
import org.tasks.injection.InjectingListActivity;
import org.tasks.preferences.ActivityPreferences;
import org.tasks.widget.WidgetHelper;
import javax.inject.Inject;
@ -48,17 +47,10 @@ public class WidgetConfigActivity extends InjectingListActivity {
FilterAdapter adapter = null;
@Inject WidgetHelper widgetHelper;
@Inject FilterCounter filterCounter;
@Inject ActivityPreferences preferences;
@Inject FilterProvider filterProvider;
private void updateWidget() {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this);
appWidgetManager.updateAppWidget(mAppWidgetId, widgetHelper.createScrollableWidget(this, mAppWidgetId));
TasksWidget.updateScrollableWidgets(this, new int[]{mAppWidgetId});
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
@ -99,8 +91,6 @@ public class WidgetConfigActivity extends InjectingListActivity {
darkTheme.isChecked(), hideCheckboxes.isChecked(), hideHeader.isChecked(),
transparent.isChecked());
updateWidget();
// Make sure we pass back the original appWidgetId
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);

@ -1,10 +1,7 @@
package org.tasks.injection;
import android.annotation.TargetApi;
import android.os.Build;
import android.widget.RemoteViewsService;
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public abstract class InjectingRemoteViewsService extends RemoteViewsService {
@Override
public void onCreate() {

@ -51,10 +51,6 @@ public class AlarmManager {
}
}
public void setInexactRepeating(long interval, PendingIntent pendingIntent) {
alarmManager.setInexactRepeating(android.app.AlarmManager.RTC, 0, interval, pendingIntent);
}
long adjustForQuietHours(long time) {
if (preferences.quietHoursEnabled()) {
DateTime dateTime = new DateTime(time);

@ -1,159 +0,0 @@
package org.tasks.widget;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import com.todoroo.andlib.utility.AndroidUtilities;
import com.todoroo.astrid.api.Filter;
import com.todoroo.astrid.api.FilterWithCustomIntent;
import com.todoroo.astrid.core.BuiltInFilterExposer;
import com.todoroo.astrid.dao.TagDataDao;
import com.todoroo.astrid.data.TagData;
import com.todoroo.astrid.tags.TagFilterExposer;
import com.todoroo.astrid.widget.TasksWidget;
import com.todoroo.astrid.widget.WidgetConfigActivity;
import org.tasks.R;
import org.tasks.intents.TaskIntents;
import org.tasks.preferences.Preferences;
import org.tasks.scheduling.AlarmManager;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
import javax.inject.Singleton;
import static android.content.Intent.FLAG_ACTIVITY_CLEAR_TOP;
import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK;
@Singleton
public class WidgetHelper {
public static int flags = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP;
private final TagDataDao tagDataDao;
private final Preferences preferences;
private AlarmManager alarmManager;
@Inject
public WidgetHelper(TagDataDao tagDataDao, Preferences preferences, AlarmManager alarmManager) {
this.tagDataDao = tagDataDao;
this.preferences = preferences;
this.alarmManager = alarmManager;
}
public RemoteViews createScrollableWidget(Context context, int id) {
Intent intent = new Intent(context, ScrollableWidgetUpdateService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
alarmManager.setInexactRepeating(TimeUnit.MINUTES.toMillis(30), pendingIntent);
Filter filter = getFilter(context, id);
Intent rvIntent = new Intent(context, ScrollableWidgetUpdateService.class);
Bundle filterBundle = new Bundle(com.todoroo.astrid.api.Filter.class.getClassLoader());
filterBundle.putParcelable(ScrollableWidgetUpdateService.FILTER, filter);
rvIntent.putExtra(ScrollableWidgetUpdateService.FILTER, filterBundle);
rvIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, id);
rvIntent.setData(Uri.parse(rvIntent.toUri(Intent.URI_INTENT_SCHEME)));
boolean darkTheme = preferences.useDarkWidgetTheme(id);
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), darkTheme ? R.layout.scrollable_widget_dark : R.layout.scrollable_widget_light);
if (preferences.getBoolean(WidgetConfigActivity.PREF_HIDE_HEADER + id, false)) {
remoteViews.setViewVisibility(R.id.widget_header, View.GONE);
}
if (preferences.getBoolean(WidgetConfigActivity.PREF_WIDGET_TRANSPARENT + id, false)) {
remoteViews.setInt(R.id.widget_header, "setBackgroundColor", android.R.color.transparent);
remoteViews.setInt(R.id.list_view, "setBackgroundColor", android.R.color.transparent);
remoteViews.setInt(R.id.empty_view, "setBackgroundColor", android.R.color.transparent);
}
remoteViews.setTextViewText(R.id.widget_title, filter.listingTitle);
remoteViews.setRemoteAdapter(R.id.list_view, rvIntent);
remoteViews.setEmptyView(R.id.list_view, R.id.empty_view);
remoteViews.setOnClickPendingIntent(R.id.widget_title, getOpenListIntent(context, filter, id));
remoteViews.setOnClickPendingIntent(R.id.widget_button, getNewTaskIntent(context, filter, id));
remoteViews.setPendingIntentTemplate(R.id.list_view, getPendingIntentTemplate(context));
return remoteViews;
}
private PendingIntent getPendingIntentTemplate(Context context) {
Intent intent = new Intent(context, TasksWidget.class);
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public PendingIntent getOpenListIntent(Context context, Filter filter, int widgetId) {
Intent intent = TaskIntents.getTaskListIntent(context, filter);
intent.setFlags(flags);
return PendingIntent.getActivity(context, widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public PendingIntent getNewTaskIntent(Context context, Filter filter, int widgetId) {
Intent intent = TaskIntents.getNewTaskIntent(context, filter);
intent.setFlags(flags);
return PendingIntent.getActivity(context, -widgetId, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public Filter getFilter(Context context, int widgetId) {
// base our filter off the inbox filter, replace stuff if we have it
Filter filter = BuiltInFilterExposer.getMyTasksFilter(context.getResources());
String sql = preferences.getStringValue(WidgetConfigActivity.PREF_SQL + widgetId);
if (sql != null) {
sql = sql.replace("tasks.userId=0", "1"); // TODO: replace dirty hack for missing column
filter.setSqlQuery(sql);
}
String title = preferences.getStringValue(WidgetConfigActivity.PREF_TITLE + widgetId);
if (title != null) {
filter.listingTitle = title;
}
String contentValues = preferences.getStringValue(WidgetConfigActivity.PREF_VALUES + widgetId);
if (contentValues != null) {
filter.valuesForNewTasks = AndroidUtilities.contentValuesFromSerializedString(contentValues);
}
String customComponent = preferences.getStringValue(WidgetConfigActivity.PREF_CUSTOM_INTENT
+ widgetId);
if (customComponent != null) {
ComponentName component = ComponentName.unflattenFromString(customComponent);
filter = new FilterWithCustomIntent(filter.listingTitle, filter.getSqlQuery(), filter.valuesForNewTasks);
((FilterWithCustomIntent) filter).customTaskList = component;
String serializedExtras = preferences.getStringValue(WidgetConfigActivity.PREF_CUSTOM_EXTRAS
+ widgetId);
((FilterWithCustomIntent) filter).customExtras = AndroidUtilities.bundleFromSerializedString(serializedExtras);
}
// Validate tagData
long id = preferences.getLong(WidgetConfigActivity.PREF_TAG_ID + widgetId, 0);
TagData tagData;
if (id > 0) {
tagData = tagDataDao.fetch(id, TagData.ID, TagData.NAME, TagData.UUID);
if (tagData != null && !tagData.getName().equals(filter.listingTitle)) { // Tag has been renamed; rebuild filter
filter = TagFilterExposer.filterFromTagData(context, tagData);
preferences.setString(WidgetConfigActivity.PREF_SQL + widgetId, filter.getSqlQuery());
preferences.setString(WidgetConfigActivity.PREF_TITLE + widgetId, filter.listingTitle);
ContentValues newTaskValues = filter.valuesForNewTasks;
String contentValuesString = null;
if (newTaskValues != null) {
contentValuesString = AndroidUtilities.contentValuesToSerializedString(newTaskValues);
}
preferences.setString(WidgetConfigActivity.PREF_VALUES + widgetId, contentValuesString);
String flattenedExtras = AndroidUtilities.bundleToSerializedString(((FilterWithCustomIntent) filter).customExtras);
if (flattenedExtras != null) {
preferences.setString(WidgetConfigActivity.PREF_CUSTOM_EXTRAS + widgetId,
flattenedExtras);
}
}
} else {
tagData = tagDataDao.getTagByName(filter.listingTitle, TagData.ID);
if (tagData != null) {
preferences.setLong(WidgetConfigActivity.PREF_TAG_ID + widgetId, tagData.getId());
}
}
return filter;
}
}
Loading…
Cancel
Save