Register content observer when voicemail table changes.

When a new voicemail is received, it is written in the voicemail table by the Voicemail service. However the new voicemail will not get updated/shown in the New Voicemail UI as the annotated call log would be stale. This CL ensures that when the voicemail is added, the annotated call log is marked dirty and refreshed. This way the new voicemail will be shown in the new voicemail UI. Since a new voicemail is also added, we want to make sure the headers for "today" and "older", their positions are also updated accordingly.

Bug: 64882313
Test: Unit tests
PiperOrigin-RevId: 179623267
Change-Id: I5dfc84f62f9f37c57ffb2dbbe7e848a58306a19d
diff --git a/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java b/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java
index 0ed1859..95fbf9d 100644
--- a/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java
+++ b/java/com/android/dialer/calllog/datasources/systemcalllog/SystemCallLogDataSource.java
@@ -27,6 +27,7 @@
 import android.os.Handler;
 import android.provider.CallLog;
 import android.provider.CallLog.Calls;
+import android.provider.VoicemailContract;
 import android.support.annotation.ColorInt;
 import android.support.annotation.MainThread;
 import android.support.annotation.Nullable;
@@ -92,13 +93,21 @@
     }
     // TODO(zachh): Need to somehow register observers if user enables permission after launch?
 
+    CallLogObserver callLogObserver =
+        new CallLogObserver(ThreadUtil.getUiThreadHandler(), appContext, contentObserverCallbacks);
+
     appContext
         .getContentResolver()
-        .registerContentObserver(
-            CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL,
-            true,
-            new CallLogObserver(
-                ThreadUtil.getUiThreadHandler(), appContext, contentObserverCallbacks));
+        .registerContentObserver(CallLog.Calls.CONTENT_URI_WITH_VOICEMAIL, true, callLogObserver);
+
+    if (!PermissionsUtil.hasAddVoicemailPermissions(appContext)) {
+      LogUtil.i("SystemCallLogDataSource.registerContentObservers", "no add voicemail permissions");
+      return;
+    }
+    // TODO(uabdullah): Need to somehow register observers if user enables permission after launch?
+    appContext
+        .getContentResolver()
+        .registerContentObserver(VoicemailContract.Status.CONTENT_URI, true, callLogObserver);
   }
 
   @Override
@@ -462,7 +471,11 @@
     @Override
     public void onChange(boolean selfChange, Uri uri) {
       Assert.isMainThread();
-      LogUtil.enterBlock("SystemCallLogDataSource.CallLogObserver.onChange");
+      LogUtil.i(
+          "SystemCallLogDataSource.CallLogObserver.onChange",
+          "Uri:%s, SelfChange:%b",
+          String.valueOf(uri),
+          selfChange);
       super.onChange(selfChange, uri);
 
       /*
diff --git a/java/com/android/dialer/voicemail/listui/NewVoicemailAdapter.java b/java/com/android/dialer/voicemail/listui/NewVoicemailAdapter.java
index d94a214..93d5cda 100644
--- a/java/com/android/dialer/voicemail/listui/NewVoicemailAdapter.java
+++ b/java/com/android/dialer/voicemail/listui/NewVoicemailAdapter.java
@@ -67,9 +67,9 @@
   private final Clock clock;
 
   /** {@link Integer#MAX_VALUE} when the "Today" header should not be displayed. */
-  private final int todayHeaderPosition;
+  private int todayHeaderPosition = Integer.MAX_VALUE;
   /** {@link Integer#MAX_VALUE} when the "Older" header should not be displayed. */
-  private final int olderHeaderPosition;
+  private int olderHeaderPosition = Integer.MAX_VALUE;
 
   private final FragmentManager fragmentManager;
   /** A valid id for {@link VoicemailEntry} is greater than 0 */
@@ -107,7 +107,15 @@
     this.clock = clock;
     this.fragmentManager = fragmentManager;
     initializeMediaPlayerListeners();
+    updateHeaderPositions();
+  }
 
+  private void updateHeaderPositions() {
+    LogUtil.i(
+        "NewVoicemailAdapter.updateHeaderPositions",
+        "before updating todayPos:%d, olderPos:%d",
+        todayHeaderPosition,
+        olderHeaderPosition);
     // Calculate header adapter positions by reading cursor.
     long currentTimeMillis = clock.currentTimeMillis();
     if (cursor.moveToNext()) {
@@ -134,6 +142,11 @@
       this.todayHeaderPosition = Integer.MAX_VALUE;
       this.olderHeaderPosition = Integer.MAX_VALUE;
     }
+    LogUtil.i(
+        "NewVoicemailAdapter.updateHeaderPositions",
+        "after updating todayPos:%d, olderPos:%d",
+        todayHeaderPosition,
+        olderHeaderPosition);
   }
 
   private void initializeMediaPlayerListeners() {
@@ -143,8 +156,10 @@
   }
 
   public void updateCursor(Cursor updatedCursor) {
+    LogUtil.enterBlock("NewVoicemailAdapter.updateCursor");
     deletedVoicemailPosition.clear();
     this.cursor = updatedCursor;
+    updateHeaderPositions();
     notifyDataSetChanged();
   }