From 32b865d86fb86f60aba9bb3b2c94f829232a4ee6 Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Wed, 21 Sep 2011 19:41:39 -0700 Subject: [PATCH] Fixed bug with tag widgets not launching tag view activity --- .../andlib/utility/AndroidUtilities.java | 119 ++++++++++++++---- astrid/AndroidManifest.xml | 7 +- .../todoroo/astrid/actfm/TagViewActivity.java | 4 + .../astrid/activity/TaskListActivity.java | 9 ++ .../todoroo/astrid/widget/TasksWidget.java | 22 ++-- .../astrid/widget/WidgetConfigActivity.java | 13 +- 6 files changed, 138 insertions(+), 36 deletions(-) diff --git a/api/src/com/todoroo/andlib/utility/AndroidUtilities.java b/api/src/com/todoroo/andlib/utility/AndroidUtilities.java index 05737ce2a..439588492 100644 --- a/api/src/com/todoroo/andlib/utility/AndroidUtilities.java +++ b/api/src/com/todoroo/andlib/utility/AndroidUtilities.java @@ -31,6 +31,7 @@ import android.graphics.BitmapFactory; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.NetworkInfo.State; +import android.os.Bundle; import android.text.InputType; import android.util.Log; import android.view.MotionEvent; @@ -160,6 +161,8 @@ public class AndroidUtilities { value.getClass()); } + // --- serialization + /** * Rips apart a content value into two string arrays, keys and value */ @@ -192,20 +195,36 @@ public class AndroidUtilities { public static String contentValuesToSerializedString(ContentValues source) { StringBuilder result = new StringBuilder(); for(Entry entry : source.valueSet()) { - result.append(entry.getKey().replace(SERIALIZATION_SEPARATOR, SEPARATOR_ESCAPE)).append( - SERIALIZATION_SEPARATOR); - Object value = entry.getValue(); - if(value instanceof Integer) - result.append('i').append(value); - else if(value instanceof Double) - result.append('d').append(value); - else if(value instanceof Long) - result.append('l').append(value); - else if(value instanceof String) - result.append('s').append(value.toString()); - else - throw new UnsupportedOperationException(value.getClass().toString()); - result.append(SERIALIZATION_SEPARATOR); + addSerialized(result, entry.getKey(), entry.getValue()); + } + return result.toString(); + } + + /** add serialized helper */ + private static void addSerialized(StringBuilder result, + String key, Object value) { + result.append(key.replace(SERIALIZATION_SEPARATOR, SEPARATOR_ESCAPE)).append( + SERIALIZATION_SEPARATOR); + if(value instanceof Integer) + result.append('i').append(value); + else if(value instanceof Double) + result.append('d').append(value); + else if(value instanceof Long) + result.append('l').append(value); + else if(value instanceof String) + result.append('s').append(value.toString().replace(SERIALIZATION_SEPARATOR, SEPARATOR_ESCAPE)); + else + throw new UnsupportedOperationException(value.getClass().toString()); + result.append(SERIALIZATION_SEPARATOR); + } + + /** + * Serializes a {@link android.os.Bundle} into a string + */ + public static String bundleToSerializedString(Bundle source) { + StringBuilder result = new StringBuilder(); + for(String key : source.keySet()) { + addSerialized(result, key, source.get(key)); } return result.toString(); } @@ -219,34 +238,80 @@ public class AndroidUtilities { if(string == null) return new ContentValues(); - String[] pairs = string.split("\\" + SERIALIZATION_SEPARATOR); //$NON-NLS-1$ ContentValues result = new ContentValues(); - for(int i = 0; i < pairs.length; i += 2) { - String key = pairs[i].replaceAll(SEPARATOR_ESCAPE, SERIALIZATION_SEPARATOR); - String value = pairs[i+1].substring(1); - try { - switch(pairs[i+1].charAt(0)) { + fromSerialized(string, result, new SerializedPut() { + public void put(ContentValues object, String key, char type, String value) throws NumberFormatException { + switch(type) { case 'i': - result.put(key, Integer.parseInt(value)); + object.put(key, Integer.parseInt(value)); break; case 'd': - result.put(key, Double.parseDouble(value)); + object.put(key, Double.parseDouble(value)); break; case 'l': - result.put(key, Long.parseLong(value)); + object.put(key, Long.parseLong(value)); break; case 's': - result.put(key, value.replace(SEPARATOR_ESCAPE, SERIALIZATION_SEPARATOR)); + object.put(key, value.replace(SEPARATOR_ESCAPE, SERIALIZATION_SEPARATOR)); break; } + } + }); + return result; + } + + /** + * Turn {@link android.os.Bundle} into a string + * @param string + * @return + */ + public static Bundle bundleFromSerializedString(String string) { + if(string == null) + return new Bundle(); + + Bundle result = new Bundle(); + fromSerialized(string, result, new SerializedPut() { + public void put(Bundle object, String key, char type, String value) throws NumberFormatException { + switch(type) { + case 'i': + object.putInt(key, Integer.parseInt(value)); + break; + case 'd': + object.putDouble(key, Double.parseDouble(value)); + break; + case 'l': + object.putLong(key, Long.parseLong(value)); + break; + case 's': + object.putString(key, value.replace(SEPARATOR_ESCAPE, SERIALIZATION_SEPARATOR)); + break; + } + } + }); + return result; + } + + public interface SerializedPut { + public void put(T object, String key, char type, String value) throws NumberFormatException; + } + + private static void fromSerialized(String string, T object, SerializedPut putter) { + String[] pairs = string.split("\\" + SERIALIZATION_SEPARATOR); //$NON-NLS-1$ + for(int i = 0; i < pairs.length; i += 2) { + String key = pairs[i].replaceAll(SEPARATOR_ESCAPE, SERIALIZATION_SEPARATOR); + String value = pairs[i+1].substring(1); + try { + putter.put(object, key, pairs[i+1].charAt(0), value); } catch (NumberFormatException e) { - // failed parse to number, try to put a string - result.put(key, value); + // failed parse to number + putter.put(object, key, 's', value); } } - return result; } + + + /** * Turn ContentValues into a string * @param string diff --git a/astrid/AndroidManifest.xml b/astrid/AndroidManifest.xml index dbad24791..e9065c73f 100644 --- a/astrid/AndroidManifest.xml +++ b/astrid/AndroidManifest.xml @@ -315,7 +315,12 @@ + android:theme="@style/Theme"> + + + + + diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/TagViewActivity.java b/astrid/plugin-src/com/todoroo/astrid/actfm/TagViewActivity.java index 42401faa2..29916144f 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/TagViewActivity.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/TagViewActivity.java @@ -168,6 +168,10 @@ public class TagViewActivity extends TaskListActivity implements OnTabChangeList if(savedInstanceState != null && savedInstanceState.containsKey(TAB_IN_PROGRESS)) { tabHost.setCurrentTab(savedInstanceState.getInt(TAB_IN_PROGRESS)); } + + System.err.println("hey my intent is " + getIntent().getExtras()); + + onNewIntent(getIntent()); } /* (non-Javadoc) diff --git a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java index 9e6d74446..a4e76192e 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskListActivity.java @@ -30,6 +30,7 @@ import android.graphics.PixelFormat; import android.net.Uri; import android.os.Bundle; import android.text.TextUtils; +import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.KeyEvent; @@ -224,12 +225,17 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, parent.addView(getListBody(parent), 1); setContentView(parent); + System.err.println("is the database null? " + database + " and " + taskService); + if(database == null) return; database.openForWriting(); + System.err.println("i'm here, son!"); setUpUiComponents(); + System.err.println("i'm here, son! # 2"); onNewIntent(getIntent()); + System.err.println("i'm here, son! # 3"); if(Preferences.getInt(AstridPreferences.P_UPGRADE_FROM, -1) > -1) upgradeService.showChangeLog(this, Preferences.getInt(AstridPreferences.P_UPGRADE_FROM, -1)); @@ -264,6 +270,8 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, protected void onNewIntent(Intent intent) { super.onNewIntent(intent); + Log.w("funky", "new intent - ", new Throwable()); + Bundle extras = intent.getExtras(); String intentAction = intent.getAction(); if (Intent.ACTION_SEARCH.equals(intentAction)) { @@ -279,6 +287,7 @@ public class TaskListActivity extends ListActivity implements OnScrollListener, return; } else if(extras != null && extras.containsKey(TOKEN_FILTER)) { filter = extras.getParcelable(TOKEN_FILTER); + System.err.println("/FILTER: " + filter.sqlQuery); isInbox = true; } else { filter = CoreFilterExposer.buildInboxFilter(getResources()); diff --git a/astrid/src/com/todoroo/astrid/widget/TasksWidget.java b/astrid/src/com/todoroo/astrid/widget/TasksWidget.java index 61720f09f..cf112bbed 100644 --- a/astrid/src/com/todoroo/astrid/widget/TasksWidget.java +++ b/astrid/src/com/todoroo/astrid/widget/TasksWidget.java @@ -9,6 +9,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Color; +import android.os.Bundle; import android.os.IBinder; import android.util.DisplayMetrics; import android.util.Log; @@ -204,14 +205,21 @@ public class TasksWidget extends AppWidgetProvider { updateForScreenSize(views); Intent listIntent = new Intent(context, TaskListActivity.class); - listIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); - listIntent.putExtra(TaskListActivity.TOKEN_SOURCE, Constants.SOURCE_WIDGET); - if(filter != null) { - listIntent.putExtra(TaskListActivity.TOKEN_FILTER, filter); - listIntent.setType(filter.sqlQuery); + String customIntent = Preferences.getStringValue(WidgetConfigActivity.PREF_CUSTOM_INTENT + + widgetId); + if(customIntent != null) { + listIntent.setComponent(ComponentName.unflattenFromString(customIntent)); + String serializedExtras = Preferences.getStringValue(WidgetConfigActivity.PREF_CUSTOM_EXTRAS + + widgetId); + Bundle extras = AndroidUtilities.bundleFromSerializedString(serializedExtras); + listIntent.putExtras(extras); } - PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, - listIntent, 0); + listIntent.putExtra(TaskListActivity.TOKEN_SOURCE, Constants.SOURCE_WIDGET); + listIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK); + listIntent.putExtra(TaskListActivity.TOKEN_FILTER, filter); + listIntent.setAction("L" + widgetId + filter.sqlQuery); + PendingIntent pendingIntent = PendingIntent.getActivity(context, widgetId, + listIntent, PendingIntent.FLAG_CANCEL_CURRENT); views.setOnClickPendingIntent(R.id.taskbody, pendingIntent); Intent editIntent = new Intent(context, TaskEditActivity.class); diff --git a/astrid/src/com/todoroo/astrid/widget/WidgetConfigActivity.java b/astrid/src/com/todoroo/astrid/widget/WidgetConfigActivity.java index 4170fa435..ef176e48a 100644 --- a/astrid/src/com/todoroo/astrid/widget/WidgetConfigActivity.java +++ b/astrid/src/com/todoroo/astrid/widget/WidgetConfigActivity.java @@ -17,6 +17,7 @@ import com.todoroo.astrid.adapter.FilterAdapter; import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.api.FilterCategory; import com.todoroo.astrid.api.FilterListItem; +import com.todoroo.astrid.api.FilterWithCustomIntent; import com.todoroo.astrid.service.StatisticsConstants; import com.todoroo.astrid.service.StatisticsService; @@ -26,7 +27,8 @@ abstract public class WidgetConfigActivity extends ExpandableListActivity { static final String PREF_TITLE = "widget-title-"; static final String PREF_SQL = "widget-sql-"; static final String PREF_VALUES = "widget-values-"; - + static final String PREF_CUSTOM_INTENT = "widget-intent-"; + static final String PREF_CUSTOM_EXTRAS = "widget-extras-"; int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; @@ -161,6 +163,15 @@ abstract public class WidgetConfigActivity extends ExpandableListActivity { Preferences.setString(WidgetConfigActivity.PREF_TITLE + mAppWidgetId, title); Preferences.setString(WidgetConfigActivity.PREF_SQL + mAppWidgetId, sql); Preferences.setString(WidgetConfigActivity.PREF_VALUES + mAppWidgetId, contentValuesString); + + if(filterListItem instanceof FilterWithCustomIntent) { + String flattenedName = ((FilterWithCustomIntent)filterListItem).customTaskList.flattenToString(); + Preferences.setString(WidgetConfigActivity.PREF_CUSTOM_INTENT + mAppWidgetId, + flattenedName); + String flattenedExtras = AndroidUtilities.bundleToSerializedString(((FilterWithCustomIntent)filterListItem).customExtras); + Preferences.setString(WidgetConfigActivity.PREF_CUSTOM_EXTRAS + mAppWidgetId, + flattenedExtras); + } } }