FM : Request Permissions for recording.

Request Permissions for recording

CRs-Fixed: 2653082
Change-Id: Ica8ab648471f25fc80c64ac24bdd7d6897849af2
diff --git a/fmapp2/AndroidManifest.xml b/fmapp2/AndroidManifest.xml
index a2aaf9f..b82b333 100644
--- a/fmapp2/AndroidManifest.xml
+++ b/fmapp2/AndroidManifest.xml
@@ -29,6 +29,7 @@
 
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.caf.fmradio" >
+    <uses-sdk android:minSdkVersion="28"  android:targetSdkVersion="28"/>
     <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
     <uses-permission android:name="android.permission.BLUETOOTH" />
     <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
@@ -38,6 +39,7 @@
     <uses-permission android:name="android.permission.INTERNET" />
     <uses-permission android:name="android.permission.READ_PHONE_STATE" />
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
+    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.RECORD_AUDIO" />
     <uses-permission android:name="android.permission.CHANGE_COMPONENT_ENABLED_STATE"/>
     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
@@ -46,7 +48,8 @@
     <uses-permission android:name="android.permission.MODIFY_PHONE_STATE" />
     <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
     <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
-    <uses-sdk android:minSdkVersion="28"  android:targetSdkVersion="28"/>
+    <uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
+    <uses-permission android:name="android.permission.CAPTURE_AUDIO_HOTWORD" />
     <application
         android:icon="@drawable/ic_launcher_fmradio"
         android:label="@string/app_name"
diff --git a/fmapp2/src/com/caf/fmradio/FMRadio.java b/fmapp2/src/com/caf/fmradio/FMRadio.java
index 4ee3494..4c9ac20 100644
--- a/fmapp2/src/com/caf/fmradio/FMRadio.java
+++ b/fmapp2/src/com/caf/fmradio/FMRadio.java
@@ -98,6 +98,8 @@
 import android.content.SharedPreferences;
 import android.graphics.Color;
 import android.graphics.Point;
+import android.Manifest;
+import android.content.pm.PackageManager;
 
 public class FMRadio extends Activity
 {
@@ -279,7 +281,39 @@
    private static String mBTsoc;
 
    private BroadcastReceiver mFmSettingReceiver = null;
+   private static String[] RECORD_PERMISSIONS = {
+            Manifest.permission.RECORD_AUDIO,
+            Manifest.permission.CAPTURE_AUDIO_OUTPUT,
+            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE
+    };
+   private static final int ALL_PERMISSIONS = 101;
 
+    @Override
+public void onRequestPermissionsResult(int requestCode, String[] permissions,  int[] grantResults) {
+    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
+    // for PERMISSION_CODE:
+    switch (requestCode){
+        case 101:
+            if(grantResults.length > 0){
+                if(grantResults[0] == PackageManager.PERMISSION_GRANTED){
+                    Log.d(LOGTAG,"  RECORD_AUDIO permission granted");
+                }
+                else{ Log.d(LOGTAG,"  no RECORD_AUDIO permissions"); }
+                if(grantResults[1] == PackageManager.PERMISSION_GRANTED){
+                    Log.d(LOGTAG,"   CAPTURE_AUDIO_OUTPUT permission granted");
+                }
+                else{  Log.d(LOGTAG,"   no CAPTURE_AUDIO_OUTPUT permissions"); }
+                if(grantResults[2] == PackageManager.PERMISSION_GRANTED){
+                    Log.d(LOGTAG,"   WRITE_EXTERNAL_STORAGE permission granted");
+                }
+                else{ Log.d(LOGTAG,"   no WRITE_EXTERNAL_STORAGE permissions"); }
+                if(grantResults[3] == PackageManager.PERMISSION_GRANTED){
+                    Log.d(LOGTAG,"  READ_EXTERNAL_STORAGE permission granted");
+                }
+                else{Log.d(LOGTAG,"  no READ_EXTERNAL_STORAGE permissions"); }
+            }
+    }
+}
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
@@ -290,6 +324,8 @@
       mCommandActive = CMD_NONE;
       mCommandFailed = CMD_NONE;
 
+      Log.d(LOGTAG, "onCreate permssion req");
+      requestPermissions(RECORD_PERMISSIONS, ALL_PERMISSIONS);
       Point p = new Point();
       getWindowManager().getDefaultDisplay().getSize(p);
       mDisplayWidth = p.x;
@@ -1723,6 +1759,8 @@
    private void startRecording() {
       if(mService != null) {
          try {
+             Log.d(LOGTAG, "startRecording permssion req");
+             requestPermissions(RECORD_PERMISSIONS, ALL_PERMISSIONS);
              mRecording = mService.startRecording();
          }catch (RemoteException e) {
              e.printStackTrace();