From 7322a3e6925663318e56d83876674c01e0479bcd Mon Sep 17 00:00:00 2001 From: n8fr8 Date: Mon, 26 Jun 2017 01:58:09 -0400 Subject: [PATCH] improve microphone sound threshold configuratoin UI/UX --- build.gradle | 8 +- src/main/AndroidManifest.xml | 10 +- .../phoneypot/SettingsActivity.java | 20 ++- .../phoneypot/sensors/MicrophoneMonitor.java | 28 ++-- .../ui/MicrophoneConfigureActivity.java | 143 ++++++++++++++++++ .../layout/activity_microphone_configure.xml | 66 ++++++++ src/main/res/layout/activity_settings.xml | 27 +++- 7 files changed, 266 insertions(+), 36 deletions(-) create mode 100644 src/main/java/info/guardianproject/phoneypot/ui/MicrophoneConfigureActivity.java create mode 100644 src/main/res/layout/activity_microphone_configure.xml diff --git a/build.gradle b/build.gradle index 24d18333..e0902b64 100644 --- a/build.gradle +++ b/build.gradle @@ -46,6 +46,8 @@ android { } dependencies { + + compile 'com.android.support:support-v4:25.3.0' compile 'com.github.satyan:sugar:1.5' compile 'com.android.support:appcompat-v7:25.3.0' @@ -55,5 +57,7 @@ dependencies { compile 'net.the4thdimension:audio-wife:1.0.3' compile 'com.github.paolorotolo:appintro:4.1.0' compile 'info.guardianproject.netcipher:netcipher:2.0.0-alpha1' - - compile 'com.nanohttpd:nanohttpd-webserver:2.2.0'} + compile 'com.nanohttpd:nanohttpd-webserver:2.2.0' + compile 'com.android.support.constraint:constraint-layout:1.0.2' + compile 'me.angrybyte.picker:picker:1.3.1' +} diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index 202a13f1..fbaf6fb8 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -39,11 +39,8 @@ - - + android:windowSoftInputMode="stateHidden" /> + + android:theme="@style/AppTheme" /> + \ No newline at end of file diff --git a/src/main/java/info/guardianproject/phoneypot/SettingsActivity.java b/src/main/java/info/guardianproject/phoneypot/SettingsActivity.java index 3c33ba63..b22e70e6 100644 --- a/src/main/java/info/guardianproject/phoneypot/SettingsActivity.java +++ b/src/main/java/info/guardianproject/phoneypot/SettingsActivity.java @@ -24,7 +24,7 @@ import android.util.Log; import android.view.Menu; import android.view.MenuItem; -import android.view.View; +import android.view.View import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.EditText; @@ -34,10 +34,12 @@ import android.widget.Spinner; import android.widget.Toast; -import org.w3c.dom.Text; +import org.w3c.dom.Text import info.guardianproject.netcipher.proxy.OrbotHelper; import info.guardianproject.phoneypot.service.WebServer; +import info.guardianproject.phoneypot.ui.MicrophoneConfigureActivity; +import me.angrybyte.numberpicker.view.ActualNumberPicker; public class SettingsActivity extends AppCompatActivity { @@ -70,12 +72,12 @@ public void onCreate(Bundle savedInstanceState) { final EditText phoneNumber = (EditText) - this.findViewById(R.id.phone_number); + this.findViewByd(R.id.phone_number); final EditText remoteAccessOnion = (EditText) this.findViewById(R.id.remote_access_onion); - final NumberPicker timerDelay = (NumberPicker) + final ActualNumberPicker timerDelay = (ActualNumberPicker) this.findViewById(R.id.timer_delay); smsCheck.setOnCheckedChangeListener(new OnCheckedChangeListener() { @@ -125,6 +127,13 @@ else if (camera.equals(PreferenceManager.OFF)) timerDelay.setMinValue(0); timerDelay.setValue(preferences.getTimerDelay()); + findViewById(R.id.action_configure_mic).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + startActivity(new Intent(SettingsActivity.this, MicrophoneConfigureActivity.class)); + } + }); + askForPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE, 1); } @@ -167,11 +176,10 @@ private void askForPermission(String permission, Integer requestCode) { private void save () { - EditText phoneNumber = (EditText) this.findViewById(R.id.phone_number); - NumberPicker timerDelay = (NumberPicker) + ActualNumberPicker timerDelay = (ActualNumberPicker) this.findViewById(R.id.timer_delay); CheckBox smsCheck = (CheckBox) this.findViewById(R.id.sms_check); diff --git a/src/main/java/info/guardianproject/phoneypot/sensors/MicrophoneMonitor.java b/src/main/java/info/guardianproject/phoneypot/sensors/MicrophoneMonitor.java index 6630887b..5ca1aa5b 100644 --- a/src/main/java/info/guardianproject/phoneypot/sensors/MicrophoneMonitor.java +++ b/src/main/java/info/guardianproject/phoneypot/sensors/MicrophoneMonitor.java @@ -35,13 +35,7 @@ public final class MicrophoneMonitor implements MicSamplerTask.MicListener { /** * Threshold for the decibels sampled */ - private double NOISE_THRESHOLD = 40.0; - - /** - * Baseline noise floor - * - */ - private double mBaseLine = -1; + private double mNoiseThreshold = 70.0; /** * Messenger used to communicate with alert service @@ -73,9 +67,17 @@ public MicrophoneMonitor(Context context) prefs = new PreferenceManager(context); if (prefs.getMicrophoneSensitivity().equals("High")) { - NOISE_THRESHOLD = 10.0; + mNoiseThreshold = 40; } else if (prefs.getMicrophoneSensitivity().equals("Medium")) { - NOISE_THRESHOLD = 20.0; + mNoiseThreshold = 60; + } + else + { + try { + //maybe it is a threshold value? + mNoiseThreshold = Double.parseDouble(prefs.getMicrophoneSensitivity()); + } + catch (Exception e){} } context.bindService(new Intent(context, @@ -126,13 +128,7 @@ public void onSignalReceived(short[] signal) { averageDB = 20 * Math.log10(Math.abs(average) / 1); } - if (mBaseLine == -1) - { - mBaseLine = averageDB; - Log.d("MicrophoneMonitor","Baseline is: " + mBaseLine); - } - - if ((averageDB-mBaseLine) > NOISE_THRESHOLD) { + if (averageDB > mNoiseThreshold) { if (!MicrophoneTaskFactory.isRecording()) { try { diff --git a/src/main/java/info/guardianproject/phoneypot/ui/MicrophoneConfigureActivity.java b/src/main/java/info/guardianproject/phoneypot/ui/MicrophoneConfigureActivity.java new file mode 100644 index 00000000..43702aa2 --- /dev/null +++ b/src/main/java/info/guardianproject/phoneypot/ui/MicrophoneConfigureActivity.java @@ -0,0 +1,143 @@ +package info.guardianproject.phoneypot.ui; + +import android.os.Message; +import android.os.RemoteException; +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.support.v7.widget.Toolbar; +import android.util.Log; +import android.view.Menu; +import android.view.MenuItem; +import android.widget.NumberPicker; +import android.widget.TextView; +import android.widget.Toast; + +import info.guardianproject.phoneypot.PreferenceManager; +import info.guardianproject.phoneypot.R; +import info.guardianproject.phoneypot.model.EventTrigger; +import info.guardianproject.phoneypot.sensors.media.AudioRecorderTask; +import info.guardianproject.phoneypot.sensors.media.MicSamplerTask; +import info.guardianproject.phoneypot.sensors.media.MicrophoneTaskFactory; +import me.angrybyte.numberpicker.view.ActualNumberPicker; + +import static info.guardianproject.phoneypot.R.id.microphone; + +public class MicrophoneConfigureActivity extends AppCompatActivity implements MicSamplerTask.MicListener { + + private MicSamplerTask microphone; + private TextView mTextLevel; + private ActualNumberPicker mNumberTrigger; + private PreferenceManager mPrefManager; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_microphone_configure); + + Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar); + setSupportActionBar(toolbar); + + setTitle("Sound Sensitivity"); + + mTextLevel = (TextView)findViewById(R.id.text_display_level); + mNumberTrigger = (ActualNumberPicker)findViewById(R.id.number_trigger_level); + + mNumberTrigger.setMinValue(0); + mNumberTrigger.setMaxValue(120); + + mPrefManager = new PreferenceManager(this.getApplicationContext()); + + if (mPrefManager.getMicrophoneSensitivity().equals("High")) { + mNumberTrigger.setValue(40); + } else if (mPrefManager.getMicrophoneSensitivity().equals("Medium")) { + mNumberTrigger.setValue(60); + } + else + { + try { + //maybe it is a threshold value? + mNumberTrigger.setValue(Integer.parseInt(mPrefManager.getMicrophoneSensitivity())); + } + catch (Exception e){} + } + + startMic(); + } + + private void startMic () + { + try { + microphone = MicrophoneTaskFactory.makeSampler(this); + microphone.setMicListener(this); + microphone.execute(); + } catch (MicrophoneTaskFactory.RecordLimitExceeded e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + @Override + protected void onDestroy() { + super.onDestroy(); + + if (microphone != null) + microphone.cancel(true); + + mPrefManager.setMicrophoneSensitivity(mNumberTrigger.getValue()+""); + } + + @Override + public void onSignalReceived(short[] signal) { + /* + * We do and average of the 512 samples + */ + int total = 0; + int count = 0; + for (short peak : signal) { + //Log.i("MicrophoneFragment", "Sampled values are: "+peak); + if (peak != 0) { + total += Math.abs(peak); + count++; + } + } + // Log.i("MicrophoneFragment", "Total value: " + total); + int average = 0; + if (count > 0) average = total / count; + /* + * We compute a value in decibels + */ + double averageDB = 0.0; + if (average != 0) { + averageDB = 20 * Math.log10(Math.abs(average) / 1); + } + + mTextLevel.setText(((int)averageDB)+""); + + if (averageDB > mNumberTrigger.getValue()) + { + Toast.makeText(this,"Sound Threshold Crossed!",Toast.LENGTH_SHORT).show(); + } + } + + @Override + public void onMicError() { + + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.monitor_start, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected (MenuItem item) { + switch (item.getItemId()){ + case R.id.menu_save: + finish(); + break; + } + return true; + } +} diff --git a/src/main/res/layout/activity_microphone_configure.xml b/src/main/res/layout/activity_microphone_configure.xml new file mode 100644 index 00000000..b9229740 --- /dev/null +++ b/src/main/res/layout/activity_microphone_configure.xml @@ -0,0 +1,66 @@ + + + + app:popupTheme="@style/AppTheme.PopupOverlay" /> + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/res/layout/activity_settings.xml b/src/main/res/layout/activity_settings.xml index ee157c00..ca4c1ed6 100644 --- a/src/main/res/layout/activity_settings.xml +++ b/src/main/res/layout/activity_settings.xml @@ -85,6 +85,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/action_configure" + android:id="@+id/action_configure_movement" /> @@ -108,6 +109,8 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/action_configure" + + android:id="@+id/action_configure_mic" /> @@ -124,12 +127,24 @@ android:layout_width="200dp" android:layout_height="wrap_content" /> - - +