From 554a9a9ffa4ae378a42433299f72b91d0b20db97 Mon Sep 17 00:00:00 2001 From: nightoftune Date: Sun, 17 Feb 2019 16:34:05 +0100 Subject: [PATCH] Fixed threshold values. --- .../tasker/plugin/TaskerConstants.java | 10 ++-- .../settings/activities/ButtonPreference.java | 55 ++++++++++++++--- .../settings/activities/NumberPreference.java | 39 +++++++++--- .../activities/TaskerEventActivity.java | 59 +++++++++++++------ 4 files changed, 124 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/plugin/TaskerConstants.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/plugin/TaskerConstants.java index f65cb78b6..a92aa82c9 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/plugin/TaskerConstants.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/plugin/TaskerConstants.java @@ -69,13 +69,13 @@ public class TaskerConstants { StringBuilder builder = new StringBuilder(constant); if (scopes != null && !scopes.isEmpty()) { for (String scope : scopes) { - if (!StringUtils.isEmpty(scope)) { + if (StringUtils.isEmpty(scope)) { continue; } - if (scopes.indexOf(scope) < scope.length() - 1) { - builder.append("_"); - } - builder.append(scopes); +// if (scopes.indexOf(scope) <= scopes.size() - 1) { + builder.append("_"); +// } + builder.append(scope.toLowerCase()); } } return builder.toString(); diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/ButtonPreference.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/ButtonPreference.java index 817b15296..48fce8439 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/ButtonPreference.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/ButtonPreference.java @@ -19,6 +19,10 @@ public class ButtonPreference extends EditTextPreference { private View.OnClickListener onClickListener; private int buttonTextResource; + private boolean buttonDisabled; + private Button button; + private View parent; + private boolean disableDialog; public ButtonPreference(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); @@ -38,19 +42,24 @@ public class ButtonPreference extends EditTextPreference { @Override protected void onBindView(final View view) { Button button = view.findViewById(R.id.tasker_button); + parent = view; if (button != null) { + this.button = button; button.setOnClickListener(onClickListener); button.setText(buttonTextResource); + disableButton(); } - view.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - if (getDialog() != null && getDialog().isShowing()) { - return; + if (!disableDialog) { + view.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (getDialog() != null && getDialog().isShowing()) { + return; + } + showDialog(null); } - showDialog(null); - } - }); + }); + } super.onBindView(view); } @@ -66,6 +75,9 @@ public class ButtonPreference extends EditTextPreference { */ public void setOnClickListener(View.OnClickListener clickListener) { this.onClickListener = clickListener; + if (button != null) { + button.setOnClickListener(onClickListener); + } } /** @@ -75,5 +87,32 @@ public class ButtonPreference extends EditTextPreference { */ public void setButtonText(int resourceId) { buttonTextResource = resourceId; + if (button != null) { + button.setText(buttonTextResource); + } } + + public void setButtonDisabled(boolean buttonDisabled) { + this.buttonDisabled = buttonDisabled; + if (button != null) { + disableButton(); + } + } + + public void setDisableDialog(boolean disableDialog) { + this.disableDialog = disableDialog; + if (parent != null) { + parent.setOnClickListener(null); + } + } + + private void disableButton() { + if (buttonDisabled) { + button.setAlpha(.5f); + } else { + button.setAlpha(1f); + } + button.setClickable(!buttonDisabled); + } + } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/NumberPreference.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/NumberPreference.java index fd76f8402..f901ff040 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/NumberPreference.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/NumberPreference.java @@ -2,29 +2,50 @@ package nodomain.freeyourgadget.gadgetbridge.tasker.settings.activities; import android.content.Context; import android.preference.DialogPreference; -import android.util.AttributeSet; import android.view.View; -import android.view.ViewGroup; import android.widget.NumberPicker; -import nodomain.freeyourgadget.gadgetbridge.R; - public class NumberPreference extends DialogPreference { - private NumberPicker numberPicker; + private long stepSize = 200; + private String[] values; + private NumberPicker currentNumberPicker; - public NumberPreference(Context context) { + public NumberPreference(Context context, String[] values, long stepSize) { super(context, null); - numberPicker = new NumberPicker(getContext()); + this.values = values; + this.stepSize = stepSize; } @Override protected View onCreateDialogView() { + NumberPicker numberPicker = new NumberPicker(getContext()); + numberPicker.setMinValue(0); + numberPicker.setMaxValue(values.length - 1); + numberPicker.setValue((int) getPersistedLong(stepSize)); + numberPicker.setDisplayedValues(values); + currentNumberPicker = numberPicker; return numberPicker; } - public NumberPicker getNumberPicker() { - return numberPicker; + @Override + protected void onDialogClosed(boolean positiveResult) { + persistLong((currentNumberPicker.getValue() * stepSize + stepSize)); } + public long getStepSize() { + return stepSize; + } + + public void setStepSize(long stepSize) { + this.stepSize = stepSize; + } + + public String[] getValues() { + return values; + } + + public void setValues(String[] values) { + this.values = values; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/TaskerEventActivity.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/TaskerEventActivity.java index 4d8c079aa..b3ef783f3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/TaskerEventActivity.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/tasker/settings/activities/TaskerEventActivity.java @@ -17,6 +17,7 @@ import android.view.MotionEvent; import android.view.View; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; import nodomain.freeyourgadget.gadgetbridge.GBApplication; @@ -68,6 +69,8 @@ public class TaskerEventActivity extends AbstractSettingsActivity { public static class TaskerEventFragment extends PreferenceFragment { + private static final int thresholdStep = 200; + private TaskerDevice device; private TaskerEventType eventType; private Prefs prefs = GBApplication.getPrefs(); @@ -76,7 +79,7 @@ public class TaskerEventActivity extends AbstractSettingsActivity { private SwitchPreference enableThreshold; private NumberPreference threshold; private ButtonPreference addTask; - private List tasks = new ArrayList<>(); + private List tasks = new ArrayList<>(); @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -115,22 +118,21 @@ public class TaskerEventActivity extends AbstractSettingsActivity { private void initThreshold() { final String key = scoped(TaskerConstants.ACTIVITY_THRESHOLD); - threshold = new NumberPreference(getActivity()); + String[] values = new String[(10000 / thresholdStep)]; + for (int i = 0; i < values.length; i++) { + values[i] = Integer.toString((i + 1) * thresholdStep); + } + threshold = new NumberPreference(getActivity(), values, thresholdStep); threshold.setKey(key); threshold.setTitle(R.string.tasker_threshold); threshold.setSummary(R.string.tasker_threshold_sum); - threshold.getNumberPicker().setMinValue(50); - threshold.getNumberPicker().setMaxValue(10000); enableThreshold.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { @Override public boolean onPreferenceChange(Preference preference, Object newValue) { if (newValue.equals(Boolean.FALSE)) { - for (EditTextPreference taskPreference : tasks) { - if (!taskPreference.getKey().equals(scoped(TaskerConstants.ACTIVITY_TASK))) { - getPreferenceScreen().removePreference(taskPreference); - } + for (ButtonPreference task : new ArrayList<>(tasks.subList(1, tasks.size()))) { + removeTask(task); } - prefs.getPreferences().edit().remove(key).commit(); } return true; } @@ -140,6 +142,7 @@ public class TaskerEventActivity extends AbstractSettingsActivity { private void initAddTask() { addTask = new ButtonPreference(getActivity()); + addTask.setDisableDialog(true); addTask.setTitle(R.string.tasker_task); addTask.setButtonText(R.string.tasker_add); addTask.setSummary(R.string.tasker_task_sum); @@ -159,15 +162,21 @@ public class TaskerEventActivity extends AbstractSettingsActivity { } String key = scoped(TaskerConstants.ACTIVITY_TASK) + "_" + i; if (prefs.getPreferences().contains(key)) { - EditTextPreference task = task(key); - tasks.add(task); + ButtonPreference task = task(key); + task.setTitle(prefs.getPreferences().getString(key, "")); + if (i == 0) { + task.setButtonDisabled(true); + } + if (i == 9) { + addTask.setButtonDisabled(true); + } getPreferenceScreen().addPreference(task); } } // Add default task if (tasks.isEmpty()) { - EditTextPreference task = task(scoped(TaskerConstants.ACTIVITY_TASK) + "_" + 0); - tasks.add(task); + ButtonPreference task = task(scoped(TaskerConstants.ACTIVITY_TASK) + "_" + 0); + task.setButtonDisabled(true); getPreferenceScreen().addPreference(task); } } @@ -176,28 +185,44 @@ public class TaskerEventActivity extends AbstractSettingsActivity { addTask.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - if (prefs.getBoolean(scoped(TaskerConstants.ACTIVITY_THRESHOLD_ENABLED), false)) { - getPreferenceScreen().addPreference(task(scoped(TaskerConstants.ACTIVITY_TASK) + "_" + (tasks.size() - 1))); + if (prefs.getBoolean(scoped(TaskerConstants.ACTIVITY_THRESHOLD_ENABLED), false) && tasks.size() < 9 || tasks.size() < 1) { + getPreferenceScreen().addPreference(task(scoped(TaskerConstants.ACTIVITY_TASK) + "_" + tasks.size())); } } }); loadTasks(); } - private EditTextPreference task(String key) { + private ButtonPreference task(String key) { final ButtonPreference task = new ButtonPreference(getActivity()); task.setKey(key); task.setSummary(R.string.tasker_task_name); task.setButtonText(R.string.tasker_remove); + task.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object newValue) { + task.setTitle(newValue.toString()); + return true; + } + }); task.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - getPreferenceScreen().removePreference(task); + removeTask(task); } }); + tasks.add(task); return task; } + private void removeTask(ButtonPreference task) { + tasks.remove(task); + getPreferenceScreen().removePreference(task); + prefs.getPreferences().edit().remove(task.getKey()).commit(); + addTask.setButtonDisabled(false); + tasks.get(0).setButtonDisabled(true); + } + } }