Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

package com.realwear.hmt1developerexamples;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaPlayer;
Expand All @@ -24,6 +26,10 @@
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
Expand Down Expand Up @@ -91,6 +97,15 @@ protected void onCreate(Bundle savedInstanceState) {
mRate16Button = (RadioButton) findViewById(R.id.rate16Button);
mRate44Button = (RadioButton) findViewById(R.id.rate44Button);
mRate48Button = (RadioButton) findViewById(R.id.rate48Button);

// Check that permissions are granted before continuing
if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED ||
ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
}
}

/**
Expand Down Expand Up @@ -489,4 +504,17 @@ private static byte[] intToByteArray(int data) {
private static byte[] shortToByteArray(short data) {
return new byte[]{(byte) (data & 0xff), (byte) ((data >> 8) & 0xff)};
}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
setResult(-1);
Toast.makeText(this, R.string.permissions_error, Toast.LENGTH_LONG).show();
finish();
}
}
}
}
3 changes: 3 additions & 0 deletions hmt1developerexamples/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,7 @@
<string name="show_help_hint">Say \"Show Help\" to see available commands</string>
<string name="speak_now">Speak Now</string>
<string name="bnf_help">Please say the time <p></p><p></p> Example: One Forty Five</string>

<!-- Resources for Audio Capture Activity -->
<string name="permissions_error">This activity cannot launch without required permissions.</string>
</resources>