| /* |
| * Copyright 2021 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.mobileer.oboetester; |
| |
| import android.Manifest; |
| import android.app.Activity; |
| import android.content.Intent; |
| import android.content.pm.PackageManager; |
| import android.support.annotation.NonNull; |
| import android.support.v4.app.ActivityCompat; |
| import android.widget.Toast; |
| |
| /** |
| * Support requesting RECORD_AUDIO permission. |
| */ |
| |
| public abstract class BaseOboeTesterActivity extends Activity |
| implements ActivityCompat.OnRequestPermissionsResultCallback { |
| |
| private static final int MY_PERMISSIONS_REQUEST_RECORD_AUDIO = 938355; |
| private Class mTestClass; |
| |
| protected void showErrorToast(String message) { |
| showToast("Error: " + message); |
| } |
| |
| protected void showToast(final String message) { |
| runOnUiThread(new Runnable() { |
| @Override |
| public void run() { |
| Toast.makeText(BaseOboeTesterActivity.this, |
| message, |
| Toast.LENGTH_SHORT).show(); |
| } |
| }); |
| } |
| |
| private boolean isRecordPermissionGranted() { |
| return (ActivityCompat.checkSelfPermission(this, |
| Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED); |
| } |
| |
| private void requestRecordPermission(){ |
| ActivityCompat.requestPermissions( |
| this, |
| new String[]{Manifest.permission.RECORD_AUDIO}, |
| MY_PERMISSIONS_REQUEST_RECORD_AUDIO); |
| } |
| |
| @Override |
| public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, |
| @NonNull int[] grantResults) { |
| |
| if (MY_PERMISSIONS_REQUEST_RECORD_AUDIO != requestCode) { |
| super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| return; |
| } |
| |
| if (grantResults.length != 1 || |
| grantResults[0] != PackageManager.PERMISSION_GRANTED) { |
| |
| Toast.makeText(getApplicationContext(), |
| getString(R.string.need_record_audio_permission), |
| Toast.LENGTH_SHORT) |
| .show(); |
| } else { |
| // Permission was granted |
| beginTestThatRequiresRecording(); |
| } |
| } |
| |
| /** |
| * If needed, request recording permission before running test. |
| */ |
| protected void launchTestThatDoesRecording(Class clazz) { |
| mTestClass = clazz; |
| if (isRecordPermissionGranted()) { |
| beginTestThatRequiresRecording(); |
| } else { |
| requestRecordPermission(); |
| } |
| } |
| |
| protected void launchTestActivity(Class clazz) { |
| Intent intent = new Intent(this, clazz); |
| startActivity(intent); |
| } |
| |
| private void beginTestThatRequiresRecording() { |
| launchTestActivity(mTestClass); |
| } |
| } |