mirror of https://github.com/tasks/tasks
Remove WidgetHelper
parent
eb58a70269
commit
434df72403
@ -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);
|
||||
}
|
||||
}
|
@ -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…
Reference in New Issue