Skip to content

Commit

Permalink
Improve task list selection when creating a new task from widget (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
korelstar authored and dmfs committed Jul 2, 2017
1 parent c53c2ec commit 7029940
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 9 deletions.
2 changes: 1 addition & 1 deletion opentasks/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
</intent-filter>
</activity>

<!-- EditTaskActivity listens for EDIT, INSERT and INDERT_OR_EDIT intents -->
<!-- EditTaskActivity listens for EDIT, INSERT and INSERT_OR_EDIT intents -->
<activity
android:name="org.dmfs.tasks.EditTaskActivity"
android:label="@string/activity_add_task_title"
Expand Down
4 changes: 2 additions & 2 deletions opentasks/src/main/java/org/dmfs/tasks/EditTaskActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ public class EditTaskActivity extends ActionBarActivity
{
private static final String ACTION_NOTE_TO_SELF = "com.google.android.gm.action.AUTO_SEND";

final static String EXTRA_DATA_BUNDLE = "org.dmfs.extra.BUNDLE";
public final static String EXTRA_DATA_BUNDLE = "org.dmfs.extra.BUNDLE";

final static String EXTRA_DATA_CONTENT_SET = "org.dmfs.DATA";
public final static String EXTRA_DATA_CONTENT_SET = "org.dmfs.DATA";

public final static String EXTRA_DATA_ACCOUNT_TYPE = "org.dmfs.ACCOUNT_TYPE";

Expand Down
6 changes: 6 additions & 0 deletions opentasks/src/main/java/org/dmfs/tasks/EditTaskFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.dmfs.tasks.model.TaskFieldAdapters;
import org.dmfs.tasks.utils.ContentValueMapper;
import org.dmfs.tasks.utils.OnModelLoadedListener;
import org.dmfs.tasks.utils.RecentlyUsedLists;
import org.dmfs.tasks.utils.TasksListCursorSpinnerAdapter;
import org.dmfs.tasks.widget.ListenableScrollView;
import org.dmfs.tasks.widget.ListenableScrollView.OnScrollListener;
Expand Down Expand Up @@ -772,6 +773,11 @@ public void saveAndExit()
mValues.ensureUpdates(RECURRENCE_VALUES);
}

if(mValues.isInsert()) {
// update recently used lists
RecentlyUsedLists.use(getContext(), mValues.getAsLong(Tasks.LIST_ID));
}

mTaskUri = mValues.persist(activity);

// return proper result
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.dmfs.provider.tasks.TaskContract.Tasks;
import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.model.TaskFieldAdapters;
import org.dmfs.tasks.utils.RecentlyUsedLists;
import org.dmfs.tasks.utils.TasksListCursorSpinnerAdapter;

import android.annotation.TargetApi;
Expand Down Expand Up @@ -362,6 +363,7 @@ private void editTask()
private void createTask()
{
ContentSet content = buildContentSet();
RecentlyUsedLists.use(getContext(), content.getAsLong(Tasks.LIST_ID)); // update recently used lists
content.persist(getActivity());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,27 @@
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.widget.RemoteViews;

import org.dmfs.provider.tasks.TaskContract;
import org.dmfs.provider.tasks.TaskContract.TaskLists;
import org.dmfs.provider.tasks.TaskContract.Tasks;
import org.dmfs.tasks.EditTaskActivity;
import org.dmfs.tasks.R;
import org.dmfs.tasks.TaskListActivity;
import org.dmfs.tasks.model.ContentSet;
import org.dmfs.tasks.utils.RecentlyUsedLists;
import org.dmfs.tasks.utils.WidgetConfigurationDatabaseHelper;

import java.util.ArrayList;


/**
* The provider for the widget on Android Honeycomb and up.
Expand All @@ -48,6 +58,7 @@
public class TaskListWidgetProvider extends AppWidgetProvider
{
private final static String TAG = "TaskListWidgetProvider";
public static String ACTION_CREATE_TASK = "CreateTask";

/*
* Override the onReceive method from the {@link BroadcastReceiver } class so that we can intercept broadcast for manual refresh of widget.
Expand All @@ -66,6 +77,51 @@ public void onReceive(Context context, Intent intent)
{
appWidgetManager.notifyAppWidgetViewDataChanged(appWidgetIds, R.id.task_list_widget_lv);
}
else if(action.equals(ACTION_CREATE_TASK))
{
int widgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, 0);
WidgetConfigurationDatabaseHelper configHelper = new WidgetConfigurationDatabaseHelper(context);
SQLiteDatabase db = configHelper.getReadableDatabase();
ArrayList<Long> widgetLists = WidgetConfigurationDatabaseHelper.loadTaskLists(db, widgetId);
db.close();
ArrayList<Long> writableLists = new ArrayList<>();
String authority = TaskContract.taskAuthority(context);
if(!widgetLists.isEmpty()) {
Cursor cursor = context.getContentResolver().query(
TaskLists.getContentUri(authority),
new String[]{TaskLists._ID},
TaskLists.SYNC_ENABLED + "=1 AND " + TaskLists._ID + " IN (" + TextUtils.join(",", widgetLists) + ")",
null,
null);
if (cursor != null) {
while (cursor.moveToNext()) {
writableLists.add(cursor.getLong(0));
}
cursor.close();
}
}
Intent editTaskIntent = new Intent(Intent.ACTION_INSERT);
editTaskIntent.setData(Tasks.getContentUri(authority));
editTaskIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if(!writableLists.isEmpty())
{
Long preselectList;
if(writableLists.size()==1) {
// if there is only one list, then select this one
preselectList = writableLists.get(0);
} else {
// if there are multiple lists, then select the most recently used
preselectList = RecentlyUsedLists.getRecentFromList(context, writableLists);
}
Log.d(getClass().getSimpleName(), "create task with preselected list "+preselectList);
ContentSet contentSet = new ContentSet(Tasks.getContentUri(authority));
contentSet.put(Tasks.LIST_ID, preselectList);
Bundle extraBundle = new Bundle();
extraBundle.putParcelable(EditTaskActivity.EXTRA_DATA_CONTENT_SET, contentSet);
editTaskIntent.putExtra(EditTaskActivity.EXTRA_DATA_BUNDLE, extraBundle);
}
context.startActivity(editTaskIntent);
}
}


Expand All @@ -83,8 +139,6 @@ protected ComponentName getComponentName(Context context)
@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
String authority = TaskContract.taskAuthority(context);

/*
* Iterate over all the widgets of this type and update them individually.
*/
Expand All @@ -106,10 +160,10 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
widget.setOnClickPendingIntent(android.R.id.button1, taskAppPI);

/** Add a pending Intent to start new Task Activity on the new Task Button */
Intent editTaskIntent = new Intent(Intent.ACTION_INSERT);
editTaskIntent.setData(Tasks.getContentUri(authority));
editTaskIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent newTaskPI = PendingIntent.getActivity(context, 0, editTaskIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent editTaskIntent = new Intent(context, TaskListWidgetProvider.class);
editTaskIntent.setAction(ACTION_CREATE_TASK);
editTaskIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);
PendingIntent newTaskPI = PendingIntent.getBroadcast(context, appWidgetIds[i], editTaskIntent, PendingIntent.FLAG_UPDATE_CURRENT);
widget.setOnClickPendingIntent(android.R.id.button2, newTaskPI);

/** Set the {@link RemoteViewsService } subclass as the adapter for the {@link ListView} in the widget. */
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package org.dmfs.tasks.utils;

import android.content.Context;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
* Helper class to record recently used lists in order to provide a pre-selection to the user.
*/
public class RecentlyUsedLists {
public static final String PREFERENCE_KEY = "RecentlyUsedLists";

/**
* Gets the lists of TaskLists ordered by recently use.
* @param context Context
* @return List of TaskLists where the most recently used list is on position 0.
*/
public static List<Long> getList(Context context) {
String strLists = PreferenceManager.getDefaultSharedPreferences(context).getString(PREFERENCE_KEY, "");
Log.v(RecentlyUsedLists.class.getSimpleName(), "getList: "+strLists);
List<Long> lists = new ArrayList<>();
if(strLists.length()>0) {
for (String lid : strLists.split(",")) {
lists.add(Long.parseLong(lid));
}
}
return lists;
}

/**
* Saves the ordered lists of TaskLists.
* @param context Context
* @param lists List of TaskLists where the most recently used list is on position 0.
*/
private static void setList(Context context, List<Long> lists) {
String strLists = TextUtils.join(",", lists);
Log.v(RecentlyUsedLists.class.getSimpleName(), "setList: "+strLists);
PreferenceManager.getDefaultSharedPreferences(context).edit().putString(PREFERENCE_KEY, strLists).commit();
}

/**
* Searches for the best suitable TaskList in dependence of which is most recently used.
* @param context Context
* @param allowedLists List of TaskLists
* @return The most recently used TaskLists from <code>allowedLists</code>
*/
public static Long getRecentFromList(Context context, List<Long> allowedLists) {
List<Long> recentlyLists = getList(context);
for (Long listId : recentlyLists) {
if(allowedLists.contains(listId)) {
return listId;
}
}
return allowedLists.get(0);
}

/**
* Mark a TaskLists as "used", which means that as new task was just created.
* @param context Context
* @param listId Id of the TaskList, where a task was just created.
*/
public static void use(Context context, Long listId) {
List<Long> lists = getList(context);
lists.remove(listId); // does nothing, if "listId" is not in "lists"
lists.add(0, listId);
setList(context, lists);
}
}

0 comments on commit 7029940

Please sign in to comment.