From 39fea551b67238cf764ff394a24dbae49a729221 Mon Sep 17 00:00:00 2001 From: Alex Baker Date: Wed, 16 Mar 2016 17:31:24 -0500 Subject: [PATCH] Prompt user to enable Tasker integration --- src/googleplay/AndroidManifest.xml | 2 + .../tasks/injection/BroadcastComponent.java | 3 + .../locale/bundle/PluginBundleValues.java | 35 ++-------- .../tasks/locale/receiver/FireReceiver.java | 41 ++++++------ .../ui/activity/TaskerSettingsActivity.java | 66 ++++++++++++------- src/main/java/org/tasks/Notifier.java | 10 ++- .../preferences/DefaultFilterProvider.java | 37 ++++++----- src/main/res/values/strings.xml | 1 + 8 files changed, 108 insertions(+), 87 deletions(-) diff --git a/src/googleplay/AndroidManifest.xml b/src/googleplay/AndroidManifest.xml index d776f7e53..aa8ba8163 100644 --- a/src/googleplay/AndroidManifest.xml +++ b/src/googleplay/AndroidManifest.xml @@ -105,6 +105,7 @@ diff --git a/src/googleplay/java/org/tasks/injection/BroadcastComponent.java b/src/googleplay/java/org/tasks/injection/BroadcastComponent.java index dc62b365d..01447ec49 100644 --- a/src/googleplay/java/org/tasks/injection/BroadcastComponent.java +++ b/src/googleplay/java/org/tasks/injection/BroadcastComponent.java @@ -1,5 +1,6 @@ package org.tasks.injection; +import org.tasks.locale.receiver.FireReceiver; import org.tasks.receivers.TeslaUnreadReceiver; import dagger.Subcomponent; @@ -7,4 +8,6 @@ import dagger.Subcomponent; @Subcomponent(modules = BroadcastModule.class) public interface BroadcastComponent extends BaseBroadcastComponent { void inject(TeslaUnreadReceiver teslaUnreadReceiver); + + void inject(FireReceiver fireReceiver); } diff --git a/src/googleplay/java/org/tasks/locale/bundle/PluginBundleValues.java b/src/googleplay/java/org/tasks/locale/bundle/PluginBundleValues.java index 4eeb8f150..8393a199a 100755 --- a/src/googleplay/java/org/tasks/locale/bundle/PluginBundleValues.java +++ b/src/googleplay/java/org/tasks/locale/bundle/PluginBundleValues.java @@ -8,9 +8,7 @@ import timber.log.Timber; public final class PluginBundleValues { - public static final String BUNDLE_EXTRA_STRING_TITLE = "org.tasks.locale.STRING_TITLE"; - public static final String BUNDLE_EXTRA_STRING_QUERY = "org.tasks.locale.STRING_QUERY"; - public static final String BUNDLE_EXTRA_STRING_VALUES = "org.tasks.locale.STRING_VALUES"; + public static final String BUNDLE_EXTRA_STRING_FILTER = "org.tasks.locale.STRING_FILTER"; public static final String BUNDLE_EXTRA_PREVIOUS_BUNDLE = "org.tasks.locale.PREVIOUS_BUNDLE"; public static final String BUNDLE_EXTRA_INT_VERSION_CODE = "org.tasks.locale.INT_VERSION_CODE"; @@ -20,16 +18,7 @@ public final class PluginBundleValues { return false; } - if (isNullOrEmpty(bundle, BUNDLE_EXTRA_STRING_TITLE)) { - return false; - } - - if (isNullOrEmpty(bundle, BUNDLE_EXTRA_STRING_QUERY)) { - return false; - } - - if (bundle.containsKey(BUNDLE_EXTRA_STRING_VALUES) && bundle.getString(BUNDLE_EXTRA_STRING_VALUES).trim().length() == 0) { - Timber.e("Empty %s", BUNDLE_EXTRA_STRING_VALUES); + if (isNullOrEmpty(bundle, BUNDLE_EXTRA_STRING_FILTER)) { return false; } @@ -51,27 +40,15 @@ public final class PluginBundleValues { return isNullOrEmpty; } - public static Bundle generateBundle(String title, String query, String values) { + public static Bundle generateBundle(String filter) { Bundle result = new Bundle(); result.putInt(BUNDLE_EXTRA_INT_VERSION_CODE, BuildConfig.VERSION_CODE); - result.putString(BUNDLE_EXTRA_STRING_TITLE, title); - result.putString(BUNDLE_EXTRA_STRING_QUERY, query); - if (values != null) { - result.putString(BUNDLE_EXTRA_STRING_VALUES, values); - } + result.putString(BUNDLE_EXTRA_STRING_FILTER, filter); return result; } - public static String getTitle(Bundle bundle) { - return bundle.getString(BUNDLE_EXTRA_STRING_TITLE); - } - - public static String getQuery(Bundle bundle) { - return bundle.getString(BUNDLE_EXTRA_STRING_QUERY); - } - - public static String getValuesForNewTasks(Bundle bundle) { - return bundle.getString(BUNDLE_EXTRA_STRING_VALUES); + public static String getFilter(Bundle bundle) { + return bundle.getString(BUNDLE_EXTRA_STRING_FILTER); } private PluginBundleValues() { diff --git a/src/googleplay/java/org/tasks/locale/receiver/FireReceiver.java b/src/googleplay/java/org/tasks/locale/receiver/FireReceiver.java index 2d0d86965..ae12e9fc8 100755 --- a/src/googleplay/java/org/tasks/locale/receiver/FireReceiver.java +++ b/src/googleplay/java/org/tasks/locale/receiver/FireReceiver.java @@ -1,35 +1,31 @@ package org.tasks.locale.receiver; -import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Bundle; +import com.todoroo.astrid.api.Filter; + +import org.tasks.Notifier; +import org.tasks.injection.BroadcastComponent; +import org.tasks.injection.InjectingBroadcastReceiver; import org.tasks.locale.bundle.PluginBundleValues; +import org.tasks.preferences.DefaultFilterProvider; + +import javax.inject.Inject; import timber.log.Timber; -public final class FireReceiver extends BroadcastReceiver { +public final class FireReceiver extends InjectingBroadcastReceiver { - protected boolean isBundleValid(final Bundle bundle) { - return PluginBundleValues.isBundleValid(bundle); - } - - protected void firePluginSetting(final Context context, final Bundle bundle) { - context.sendBroadcast(new Intent() {{ - setComponent(new ComponentName("org.tasks", "org.tasks.receivers.ListNotificationReceiver")); - putExtra("extra_filter_title", PluginBundleValues.getTitle(bundle)); - putExtra("extra_filter_query", PluginBundleValues.getQuery(bundle)); - String valuesForNewTasks = PluginBundleValues.getValuesForNewTasks(bundle); - if (valuesForNewTasks != null) { - putExtra("extra_filter_values", valuesForNewTasks); - } - }}); - } + @Inject Notifier notifier; + @Inject DefaultFilterProvider defaultFilterProvider; @Override public final void onReceive(final Context context, final Intent intent) { + super.onReceive(context, intent); + Timber.d("Received %s", intent); //$NON-NLS-1$ /* @@ -67,12 +63,19 @@ public final class FireReceiver extends BroadcastReceiver { return; } - if (!isBundleValid(bundle)) { + if (!PluginBundleValues.isBundleValid(bundle)) { Timber.e("%s is invalid", com.twofortyfouram.locale.api.Intent.EXTRA_BUNDLE); //$NON-NLS-1$ return; } - firePluginSetting(context, bundle); + Filter filter = defaultFilterProvider.getFilterFromPreference( + bundle.getString(PluginBundleValues.BUNDLE_EXTRA_STRING_FILTER)); + notifier.triggerFilterNotification(filter); + } + + @Override + protected void inject(BroadcastComponent component) { + component.inject(this); } } diff --git a/src/googleplay/java/org/tasks/locale/ui/activity/TaskerSettingsActivity.java b/src/googleplay/java/org/tasks/locale/ui/activity/TaskerSettingsActivity.java index 03281facf..c0f876911 100755 --- a/src/googleplay/java/org/tasks/locale/ui/activity/TaskerSettingsActivity.java +++ b/src/googleplay/java/org/tasks/locale/ui/activity/TaskerSettingsActivity.java @@ -12,11 +12,16 @@ import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; +import com.todoroo.astrid.api.Filter; + import org.tasks.R; import org.tasks.activities.FilterSelectionActivity; +import org.tasks.dialogs.DialogBuilder; import org.tasks.injection.ActivityComponent; import org.tasks.locale.bundle.PluginBundleValues; import org.tasks.preferences.ActivityPreferences; +import org.tasks.preferences.BasicPreferences; +import org.tasks.preferences.DefaultFilterProvider; import org.tasks.ui.MenuColorizer; import java.util.Set; @@ -30,15 +35,16 @@ import butterknife.OnClick; public final class TaskerSettingsActivity extends AbstractFragmentPluginAppCompatActivity { private static final int REQUEST_SELECT_FILTER = 10124; + private static final String EXTRA_FILTER = "extra_filter"; @Bind(R.id.toolbar) Toolbar toolbar; @Inject ActivityPreferences preferences; + @Inject DialogBuilder dialogBuilder; + @Inject DefaultFilterProvider defaultFilterProvider; private Bundle previousBundle; - private String title; - private String query; - private String values; + private Filter filter; @Override protected void onCreate(final Bundle savedInstanceState) { @@ -49,11 +55,11 @@ public final class TaskerSettingsActivity extends AbstractFragmentPluginAppCompa if (savedInstanceState != null) { previousBundle = savedInstanceState.getParcelable(PluginBundleValues.BUNDLE_EXTRA_PREVIOUS_BUNDLE); - title = savedInstanceState.getString(PluginBundleValues.BUNDLE_EXTRA_STRING_TITLE); - query = savedInstanceState.getString(PluginBundleValues.BUNDLE_EXTRA_STRING_QUERY); - values = savedInstanceState.getString(PluginBundleValues.BUNDLE_EXTRA_STRING_VALUES); - updateActivity(); + filter = savedInstanceState.getParcelable(EXTRA_FILTER); + } else { + filter = defaultFilterProvider.getDefaultFilter(); } + updateView(); setSupportActionBar(toolbar); ActionBar supportActionBar = getSupportActionBar(); @@ -64,19 +70,38 @@ public final class TaskerSettingsActivity extends AbstractFragmentPluginAppCompa supportActionBar.setHomeAsUpIndicator(drawable); supportActionBar.setDisplayShowTitleEnabled(false); } + + if (!preferences.getBoolean(R.string.p_tasker_enabled, false)) { + dialogBuilder.newMessageDialog(R.string.tasker_disabled_warning) + .setPositiveButton(R.string.TLA_menu_settings, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + startActivity(new Intent(TaskerSettingsActivity.this, BasicPreferences.class)); + cancel(); + } + }) + .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + cancel(); + } + }) + .show(); + } } @OnClick(R.id.filter_selection) void selectFilter() { - startActivityForResult(new Intent(TaskerSettingsActivity.this, FilterSelectionActivity.class), REQUEST_SELECT_FILTER); + startActivityForResult(new Intent(TaskerSettingsActivity.this, FilterSelectionActivity.class) {{ + putExtra(FilterSelectionActivity.EXTRA_RETURN_FILTER, true); + }}, REQUEST_SELECT_FILTER); } @Override public void onPostCreateWithPreviousResult(final Bundle previousBundle, final String previousBlurb) { this.previousBundle = previousBundle; - title = PluginBundleValues.getTitle(previousBundle); - query = PluginBundleValues.getQuery(previousBundle); - updateActivity(); + this.filter = defaultFilterProvider.getFilterFromPreference(PluginBundleValues.getFilter(previousBundle)); + updateView(); } @Override @@ -86,12 +111,12 @@ public final class TaskerSettingsActivity extends AbstractFragmentPluginAppCompa @Override public Bundle getResultBundle() { - return PluginBundleValues.generateBundle(title, query, values); + return PluginBundleValues.generateBundle(defaultFilterProvider.getFilterPreferenceValue(filter)); } @Override public String getResultBlurb(final Bundle bundle) { - return PluginBundleValues.getTitle(bundle); + return filter.listingTitle; } @Override @@ -176,10 +201,8 @@ public final class TaskerSettingsActivity extends AbstractFragmentPluginAppCompa protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_SELECT_FILTER) { if (resultCode == RESULT_OK) { - title = data.getStringExtra("extra_filter_name"); - query = data.getStringExtra("extra_filter_query"); - values = data.getStringExtra("extra_filter_values"); - updateActivity(); + filter = data.getParcelableExtra(FilterSelectionActivity.EXTRA_FILTER); + updateView(); } return; @@ -191,13 +214,12 @@ public final class TaskerSettingsActivity extends AbstractFragmentPluginAppCompa protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putParcelable(PluginBundleValues.BUNDLE_EXTRA_PREVIOUS_BUNDLE, previousBundle); - outState.putString(PluginBundleValues.BUNDLE_EXTRA_STRING_TITLE, title); - outState.putString(PluginBundleValues.BUNDLE_EXTRA_STRING_QUERY, query); - outState.putString(PluginBundleValues.BUNDLE_EXTRA_STRING_VALUES, values); + outState.putParcelable(EXTRA_FILTER, filter); } - private void updateActivity() { - ((TextView) findViewById(R.id.text_view)).setText(title); + private void updateView() { + ((TextView) findViewById(R.id.text_view)) + .setText(filter.listingTitle); } @Override diff --git a/src/main/java/org/tasks/Notifier.java b/src/main/java/org/tasks/Notifier.java index 566f1c3d7..a91ca360d 100644 --- a/src/main/java/org/tasks/Notifier.java +++ b/src/main/java/org/tasks/Notifier.java @@ -17,7 +17,6 @@ import com.todoroo.andlib.data.TodorooCursor; import com.todoroo.andlib.utility.AndroidUtilities; import com.todoroo.andlib.utility.DateUtilities; import com.todoroo.astrid.activity.TaskListActivity; -import com.todoroo.astrid.activity.TaskListFragment; import com.todoroo.astrid.api.Filter; import com.todoroo.astrid.dao.TaskDao; import com.todoroo.astrid.data.Task; @@ -133,7 +132,14 @@ public class Notifier { return b; } + @Deprecated public void triggerFilterNotification(final String title, final String query, final String valuesForNewTasks) { + triggerFilterNotification(new Filter(title, query, AndroidUtilities.contentValuesFromSerializedString(valuesForNewTasks))); + } + + public void triggerFilterNotification(final Filter filter) { + String title = filter.listingTitle; + String query = filter.getSqlQuery(); TodorooCursor taskTodorooCursor = null; int count; try { @@ -158,7 +164,7 @@ public class Notifier { PendingIntent pendingIntent = PendingIntent.getActivity(context, (title + query).hashCode(), new Intent(context, TaskListActivity.class) {{ setFlags(FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_MULTIPLE_TASK); - putExtra(TaskListActivity.OPEN_FILTER, new Filter(title, query, AndroidUtilities.contentValuesFromSerializedString(valuesForNewTasks))); + putExtra(TaskListActivity.OPEN_FILTER, filter); }}, PendingIntent.FLAG_UPDATE_CURRENT); Notification notification = new NotificationCompat.Builder(context) diff --git a/src/main/java/org/tasks/preferences/DefaultFilterProvider.java b/src/main/java/org/tasks/preferences/DefaultFilterProvider.java index 5aa8656c8..b55407d2d 100644 --- a/src/main/java/org/tasks/preferences/DefaultFilterProvider.java +++ b/src/main/java/org/tasks/preferences/DefaultFilterProvider.java @@ -64,10 +64,13 @@ public class DefaultFilterProvider { } public Filter getDefaultFilter() { - String defaultList = preferences.getStringValue(R.string.p_default_list); - if (!isNullOrEmpty(defaultList)) { + return getFilterFromPreference(preferences.getStringValue(R.string.p_default_list)); + } + + public Filter getFilterFromPreference(String preferenceValue) { + if (!isNullOrEmpty(preferenceValue)) { try { - Filter filter = loadFilterFromPreferences(defaultList); + Filter filter = loadFilter(preferenceValue); if (filter != null) { return filter; } @@ -78,8 +81,8 @@ public class DefaultFilterProvider { return BuiltInFilterExposer.getMyTasksFilter(context.getResources()); } - private Filter loadFilterFromPreferences(String defaultList) { - String[] split = defaultList.split(":"); + private Filter loadFilter(String preferenceValue) { + String[] split = preferenceValue.split(":"); switch (Integer.parseInt(split[0])) { case TYPE_FILTER: return getBuiltInFilter(Integer.parseInt(split[1])); @@ -96,25 +99,29 @@ public class DefaultFilterProvider { public void setDefaultFilter(Filter filter) { tracker.reportEvent(Tracking.Events.SET_DEFAULT_LIST); + String filterPreferenceValue = getFilterPreferenceValue(filter); + if (!Strings.isNullOrEmpty(filterPreferenceValue)) { + preferences.setString(R.string.p_default_list, filterPreferenceValue); + } + } + + public String getFilterPreferenceValue(Filter filter) { int filterType = getFilterType(filter); switch (filterType) { case TYPE_FILTER: - setDefaultFilter(filterType, getBuiltInFilterId(filter)); - break; + return getFilterPreference(filterType, getBuiltInFilterId(filter)); case TYPE_CUSTOM_FILTER: - setDefaultFilter(filterType, ((CustomFilter) filter).getId()); - break; + return getFilterPreference(filterType, ((CustomFilter) filter).getId()); case TYPE_TAG: - setDefaultFilter(filterType, ((FilterWithCustomIntent) filter).customExtras.getString(TagViewFragment.EXTRA_TAG_UUID)); - break; + return getFilterPreference(filterType, ((FilterWithCustomIntent) filter).customExtras.getString(TagViewFragment.EXTRA_TAG_UUID)); case TYPE_GOOGLE_TASKS: - setDefaultFilter(filterType, ((FilterWithCustomIntent) filter).customExtras.getLong(GtasksListFragment.TOKEN_STORE_ID, 0L)); - break; + return getFilterPreference(filterType, ((FilterWithCustomIntent) filter).customExtras.getLong(GtasksListFragment.TOKEN_STORE_ID, 0L)); } + return null; } - private void setDefaultFilter(int type, T value) { - preferences.setString(R.string.p_default_list, String.format("%s:%s", type, value)); + private String getFilterPreference(int type, T value) { + return String.format("%s:%s", type, value); } private int getFilterType(Filter filter) { diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index 46fc15ce9..a7ea5e70c 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -863,6 +863,7 @@ File %1$s contained %2$s.\n\n Display a badge for the number of active tasks in your default list. Requires TeslaUnread for Nova Launcher Context-aware list notifications. Requires Tasker or Locale Donations are greatly appreciated + Enable Tasker integration in Tasks settings