Skip to content
This repository has been archived by the owner on Nov 19, 2022. It is now read-only.

Commit

Permalink
Stable version. Map displays location, Measure view records the raw d…
Browse files Browse the repository at this point in the history
…B value, no weighting yet
  • Loading branch information
gworkman committed Dec 3, 2017
1 parent f2d19b4 commit 959c42d
Show file tree
Hide file tree
Showing 6 changed files with 196 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,6 @@ dependencies {
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.google.android.gms:play-services-maps:11.6.2'
implementation 'com.google.android.gms:play-services-location:11.6.2'
implementation 'com.android.support:design:27.0.1'
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package edu.osu.sphs.soundmap.activities;

import android.animation.ArgbEvaluator;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

Expand Down
69 changes: 55 additions & 14 deletions app/src/main/java/edu/osu/sphs/soundmap/fragments/MapFragment.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
package edu.osu.sphs.soundmap.fragments;

import android.content.Context;
import android.net.Uri;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.tasks.OnSuccessListener;

import edu.osu.sphs.soundmap.R;
import edu.osu.sphs.soundmap.util.Values;

/**
* A simple {@link Fragment} subclass.
* Use the {@link MapFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class MapFragment extends Fragment {
public class MapFragment extends Fragment implements OnMapReadyCallback, OnSuccessListener<Location> {

private MapView map;
private MapView mapView;
private FusedLocationProviderClient locationProviderClient;
private GoogleMap googleMap;

public MapFragment() {
// Required empty public constructor
Expand Down Expand Up @@ -55,15 +65,46 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
map = view.findViewById(R.id.map_view);
map.onCreate(savedInstanceState);
map.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap googleMap) {
// do nothing!

map.onResume();
}
});
mapView = view.findViewById(R.id.map_view);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
locationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
}

@Override
public void onMapReady(GoogleMap googleMap) {
this.googleMap = googleMap;
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
googleMap.setMyLocationEnabled(true);
locationProviderClient.getLastLocation().addOnSuccessListener(this);
mapView.onResume();
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION}, Values.LOCATION_REQUEST_CODE);

mapView.onResume();
}
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case Values.LOCATION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mapView.getMapAsync(this);
}
}
}

@Override
public void onSuccess(Location location) {
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13.0f));
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,24 @@
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Locale;

import edu.osu.sphs.soundmap.R;
import edu.osu.sphs.soundmap.util.MeasureTask;

/**
* A simple {@link Fragment} subclass.
* Use the {@link MeasureFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class MeasureFragment extends Fragment implements View.OnClickListener {
public class MeasureFragment extends Fragment implements View.OnClickListener, MeasureTask.OnUpdateCallback {

private TextView timer;
private TextView dB;
private FloatingActionButton fab;
private boolean isRunning = false;
private CountDownTimer chronometer;
private MeasureTask measureTask;

public MeasureFragment() {
// Required empty public constructor
Expand Down Expand Up @@ -55,11 +60,17 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
timer = view.findViewById(R.id.timer);
dB = view.findViewById(R.id.dB);
}

// This is the onClick method for the fab in MainActivity
@Override
public void onClick(View v) {
if (measureTask == null) {
measureTask = new MeasureTask();
measureTask.setCallback(this);
}

if (!isRunning) {
fab = (FloatingActionButton) v;
chronometer = new CountDownTimer(30000, 100) {
Expand All @@ -74,16 +85,27 @@ public void onTick(long millisUntilFinished) {
@Override
public void onFinish() {
fab.setImageResource(R.drawable.ic_record);
isRunning = false;
measureTask = null;
}
}.start();
fab.setImageResource(R.drawable.ic_stop);
isRunning = true;
measureTask.execute(getContext().getFilesDir().getPath() + "/temp");
} else {
chronometer.cancel();
String timerResetValue = "30.0 seconds";
timer.setText(timerResetValue);
fab.setImageResource(R.drawable.ic_record);
isRunning = false;
measureTask.cancel(true);
measureTask = null;
}
}

@Override
public void onUpdate(double dB) {
String text = String.format(Locale.US, "%.02f", dB) + " dB";
this.dB.setText(text);
}
}
101 changes: 101 additions & 0 deletions app/src/main/java/edu/osu/sphs/soundmap/util/MeasureTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package edu.osu.sphs.soundmap.util;

import android.media.MediaRecorder;
import android.os.AsyncTask;

import java.io.IOException;

import static java.lang.Math.log10;

/**
* Created by Gus on 12/3/2017. A tool to get the dB value in the background asynchronously
*/

public class MeasureTask extends AsyncTask<String, Double, Double> {

private double average;
private MediaRecorder recorder;
private OnUpdateCallback callback;

public MeasureTask setCallback(OnUpdateCallback callback) {
this.callback = callback;
return this;
}

public void removeCallback() {
this.callback = null;
}

@Override
protected Double doInBackground(String... files) {
if (files.length > 0) {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_WB);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB);
recorder.setOutputFile(files[0]);

try {
recorder.prepare();
recorder.start();
} catch (IOException e) {
e.printStackTrace();
}

long endTime = System.currentTimeMillis() + 30000;
int count = 1;

while (System.currentTimeMillis() < endTime) {
double amplitude = recorder.getMaxAmplitude();
publishProgress(amplitude);

try {
Thread.sleep(333);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

recorder.stop();
recorder.reset();
recorder.release();
recorder = null;

} else {
new Error("Recording file path output parameters must be greater than one").printStackTrace();
}

return average;
}

@Override
protected void onPostExecute(Double d) {
if (recorder != null) {
recorder.stop();
recorder.reset();
recorder.reset();
recorder = null;
}
}

@Override
protected void onProgressUpdate(Double... values) {
if (callback != null) {
callback.onUpdate(20 * log10(values[0] / 1.0));
}
}

@Override
protected void onCancelled() {
if (recorder != null) {
recorder.stop();
recorder.reset();
recorder.reset();
recorder = null;
}
}

public interface OnUpdateCallback {
void onUpdate(double averageDB);
}
}
15 changes: 15 additions & 0 deletions app/src/main/java/edu/osu/sphs/soundmap/util/Values.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package edu.osu.sphs.soundmap.util;

/**
* Created by Gus on 12/2/2017.
*/

public class Values {

/**
* Permission request codes
*/
public static final int LOCATION_REQUEST_CODE = 11;
public static final int AUDIO_REQUEST_CODE = 12;

}

0 comments on commit 959c42d

Please sign in to comment.