Add autorecord feature

forwarport to Q

Change-Id: I0db0630eb7a4aa026b994d19cf2f9e735f337688
diff --git a/java/com/android/dialer/app/res/values/cm_strings.xml b/java/com/android/dialer/app/res/values/cm_strings.xml
index 1dcdb2b..3f7602d 100644
--- a/java/com/android/dialer/app/res/values/cm_strings.xml
+++ b/java/com/android/dialer/app/res/values/cm_strings.xml
@@ -38,6 +38,8 @@
     <string name="call_recording_format">Audio format</string>
     <string name="wb_amr_format" translatable="false">AMR-WB</string>
     <string name="aac_format" translatable="false">AAC</string>
+    <string name="auto_call_recording_title">Auto call recording</string>
+    <string name="auto_call_recording_key" translatable="false">auto_call_recording</string>
 
     <string name="call_via">Call via</string>
     <string name="call_via_dialog_title">Call via\u2026</string>
diff --git a/java/com/android/dialer/app/res/xml/sound_settings.xml b/java/com/android/dialer/app/res/xml/sound_settings.xml
index aa02587..673ec56 100644
--- a/java/com/android/dialer/app/res/xml/sound_settings.xml
+++ b/java/com/android/dialer/app/res/xml/sound_settings.xml
@@ -83,6 +83,11 @@
       android:entryValues="@array/call_recording_encoder_values"
       android:defaultValue="0" />
 
+  <SwitchPreference
+    android:defaultValue="false"
+    android:key="@string/auto_call_recording_key"
+    android:title="@string/auto_call_recording_title"/>
+
   </PreferenceCategory>
 
 </PreferenceScreen>
diff --git a/java/com/android/incallui/CallButtonPresenter.java b/java/com/android/incallui/CallButtonPresenter.java
index 5351c74..5d9e41d 100644
--- a/java/com/android/incallui/CallButtonPresenter.java
+++ b/java/com/android/incallui/CallButtonPresenter.java
@@ -22,6 +22,7 @@
 import android.content.pm.PackageManager;
 import android.os.Bundle;
 import android.os.Trace;
+import android.os.Handler;
 import android.preference.PreferenceManager;
 import android.support.v4.app.Fragment;
 import android.support.v4.os.UserManagerCompat;
@@ -74,6 +75,7 @@
   private DialerCall call;
   private boolean isInCallButtonUiReady;
   private PhoneAccountHandle otherAccount;
+  private boolean isRecording = false;
 
   private CallRecorder.RecordingProgressListener recordingProgressListener =
       new CallRecorder.RecordingProgressListener() {
@@ -114,6 +116,11 @@
 
     CallRecorder recorder = CallRecorder.getInstance();
     recorder.addRecordingProgressListener(recordingProgressListener);
+    if(recorder.isRecording()){
+      inCallButtonUi.setCallRecordingState(true);
+    } else {
+      inCallButtonUi.setCallRecordingState(false);
+    }
 
     // Update the buttons state immediately for the current call
     onStateChange(InCallState.NO_CALLS, inCallPresenter.getInCallState(), CallList.getInstance());
@@ -144,6 +151,9 @@
   @Override
   public void onStateChange(InCallState oldState, InCallState newState, CallList callList) {
     Trace.beginSection("CallButtonPresenter.onStateChange");
+    CallRecorder recorder = CallRecorder.getInstance();
+    boolean isEnabled = PreferenceManager.getDefaultSharedPreferences(context).getBoolean(context.getString(R.string.auto_call_recording_key), false);
+
     if (call != null) {
       call.removeListener(this);
     }
@@ -152,6 +162,15 @@
     } else if (newState == InCallState.INCALL) {
       call = callList.getActiveOrBackgroundCall();
 
+    if (!isRecording && isEnabled && call != null) {
+                isRecording = true;
+                new Handler().postDelayed(new Runnable() {
+                    @Override
+                    public void run() {
+                        callRecordClicked(true);
+                    }
+                }, 500);
+    }
       // When connected to voice mail, automatically shows the dialpad.
       // (On previous releases we showed it when in-call shows up, before waiting for
       // OUTGOING.  We may want to do that once we start showing "Voice mail" label on
@@ -167,6 +186,9 @@
       }
       call = callList.getIncomingCall();
     } else {
+      if (isEnabled && recorder.isRecording()) {
+         recorder.finishRecording();
+      }
       call = null;
     }
 
@@ -333,21 +355,7 @@
   public void callRecordClicked(boolean checked) {
     CallRecorder recorder = CallRecorder.getInstance();
     if (checked) {
-      final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
-      boolean warningPresented = prefs.getBoolean(KEY_RECORDING_WARNING_PRESENTED, false);
-      if (!warningPresented) {
-        new AlertDialog.Builder(getActivity())
-            .setTitle(R.string.recording_warning_title)
-            .setMessage(R.string.recording_warning_text)
-            .setPositiveButton(R.string.onscreenCallRecordText, (dialog, which) -> {
-              prefs.edit()
-                  .putBoolean(KEY_RECORDING_WARNING_PRESENTED, true)
-                  .apply();
-              startCallRecordingOrAskForPermission();
-            })
-            .setNegativeButton(android.R.string.cancel, null)
-            .show();
-      } else {
+       if(!recorder.isRecording()) {
         startCallRecordingOrAskForPermission();
       }
     } else {