Fixed threshold values.

This commit is contained in:
nightoftune 2019-02-17 16:34:05 +01:00
parent 82cec63c94
commit 554a9a9ffa
4 changed files with 124 additions and 39 deletions

View File

@ -69,13 +69,13 @@ public class TaskerConstants {
StringBuilder builder = new StringBuilder(constant); StringBuilder builder = new StringBuilder(constant);
if (scopes != null && !scopes.isEmpty()) { if (scopes != null && !scopes.isEmpty()) {
for (String scope : scopes) { for (String scope : scopes) {
if (!StringUtils.isEmpty(scope)) { if (StringUtils.isEmpty(scope)) {
continue; continue;
} }
if (scopes.indexOf(scope) < scope.length() - 1) { // if (scopes.indexOf(scope) <= scopes.size() - 1) {
builder.append("_"); builder.append("_");
} // }
builder.append(scopes); builder.append(scope.toLowerCase());
} }
} }
return builder.toString(); return builder.toString();

View File

@ -19,6 +19,10 @@ public class ButtonPreference extends EditTextPreference {
private View.OnClickListener onClickListener; private View.OnClickListener onClickListener;
private int buttonTextResource; private int buttonTextResource;
private boolean buttonDisabled;
private Button button;
private View parent;
private boolean disableDialog;
public ButtonPreference(Context context, AttributeSet attrs, int defStyleAttr) { public ButtonPreference(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr); super(context, attrs, defStyleAttr);
@ -38,19 +42,24 @@ public class ButtonPreference extends EditTextPreference {
@Override @Override
protected void onBindView(final View view) { protected void onBindView(final View view) {
Button button = view.findViewById(R.id.tasker_button); Button button = view.findViewById(R.id.tasker_button);
parent = view;
if (button != null) { if (button != null) {
this.button = button;
button.setOnClickListener(onClickListener); button.setOnClickListener(onClickListener);
button.setText(buttonTextResource); button.setText(buttonTextResource);
disableButton();
} }
view.setOnClickListener(new View.OnClickListener() { if (!disableDialog) {
@Override view.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { @Override
if (getDialog() != null && getDialog().isShowing()) { public void onClick(View v) {
return; if (getDialog() != null && getDialog().isShowing()) {
return;
}
showDialog(null);
} }
showDialog(null); });
} }
});
super.onBindView(view); super.onBindView(view);
} }
@ -66,6 +75,9 @@ public class ButtonPreference extends EditTextPreference {
*/ */
public void setOnClickListener(View.OnClickListener clickListener) { public void setOnClickListener(View.OnClickListener clickListener) {
this.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) { public void setButtonText(int resourceId) {
buttonTextResource = 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);
}
} }

View File

@ -2,29 +2,50 @@ package nodomain.freeyourgadget.gadgetbridge.tasker.settings.activities;
import android.content.Context; import android.content.Context;
import android.preference.DialogPreference; import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.NumberPicker; import android.widget.NumberPicker;
import nodomain.freeyourgadget.gadgetbridge.R;
public class NumberPreference extends DialogPreference { 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); super(context, null);
numberPicker = new NumberPicker(getContext()); this.values = values;
this.stepSize = stepSize;
} }
@Override @Override
protected View onCreateDialogView() { 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; return numberPicker;
} }
public NumberPicker getNumberPicker() { @Override
return numberPicker; 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;
}
} }

View File

@ -17,6 +17,7 @@ import android.view.MotionEvent;
import android.view.View; import android.view.View;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator;
import java.util.List; import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBApplication;
@ -68,6 +69,8 @@ public class TaskerEventActivity extends AbstractSettingsActivity {
public static class TaskerEventFragment extends PreferenceFragment { public static class TaskerEventFragment extends PreferenceFragment {
private static final int thresholdStep = 200;
private TaskerDevice device; private TaskerDevice device;
private TaskerEventType eventType; private TaskerEventType eventType;
private Prefs prefs = GBApplication.getPrefs(); private Prefs prefs = GBApplication.getPrefs();
@ -76,7 +79,7 @@ public class TaskerEventActivity extends AbstractSettingsActivity {
private SwitchPreference enableThreshold; private SwitchPreference enableThreshold;
private NumberPreference threshold; private NumberPreference threshold;
private ButtonPreference addTask; private ButtonPreference addTask;
private List<EditTextPreference> tasks = new ArrayList<>(); private List<ButtonPreference> tasks = new ArrayList<>();
@Override @Override
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
@ -115,22 +118,21 @@ public class TaskerEventActivity extends AbstractSettingsActivity {
private void initThreshold() { private void initThreshold() {
final String key = scoped(TaskerConstants.ACTIVITY_THRESHOLD); 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.setKey(key);
threshold.setTitle(R.string.tasker_threshold); threshold.setTitle(R.string.tasker_threshold);
threshold.setSummary(R.string.tasker_threshold_sum); threshold.setSummary(R.string.tasker_threshold_sum);
threshold.getNumberPicker().setMinValue(50);
threshold.getNumberPicker().setMaxValue(10000);
enableThreshold.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { enableThreshold.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override @Override
public boolean onPreferenceChange(Preference preference, Object newValue) { public boolean onPreferenceChange(Preference preference, Object newValue) {
if (newValue.equals(Boolean.FALSE)) { if (newValue.equals(Boolean.FALSE)) {
for (EditTextPreference taskPreference : tasks) { for (ButtonPreference task : new ArrayList<>(tasks.subList(1, tasks.size()))) {
if (!taskPreference.getKey().equals(scoped(TaskerConstants.ACTIVITY_TASK))) { removeTask(task);
getPreferenceScreen().removePreference(taskPreference);
}
} }
prefs.getPreferences().edit().remove(key).commit();
} }
return true; return true;
} }
@ -140,6 +142,7 @@ public class TaskerEventActivity extends AbstractSettingsActivity {
private void initAddTask() { private void initAddTask() {
addTask = new ButtonPreference(getActivity()); addTask = new ButtonPreference(getActivity());
addTask.setDisableDialog(true);
addTask.setTitle(R.string.tasker_task); addTask.setTitle(R.string.tasker_task);
addTask.setButtonText(R.string.tasker_add); addTask.setButtonText(R.string.tasker_add);
addTask.setSummary(R.string.tasker_task_sum); addTask.setSummary(R.string.tasker_task_sum);
@ -159,15 +162,21 @@ public class TaskerEventActivity extends AbstractSettingsActivity {
} }
String key = scoped(TaskerConstants.ACTIVITY_TASK) + "_" + i; String key = scoped(TaskerConstants.ACTIVITY_TASK) + "_" + i;
if (prefs.getPreferences().contains(key)) { if (prefs.getPreferences().contains(key)) {
EditTextPreference task = task(key); ButtonPreference task = task(key);
tasks.add(task); task.setTitle(prefs.getPreferences().getString(key, ""));
if (i == 0) {
task.setButtonDisabled(true);
}
if (i == 9) {
addTask.setButtonDisabled(true);
}
getPreferenceScreen().addPreference(task); getPreferenceScreen().addPreference(task);
} }
} }
// Add default task // Add default task
if (tasks.isEmpty()) { if (tasks.isEmpty()) {
EditTextPreference task = task(scoped(TaskerConstants.ACTIVITY_TASK) + "_" + 0); ButtonPreference task = task(scoped(TaskerConstants.ACTIVITY_TASK) + "_" + 0);
tasks.add(task); task.setButtonDisabled(true);
getPreferenceScreen().addPreference(task); getPreferenceScreen().addPreference(task);
} }
} }
@ -176,28 +185,44 @@ public class TaskerEventActivity extends AbstractSettingsActivity {
addTask.setOnClickListener(new View.OnClickListener() { addTask.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (prefs.getBoolean(scoped(TaskerConstants.ACTIVITY_THRESHOLD_ENABLED), false)) { if (prefs.getBoolean(scoped(TaskerConstants.ACTIVITY_THRESHOLD_ENABLED), false) && tasks.size() < 9 || tasks.size() < 1) {
getPreferenceScreen().addPreference(task(scoped(TaskerConstants.ACTIVITY_TASK) + "_" + (tasks.size() - 1))); getPreferenceScreen().addPreference(task(scoped(TaskerConstants.ACTIVITY_TASK) + "_" + tasks.size()));
} }
} }
}); });
loadTasks(); loadTasks();
} }
private EditTextPreference task(String key) { private ButtonPreference task(String key) {
final ButtonPreference task = new ButtonPreference(getActivity()); final ButtonPreference task = new ButtonPreference(getActivity());
task.setKey(key); task.setKey(key);
task.setSummary(R.string.tasker_task_name); task.setSummary(R.string.tasker_task_name);
task.setButtonText(R.string.tasker_remove); 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() { task.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
getPreferenceScreen().removePreference(task); removeTask(task);
} }
}); });
tasks.add(task);
return 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);
}
} }
} }