From 6edd6ae637cca834f5838fbac08b7c1568e0548c Mon Sep 17 00:00:00 2001 From: mmk2410 Date: Mon, 3 Aug 2015 17:15:36 +0200 Subject: [PATCH] Instant converting --- .../morseconverter/MainActivity.java | 9 - .../fragments/MainFragment.java | 89 ++++--- .../fragments/MorseFragment.java | 84 ++++--- .../res/layout-large-land/fragment_main.xml | 204 ++++++--------- .../res/layout-large-land/fragment_morse.xml | 213 ++++++---------- app/src/main/res/layout/fragment_main.xml | 237 ++++++++---------- app/src/main/res/layout/fragment_morse.xml | 237 ++++++++---------- 7 files changed, 465 insertions(+), 608 deletions(-) diff --git a/app/src/main/java/de/marcelkapfer/morseconverter/MainActivity.java b/app/src/main/java/de/marcelkapfer/morseconverter/MainActivity.java index bea338a..786b59a 100644 --- a/app/src/main/java/de/marcelkapfer/morseconverter/MainActivity.java +++ b/app/src/main/java/de/marcelkapfer/morseconverter/MainActivity.java @@ -28,7 +28,6 @@ import android.content.Intent; import android.content.res.Resources; import android.os.Bundle; import android.support.v4.app.FragmentActivity; -import android.util.DisplayMetrics; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.EditText; @@ -94,14 +93,6 @@ public class MainActivity extends MaterialNavigationDrawer implements BillingPro this.disableLearningPattern(); //Doesn't open the drawer always when the app starts setBackPattern(MaterialNavigationDrawer.BACKPATTERN_BACK_TO_FIRST); - // Enables shadow only on devices with landscape. Necessary because of the list views - DisplayMetrics metrics = new DisplayMetrics(); - getWindowManager().getDefaultDisplay().getMetrics(metrics); - - if(metrics.widthPixels < metrics.heightPixels){ - enableToolbarElevation(); - } - mDrawerToggle = new MaterialActionBarDrawerToggle(this, null, null, 0, 0){ public void onDrawerClosed(View view){ diff --git a/app/src/main/java/de/marcelkapfer/morseconverter/fragments/MainFragment.java b/app/src/main/java/de/marcelkapfer/morseconverter/fragments/MainFragment.java index 84050f9..37c8d10 100644 --- a/app/src/main/java/de/marcelkapfer/morseconverter/fragments/MainFragment.java +++ b/app/src/main/java/de/marcelkapfer/morseconverter/fragments/MainFragment.java @@ -26,6 +26,8 @@ import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v7.widget.CardView; +import android.text.Editable; +import android.text.TextWatcher; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -36,8 +38,11 @@ import android.widget.TextView; import de.marcelkapfer.morseconverter.MainActivity; import de.marcelkapfer.morseconverter.R; +import de.marcelkapfer.morseconverter.engine.DecodeNormalMorseManager; import de.marcelkapfer.morseconverter.engine.DecodeWrittenMorseManager; +import de.marcelkapfer.morseconverter.engine.EncodeNormalMorseManager; import de.marcelkapfer.morseconverter.engine.EncodeWrittenMorseManager; +import de.marcelkapfer.morseconverter.intelligentCodeRecognization.NormalMorseCodeRecognization; import de.marcelkapfer.morseconverter.intelligentCodeRecognization.WrittenMorseCodeRecognization; /** @@ -52,15 +57,6 @@ public class MainFragment extends Fragment{ @Override public void onStart() { - LinearLayout writtenMorseConvertButton = (LinearLayout) getActivity().findViewById(R.id.writtenMorseEncodeButton); - - writtenMorseConvertButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - writtenMorseConvert(getActivity()); - } - }); - LinearLayout writtenMorseCopy = (LinearLayout) getActivity().findViewById(R.id.copyWrittenMorse); writtenMorseCopy.setOnClickListener(new View.OnClickListener() { @@ -79,27 +75,61 @@ public class MainFragment extends Fragment{ } }); + EditText input = (EditText) getActivity().findViewById(R.id.editTextWrittenMorse); + + final ConvertThread convertThread = new ConvertThread(getActivity()); + + input.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + + @Override + public void afterTextChanged(Editable s) { + convertThread.run(); + } + }); + super.onStart(); } - public void writtenMorseConvert(FragmentActivity fragmentActivity){ - try { - EditText input = (EditText) fragmentActivity.findViewById(R.id.editTextWrittenMorse); - TextView output = (TextView) fragmentActivity.findViewById(R.id.outputWrittenMorse); - CardView cardView = (CardView) fragmentActivity.findViewById(R.id.cardViewWrittenMorseOutput); - if(cardView.getVisibility() == View.INVISIBLE){ - cardView.setVisibility(View.VISIBLE); - } - String inputString = input.getText().toString(); - if(WrittenMorseCodeRecognization.isCode(inputString)) { - output.setText(EncodeWrittenMorseManager.getEncodedString(inputString)); - } else { - output.setText(DecodeWrittenMorseManager.getDecodedString(inputString)); - } - closeKeyboard(); - } catch (Exception e){ - e.printStackTrace(); + class ConvertThread extends Thread { + + FragmentActivity fragmentActivity; + + ConvertThread(FragmentActivity fA) { + fragmentActivity = fA; } + + public void run() { + writtenMorseConvert(fragmentActivity); + } + + public void writtenMorseConvert(FragmentActivity fragmentActivity) { + try { + EditText input = (EditText) fragmentActivity.findViewById(R.id.editTextWrittenMorse); + TextView output = (TextView) fragmentActivity.findViewById(R.id.outputWrittenMorse); + CardView cardView = (CardView) fragmentActivity.findViewById(R.id.cardViewWrittenMorseOutput); + if (cardView.getVisibility() == View.INVISIBLE) { + cardView.setVisibility(View.VISIBLE); + } + String inputString = input.getText().toString(); + if (WrittenMorseCodeRecognization.isCode(inputString)) { + output.setText(EncodeWrittenMorseManager.getEncodedString(inputString)); + } else { + output.setText(DecodeWrittenMorseManager.getDecodedString(inputString)); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } public void copyWrittenMorse(FragmentActivity fragmentActivity){ @@ -112,11 +142,4 @@ public class MainFragment extends Fragment{ MainActivity.share(message.getText().toString(), fragmentActivity); } - public void closeKeyboard(){ - InputMethodManager imm = (InputMethodManager) getActivity().getSystemService( - Context.INPUT_METHOD_SERVICE); - EditText myEditText = (EditText) getActivity().findViewById(R.id.editTextWrittenMorse); - imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); - - } } diff --git a/app/src/main/java/de/marcelkapfer/morseconverter/fragments/MorseFragment.java b/app/src/main/java/de/marcelkapfer/morseconverter/fragments/MorseFragment.java index 4b6b83c..da675da 100644 --- a/app/src/main/java/de/marcelkapfer/morseconverter/fragments/MorseFragment.java +++ b/app/src/main/java/de/marcelkapfer/morseconverter/fragments/MorseFragment.java @@ -58,15 +58,6 @@ public class MorseFragment extends Fragment { @Override public void onStart() { - LinearLayout normalMorseEncodeButton = (LinearLayout) getActivity().findViewById(R.id.normalMorseEncodeButton); - - normalMorseEncodeButton.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - normalMorseConvert(getActivity()); - } - }); - LinearLayout normalMorseCopy = (LinearLayout) getActivity().findViewById(R.id.copyNormalMorse); normalMorseCopy.setOnClickListener(new View.OnClickListener() { @@ -85,28 +76,61 @@ public class MorseFragment extends Fragment { } }); + EditText input = (EditText) getActivity().findViewById(R.id.editTextNormalMorse); + + final ConvertThread convertThread = new ConvertThread(getActivity()); + + input.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) { + + } + + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) { + + } + + @Override + public void afterTextChanged(Editable s) { + convertThread.run(); + } + }); super.onStart(); } - public void normalMorseConvert(FragmentActivity fragmentActivity) { - try { - EditText input = (EditText) fragmentActivity.findViewById(R.id.editTextNormalMorse); - TextView output = (TextView) fragmentActivity.findViewById(R.id.outputNormalMorse); - CardView cardView = (CardView) fragmentActivity.findViewById(R.id.cardViewNormalMorseOutput); - if (cardView.getVisibility() == View.INVISIBLE) { - cardView.setVisibility(View.VISIBLE); - } - String inputString = input.getText().toString(); - if (NormalMorseCodeRecognization.isCode(inputString)) { - output.setText(EncodeNormalMorseManager.getEncodedString(inputString)); - } else { - output.setText(DecodeNormalMorseManager.getDecodedString(inputString)); - } - closeKeyboard(); - } catch (Exception e) { - e.printStackTrace(); + class ConvertThread extends Thread { + + FragmentActivity fragmentActivity; + + ConvertThread(FragmentActivity fA) { + fragmentActivity = fA; } + + public void run() { + normalMorseConvert(fragmentActivity); + } + + public void normalMorseConvert(FragmentActivity fragmentActivity) { + try { + EditText input = (EditText) fragmentActivity.findViewById(R.id.editTextNormalMorse); + TextView output = (TextView) fragmentActivity.findViewById(R.id.outputNormalMorse); + CardView cardView = (CardView) fragmentActivity.findViewById(R.id.cardViewNormalMorseOutput); + if (cardView.getVisibility() == View.INVISIBLE) { + cardView.setVisibility(View.VISIBLE); + } + String inputString = input.getText().toString(); + if (NormalMorseCodeRecognization.isCode(inputString)) { + output.setText(EncodeNormalMorseManager.getEncodedString(inputString)); + } else { + output.setText(DecodeNormalMorseManager.getDecodedString(inputString)); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + } public void copyNormalMorse(FragmentActivity fragmentActivity) { @@ -118,12 +142,4 @@ public class MorseFragment extends Fragment { TextView message = (TextView) fragmentActivity.findViewById(R.id.outputNormalMorse); MainActivity.share(message.getText().toString(), fragmentActivity); } - - public void closeKeyboard() { - InputMethodManager imm = (InputMethodManager) getActivity().getSystemService( - Context.INPUT_METHOD_SERVICE); - EditText myEditText = (EditText) getActivity().findViewById(R.id.editTextNormalMorse); - imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); - - } } \ No newline at end of file diff --git a/app/src/main/res/layout-large-land/fragment_main.xml b/app/src/main/res/layout-large-land/fragment_main.xml index 5063f52..ceb4bf9 100644 --- a/app/src/main/res/layout-large-land/fragment_main.xml +++ b/app/src/main/res/layout-large-land/fragment_main.xml @@ -1,5 +1,4 @@ - -