summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/com
diff options
context:
space:
mode:
authormaks <maks@jopek.eu>2022-10-26 12:54:16 +0200
committermaks <maks@jopek.eu>2022-10-26 12:54:16 +0200
commit81ff51148575b9ad4bc16ebf18afa5819174c7e6 (patch)
tree9a62c9b8bd8f7118716cafdf576acb7bd80bcfe0 /app/src/main/java/com
downloadapplicationdevrification-81ff51148575b9ad4bc16ebf18afa5819174c7e6.tar.gz
applicationdevrification-81ff51148575b9ad4bc16ebf18afa5819174c7e6.tar.zst
applicationdevrification-81ff51148575b9ad4bc16ebf18afa5819174c7e6.zip
Initial commit, w/ v1.0.0
Diffstat (limited to 'app/src/main/java/com')
-rw-r--r--app/src/main/java/com/jopek/applicationdevrification/MainActivity.java208
-rw-r--r--app/src/main/java/com/jopek/applicationdevrification/Requests.java105
2 files changed, 313 insertions, 0 deletions
diff --git a/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java b/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java
new file mode 100644
index 0000000..e9c9603
--- /dev/null
+++ b/app/src/main/java/com/jopek/applicationdevrification/MainActivity.java
@@ -0,0 +1,208 @@
+package com.jopek.applicationdevrification;
+
+import static android.widget.ArrayAdapter.createFromResource;
+
+import android.content.Context;
+import android.graphics.Color;
+import android.os.Bundle;
+import android.text.InputFilter;
+import android.text.InputType;
+import android.util.Log;
+import android.view.View;
+import android.view.inputmethod.InputMethodManager;
+import android.widget.AdapterView;
+import android.widget.ArrayAdapter;
+import android.widget.Button;
+import android.widget.EditText;
+import android.widget.Spinner;
+import android.widget.TextView;
+
+import androidx.appcompat.app.AppCompatActivity;
+
+import java.util.regex.Pattern;
+
+enum CodeOption {
+ zipCode,
+ address,
+ phone,
+ email,
+ pesel,
+ idcard,
+ nameNSurname,
+ password,
+ censorship,
+}
+
+public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
+ ArrayAdapter<CharSequence> adapter;
+ EditText input;
+ TextView output;
+ CodeOption selOption;
+
+ @Override
+ protected void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.activity_main);
+
+ Spinner spinner = findViewById(R.id.spinner);
+ adapter = createFromResource(this, R.array.codes_array, android.R.layout.simple_spinner_item);
+ adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+ spinner.setAdapter(adapter);
+ spinner.setOnItemSelectedListener(this);
+
+ Button btn = findViewById(R.id.btnValidate);
+ btn.setOnClickListener(v -> validate());
+
+ input = findViewById(R.id.txtInput);
+
+ output = findViewById(R.id.txtOutput);
+ }
+
+ private void validate() {
+ String in = input.getText().toString();
+ String gender = "";
+ boolean result = false;
+
+ if (in.length() == 0) {
+ result = false;
+ } else if (selOption == CodeOption.zipCode) {
+ Requests requests = new Requests();
+ requests.get("https://jopek.eu/maks/get-city?code=" + in, txt -> {
+ validateResult(txt.length() != 0, "");
+ });
+ return;
+ } else if (selOption == CodeOption.address) {
+ Pattern p = Pattern.compile("^([a-ząśćżźńłęó]+ )+\\d+[a-z]?$", Pattern.CASE_INSENSITIVE);
+ result = p.matcher(in).matches();
+ } else if (selOption == CodeOption.phone) {
+ Pattern p;
+ if (in.charAt(0) == '+') {
+ p = Pattern.compile("\\+(\\d\\d?\\d?)\\d{9}", Pattern.CASE_INSENSITIVE);
+ } else {
+ p = Pattern.compile("\\d{7}(\\d{2})?", Pattern.CASE_INSENSITIVE);
+ }
+ result = p.matcher(in).matches();
+ } else if (selOption == CodeOption.email) {
+ Pattern p = Pattern.compile("^[a-zA-Z][\\w.]+[\\w]@[a-z]{2,}\\.[a-z]{2,}$");
+ result = p.matcher(in).matches();
+ } else if (selOption == CodeOption.pesel) {
+ int lastDigit = Integer.parseInt(in.substring(10, 11));
+ int a = Integer.parseInt(in.substring(0, 1));
+ int b = Integer.parseInt(in.substring(1, 2));
+ int c = Integer.parseInt(in.substring(2, 3));
+ int d = Integer.parseInt(in.substring(3, 4));
+ int e = Integer.parseInt(in.substring(4, 5));
+ int f = Integer.parseInt(in.substring(5, 6));
+ int g = Integer.parseInt(in.substring(6, 7));
+ int h = Integer.parseInt(in.substring(7, 8));
+ int i = Integer.parseInt(in.substring(8, 9));
+ int j = Integer.parseInt(in.substring(9, 10));
+ int sum = 9 * a + 7 * b + 3 * c + 1 * d + 9 * e + 7 * f + 3 * g + 1 * h + 9 * i + 7 * j;
+ result = sum % 10 == lastDigit;
+ if (result) {
+ int plec = Integer.parseInt(in.substring(9, 10));
+ if (plec % 2 == 0 || plec == 0) {
+ gender = "K";
+ } else {
+ gender = "M";
+ }
+ }
+ } else if (selOption == CodeOption.idcard) {
+ Pattern p = Pattern.compile(".*\\d.*");
+ result = p.matcher(in).matches();
+ if (result) {
+ p = Pattern.compile("^[a-z][a-z\\d]+$", Pattern.CASE_INSENSITIVE);
+ result = p.matcher(in).matches();
+ }
+ } else if (selOption == CodeOption.nameNSurname) {
+ Pattern p = Pattern.compile("^[a-z]+ [a-z]+(-[a-z]+)?$", Pattern.CASE_INSENSITIVE);
+ result = p.matcher(in).matches();
+ } else if (selOption == CodeOption.password) {
+ result = in.matches(".*[A-Z].*");
+ Log.d("maks", "validate: " + result);
+ result = result && in.matches(".*[a-z].*");
+ Log.d("maks", "validate: " + result);
+ result = result && in.matches(".*([^\\w\\s]|_).*");
+ Log.d("maks", "validate: " + result);
+ result = result && in.matches(".*\\d.*");
+ Log.d("maks", "validate: " + result);
+ result = result && in.matches(".{7,}");
+ Log.d("maks", "validate: " + result);
+ } else if (selOption == CodeOption.censorship) {
+ String[] illegal = new String[]{"wtf", "rabarbar", "kobieta", "bezalkoholowe", "kamienskiego"};
+ result = true;
+ for (String ill : illegal) {
+ result = result && !in.matches(".*" + ill + ".*");
+ }
+ }
+
+ validateResult(result, gender);
+ }
+
+ private void validateResult(boolean win, String gender) {
+ View view = this.getCurrentFocus();
+ if (view != null) {
+ InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
+ imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
+ }
+
+ if (win) {
+ output.setTextColor(Color.rgb(0, 255, 0));
+ if (gender.length() == 0)
+ output.setText("CORRECT");
+ else
+ output.setText("CORRECT (" + gender + ")");
+ } else {
+ output.setTextColor(Color.rgb(255, 0, 0));
+ output.setText("WRONG");
+ }
+ }
+
+ @Override
+ public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
+ CharSequence selectedOption = adapter.getItem(position);
+ if ("zip-code".contentEquals(selectedOption)) {
+ selOption = CodeOption.zipCode;
+ } else if ("address".contentEquals(selectedOption)) {
+ selOption = CodeOption.address// input.setMinLines(1);
+// input.setLines(2);
+// input.setMaxLines(10);
+;
+ } else if ("phone".contentEquals(selectedOption)) {
+ selOption = CodeOption.phone;
+ } else if ("email".contentEquals(selectedOption)) {
+ selOption = CodeOption.email;
+ } else if ("pesel".contentEquals(selectedOption)) {
+ selOption = CodeOption.pesel;
+ } else if ("idcard".contentEquals(selectedOption)) {
+ selOption = CodeOption.idcard;
+ } else if ("name&surname".contentEquals(selectedOption)) {
+ selOption = CodeOption.nameNSurname;
+ } else if ("password".contentEquals(selectedOption)) {
+ selOption = CodeOption.password;
+ } else if ("censorship".contentEquals(selectedOption)) {
+ selOption = CodeOption.censorship;
+ }
+
+ input.setFilters(new InputFilter[]{});
+ input.setInputType(InputType.TYPE_CLASS_TEXT);
+ input.setSingleLine(true);
+ if (selOption == CodeOption.zipCode) {
+ input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(5)});
+ input.setInputType(InputType.TYPE_CLASS_NUMBER);
+ } else if (selOption == CodeOption.phone) {
+ input.setInputType(InputType.TYPE_CLASS_PHONE);
+ } else if (selOption == CodeOption.pesel) {
+ input.setFilters(new InputFilter[]{new InputFilter.LengthFilter(11)});
+ input.setInputType(InputType.TYPE_CLASS_NUMBER);
+ } else if (selOption == CodeOption.password || selOption == CodeOption.censorship) {
+ input.setInputType(InputType.TYPE_TEXT_FLAG_MULTI_LINE);
+ input.setSingleLine(false);
+ }
+ }
+
+ @Override
+ public void onNothingSelected(AdapterView<?> parent) {
+
+ }
+} \ No newline at end of file
diff --git a/app/src/main/java/com/jopek/applicationdevrification/Requests.java b/app/src/main/java/com/jopek/applicationdevrification/Requests.java
new file mode 100644
index 0000000..3f494b6
--- /dev/null
+++ b/app/src/main/java/com/jopek/applicationdevrification/Requests.java
@@ -0,0 +1,105 @@
+package com.jopek.applicationdevrification;
+
+import android.os.AsyncTask;
+
+import org.json.JSONException;
+import org.json.JSONObject;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.InputStreamReader;
+import java.net.URL;
+import java.util.function.Consumer;
+
+//class RequestImage extends AsyncTask<String, Void, Bitmap> {
+// ImageView bmImage;
+// Consumer<Bitmap> cb;
+//
+// public void setRemoteSrc(ImageView bmImage, String url) {
+// this.bmImage = bmImage;
+// this.execute(url);
+// }
+//
+// public void getBitmap(String url, Consumer<Bitmap> cb) {
+// this.cb = cb;
+// this.execute(url);
+// }
+//
+// protected Bitmap doInBackground(String... urls) {
+// String urldisplay = urls[0];
+// Bitmap bmp = null;
+// try {
+// InputStream in = new URL(urldisplay).openStream();
+// bmp = BitmapFactory.decodeStream(in);
+// } catch (Exception e) {
+// e.printStackTrace();
+// }
+// return bmp;
+// }
+//
+// protected void onPostExecute(Bitmap result) {
+// if (cb == null)
+// bmImage.setImageBitmap(result);
+// else cb.accept(result);
+// }
+//}
+
+public class Requests extends AsyncTask<String, Integer, String> {
+ Consumer<String> cb;
+ Consumer<JSONObject> cbJson;
+ boolean retJson = false;
+
+ public void get(String url, Consumer<String> cb) {
+ this.cb = cb;
+ this.execute(url);
+ }
+
+ public void getJson(String url, Consumer<JSONObject> cb) {
+ this.cbJson = cb;
+ this.retJson = true;
+ this.execute(url);
+ }
+
+ protected String doInBackground(String... urls) {
+ InputStream in = null;
+ try {
+ URL url = new URL(urls[0]);
+ in = url.openStream();
+ BufferedReader rd = new BufferedReader(new InputStreamReader(in));
+ String content = "", line;
+ while ((line = rd.readLine()) != null) {
+ content += line + "\n";
+ }
+ in.close();
+ return content;
+ } catch (IOException exception) {
+ if (in != null) {
+ try {
+ in.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ exception.printStackTrace();
+ return "ERROR";
+ }
+ }
+
+ protected void onProgressUpdate(Integer... progress) {
+ }
+
+ protected void onPostExecute(String result) {
+ // this is executed on the main thread after the process is over
+ // update your UI here
+ if (retJson) {
+ try {
+ cbJson.accept(new JSONObject(result));
+ } catch (JSONException e) {
+ e.printStackTrace();
+ }
+ } else {
+ cb.accept(result);
+ }
+ }
+}