Skip to content

Commit

Permalink
ask for permission at runtime if needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
quaap committed Nov 19, 2016
1 parent ea7ec39 commit 4ba161a
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 7 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ The app offers a few ways to calculate the audio level to display:

Future Features?
* Make meter fit a variety of screen sizes.
* Allow a log of the readings
* Allow a log of the readings.
* Fancier display.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.quaap.audiometer"
android:versionCode="0"
android:versionName="0.1">
android:versionCode="1"
android:versionName="1.0">

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
Expand Down
49 changes: 45 additions & 4 deletions app/src/main/java/com/quaap/audiometer/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,16 @@
*/


import android.Manifest;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;

import android.view.View;
Expand All @@ -34,6 +39,7 @@
import android.widget.SeekBar;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;
import java.util.concurrent.atomic.AtomicInteger;

Expand Down Expand Up @@ -87,7 +93,9 @@ public void onStopTrackingTouch(SeekBar seekBar) {
@Override
public void onClick(View view) {
if (onoff.isChecked()) {
startit();
if (!startit()) {
onoff.setChecked(false);
}
} else {
stopit();
}
Expand Down Expand Up @@ -123,9 +131,38 @@ public void onNothingSelected(AdapterView<?> adapterView) {
levelType.setSelection(levelTypeAdapter.getPosition(levM));

levelMethodChanged((MicLevelReader.LevelMethod)levelType.getSelectedItem());
checkMicrophoneAccess();
}


private boolean checkMicrophoneAccess() {
if (ContextCompat.checkSelfPermission(this,Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECORD_AUDIO},
REQUEST_RECORD_AUDIO);
return false;
}
return true;
}

private static final int REQUEST_RECORD_AUDIO = 121;

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_RECORD_AUDIO: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {

Toast.makeText(this, "Yay!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "This application will not function without access to the microphone", Toast.LENGTH_LONG).show();
}
}
}
}

private void levelMethodChanged(MicLevelReader.LevelMethod levelMethod) {
mMicLevelReader.setLevelMethod(levelMethod);
mMeterView.setmMeterMax(mMicLevelReader.getMaxLevel());
Expand Down Expand Up @@ -174,9 +211,13 @@ public void handleMessage(Message msg) {
};


public void startit() {
mRecorderThread = new Thread(mMicLevelReader, "AudioListener Thread");
mRecorderThread.start();
public boolean startit() {
if (checkMicrophoneAccess()) {
mRecorderThread = new Thread(mMicLevelReader, "AudioListener Thread");
mRecorderThread.start();
return true;
}
return false;
}


Expand Down
Binary file modified app/src/main/res/drawable/audiometer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed app/src/main/res/mipmap-hdpi/ic_launcher.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-mdpi/ic_launcher.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-xhdpi/ic_launcher.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-xxhdpi/ic_launcher.png
Binary file not shown.
Binary file removed app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
Binary file not shown.

0 comments on commit 4ba161a

Please # to comment.