From 966bab857ea0de5a81bfff4e7960869498a547b6 Mon Sep 17 00:00:00 2001 From: mmk2410 Date: Tue, 17 Feb 2015 03:13:59 +0100 Subject: [PATCH] Imported Converting Engine; Layout Animations; Copy and Share action --- .idea/workspace.xml | 537 +++++++++--------- .../DecodeNormalMorseManager.java | 241 ++++++++ .../DecodeWrittenMorseManager.java | 240 ++++++++ .../EncodeNormalMorseManager.java | 186 ++++++ .../EncodeWrittenMorseManager.java | 185 ++++++ .../morseconverter/MainActivity.java | 107 +++- app/src/main/res/layout/fragment_main.xml | 55 +- app/src/main/res/layout/fragment_morse.xml | 55 +- app/src/main/res/values/styles.xml | 10 +- 9 files changed, 1288 insertions(+), 328 deletions(-) create mode 100644 app/src/main/java/de/marcelkapfer/morseconverter/DecodeNormalMorseManager.java create mode 100644 app/src/main/java/de/marcelkapfer/morseconverter/DecodeWrittenMorseManager.java create mode 100644 app/src/main/java/de/marcelkapfer/morseconverter/EncodeNormalMorseManager.java create mode 100644 app/src/main/java/de/marcelkapfer/morseconverter/EncodeWrittenMorseManager.java diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 95061b7..94cee9a 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -45,14 +45,13 @@ - - - + + + + - - @@ -77,107 +76,74 @@ - + + + + + + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -185,12 +151,22 @@ + + + + + + + + + + - - + + @@ -204,6 +180,11 @@ + + + + @@ -1050,6 +1037,7 @@ + @@ -1256,10 +1244,14 @@ 1424019537735 1424019537735 - - + @@ -1283,29 +1275,29 @@ - + - + - + - - - - + + + + - - - - - + + + + + - - + + - + @@ -1321,28 +1313,28 @@ - - + + - - - - + + + + - + - - - - + + + + - + - - + + @@ -1369,31 +1361,25 @@ - - - - - - - - - - - - + + + @@ -1449,15 +1435,15 @@ - - - + + + @@ -1484,28 +1470,26 @@ - - - - - + + - - - + + + + @@ -1515,17 +1499,22 @@ - - - + + + + + + + + @@ -1559,28 +1548,26 @@ - - - - - + + - - - + + + + @@ -1590,17 +1577,22 @@ - - - + + + + + + + + @@ -1623,14 +1615,6 @@ - - - - - - - - @@ -1672,13 +1656,6 @@ - - - - - - - @@ -1702,13 +1679,6 @@ - - - - - - - @@ -1716,104 +1686,129 @@ - + - - + + + + + + + + + + + + + + + + + + + + + + + - - - - + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + - - - - - + + + + + + + - - - - - - - - - - diff --git a/app/src/main/java/de/marcelkapfer/morseconverter/DecodeNormalMorseManager.java b/app/src/main/java/de/marcelkapfer/morseconverter/DecodeNormalMorseManager.java new file mode 100644 index 0000000..3f4d0ca --- /dev/null +++ b/app/src/main/java/de/marcelkapfer/morseconverter/DecodeNormalMorseManager.java @@ -0,0 +1,241 @@ +package de.marcelkapfer.morseconverter; + +public class DecodeNormalMorseManager { + + private String inputMessage; + + DecodeNormalMorseManager(String string) { + inputMessage = string; + } + + public void setString(String string) { + inputMessage = string; + } + + public String getString() { + return inputMessage; + } + + /* + * Code for converting normal letters into morse code + */ + + public String getDecodedString() { + StringBuffer message = new StringBuffer(inputMessage); + if (message.toString().endsWith(" ")) { + message = message.deleteCharAt(message.length() - 1); + } + // Variables + // Variables + StringBuffer input = new StringBuffer(); + input = input.replace(0, input.length(), message.toString().toUpperCase()); + StringBuffer output = new StringBuffer(); + if (input.toString().equals("")) { + output.replace(0, output.length(), "Please enter at least one character"); + } else if (input.toString().equals("LETTERSPACE")) { + output.replace(0, output.length(), " "); + } else if (input.toString().equals("END OF WORK")) { + output.replace(0, output.length(), "...-.-"); + } else if (input.toString().equals("ERROR")) { + output.replace(0, output.length(), "........"); + } else if (input.toString().equals("STARTING SIGNAL")) { + output.replace(0, output.length(), "-.-.-"); + } else if (input.toString().equals("ENDING SIGNAL")) { + output.replace(0, output.length(), ".-.-."); + } else if (input.toString().equals("UNDERSTOOD")) { + output.replace(0, output.length(), "...-."); + } else if (input.toString().equals("WAIT")) { + output.replace(0, output.length(), ".-..."); + } else if (input.toString().equals("SOS")) { + output.replace(0, output.length(), "...---..."); + } else if (input.toString().equals("LETTER SPACE")) { + output.replace(0, output.length(), " "); + } else if (input.toString().equals("WORD SPACE")) { + output.replace(0, output.length(), " "); + } else { + for (int c = input.length(); c > 0; c--) { + if (input.toString().startsWith(" ")) { + if (output.toString().endsWith(" ")) { + output.delete(output.length() - 3, output.length()); + } + output.append(" "); + input.delete(0, 1); + } else if (input.toString().startsWith("A")) { + output.append(".- "); + input.delete(0, 1); + } else if (input.toString().startsWith("B")) { + output.append("-... "); + input.delete(0, 1); + } else if (input.toString().startsWith("C")) { + output.append("-.-. "); + input.delete(0, 1); + } else if (input.toString().startsWith("D")) { + output.append("-.. "); + input.delete(0, 1); + } else if (input.toString().startsWith("E")) { + output.append(". "); + input.delete(0, 1); + } else if (input.toString().startsWith("F")) { + output.append("..-. "); + input.delete(0, 1); + } else if (input.toString().startsWith("G")) { + output.append("--. "); + input.delete(0, 1); + } else if (input.toString().startsWith("H")) { + output.append(".... "); + input.delete(0, 1); + } else if (input.toString().startsWith("I")) { + output.append(".. "); + input.delete(0, 1); + } else if (input.toString().startsWith("J")) { + output.append(".--- "); + input.delete(0, 1); + } else if (input.toString().startsWith("K")) { + output.append("-.- "); + input.delete(0, 1); + } else if (input.toString().startsWith("L")) { + output.append(".-.. "); + input.delete(0, 1); + } else if (input.toString().startsWith("M")) { + output.append("-- "); + input.delete(0, 1); + } else if (input.toString().startsWith("N")) { + output.append("-. "); + input.delete(0, 1); + } else if (input.toString().startsWith("O")) { + output.append("--- "); + input.delete(0, 1); + } else if (input.toString().startsWith("P")) { + output.append(".--. "); + input.delete(0, 1); + } else if (input.toString().startsWith("Q")) { + output.append("--.- "); + input.delete(0, 1); + } else if (input.toString().startsWith("R")) { + output.append(".-. "); + input.delete(0, 1); + } else if (input.toString().startsWith("S")) { + output.append("... "); + input.delete(0, 1); + } else if (input.toString().startsWith("T")) { + output.append("- "); + input.delete(0, 1); + } else if (input.toString().startsWith("U")) { + output.append("..- "); + input.delete(0, 1); + } else if (input.toString().startsWith("V")) { + output.append("...- "); + input.delete(0, 1); + } else if (input.toString().startsWith("W")) { + output.append(".-- "); + input.delete(0, 1); + } else if (input.toString().startsWith("X")) { + output.append("-..- "); + input.delete(0, 1); + } else if (input.toString().startsWith("Y")) { + output.append("-.-- "); + input.delete(0, 1); + } else if (input.toString().startsWith("Z")) { + output.append("--.. "); + input.delete(0, 1); + } else if (input.toString().startsWith("0")) { + output.append("----- "); + input.delete(0, 1); + } else if (input.toString().startsWith("1")) { + output.append(".---- "); + input.delete(0, 1); + } else if (input.toString().startsWith("2")) { + output.append("..--- "); + input.delete(0, 1); + } else if (input.toString().startsWith("3")) { + output.append("...-- "); + input.delete(0, 1); + } else if (input.toString().startsWith("4")) { + output.append("....- "); + input.delete(0, 1); + } else if (input.toString().startsWith("5")) { + output.append("..... "); + input.delete(0, 1); + } else if (input.toString().startsWith("6")) { + output.append("-.... "); + input.delete(0, 1); + } else if (input.toString().startsWith("7")) { + output.append("--... "); + input.delete(0, 1); + } else if (input.toString().startsWith("8")) { + output.append("---.. "); + input.delete(0, 1); + } else if (input.toString().startsWith("9")) { + output.append("----. "); + input.delete(0, 1); + } else if (input.toString().startsWith("Ä")) { + output.append(".-.- "); + input.delete(0, 1); + } else if (input.toString().startsWith("Ö")) { + output.append("---. "); + input.delete(0, 1); + } else if (input.toString().startsWith("Ü")) { + output.append("..-- "); + input.delete(0, 1); + } else if (input.toString().startsWith("ß")) { + output.append("...--... "); + input.delete(0, 1); + } else if (input.toString().startsWith(".")) { + output.append(".-.-.- "); + input.delete(0, 1); + } else if (input.toString().startsWith(",")) { + output.append("--..-- "); + input.delete(0, 1); + } else if (input.toString().startsWith(":")) { + output.append("---... "); + input.delete(0, 1); + } else if (input.toString().startsWith(";")) { + output.append("-.-.-. "); + input.delete(0, 1); + } else if (input.toString().startsWith("?")) { + output.append("..--.. "); + input.delete(0, 1); + } else if (input.toString().startsWith("!")) { + output.append("-.-.-- "); + input.delete(0, 1); + } else if (input.toString().startsWith("-")) { + output.append("-....- "); + input.delete(0, 1); + } else if (input.toString().startsWith("_")) { + output.append("..--.- "); + input.delete(0, 1); + } else if (input.toString().startsWith("(")) { + output.append("-.--. "); + input.delete(0, 1); + } else if (input.toString().startsWith(")")) { + output.append("-.--.- "); + input.delete(0, 1); + } else if (input.toString().startsWith("=")) { + output.append("-...- "); + input.delete(0, 1); + } else if (input.toString().startsWith("+")) { + output.append(".-.-. "); + input.delete(0, 1); + } else if (input.toString().startsWith("/")) { + output.append("-..-. "); + input.delete(0, 1); + } else if (input.toString().startsWith("@")) { + output.append(".--.-. "); + input.delete(0, 1); + } else if (input.toString().startsWith("'")) { + output.append(".----. "); + input.delete(0, 1); + } else if (input.toString().startsWith("$")) { + output.append("...-..- "); + input.delete(0, 1); + } else { + output.replace(0, output.length(), "Code not listed or wrong."); + } + } + if (output.toString().endsWith(" ")) { + output.delete(output.length() - 3, output.length()); + } + } + return output.toString(); + } +} \ No newline at end of file diff --git a/app/src/main/java/de/marcelkapfer/morseconverter/DecodeWrittenMorseManager.java b/app/src/main/java/de/marcelkapfer/morseconverter/DecodeWrittenMorseManager.java new file mode 100644 index 0000000..68d35aa --- /dev/null +++ b/app/src/main/java/de/marcelkapfer/morseconverter/DecodeWrittenMorseManager.java @@ -0,0 +1,240 @@ +package de.marcelkapfer.morseconverter; + +public class DecodeWrittenMorseManager { + + private String inputMessage; + + DecodeWrittenMorseManager(String string) { + inputMessage = string; + } + + public void setString(String string) { + inputMessage = string; + } + + public String getString() { + return inputMessage; + } + + /* + * Code for converting latin letters into written morse + */ + public String getDecodedString() { + StringBuffer message = new StringBuffer(inputMessage); + if (message.toString().endsWith(" ")) { + message = message.deleteCharAt(message.length() - 1); + } + // Variables + StringBuffer input = new StringBuffer(); + input = input.replace(0, input.length(), message.toString().toUpperCase()); + StringBuffer output = new StringBuffer(); + if (input.toString().equals("")) { + output.replace(0, output.length(), "Please enter at least one character"); + } else if (input.toString().equals("LETTERSPACE")) { + output.replace(0, output.length(), "#"); + } else if (input.toString().equals("END OF WORK")) { + output.replace(0, output.length(), "000101"); + } else if (input.toString().equals("ERROR")) { + output.replace(0, output.length(), "00000000"); + } else if (input.toString().equals("STARTING SIGNAL")) { + output.replace(0, output.length(), "10101"); + } else if (input.toString().equals("ENDING SIGNAL")) { + output.replace(0, output.length(), "01010"); + } else if (input.toString().equals("UNDERSTOOD")) { + output.replace(0, output.length(), "00010"); + } else if (input.toString().equals("WAIT")) { + output.replace(0, output.length(), "01000"); + } else if (input.toString().equals("SOS")) { + output.replace(0, output.length(), "000111000"); + } else if (input.toString().equals("LETTER SPACE")) { + output.replace(0, output.length(), "##"); + } else if (input.toString().equals("WORD SPACE")) { + output.replace(0, output.length(), "+"); + } else { + for (int c = input.length(); c > 0; c--) { + + if (input.toString().startsWith(" ")) { + if (output.toString().endsWith("#")) { + output.delete(output.length() - 1, output.length()); + } + output.append("+"); + input.delete(0, 1); + } else if (input.toString().startsWith("A")) { + output.append("01#"); + input.delete(0, 1); + } else if (input.toString().startsWith("B")) { + output.append("1000#"); + input.delete(0, 1); + } else if (input.toString().startsWith("C")) { + output.append("1010#"); + input.delete(0, 1); + } else if (input.toString().startsWith("D")) { + output.append("100#"); + input.delete(0, 1); + } else if (input.toString().startsWith("E")) { + output.append("0#"); + input.delete(0, 1); + } else if (input.toString().startsWith("F")) { + output.append("0010#"); + input.delete(0, 1); + } else if (input.toString().startsWith("G")) { + output.append("110#"); + input.delete(0, 1); + } else if (input.toString().startsWith("H")) { + output.append("0000#"); + input.delete(0, 1); + } else if (input.toString().startsWith("I")) { + output.append("00#"); + input.delete(0, 1); + } else if (input.toString().startsWith("J")) { + output.append("0111#"); + input.delete(0, 1); + } else if (input.toString().startsWith("K")) { + output.append("101#"); + input.delete(0, 1); + } else if (input.toString().startsWith("L")) { + output.append("0100#"); + input.delete(0, 1); + } else if (input.toString().startsWith("M")) { + output.append("11#"); + input.delete(0, 1); + } else if (input.toString().startsWith("N")) { + output.append("10#"); + input.delete(0, 1); + } else if (input.toString().startsWith("O")) { + output.append("111#"); + input.delete(0, 1); + } else if (input.toString().startsWith("P")) { + output.append("0110#"); + input.delete(0, 1); + } else if (input.toString().startsWith("Q")) { + output.append("1101#"); + input.delete(0, 1); + } else if (input.toString().startsWith("R")) { + output.append("010#"); + input.delete(0, 1); + } else if (input.toString().startsWith("S")) { + output.append("000#"); + input.delete(0, 1); + } else if (input.toString().startsWith("T")) { + output.append("1#"); + input.delete(0, 1); + } else if (input.toString().startsWith("U")) { + output.append("001#"); + input.delete(0, 1); + } else if (input.toString().startsWith("V")) { + output.append("0001#"); + input.delete(0, 1); + } else if (input.toString().startsWith("W")) { + output.append("011#"); + input.delete(0, 1); + } else if (input.toString().startsWith("X")) { + output.append("1001#"); + input.delete(0, 1); + } else if (input.toString().startsWith("Y")) { + output.append("1011#"); + input.delete(0, 1); + } else if (input.toString().startsWith("Z")) { + output.append("1100#"); + input.delete(0, 1); + } else if (input.toString().startsWith("0")) { + output.append("11111#"); + input.delete(0, 1); + } else if (input.toString().startsWith("1")) { + output.append("01111#"); + input.delete(0, 1); + } else if (input.toString().startsWith("2")) { + output.append("00111#"); + input.delete(0, 1); + } else if (input.toString().startsWith("3")) { + output.append("00011#"); + input.delete(0, 1); + } else if (input.toString().startsWith("4")) { + output.append("00001#"); + input.delete(0, 1); + } else if (input.toString().startsWith("5")) { + output.append("00000#"); + input.delete(0, 1); + } else if (input.toString().startsWith("6")) { + output.append("10000#"); + input.delete(0, 1); + } else if (input.toString().startsWith("7")) { + output.append("11000#"); + input.delete(0, 1); + } else if (input.toString().startsWith("8")) { + output.append("11100#"); + input.delete(0, 1); + } else if (input.toString().startsWith("9")) { + output.append("11110#"); + input.delete(0, 1); + } else if (input.toString().startsWith("Ä")) { + output.append("0101#"); + input.delete(0, 1); + } else if (input.toString().startsWith("Ö")) { + output.append("1110#"); + input.delete(0, 1); + } else if (input.toString().startsWith("Ü")) { + output.append("0011#"); + input.delete(0, 1); + } else if (input.toString().startsWith("ß")) { + output.append("00011000#"); + input.delete(0, 1); + } else if (input.toString().startsWith(".")) { + output.append("010101#"); + input.delete(0, 1); + } else if (input.toString().startsWith(",")) { + output.append("110011#"); + input.delete(0, 1); + } else if (input.toString().startsWith(":")) { + output.append("111000#"); + input.delete(0, 1); + } else if (input.toString().startsWith(";")) { + output.append("101010#"); + input.delete(0, 1); + } else if (input.toString().startsWith("?")) { + output.append("001100#"); + input.delete(0, 1); + } else if (input.toString().startsWith("!")) { + output.append("101011#"); + input.delete(0, 1); + } else if (input.toString().startsWith("-")) { + output.append("100001#"); + input.delete(0, 1); + } else if (input.toString().startsWith("_")) { + output.append("001101#"); + input.delete(0, 1); + } else if (input.toString().startsWith("(")) { + output.append("10110#"); + input.delete(0, 1); + } else if (input.toString().startsWith(")")) { + output.append("101101#"); + input.delete(0, 1); + } else if (input.toString().startsWith("=")) { + output.append("10001#"); + input.delete(0, 1); + } else if (input.toString().startsWith("+")) { + output.append("01010#"); + input.delete(0, 1); + } else if (input.toString().startsWith("/")) { + output.append("10010#"); + input.delete(0, 1); + } else if (input.toString().startsWith("@")) { + output.append("011010#"); + input.delete(0, 1); + } else if (input.toString().startsWith("'")) { + output.append("011110#"); + input.delete(0, 1); + } else if (input.toString().startsWith("$")) { + output.append("0001001#"); + input.delete(0, 1); + } else { + output.replace(0, output.length(), "Code not listed or wrong."); + } + } + if (output.toString().endsWith("#")) { + output.delete(output.length() - 1, output.length()); + } + } + return output.toString(); + } +} \ No newline at end of file diff --git a/app/src/main/java/de/marcelkapfer/morseconverter/EncodeNormalMorseManager.java b/app/src/main/java/de/marcelkapfer/morseconverter/EncodeNormalMorseManager.java new file mode 100644 index 0000000..a708d8d --- /dev/null +++ b/app/src/main/java/de/marcelkapfer/morseconverter/EncodeNormalMorseManager.java @@ -0,0 +1,186 @@ +package de.marcelkapfer.morseconverter; + +/* + * Code for converting morse code into latin letters + */ +public class EncodeNormalMorseManager { + + private String inputMessage; + + EncodeNormalMorseManager(String string) { + inputMessage = string; + } + + public void setString(String string){ + inputMessage = string; + } + + public String getString(){ + return inputMessage; + } + + + public String getEncodedString() { + // Converts the input string into a StringBuffer + StringBuffer message = new StringBuffer(inputMessage); + // Delete the space at the end which is automatically set through some keyboards + if (message.toString().endsWith(" ")) { + message = message.deleteCharAt(message.length() - 1); + } + // Declaring variables + String input; + StringBuffer output = new StringBuffer(); + input = message.toString() + " "; + StringBuffer inputToSign = new StringBuffer(input); + while (!inputToSign.toString().equals(" ")) { + int d = 0; + boolean signFull = true; + StringBuffer sign = new StringBuffer(); + while (signFull) { + if (inputToSign.toString().startsWith(" ")) { + output.append(" "); + inputToSign.delete(d, d + 7); + } else if (inputToSign.toString().substring(d, d + 3).equals(" ")) { + if (d == 0) { + inputToSign.delete(0, 3); + } else { + sign.replace(0, sign.length(), inputToSign + .toString().substring(0, d)); + inputToSign.delete(0, d); + signFull = false; + } + } else { + d++; + } + } + if (sign.toString().equals(".-")) { + output.append("A"); + } else if (sign.toString().equals("-...")) { + output.append("B"); + } else if (sign.toString().equals("-.-.")) { + output.append("C"); + } else if (sign.toString().equals("-..")) { + output.append("D"); + } else if (sign.toString().equals(".")) { + output.append("E"); + } else if (sign.toString().equals("..-.")) { + output.append("F"); + } else if (sign.toString().equals("--.")) { + output.append("G"); + } else if (sign.toString().equals("....")) { + output.append("H"); + } else if (sign.toString().equals("..")) { + output.append("I"); + } else if (sign.toString().equals(".---")) { + output.append("J"); + } else if (sign.toString().equals("-.-")) { + output.append("K"); + } else if (sign.toString().equals(".-..")) { + output.append("L"); + } else if (sign.toString().equals("--")) { + output.append("M"); + } else if (sign.toString().equals("-.")) { + output.append("N"); + } else if (sign.toString().equals("---")) { + output.append("O"); + } else if (sign.toString().equals(".--.")) { + output.append("P"); + } else if (sign.toString().equals("--.-")) { + output.append("Q"); + } else if (sign.toString().equals(".-.")) { + output.append("R"); + } else if (sign.toString().equals("...")) { + output.append("S"); + } else if (sign.toString().equals("-")) { + output.append("T"); + } else if (sign.toString().equals("..-")) { + output.append("U"); + } else if (sign.toString().equals("...-")) { + output.append("V"); + } else if (sign.toString().equals(".--")) { + output.append("W"); + } else if (sign.toString().equals("-..-")) { + output.append("X"); + } else if (sign.toString().equals("-.--")) { + output.append("Y"); + } else if (sign.toString().equals("--..")) { + output.append("Z"); + } else if (sign.toString().equals("-----")) { + output.append(". (zero)"); + } else if (sign.toString().equals(".----")) { + output.append("-"); + } else if (sign.toString().equals("..---")) { + output.append("2"); + } else if (sign.toString().equals("...--")) { + output.append("3"); + } else if (sign.toString().equals("....-")) { + output.append("4"); + } else if (sign.toString().equals(".....")) { + output.append("5"); + } else if (sign.toString().equals("-....")) { + output.append("6"); + } else if (sign.toString().equals("--...")) { + output.append("7"); + } else if (sign.toString().equals("---..")) { + output.append("8"); + } else if (sign.toString().equals("----.")) { + output.append("9"); + } else if (sign.toString().equals(".-.-")) { + output.append("Ä"); + } else if (sign.toString().equals("---.")) { + output.append("Ö"); + } else if (sign.toString().equals("..--")) { + output.append("Ü"); + } else if (sign.toString().equals("...--...")) { + output.append("ß"); + } else if (sign.toString().equals("----")) { + output.append("CH"); + } else if (sign.toString().equals(".-.-.-")) { + output.append("."); + } else if (sign.toString().equals("--..--")) { + output.append(","); + } else if (sign.toString().equals("---...")) { + output.append(":"); + } else if (sign.toString().equals("-.-.-.")) { + output.append(";"); + } else if (sign.toString().equals("..--..")) { + output.append("?"); + } else if (sign.toString().equals("-.-.--")) { + output.append("!"); + } else if (sign.toString().equals("-....-")) { + output.append("-"); + } else if (sign.toString().equals("..--.-")) { + output.append("_"); + } else if (sign.toString().equals("-.--.")) { + output.append("("); + } else if (sign.toString().equals("-.--.-")) { + output.append(")"); + } else if (sign.toString().equals(".----.")) { + output.append("'"); + } else if (sign.toString().equals("-...-")) { + output.append("="); + } else if (sign.toString().equals(".-.-.")) { + output.append("+ or End of the signal"); + } else if (sign.toString().equals("-..-.")) { + output.append("/"); + } else if (sign.toString().equals(".--.-.")) { + output.append("@"); + } else if (sign.toString().equals("-.-.-")) { + output.append("Begin of the signal"); + } else if (sign.toString().equals("-...-")) { + output.append("Wait"); + } else if (sign.toString().equals("...-.")) { + output.append("Understood"); + } else if (sign.toString().equals("...-.-")) { + output.append("End of work"); + } else if (sign.toString().equals("...---...")) { + output.append("SOS"); + } else if (sign.toString().equals("........")) { + output.append("Error"); + } else { + output.replace(0, output.length(), "Code not listed or wrong."); + } + } + return output.toString(); + } +} \ No newline at end of file diff --git a/app/src/main/java/de/marcelkapfer/morseconverter/EncodeWrittenMorseManager.java b/app/src/main/java/de/marcelkapfer/morseconverter/EncodeWrittenMorseManager.java new file mode 100644 index 0000000..eba4564 --- /dev/null +++ b/app/src/main/java/de/marcelkapfer/morseconverter/EncodeWrittenMorseManager.java @@ -0,0 +1,185 @@ +package de.marcelkapfer.morseconverter; + /* + * Code for converting writtenMorse to latin letters + */ + +public class EncodeWrittenMorseManager { + + private String inputMessage; + + EncodeWrittenMorseManager(String string){ + inputMessage = string; + } + + public String getString(){ + return inputMessage; + } + + public void setString(String string){ + inputMessage = string; + } + + public String getEncodedString() { + StringBuffer message = new StringBuffer(inputMessage); + if (message.toString().endsWith(" ")) { + message = message.deleteCharAt(message.length() - 1); + } + // Variables + String input; + StringBuffer output = new StringBuffer(); + input = message.toString().toUpperCase() + "#"; + StringBuffer inputToSign = new StringBuffer(input); + while (!inputToSign.toString().equals("#")) { + int d = 0; + boolean signFull = true; + StringBuffer sign = new StringBuffer(); + while (signFull) { + if (inputToSign.toString().charAt(d) == '+' + || inputToSign.toString().charAt(d) == '#') { + if (d == 0) { + if (inputToSign.toString().startsWith("+")) { + output.append(" "); + } + inputToSign.deleteCharAt(0); + } else { + sign.replace(0, sign.length(), inputToSign + .toString().substring(0, d)); + inputToSign.delete(0, d); + signFull = false; + } + } else { + d++; + } + } + if (sign.toString().equals("01")) { + output.append("A"); + } else if (sign.toString().equals("1000")) { + output.append("B"); + } else if (sign.toString().equals("1010")) { + output.append("C"); + } else if (sign.toString().equals("100")) { + output.append("D"); + } else if (sign.toString().equals("0")) { + output.append("E"); + } else if (sign.toString().equals("0010")) { + output.append("F"); + } else if (sign.toString().equals("110")) { + output.append("G"); + } else if (sign.toString().equals("0000")) { + output.append("H"); + } else if (sign.toString().equals("00")) { + output.append("I"); + } else if (sign.toString().equals("0111")) { + output.append("J"); + } else if (sign.toString().equals("101")) { + output.append("K"); + } else if (sign.toString().equals("0100")) { + output.append("L"); + } else if (sign.toString().equals("11")) { + output.append("M"); + } else if (sign.toString().equals("10")) { + output.append("N"); + } else if (sign.toString().equals("111")) { + output.append("O"); + } else if (sign.toString().equals("0110")) { + output.append("P"); + } else if (sign.toString().equals("1101")) { + output.append("Q"); + } else if (sign.toString().equals("010")) { + output.append("R"); + } else if (sign.toString().equals("000")) { + output.append("S"); + } else if (sign.toString().equals("1")) { + output.append("T"); + } else if (sign.toString().equals("001")) { + output.append("U"); + } else if (sign.toString().equals("0001")) { + output.append("V"); + } else if (sign.toString().equals("011")) { + output.append("W"); + } else if (sign.toString().equals("1001")) { + output.append("X"); + } else if (sign.toString().equals("1011")) { + output.append("Y"); + } else if (sign.toString().equals("1100")) { + output.append("Z"); + } else if (sign.toString().equals("11111")) { + output.append("0 (zero)"); + } else if (sign.toString().equals("01111")) { + output.append("1"); + } else if (sign.toString().equals("00111")) { + output.append("2"); + } else if (sign.toString().equals("00011")) { + output.append("3"); + } else if (sign.toString().equals("00001")) { + output.append("4"); + } else if (sign.toString().equals("00000")) { + output.append("5"); + } else if (sign.toString().equals("10000")) { + output.append("6"); + } else if (sign.toString().equals("11000")) { + output.append("7"); + } else if (sign.toString().equals("11100")) { + output.append("8"); + } else if (sign.toString().equals("11110")) { + output.append("9"); + } else if (sign.toString().equals("0101")) { + output.append("Ä"); + } else if (sign.toString().equals("1110")) { + output.append("Ö"); + } else if (sign.toString().equals("0011")) { + output.append("Ü"); + } else if (sign.toString().equals("00011000")) { + output.append("ß"); + } else if (sign.toString().equals("1111")) { + output.append("CH"); + } else if (sign.toString().equals("010101")) { + output.append("."); + } else if (sign.toString().equals("110011")) { + output.append(","); + } else if (sign.toString().equals("111000")) { + output.append(":"); + } else if (sign.toString().equals("101010")) { + output.append(";"); + } else if (sign.toString().equals("001100")) { + output.append("?"); + } else if (sign.toString().equals("101011")) { + output.append("!"); + } else if (sign.toString().equals("100001")) { + output.append("-"); + } else if (sign.toString().equals("001101")) { + output.append("_"); + } else if (sign.toString().equals("10110")) { + output.append("("); + } else if (sign.toString().equals("101101")) { + output.append(")"); + } else if (sign.toString().equals("011110")) { + output.append("'"); + } else if (sign.toString().equals("10001")) { + output.append("="); + } else if (sign.toString().equals("01010")) { + output.append("+ or End of the signal"); + } else if (sign.toString().equals("10010")) { + output.append("/"); + } else if (sign.toString().equals("011010")) { + output.append("@"); + } else if (sign.toString().equals("10101")) { + output.append("Begin of the signal"); + } else if (sign.toString().equals("10001")) { + output.append("Wait"); + } else if (sign.toString().equals("00010")) { + output.append("Understood"); + } else if (sign.toString().equals("000101")) { + output.append("End of work"); + } else if (sign.toString().equals("000111000")) { + output.append("SOS"); + } else if (sign.toString().equals("00000000")) { + output.append("Error"); + } else { + output.replace(0, output.length(), "Code not listed or wrong."); + } + } + return output.toString(); + } + +} diff --git a/app/src/main/java/de/marcelkapfer/morseconverter/MainActivity.java b/app/src/main/java/de/marcelkapfer/morseconverter/MainActivity.java index 778b7eb..24d0e30 100644 --- a/app/src/main/java/de/marcelkapfer/morseconverter/MainActivity.java +++ b/app/src/main/java/de/marcelkapfer/morseconverter/MainActivity.java @@ -1,5 +1,8 @@ package de.marcelkapfer.morseconverter; +import android.content.ClipData; +import android.content.ClipboardManager; +import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.net.Uri; @@ -39,43 +42,63 @@ public class MainActivity extends MaterialNavigationDrawer { } public void normalMorseEncode(View view){ - EditText text = (EditText) findViewById(R.id.editTextNormalMorse); - TextView test = (TextView) findViewById(R.id.outputNormalMorse); - CardView cardView = (CardView) findViewById(R.id.cardViewNormalMorseOutput); - if(cardView.getVisibility() == View.INVISIBLE){ - cardView.setVisibility(View.VISIBLE); + try{ + EditText input = (EditText) findViewById(R.id.editTextNormalMorse); + TextView output = (TextView) findViewById(R.id.outputNormalMorse); + CardView cardView = (CardView) findViewById(R.id.cardViewNormalMorseOutput); + if(cardView.getVisibility() == View.INVISIBLE){ + cardView.setVisibility(View.VISIBLE); + } + EncodeNormalMorseManager message = new EncodeNormalMorseManager(input.getText().toString()); + output.setText(message.getEncodedString()); + } catch (Exception e){ + e.printStackTrace(); } - test.setText(text.getText()); } public void normalMorseDecode(View view){ - EditText text = (EditText) findViewById(R.id.editTextNormalMorse); - TextView test = (TextView) findViewById(R.id.outputNormalMorse); - CardView cardView = (CardView) findViewById(R.id.cardViewNormalMorseOutput); - if(cardView.getVisibility() == View.INVISIBLE){ - cardView.setVisibility(View.VISIBLE); + try { + EditText input = (EditText) findViewById(R.id.editTextNormalMorse); + TextView output = (TextView) findViewById(R.id.outputNormalMorse); + CardView cardView = (CardView) findViewById(R.id.cardViewNormalMorseOutput); + if(cardView.getVisibility() == View.INVISIBLE){ + cardView.setVisibility(View.VISIBLE); + } + DecodeNormalMorseManager message = new DecodeNormalMorseManager(input.getText().toString()); + output.setText(message.getDecodedString()); + } catch (Exception e){ + e.printStackTrace(); } - test.setText(text.getText()); } public void writtenMorseEncode(View view){ - EditText text = (EditText) findViewById(R.id.editTextWrittenMorse); - TextView test = (TextView) findViewById(R.id.outputWrittenMorse); - CardView cardView = (CardView) findViewById(R.id.cardViewWrittenMorseOutput); - if(cardView.getVisibility() == View.INVISIBLE){ - cardView.setVisibility(View.VISIBLE); + try { + EditText input = (EditText) findViewById(R.id.editTextWrittenMorse); + TextView output = (TextView) findViewById(R.id.outputWrittenMorse); + CardView cardView = (CardView) findViewById(R.id.cardViewWrittenMorseOutput); + if(cardView.getVisibility() == View.INVISIBLE){ + cardView.setVisibility(View.VISIBLE); + } + EncodeWrittenMorseManager message = new EncodeWrittenMorseManager(input.getText().toString()); + output.setText(message.getEncodedString()); + } catch (Exception e){ + e.printStackTrace(); } - test.setText(text.getText()); } public void writtenMorseDecode(View view){ - EditText text = (EditText) findViewById(R.id.editTextWrittenMorse); - TextView test = (TextView) findViewById(R.id.outputWrittenMorse); - CardView cardView = (CardView) findViewById(R.id.cardViewWrittenMorseOutput); - if(cardView.getVisibility() == View.INVISIBLE){ - cardView.setVisibility(View.VISIBLE); + try { + EditText input = (EditText) findViewById(R.id.editTextWrittenMorse); + TextView output = (TextView) findViewById(R.id.outputWrittenMorse); + CardView cardView = (CardView) findViewById(R.id.cardViewWrittenMorseOutput); + if(cardView.getVisibility() == View.INVISIBLE){ + cardView.setVisibility(View.VISIBLE); + } + DecodeWrittenMorseManager message = new DecodeWrittenMorseManager(input.getText().toString()); + output.setText(message.getDecodedString()); + } catch (Exception e){ + e.printStackTrace(); } - test.setText(text.getText()); } //called when clicking on the version entry in the about fragment @@ -165,4 +188,40 @@ public class MainActivity extends MaterialNavigationDrawer { e.printStackTrace(); } } + + public void shareWrittenMorse(View view){ + TextView message = (TextView) findViewById(R.id.outputWrittenMorse); + share(message.getText().toString()); + } + + public void shareNormalMorse(View view){ + TextView message = (TextView) findViewById(R.id.outputNormalMorse); + share(message.getText().toString()); + } + + public void share(String string){ + Intent intent = getIntent(); + String message = intent.getStringExtra(string); + Intent sendIntent = new Intent(); + sendIntent.setAction(Intent.ACTION_SEND); + sendIntent.putExtra(Intent.EXTRA_TEXT, message); + sendIntent.setType("text/plain"); + startActivity(Intent.createChooser(sendIntent, getResources().getText(R.string.send_to))); + } + + public void copyWrittenMorse(View view){ + TextView message = (TextView) findViewById(R.id.outputWrittenMorse); + copy(message.getText().toString()); + } + + public void copyNormalMorse(View view){ + TextView message = (TextView) findViewById(R.id.outputNormalMorse); + copy(message.getText().toString()); + } + + public void copy(String string){ + ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE); + ClipData clip = ClipData.newPlainText("Message", string); + clipboard.setPrimaryClip(clip); + } } diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml index 10baac2..34b01cb 100644 --- a/app/src/main/res/layout/fragment_main.xml +++ b/app/src/main/res/layout/fragment_main.xml @@ -88,16 +88,28 @@ android:layout_gravity="right" android:baselineAligned="false"> - + android:onClick="writtenMorseDecode" > - + + + + + android:text="@string/button_encode" > + + + + + @@ -132,6 +144,8 @@ android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginBottom="8dp" + android:minHeight="64dp" + android:gravity="center_vertical" android:hint="@string/output_message" android:singleLine="false" android:background="@color/cardview_light_background"/> @@ -152,16 +166,27 @@ android:layout_gravity="right" android:baselineAligned="false"> - + android:onClick="copyNormalMorse" > + + + + + + + + + + - diff --git a/app/src/main/res/layout/fragment_morse.xml b/app/src/main/res/layout/fragment_morse.xml index cb300ad..77e8d74 100644 --- a/app/src/main/res/layout/fragment_morse.xml +++ b/app/src/main/res/layout/fragment_morse.xml @@ -88,16 +88,26 @@ android:layout_gravity="right" android:baselineAligned="false"> - + android:layout_marginRight="8dp"> - + + + + + + + + + @@ -130,6 +140,8 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" + android:minHeight="64dp" + android:gravity="center_vertical" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:layout_marginBottom="8dp" @@ -154,16 +166,27 @@ android:layout_gravity="right" android:baselineAligned="false"> - + android:onClick="copyNormalMorse" > + + + + + + + + + + - diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 3d87643..661bf75 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -37,7 +37,7 @@ @color/ripple_material_light - + + \ No newline at end of file