Dialer:Fix FC when it disables READ/WIRTE CALL LOG permission

-It needs to check/grant Manifest.permission.WRITE_CALL_LOG and
 Manifest.permission.WRITE_CALL_LOG before calling
 queryLastOutgoingCall() function to avoid FC.

Change-Id: I0689270a1b59039121372de0e77b9cb28e3ea9db
CRs-Fixed: 1011439
diff --git a/src/com/android/dialer/dialpad/DialpadFragment.java b/src/com/android/dialer/dialpad/DialpadFragment.java
index 8dab294..0e89101 100644
--- a/src/com/android/dialer/dialpad/DialpadFragment.java
+++ b/src/com/android/dialer/dialpad/DialpadFragment.java
@@ -99,6 +99,9 @@
 import com.android.phone.common.dialpad.DialpadKeyButton;
 import com.android.phone.common.dialpad.DialpadView;
 
+import static android.Manifest.permission.READ_CALL_LOG;
+import static android.Manifest.permission.WRITE_CALL_LOG;
+
 import java.util.HashSet;
 import java.util.List;
 
@@ -113,8 +116,9 @@
         DialpadKeyButton.OnPressedListener {
     private static final String TAG = "DialpadFragment";
 
-    /* define for Activity permission request for Android6.0 */
+    /* define for Activity permission request for Android >= 6.0 */
     private static final int PERMISSION_REQUEST_CODE_LOCATION = 2;
+    private static final int READ_WRITE_CALL_LOG_PERMISSION_REQUEST_CODE = 3;
     /**
      * LinearLayout with getter and setter methods for the translationY property using floats,
      * for animation purposes.
@@ -192,7 +196,6 @@
     /** Stream type used to play the DTMF tones off call, and mapped to the volume control keys */
     private static final int DIAL_TONE_STREAM_TYPE = AudioManager.STREAM_DTMF;
 
-
     private OnDialpadQueryChangedListener mDialpadQueryListener;
 
     private DialpadView mDialpadView;
@@ -265,6 +268,8 @@
 
     private CallStateReceiver mCallStateReceiver;
 
+    private boolean mHasReadAndWriteCallLogPermission = false;
+
     private class CallStateReceiver extends BroadcastReceiver {
         /**
          * Receive call state changes so that we can take down the
@@ -752,8 +757,16 @@
         final StopWatch stopWatch = StopWatch.start("Dialpad.onResume");
 
         // Query the last dialed number. Do it first because hitting
+        mHasReadAndWriteCallLogPermission =
+                PermissionsUtil.hasPermission(getActivity(), READ_CALL_LOG) &&
+                PermissionsUtil.hasPermission(getActivity(), WRITE_CALL_LOG);
         // the DB is 'slow'. This call is asynchronous.
-        queryLastOutgoingCall();
+        if (mHasReadAndWriteCallLogPermission){
+            queryLastOutgoingCall();
+        } else {
+            ActivityCompat.requestPermissions(getActivity(), new String[] {READ_CALL_LOG,
+                    WRITE_CALL_LOG}, READ_WRITE_CALL_LOG_PERMISSION_REQUEST_CODE);
+        }
 
         stopWatch.lap("qloc");
 
@@ -2045,6 +2058,15 @@
                     handleDialButtonPressed();
                 }
                 break;
+            case READ_WRITE_CALL_LOG_PERMISSION_REQUEST_CODE:
+                for (int i = 0; i < grantResults.length; i++) {
+                    mHasReadAndWriteCallLogPermission &=
+                            PackageManager.PERMISSION_GRANTED == grantResults[i];
+                }
+                if (mHasReadAndWriteCallLogPermission) {
+                    queryLastOutgoingCall();
+                }
+                break;
             default:
                 super.onRequestPermissionsResult(requestCode, permissions, grantResults);
         }