Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added input limit #2073

Merged
merged 1 commit into from
Jan 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions app/src/main/java/io/pslab/InputMinMaxFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.pslab;

import android.text.InputFilter;
import android.text.Spanned;

public class InputMinMaxFilter implements InputFilter {

private int min;
private int max;

public InputMinMaxFilter(int min, int max) {
this.min = min;
this.max = max;
}
public InputMinMaxFilter(String min, String max) {
this.min = Integer.parseInt(min);
this.max = Integer.parseInt(max);
}
private boolean isInRange(int a, int b, int c) {
return b > a ? c >= a && c <= b : c >= b && c <= a;
}
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
try {
String newVal = dest.toString().substring(0, dstart) + dest.toString().substring(dend, dest.toString().length());
newVal = newVal.substring(0, dstart) + source.toString() + newVal.substring(dstart, newVal.length());
int input = Integer.parseInt(newVal);
if (newVal.length() >= 4)
return "";
if (isInRange(min, max, input))
return null;
} catch (NumberFormatException nfe) {
}
return "";
}
}
7 changes: 7 additions & 0 deletions app/src/main/java/io/pslab/activity/RoboticArmActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.InputFilter;
import android.util.TypedValue;
import android.view.Display;
import android.view.DragEvent;
Expand All @@ -41,6 +42,7 @@

import butterknife.BindView;
import butterknife.ButterKnife;
import io.pslab.InputMinMaxFilter;
import io.pslab.R;
import io.pslab.communication.ScienceLab;
import io.pslab.models.SensorDataBlock;
Expand Down Expand Up @@ -155,6 +157,11 @@ public void onClick(View v) {
degreeText3.setText(getResources().getString(R.string.zero));
degreeText4.setText(getResources().getString(R.string.zero));

degreeText1.setFilters(new InputFilter[]{new InputMinMaxFilter(0,360)});
degreeText2.setFilters(new InputFilter[]{new InputMinMaxFilter(0,360)});
degreeText3.setFilters(new InputFilter[]{new InputMinMaxFilter(0,360)});
degreeText4.setFilters(new InputFilter[]{new InputMinMaxFilter(0,360)});

LinearLayout.LayoutParams servoControllerParams = new LinearLayout.LayoutParams(screen_width / 4 - 4, screen_height / 2 - 4);
servoControllerParams.setMargins(2, 5, 2, 0);
servo1Layout.setLayoutParams(servoControllerParams);
Expand Down