Music: Update SoundRecorder to SDK23

Cherry pick the one commit from the upstream CAF branch LA.BF.1.1.3
(revision rb1.15) that allows the AOSP sound recorder app to be
compatible with runtime permissions.

FPIIM-653

Original commit message:

update SoundRecorder with new permission api in SDK23.

CRs-Fixed: 970233

Conflicts:
	src/com/android/soundrecorder/SoundRecorder.java

Change-Id: I0c54929b3b4e279a835df7014ecf182f6c7a215c
diff --git a/AndroidManifest.xml b/AndroidManifest.xml
index bd4a75d..629c7d3 100644
--- a/AndroidManifest.xml
+++ b/AndroidManifest.xml
@@ -21,7 +21,7 @@
 
     <uses-sdk
         android:minSdkVersion="21"
-        android:targetSdkVersion="21" />
+        android:targetSdkVersion="23" />
 
     <uses-permission android:name="android.permission.RECORD_AUDIO" />
     <uses-permission android:name="android.permission.INTERNET" />
diff --git a/src/com/android/soundrecorder/SoundRecorder.java b/src/com/android/soundrecorder/SoundRecorder.java
index 69edf1d..d22b481 100644
--- a/src/com/android/soundrecorder/SoundRecorder.java
+++ b/src/com/android/soundrecorder/SoundRecorder.java
@@ -18,8 +18,11 @@
 
 import java.io.File;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.List;
 
+import android.Manifest;
 import android.app.Activity;
 import android.app.AlertDialog;
 import android.content.ContentResolver;
@@ -29,6 +32,9 @@
 import android.content.DialogInterface;
 import android.content.IntentFilter;
 import android.content.BroadcastReceiver;
+import android.content.SharedPreferences;
+import android.content.SharedPreferences.Editor;
+import android.content.pm.PackageManager;
 import android.content.res.Configuration;
 import android.content.res.Resources;
 import android.database.Cursor;
@@ -36,6 +42,7 @@
 import android.media.MediaRecorder;
 import android.media.AudioManager.OnAudioFocusChangeListener;
 import android.net.Uri;
+import android.os.Build;
 import android.os.Bundle;
 import android.os.Environment;
 import android.os.Handler;
@@ -144,6 +151,9 @@
            we won't run out of disk. */
         
         // at mBlocksChangedTime we had this much time
+        if (mBytesPerSecond == 0) {
+           mBytesPerSecond = 1;
+        }
         long result = mLastBlocks*blockSize/mBytesPerSecond;
         // so now we have this much time
         result -= (now - mBlocksChangedTime)/1000;
@@ -475,14 +485,65 @@
         }
     };
 
+    private boolean checkOperationPermission(String[] permissionName, int operationHandle) {
+        boolean isPermissionGranted = true;
+        List needRequestPermission = new ArrayList<String>();
+        for (String tmp : permissionName) {
+            isPermissionGranted = (PackageManager.PERMISSION_GRANTED == checkSelfPermission(tmp));
+            if (!isPermissionGranted) {
+                needRequestPermission.add(tmp);
+            }
+        }
+        if (isPermissionGranted) {
+            return true;
+        } else {
+            String[] needRequestPermissionArray = new String[needRequestPermission.size()];
+            needRequestPermission.toArray(needRequestPermissionArray);
+            requestPermissions(needRequestPermissionArray, operationHandle);
+            return false;
+        }
+    }
+
+    private String[] getOperationPermissionName(int operation) {
+        switch (operation) {
+        case R.id.recordButton:
+            return new String[]{Manifest.permission.READ_PHONE_STATE,
+                                Manifest.permission.RECORD_AUDIO};
+        case R.id.acceptButton:
+            return new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE};
+        default:
+            return null;
+        }
+    }
+
+    @Override
+    public void onRequestPermissionsResult(int requestCode, String[] permissions,
+                int[] grantResults) {
+        for (int i : grantResults) {
+            if (i != PackageManager.PERMISSION_GRANTED)
+               return;
+        }
+        processClickEvent(requestCode);
+    }
+
     /*
      * Handle the buttons.
      */
     public void onClick(View button) {
         if (!button.isEnabled())
             return;
+        if (Build.VERSION.SDK_INT >= 23) {
+            String[] operationPermissionNames = getOperationPermissionName(button.getId());
+            if (operationPermissionNames == null ||
+                checkOperationPermission(operationPermissionNames, button.getId()))
+                processClickEvent(button.getId());
+        } else {
+            processClickEvent(button.getId());
+        }
+    }
 
-        switch (button.getId()) {
+    private void processClickEvent(int viewId) {
+        switch (viewId) {
             case R.id.recordButton:
                 mRemainingTimeCalculator.reset();
                 if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {