Delete locally transcribed transcripts when transcription is turned off.

When a user turns off transcription setting we should delete all the voicemails locally transcribed by google. We do this by deleting all the transcripts associated with the source package of the app.

Bug: 74033229,76167428
Test: Unit Test
PiperOrigin-RevId: 190159353
Change-Id: I328aece594aa0e66de59e43fa3619b5e9ae15f78
diff --git a/java/com/android/voicemail/impl/settings/VisualVoicemailSettingsUtil.java b/java/com/android/voicemail/impl/settings/VisualVoicemailSettingsUtil.java
index 3e00698..e7248c4 100644
--- a/java/com/android/voicemail/impl/settings/VisualVoicemailSettingsUtil.java
+++ b/java/com/android/voicemail/impl/settings/VisualVoicemailSettingsUtil.java
@@ -15,18 +15,22 @@
  */
 package com.android.voicemail.impl.settings;
 
+import android.content.ContentValues;
 import android.content.Context;
+import android.provider.CallLog;
+import android.provider.CallLog.Calls;
+import android.provider.VoicemailContract.Voicemails;
 import android.support.annotation.VisibleForTesting;
 import android.telecom.PhoneAccountHandle;
 import com.android.dialer.common.Assert;
 import com.android.dialer.common.concurrent.DialerExecutor.Worker;
 import com.android.dialer.common.concurrent.DialerExecutorComponent;
+import com.android.dialer.common.database.Selection;
 import com.android.voicemail.VoicemailComponent;
 import com.android.voicemail.impl.OmtpVvmCarrierConfigHelper;
 import com.android.voicemail.impl.VisualVoicemailPreferences;
 import com.android.voicemail.impl.VvmLog;
 import com.android.voicemail.impl.sync.VvmAccountManager;
-import com.android.voicemail.impl.utils.VoicemailDatabaseUtil;
 
 /** Save whether or not a particular account is enabled in shared to be retrieved later. */
 public class VisualVoicemailSettingsUtil {
@@ -88,6 +92,28 @@
         .edit()
         .putBoolean(TRANSCRIBE_VOICEMAILS_KEY, isEnabled)
         .apply();
+
+    if (!isEnabled) {
+      VvmLog.i(
+          "VisualVoicemailSettingsUtil.setVoicemailTranscriptionEnabled",
+          "clear all Google transcribed voicemail.");
+      DialerExecutorComponent.get(context)
+          .dialerExecutorFactory()
+          .createNonUiTaskBuilder(new ClearGoogleTranscribedVoicemailTranscriptionWorker(context))
+          .onSuccess(
+              (result) ->
+                  VvmLog.i(
+                      "VisualVoicemailSettingsUtil.setVoicemailTranscriptionEnabled",
+                      "voicemail transciptions cleared successfully"))
+          .onFailure(
+              (throwable) ->
+                  VvmLog.e(
+                      "VisualVoicemailSettingsUtil.setVoicemailTranscriptionEnabled",
+                      "unable to clear Google transcribed voicemails",
+                      throwable))
+          .build()
+          .executeParallel(null);
+    }
   }
 
   public static void setVoicemailDonationEnabled(
@@ -153,6 +179,7 @@
     return prefs.contains(IS_ENABLED_KEY);
   }
 
+  /** Delete all the voicemails whose source_package field matches this package */
   private static class VoicemailDeleteWorker implements Worker<Void, Void> {
     private final Context context;
 
@@ -162,9 +189,53 @@
 
     @Override
     public Void doInBackground(Void unused) {
-      int deleted = VoicemailDatabaseUtil.deleteAll(context);
+      int deleted =
+          context
+              .getContentResolver()
+              .delete(Voicemails.buildSourceUri(context.getPackageName()), null, null);
+
       VvmLog.i("VisualVoicemailSettingsUtil.doInBackground", "deleted " + deleted + " voicemails");
       return null;
     }
   }
+
+  /**
+   * Clears all the voicemail transcripts in the call log whose source_package field matches this
+   * package
+   */
+  private static class ClearGoogleTranscribedVoicemailTranscriptionWorker
+      implements Worker<Void, Void> {
+    private final Context context;
+
+    ClearGoogleTranscribedVoicemailTranscriptionWorker(Context context) {
+      this.context = context;
+    }
+
+    @Override
+    public Void doInBackground(Void unused) {
+
+      ContentValues contentValues = new ContentValues();
+      contentValues.put(Voicemails.TRANSCRIPTION, "");
+
+      Selection selection =
+          Selection.builder()
+              .and(Selection.column(CallLog.Calls.TYPE).is("=", Calls.VOICEMAIL_TYPE))
+              .and(Selection.column(Voicemails.SOURCE_PACKAGE).is("=", context.getPackageName()))
+              .build();
+
+      int cleared =
+          context
+              .getContentResolver()
+              .update(
+                  Calls.CONTENT_URI_WITH_VOICEMAIL,
+                  contentValues,
+                  selection.getSelection(),
+                  selection.getSelectionArgs());
+
+      VvmLog.i(
+          "VisualVoicemailSettingsUtil.doInBackground",
+          "cleared " + cleared + " voicemail transcription");
+      return null;
+    }
+  }
 }
diff --git a/java/com/android/voicemail/impl/utils/VoicemailDatabaseUtil.java b/java/com/android/voicemail/impl/utils/VoicemailDatabaseUtil.java
index ef5447d..711d6a8 100644
--- a/java/com/android/voicemail/impl/utils/VoicemailDatabaseUtil.java
+++ b/java/com/android/voicemail/impl/utils/VoicemailDatabaseUtil.java
@@ -57,16 +57,6 @@
     return voicemails.size();
   }
 
-  /**
-   * Delete all the voicemails whose source_package field matches this package
-   *
-   * @return the number of voicemails deleted
-   */
-  public static int deleteAll(Context context) {
-    ContentResolver contentResolver = context.getContentResolver();
-    return contentResolver.delete(Voicemails.buildSourceUri(context.getPackageName()), null, null);
-  }
-
   /** Maps structured {@link Voicemail} to {@link ContentValues} in content provider. */
   private static ContentValues getContentValues(Voicemail voicemail) {
     ContentValues contentValues = new ContentValues();