Skip to content

armgit / haven

Public Imported from GitHub
0

Haven is for people who need a way to protect their personal spaces and possessions without compromising their own privacy, through an Android app and on-device sensors

Updated graphical volume picker. Fixed running background sound-sampling when stopped application.

Luca Bonato7f3e129f147b
diff --git a/src/me/ziccard/secureit/MicrophoneVolumePicker.java b/src/me/ziccard/secureit/MicrophoneVolumePicker.java
index bc2a731..fc315ff 100644
--- a/src/me/ziccard/secureit/MicrophoneVolumePicker.java
+++ b/src/me/ziccard/secureit/MicrophoneVolumePicker.java
@@ -5,376 +5,180 @@ import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
-import android.os.Bundle;
-import android.util.AttributeSet;
import android.util.Log;
-import android.view.Display;
import android.view.View;
public class MicrophoneVolumePicker extends View {
- Paint paint = new Paint();
-
- final int GREEN = 8453888;
- final int ORANGE = 16744448;
- final int RED = 14549506;
-
- private boolean _isMono = false;
-
- //margini dal bordo
- final int PADDING_TOP = 30;
- final int PADDING_LEFT = 30;
- final int PADDING_RIGHT = 30;
- final int PADDING_BOTTOM = 30;
-
-
- // DRAWING AREAS
- private int _canvasWidth; //in android canvas.getWidth()
- private int _canvasHeight; //in android canvas.getHeight()
-
- //area disponibile per il disegno
- private int _drawableAreaHeight;
- private int _drawableAreaWidth;
-
-
- // noise & cliping value
- final int FULL_SCALE = 120;
- final int NOISE_TRESHOLD = 50;
- final int CLIPPING_TRESHOLD = 70;
-
- // ogetto canvas
- // private var _canvas:Shape;
-
-
- // fattore di decellerazione
- final double FACTOR = 0.05;
-
- // valori visualizzati
- private double _microfoneLeftValue;
- private double _microfoneRightValue;
-
- //read values
- private double microfoneReadLeftValue;
- private double microfoneReadRightValue;
-
- public MicrophoneVolumePicker(Context context) {
- super(context);
- }
-
- /**
- * Sets the read decibels values (for stereo)
- */
- public void setValues(double leftValue, double rightValue) {
- microfoneReadLeftValue = leftValue;
- microfoneReadRightValue = rightValue;
- }
-
- @Override
- public void onDraw(Canvas canvas) {
-
- Log.i("MicrophoneVolumePicker","Creating view");
-
- _canvasWidth = canvas.getWidth();
- _canvasHeight = canvas.getHeight();
-
- Log.i("MicrophoneVolumePicker","CanvasWidth: "+_canvasWidth + " CanvasHeight: "+_canvasHeight);
-
- _drawableAreaHeight = _canvasHeight-PADDING_TOP-PADDING_BOTTOM;
- _drawableAreaWidth = _canvasWidth-PADDING_LEFT-PADDING_RIGHT;
-
- if(_isMono) _microfoneLeftValue = _microfoneRightValue;
-
- // DECELLAROZIONE PER SMOOTHING DEI DATI IN INGRESSO
- _microfoneLeftValue =
- ((microfoneReadLeftValue < _microfoneLeftValue) ?
- //SE VALORE RILEVATO E' MINORE APPLICA DECELLERAZIONE
- ((microfoneReadLeftValue * FACTOR) + (_microfoneLeftValue * (1 - FACTOR))) :
- // ALTRIMENTI VAI AL VALORE APPENA RILEVATO
- microfoneReadLeftValue);
- _microfoneRightValue =
- ((microfoneReadRightValue<_microfoneRightValue) ?
- ((microfoneReadRightValue * FACTOR) + (_microfoneRightValue * (1 - FACTOR))):
- microfoneReadRightValue);
-
- //QUI EVENTUALI PROCEDURE DI QUANTIZZAZIONE E NORMALIZZAZIONE NELL'INTERVALLO 0-120 del segnale
-
- //DRAW LEFT CHANNEL
- // TODO: riscrivere gli if sotto forma di bound in Math.min e Math.max
- int originX = PADDING_LEFT+_drawableAreaWidth/2 - 50;
- int originY;
- if(_microfoneLeftValue > CLIPPING_TRESHOLD){
- //Se è maggiore della soglia di clipping
- paint.setColor(Color.RED);
- // ATTENZIONE BASATO SU ASSI X,Y = (0,0) CENTRATI IN ALTO A SINISTRA. POTREBBE RICHIEDERE l'inversione
- // dell'asse Y in accordo con l'origine in android
- originY = PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE-CLIPPING_TRESHOLD);
-
- canvas.drawRect(new Rect(originX,
- originY,
- originX + 40,
- // 40 l'effettiva larghezza del rettangolo
- originY -(int)((_microfoneLeftValue-CLIPPING_TRESHOLD)*(_drawableAreaHeight/FULL_SCALE))),
- paint);
+ Paint paint = new Paint();
- }
- if(_microfoneLeftValue > NOISE_TRESHOLD){
- //Se è maggiore della soglia di noise
- // TODO: cambiare sto colore giallo in un arancio decente!
- paint.setColor(Color.YELLOW); // in android paint.setColor(Color.YELLOW);
- originY = PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE-NOISE_TRESHOLD);
-
-
- canvas.drawRect(new Rect(originX,
- originY,
- originX+40,
- originY-Math.min((int)(CLIPPING_TRESHOLD-NOISE_TRESHOLD),(int)(_microfoneLeftValue-NOISE_TRESHOLD))
- *(_drawableAreaHeight/FULL_SCALE)),
- paint);
- }
+ final int GREEN = 8453888; // 80FF00
+ final int ORANGE = 16744448; // FF8000
+ final int RED = 14549506; // DE0202
- // sicuramente ha una parte di volume inferiore a noise
- paint.setColor(Color.GREEN);
- originY = PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE);
+ private boolean _isMono = false;
+
+ // margini dal bordo
+ final int PADDING_TOP = 30;
+ final int PADDING_LEFT = 30;
+ final int PADDING_RIGHT = 30;
+ final int PADDING_BOTTOM = 30;
+
+ // DRAWING AREAS
+ private int _canvasWidth; // in android canvas.getWidth()
+ private int _canvasHeight; // in android canvas.getHeight()
+
+ // area disponibile per il disegno
+ private int _drawableAreaHeight;
+ private int _drawableAreaWidth;
+
+ // noise & cliping value
+ final int FULL_SCALE = 120;
+ int NOISE_THRESHOLD = 50;
+ final int CLIPPING_THRESHOLD = 70;
- canvas.drawRect(new Rect(originX,
- originY,
- originX+40,
- originY-Math.min((int)NOISE_TRESHOLD,(int)(_microfoneLeftValue))*(_drawableAreaHeight/FULL_SCALE)),
- paint);
+ // ogetto canvas
+ // private var _canvas:Shape;
+ // fattore di decellerazione
+ final double FACTOR = 0.05;
+
+ // valori visualizzati
+ private double _microfoneLeftValue;
+ private double _microfoneRightValue;
+
+ // read values
+ private double microfoneReadLeftValue;
+ private double microfoneReadRightValue;
+
+ public MicrophoneVolumePicker(Context context) {
+ super(context);
+ this.paint.setTextSize(19f);
+ }
+
+ public void setNoiseThreshold(double noiseThreshold) {
+ this.NOISE_THRESHOLD = (int)noiseThreshold;
+ }
+
+ /**
+ * Sets the read decibels values (for stereo)
+ */
+ public void setValues(double leftValue, double rightValue) {
+ microfoneReadLeftValue = leftValue;
+ microfoneReadRightValue = rightValue;
+ }
+
+ @Override
+ public void onDraw(Canvas canvas) {
+
+ Log.i("MicrophoneVolumePicker", "Creating view");
+
+ _canvasWidth = canvas.getWidth();
+ _canvasHeight = canvas.getHeight();
+
+ Log.i("MicrophoneVolumePicker", "CanvasWidth: " + _canvasWidth
+ + " CanvasHeight: " + _canvasHeight);
+
+ _drawableAreaHeight = _canvasHeight - PADDING_TOP - PADDING_BOTTOM;
+ _drawableAreaWidth = _canvasWidth - PADDING_LEFT - PADDING_RIGHT;
+
+ if (_isMono)
+ _microfoneLeftValue = _microfoneRightValue;
+
+ _microfoneLeftValue = microfoneReadLeftValue;
+ _microfoneRightValue = microfoneReadRightValue;
+
+ // DECELLAROZIONE PER SMOOTHING DEI DATI IN INGRESSO
+ _microfoneLeftValue = ((microfoneReadLeftValue < _microfoneLeftValue) ?
+ // SE VALORE RILEVATO E' MINORE APPLICA DECELLERAZIONE
+ ((microfoneReadLeftValue * FACTOR) + (_microfoneLeftValue * (1 - FACTOR)))
+ :
+ // ALTRIMENTI VAI AL VALORE APPENA RILEVATO
+ microfoneReadLeftValue);
+ _microfoneRightValue = ((microfoneReadRightValue < _microfoneRightValue) ? ((microfoneReadRightValue * FACTOR) + (_microfoneRightValue * (1 - FACTOR)))
+ : microfoneReadRightValue);
+
+ // QUI EVENTUALI PROCEDURE DI QUANTIZZAZIONE E NORMALIZZAZIONE
+ // NELL'INTERVALLO 0-120 del segnale
+
+ int colomnWidth = 40;
+ // DRAW LEFT CHANNEL
+ int originX = PADDING_LEFT + _drawableAreaWidth / 2 - 50;
+ drawVolumeRect(canvas, originX, colomnWidth, _microfoneLeftValue);
// DRAWING RIGHT CHANNEL
- originX = PADDING_LEFT+_drawableAreaWidth/2 + 50;
- if(_microfoneRightValue > CLIPPING_TRESHOLD){
- //Se è maggiore della soglia di clipping
- paint.setColor(Color.RED);
- // ATTENZIONE BASATO SU ASSI X,Y = (0,0) CENTRATI IN ALTO A SINISTRA. POTREBBE RICHIEDERE l'inversione
- // dell'asse Y in accordo con l'origine in android
- originY = PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE-CLIPPING_TRESHOLD);
-
- canvas.drawRect(new Rect(originX,
- originY,
- originX - 40,
- // 40 l'effettiva larghezza del rettangolo
- originY - (int)((_microfoneRightValue-CLIPPING_TRESHOLD)*(_drawableAreaHeight/FULL_SCALE))),
- paint);
+ originX = PADDING_LEFT + _drawableAreaWidth / 2 + 50;
+ drawVolumeRect(canvas, originX, colomnWidth, _microfoneRightValue);
+
+ // draw plot
+ drawPlot(canvas, colomnWidth);
- }
- if(_microfoneRightValue > NOISE_TRESHOLD){
- //Se è maggiore della soglia di noise
- // TODO: cambiare sto colore giallo in un arancio decente!
- paint.setColor(Color.YELLOW); // in android paint.setColor(Color.YELLOW);
- originY = PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE-NOISE_TRESHOLD);
-
-
- canvas.drawRect(new Rect(originX,
- originY,
- originX-40,
- originY-Math.min((int)(CLIPPING_TRESHOLD-NOISE_TRESHOLD),(int)(_microfoneRightValue-NOISE_TRESHOLD))
- *(_drawableAreaHeight/FULL_SCALE)),
- paint);
- }
+ }
- // sicuramente ha una parte di volume inferiore a noise
- paint.setColor(Color.GREEN);
- originY = PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE);
+ private void drawVolumeRect(Canvas canvas,
+ int originX,
+ int width,
+ double value) {
+
+ double oneDbHeight = _drawableAreaHeight/((double)FULL_SCALE);
- canvas.drawRect(new Rect(originX,
- originY,
- originX-40,
- originY-Math.min((int)NOISE_TRESHOLD,(int)(_microfoneRightValue))*(_drawableAreaHeight/FULL_SCALE)),
- paint);
+ int top = 0;
+ int bottom = 0;
+ // draws red rectangle: when sampled audio is greater than clipping threshold
+ if (value > CLIPPING_THRESHOLD) {
+ paint.setColor(Color.RED);
+ // determines the starting y-point for red area
+ bottom = PADDING_TOP + ((int)(oneDbHeight*(FULL_SCALE - CLIPPING_THRESHOLD)));
+ // determines the ending y-point for red area
+ top = PADDING_TOP + ((int)(oneDbHeight*(FULL_SCALE - value)));
- }
+ canvas.drawRect(new Rect(originX, top, originX + width, bottom), paint);
-}
-
-
-/*
-
-// microfono registra in mono
- private var _isMono:Boolean = false;
-
- //margini dal bordo
- private const PADDING_TOP:int = 30;
- private const PADDING_LEFT:int = 30;
- private const PADDING_RIGHT:int = 30;
- private const PADDING_BOTTOM:int = 30;
-
-
- // DRAWING AREAS
-
-
- private var _canvasWidth:int; //in android canvas.getWidth()
- private var _canvasHeight:int; //in android canvas.getHeight()
-
- //area disponibile per il disegno
- private var _drawableAreaHeight:int;
- private var _drawableAreaWidth:int;
-
-
- // noise & cliping value
- private const FULL_SCALE:int = 120;
- private const NOISE_TRESHOLD:int = 70;
- private const CLIPPING_TRESHOLD:int = 100;
-
- // ogetto canvas
- private var _canvas:Shape;
-
-
- // fattore di decellerazione
- private const FACTOR:Number = 0.05;
-
- // valori visualizzati
- private var _microfoneLeftValue:int;
- private var _microfoneRightValue:int;
-
- public function DocumentClass() {
- _canvas = new Shape();
- addChild(_canvas);
- // ispirato http://www.healthyhearing.com/uploads/images/new/dB%20Volume%20Meter.jpg
-
- // inizializzo variabile per il disegno
- _canvasWidth = 320; //in android canvas.getWidth()
- _canvasHeight = 480; //in android canvas.getHeight()
-
- //area disponibile per il disegno
- _drawableAreaHeight = _canvasHeight-PADDING_TOP-PADDING_BOTTOM;
- _drawableAreaWidth = _canvasWidth-PADDING_LEFT-PADDING_RIGHT;
-
-
-
-
- // DISEGNA LE LABEL CON I VALORI
- drawLabel();
- this.addEventListener(Event.ENTER_FRAME, onDraw);
- }
-
- private function drawLabel():void{
-
- var canvas:Shape = _canvas; // oggetto fittizio per similitudine
- for(var i:int = 0; i<=FULL_SCALE/10;i++){
- var labelTextField:TextField = new TextField();
- labelTextField.width = 30;
- var myFormat:TextFormat = new TextFormat();
- myFormat.align = TextFormatAlign.CENTER;
- labelTextField.x = PADDING_LEFT+_drawableAreaWidth/2 - 90;
- labelTextField.y = PADDING_TOP+(_drawableAreaHeight/FULL_SCALE)*(10*i)-8;
- labelTextField.defaultTextFormat = myFormat;
- canvas.stage.addChild(labelTextField);
- labelTextField.text = ""+(FULL_SCALE/10-i)*10;
-
- // RIGHT LABEL
- labelTextField = new TextField();
- labelTextField.width = 30;
- labelTextField.defaultTextFormat = myFormat;
- labelTextField.text = ""+((FULL_SCALE/10)-i)*10;
-
- labelTextField.x = PADDING_LEFT+_drawableAreaWidth/2 + 55;
- labelTextField.y = PADDING_TOP+(_drawableAreaHeight/FULL_SCALE)*(10*i)-8;
- canvas.stage.addChild(labelTextField);
- }
-
- }
- protected function onDraw(e:Event):void{
- // giusto per similudine facciamo finta che la canvas
- // passata nell'onDraw di Android sia questa
- var canvas:Shape = _canvas;
-
- // valori in input
- var microfoneReadLeftValue:int = FULL_SCALE*Math.random();
- var microfoneReadRightValue:int = FULL_SCALE*Math.random();
- if(_isMono) _microfoneLeftValue = _microfoneRightValue;
-
-
- _microfoneLeftValue = (microfoneReadLeftValue<_microfoneLeftValue) ? // SE VALORE RILEVATO E' MINORE
- // APPLICA DECELLERAZIONE
- (microfoneReadLeftValue * FACTOR) + (_microfoneLeftValue * (1 - FACTOR)) :
- // ALTRIMENTI VAI AL VALORE APPENA RILEVATO
- microfoneReadLeftValue;
- _microfoneRightValue = (microfoneReadRightValue<_microfoneRightValue) ?
- (microfoneReadRightValue * FACTOR) + (_microfoneRightValue * (1 - FACTOR)):
- microfoneReadRightValue;
-
-
- //QUI EVENTUALI PROCEDURE DI QUANTIZZAZIONE E NORMALIZZAZIONE NELL'INTERVALLO 0-120 del segnale
-
-
- //SMOOTHING DEI VALORI
-
-
- canvas.graphics.clear() //in android non dovrebbe essere necessario questo comando
-
-
- if(_microfoneLeftValue > CLIPPING_TRESHOLD){
- //Se è maggiore della soglia di clipping
- canvas.graphics.beginFill(0xff0000); // in android paint.setColor(Color.RED);
- canvas.graphics.drawRect(PADDING_LEFT+_drawableAreaWidth/2 - 50,
- PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE-CLIPPING_TRESHOLD),
- 40,
- -((_microfoneLeftValue-CLIPPING_TRESHOLD)*(_drawableAreaHeight/FULL_SCALE)));
- }
- if(_microfoneLeftValue > NOISE_TRESHOLD){
- //Se è maggiore della soglia di noise
- canvas.graphics.beginFill(0xff7700); // in android paint.setColor(Color.YELLOW);
- canvas.graphics.drawRect(PADDING_LEFT+_drawableAreaWidth/2 - 50,
- PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE-NOISE_TRESHOLD),
- 40,
- -Math.min((CLIPPING_TRESHOLD-NOISE_TRESHOLD),(_microfoneLeftValue-NOISE_TRESHOLD))
- *(_drawableAreaHeight/FULL_SCALE));
- }
- // sicuramente ha una parte di volume inferiore a noise
- canvas.graphics.beginFill(0x22ff22); // in android paint.setColor(Color.GREEN);
- canvas.graphics.drawRect(PADDING_LEFT+_drawableAreaWidth/2 - 50,
- PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE), // semanticamente corretto, tie!
- 40,
- -Math.min(NOISE_TRESHOLD,(_microfoneLeftValue))*(_drawableAreaHeight/FULL_SCALE));
-
-
- // CANALE DESTRO
-
- if(_microfoneRightValue > CLIPPING_TRESHOLD){
- //Se è maggiore della soglia di clipping
- canvas.graphics.beginFill(0xff0000); // in android paint.setColor(Color.RED);
- canvas.graphics.drawRect(PADDING_LEFT+_drawableAreaWidth/2 + 50,
- PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE-CLIPPING_TRESHOLD),
- -40,
- -((_microfoneRightValue-CLIPPING_TRESHOLD)*(_drawableAreaHeight/FULL_SCALE)));
- }
- if(_microfoneRightValue > NOISE_TRESHOLD){
- //Se è maggiore della soglia di noise
- canvas.graphics.beginFill(0xff7700); // in android paint.setColor(Color.YELLOW);
- canvas.graphics.drawRect(PADDING_LEFT+_drawableAreaWidth/2 + 50,
- PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE-NOISE_TRESHOLD),
- -40,
- -Math.min((CLIPPING_TRESHOLD-NOISE_TRESHOLD),(_microfoneRightValue-NOISE_TRESHOLD))
- *(_drawableAreaHeight/FULL_SCALE));
- }
- // sicuramente ha una parte di volume inferiore a noise
- canvas.graphics.beginFill(0x22ff22); // in android paint.setColor(Color.GREEN);
- canvas.graphics.drawRect(PADDING_LEFT+_drawableAreaWidth/2 + 50,
- PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(FULL_SCALE), // semanticamente corretto, tie!
- -40,
- -Math.min(NOISE_TRESHOLD,(_microfoneRightValue))*(_drawableAreaHeight/FULL_SCALE));
-
-
-
- // DISEGNA UN PO' DI COSE SOPRA. NON SONO SICURO DELL'EQUIVALENTE IN ANDROID.
- // ANDREBBE IN OGNI CASO CHIAMATA UNA SOLA VOLTA E NON AD OGNI onDRAW COME FACCIO QUI
- drawInfos();
- }
-
- private function drawInfos():void{
- var canvas:Shape = _canvas; // oggetto fittizio per similitudine
- for(var i:int = 0; i<=FULL_SCALE/10;i++){
- canvas.graphics.lineStyle(1,0x777777);
- canvas.graphics.moveTo(PADDING_LEFT+_drawableAreaWidth/2 - 60,
- PADDING_TOP+(_drawableAreaHeight/FULL_SCALE)*(10*i));
- canvas.graphics.lineTo(PADDING_LEFT+_drawableAreaWidth/2 + 60,
- PADDING_TOP+_drawableAreaHeight/FULL_SCALE*(10*i));
-
- }
- }
+ }
+ // draws orange rectangle: when sampled audio is greater than noise threshold
+ if (value > NOISE_THRESHOLD) {
+ paint.setColor(Color.rgb(0xFF, 0x80, 0x00));
+ // determines the starting y-point for orange area
+ bottom = PADDING_TOP + ((int)(oneDbHeight*(FULL_SCALE - NOISE_THRESHOLD)));
+ // determines the ending y-point for orange area:
+ // if the sampled value is lower than clipping threshold then the value is the
+ // ending point otherwise the starting point of the red area is the ending point
+ top = PADDING_TOP + ((int)(oneDbHeight*(FULL_SCALE - Math.min(value, CLIPPING_THRESHOLD))));
+
+ canvas.drawRect(new Rect(originX, top, originX + width, bottom), paint);
}
+ // draws green rectangle: there must be something under noise threshold
+ paint.setColor(Color.GREEN);
+ bottom = PADDING_TOP + ((int)(oneDbHeight*FULL_SCALE));
+ top = PADDING_TOP + ((int)(oneDbHeight*(FULL_SCALE - Math.min(value, NOISE_THRESHOLD))));
+
+ canvas.drawRect(new Rect(originX, top, originX + width, bottom), paint);
+
+ }
+
+ private void drawPlot(Canvas canvas, int width) {
+ float step = 10*(_drawableAreaHeight/((float)FULL_SCALE));
+ float lineLevel = PADDING_TOP + _drawableAreaHeight;
+ float left = PADDING_LEFT + _drawableAreaWidth/2 - 60;
+ float right = PADDING_LEFT + _drawableAreaWidth/2 + 100;
+ for (int i=0; i< FULL_SCALE/10 + 1; i++) {
+ paint.setColor(Color.BLACK);
+ if (i*10 == NOISE_THRESHOLD || i*10 == CLIPPING_THRESHOLD) {
+ paint.setStrokeWidth(3.0f);
+ canvas.drawLine(left, lineLevel, right, lineLevel, paint);
+ paint.setStrokeWidth(0);
+ } else {
+ canvas.drawLine(left, lineLevel, right, lineLevel, paint);
+ }
+ if (i%2 == 0) {
+ paint.setTextAlign(Paint.Align.RIGHT);
+ canvas.drawText(Integer.toString(i*10), left - 10, lineLevel, paint);
+ } else {
+ paint.setTextAlign(Paint.Align.LEFT);
+ canvas.drawText(Integer.toString(i*10), right + 10, lineLevel, paint);
+ }
+ lineLevel = lineLevel - step;
+ }
+ }
-*/
\ No newline at end of file
+}
\ No newline at end of file
diff --git a/src/me/ziccard/secureit/fragment/MicrophoneFragment.java b/src/me/ziccard/secureit/fragment/MicrophoneFragment.java
index 74c2308..4eb28d1 100644
--- a/src/me/ziccard/secureit/fragment/MicrophoneFragment.java
+++ b/src/me/ziccard/secureit/fragment/MicrophoneFragment.java
@@ -14,24 +14,19 @@ import android.util.Log;
import me.ziccard.secureit.MicrophoneVolumePicker;
import me.ziccard.secureit.R;
import me.ziccard.secureit.SecureItPreferences;
-import me.ziccard.secureit.VolumeDynamicSeries;
import me.ziccard.secureit.async.MicSamplerTask;
import me.ziccard.secureit.service.BluetoothService;
-import com.androidplot.xy.*;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
-import android.widget.Toast;
public final class MicrophoneFragment extends Fragment implements MicSamplerTask.MicListener {
private MicSamplerTask microphone;
-
- //private XYPlot plot;
private TextView microphoneText;
@@ -48,7 +43,7 @@ public final class MicrophoneFragment extends Fragment implements MicSamplerTask
/**
* Threshold for the decibels sampled
*/
- private double NOYSE_THRESHOLD = 60.0;
+ private double NOISE_THRESHOLD = 60.0;
/**
* Messenger used to communicate with alert service
@@ -69,9 +64,6 @@ public final class MicrophoneFragment extends Fragment implements MicSamplerTask
serviceMessenger = null;
}
};
-
-
- //private VolumeDynamicSeries series = new VolumeDynamicSeries(0, "Volume");
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
@@ -86,9 +78,9 @@ public final class MicrophoneFragment extends Fragment implements MicSamplerTask
prefs = new SecureItPreferences(getActivity());
if (prefs.getMicrophoneSensitivity().equals("High")) {
- NOYSE_THRESHOLD = 30.0;
+ NOISE_THRESHOLD = 30.0;
} else if (prefs.getMicrophoneSensitivity().equals("Medium")) {
- NOYSE_THRESHOLD = 40.0;
+ NOISE_THRESHOLD = 40.0;
}
getActivity().bindService(new Intent(getActivity(),
@@ -106,51 +98,20 @@ public final class MicrophoneFragment extends Fragment implements MicSamplerTask
microphoneText = (TextView) getActivity().findViewById(R.id.microphone);
-// // initialize our XYPlot reference:
-// plot = (XYPlot) getActivity().findViewById(R.id.mySimpleXYPlot);
-//
-// // Define formatting and add series
-// LineAndPointFormatter f1 = new LineAndPointFormatter(Color.rgb(0, 0, 200), null, Color.rgb(0, 0, 80));
-// plot.addSeries(series, f1);
-//
-// // apply some transparency to the series
-// f1.getFillPaint().setAlpha(50);
-//
-// plot.setDomainStepMode(XYStepMode.SUBDIVIDE);
-// plot.setDomainStepValue(1);
-//
-// // thin out domain/range tick labels so they dont overlap each other:
-// //plot.setTicksPerDomainLabel(5);
-// //plot.setTicksPerRangeLabel(3);
-//
-// // freeze the range boundaries:
-// plot.setRangeBoundaries(0, 120, BoundaryMode.FIXED);
-// //plot.setTicksPerRangeLabel(10000);
-// //mySimpleXYPlot.setTicksPerDomainLabel(5);
-//
-// // customize our domain/range labels
-// plot.setDomainLabel("");
-// plot.setRangeLabel("");
-// plot.getLegendWidget().setVisible(false);
-//
-// // get rid of decimal points in our domain labels:
-// plot.setDomainValueFormat(new DecimalFormat("0"));
-// //plot.setRangeValueFormat(new DecimalFormat("0"));
-//
-// // by default, AndroidPlot displays developer guides to aid in laying out your plot.
-// // To get rid of them call disableAllMarkup():
-// plot.disableAllMarkup();
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout layout = (LinearLayout) getActivity().findViewById(R.id.linear_layout);
picker = new MicrophoneVolumePicker(this.getActivity());
+ picker.setNoiseThreshold(NOISE_THRESHOLD);
layout.addView(picker, params);
-
- microphone = new MicSamplerTask();
- microphone.setMicListener(this);
- microphone.execute();
+
+ if (microphone == null) {
+ microphone = new MicSamplerTask();
+ microphone.setMicListener(this);
+ microphone.execute();
+ }
}
@Override
@@ -192,7 +153,7 @@ public final class MicrophoneFragment extends Fragment implements MicSamplerTask
picker.setValues(averageDB, averageDB);
picker.invalidate();
- if (averageDB > NOYSE_THRESHOLD) {
+ if (averageDB > NOISE_THRESHOLD) {
Message message = new Message();
message.what = BluetoothService.MICROPHONE_MESSAGE;
try {