From 5286f176c1946facf54601301854b225e25af04d Mon Sep 17 00:00:00 2001 From: Ahmet Inan Date: Sat, 11 Jul 2015 15:47:10 +0200 Subject: [PATCH] catch decoder init errors and prepare to send email --- app/src/main/java/xdsopl/robot36/Decoder.java | 4 +- .../java/xdsopl/robot36/MainActivity.java | 54 ++++++++++++++++--- app/src/main/res/menu/menu_main.xml | 27 +++++----- app/src/main/res/values-de/strings.xml | 4 ++ app/src/main/res/values/strings.xml | 6 +++ 5 files changed, 75 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/xdsopl/robot36/Decoder.java b/app/src/main/java/xdsopl/robot36/Decoder.java index c16f1d3..7b97e2c 100644 --- a/app/src/main/java/xdsopl/robot36/Decoder.java +++ b/app/src/main/java/xdsopl/robot36/Decoder.java @@ -101,7 +101,7 @@ public class Decoder { } }; - public Decoder(MainActivity activity, SpectrumView spectrum, SpectrumView spectrogram, ImageView image, VUMeterView meter) { + public Decoder(MainActivity activity, SpectrumView spectrum, SpectrumView spectrogram, ImageView image, VUMeterView meter) throws Exception { this.image = image; this.spectrogram = spectrogram; this.spectrum = spectrum; @@ -112,6 +112,8 @@ public class Decoder { spectrogramBuffer = new int[spectrogram.bitmap.getWidth() * spectrogram.bitmap.getHeight()]; int bufferSizeInBytes = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat); + if (bufferSizeInBytes <= 0) + throw new Exception("Unable to open audio with " + sampleRate + "Hz samplerate"); int bufferSizeInSamples = bufferSizeInBytes / 2; int framesPerSecond = Math.max(1, sampleRate / bufferSizeInSamples); audioBuffer = new short[framesPerSecond * bufferSizeInSamples]; diff --git a/app/src/main/java/xdsopl/robot36/MainActivity.java b/app/src/main/java/xdsopl/robot36/MainActivity.java index 928ff65..53da4bc 100644 --- a/app/src/main/java/xdsopl/robot36/MainActivity.java +++ b/app/src/main/java/xdsopl/robot36/MainActivity.java @@ -18,11 +18,13 @@ limitations under the License. package xdsopl.robot36; import android.app.Activity; +import android.app.AlertDialog; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.ContentResolver; import android.content.ContentValues; import android.content.Context; +import android.content.DialogInterface; import android.content.Intent; import android.content.res.Configuration; import android.graphics.Bitmap; @@ -143,15 +145,51 @@ public class MainActivity extends Activity { menu.setGroupEnabled(R.id.group_decoder, false); } + private Intent createEmailIntent(final String subject, final String text) { + Intent intent = new Intent(Intent.ACTION_SEND); + intent.setType("text/email"); + intent.putExtra(Intent.EXTRA_EMAIL, new String[]{ getString(R.string.email_address) }); + intent.putExtra(Intent.EXTRA_SUBJECT, subject); + intent.putExtra(Intent.EXTRA_TEXT, text); + return intent; + } + + private void showErrorMessage(final String title, final String shortText, final String longText) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle(title); + builder.setMessage(shortText); + builder.setNeutralButton(getString(R.string.btn_ok), null); + builder.setPositiveButton(getString(R.string.btn_send_email), new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + Intent intent = createEmailIntent(getString(R.string.email_subject), longText); + startActivity(Intent.createChooser(intent, getString(R.string.chooser_title))); + } + }); + builder.show(); + } + + private String createMessage(Exception ex) { + String message = ex.getMessage() + "\n"; + for (StackTraceElement el : ex.getStackTrace()) + message += "\n" + el.toString(); + return message; + } + protected void startDecoder() { if (decoder != null) return; - decoder = new Decoder(this, - (SpectrumView)findViewById(R.id.spectrum), - (SpectrumView)findViewById(R.id.spectrogram), - (ImageView)findViewById(R.id.image), - (VUMeterView)findViewById(R.id.meter) - ); + try { + decoder = new Decoder(this, + (SpectrumView) findViewById(R.id.spectrum), + (SpectrumView) findViewById(R.id.spectrogram), + (ImageView) findViewById(R.id.image), + (VUMeterView) findViewById(R.id.meter) + ); + } catch (Exception e) { + showErrorMessage(getString(R.string.decoder_error), e.getMessage(), createMessage(e)); + return; + } decoder.enable_analyzer(enableAnalyzer); showNotification(); if (menu != null) { @@ -191,6 +229,10 @@ public class MainActivity extends Activity { public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); this.menu = menu; + if (decoder != null) { + menu.findItem(R.id.action_toggle_decoder).setIcon(getResources().getDrawable(android.R.drawable.ic_media_pause)); + menu.setGroupEnabled(R.id.group_decoder, true); + } share = (ShareActionProvider)menu.findItem(R.id.menu_item_share).getActionProvider(); return true; } diff --git a/app/src/main/res/menu/menu_main.xml b/app/src/main/res/menu/menu_main.xml index a418e20..a5fba37 100644 --- a/app/src/main/res/menu/menu_main.xml +++ b/app/src/main/res/menu/menu_main.xml @@ -2,25 +2,26 @@ xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> - - - - + + + + + Martin Modi Scottie Modi Wraase Modi + Ok + Email senden + Fehlermeldung senden: + Dekodierer Fehler diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ef43100..30e796a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -50,4 +50,10 @@ Martin Modes Scottie Modes Wraase Modes + OK + Send Email + [BUG] Robot36 - SSTV Image Decoder + Send Bug Report: + xdsopl@googlemail.com + Decoder error