Skip to content

Commit

Permalink
improve microphone sound threshold configuratoin UI/UX
Browse files Browse the repository at this point in the history
  • Loading branch information
n8fr8 committed Jun 26, 2017
1 parent bbd6ec8 commit 7322a3e
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 36 deletions.
8 changes: 6 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
}
10 changes: 4 additions & 6 deletions src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,8 @@
<activity
android:name=".SettingsActivity"
android:label="@string/title_activity_start"
android:windowSoftInputMode="stateHidden"></activity>
<activity
android:name=".ui.PPAppIntro"
/>

android:windowSoftInputMode="stateHidden" />
<activity android:name=".ui.PPAppIntro" />
<activity
android:name=".MonitorActivity"
android:label="@string/app_name"
Expand All @@ -68,7 +65,8 @@
<activity
android:name=".ui.EventActivity"
android:label="@string/title_activity_event"
android:theme="@style/AppTheme"></activity>
android:theme="@style/AppTheme" />
<activity android:name=".ui.MicrophoneConfigureActivity"></activity>
</application>

</manifest>
20 changes: 14 additions & 6 deletions src/main/java/info/guardianproject/phoneypot/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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);

}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
66 changes: 66 additions & 0 deletions src/main/res/layout/activity_microphone_configure.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay"/>
app:popupTheme="@style/AppTheme.PopupOverlay" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Current Audio Level"
/>
<TextView
android:id="@+id/text_display_level"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="80dp"
android:text="0"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Configure Trigger Level"
android:layout_marginTop="15dp"
/>

<me.angrybyte.numberpicker.view.ActualNumberPicker
android:id="@+id/number_trigger_level"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="3dp"
android:background="#FFFFFFFF"
app:bar_color="@android:color/darker_gray"
app:bar_width="1dp"
app:draw_over_controls="true"
app:max_value="100"
app:min_value="0"
app:show_text="true"
app:show_bars="true"
app:show_controls="false"
app:show_fast_controls="false"
app:text_color="@android:color/darker_gray"
app:text_size="16sp" />

</LinearLayout>
</ScrollView>
</LinearLayout>
Loading

0 comments on commit 7322a3e

Please sign in to comment.