Message list should update if a message is sent while the main activity is paused. am: 53dcbec2a4

Original change: https://googleplex-android-review.googlesource.com/c/platform/packages/apps/Car/Messenger/+/16042095

Change-Id: Ic544b28afe068a16fbf88227b340d10f2fd97e4d
diff --git a/src/com/android/car/messenger/core/interfaces/DataModel.java b/src/com/android/car/messenger/core/interfaces/DataModel.java
index 2822ecf..b634d4c 100644
--- a/src/com/android/car/messenger/core/interfaces/DataModel.java
+++ b/src/com/android/car/messenger/core/interfaces/DataModel.java
@@ -41,10 +41,10 @@
     LiveData<Collection<UserAccount>> getAccounts();
 
     /**
-     * Call this to reload user account live data. This is useful when resuming an activity, to
-     * ensure no account changes was missed.
+     * Call this to reload data. This is useful when resuming an activity, to ensure no account
+     * changes was missed or other changes were missed.
      */
-    void refreshUserAccounts();
+    void refresh();
 
     /**
      * Get collection of conversations for the given account.
diff --git a/src/com/android/car/messenger/core/ui/launcher/MessageLauncherActivity.java b/src/com/android/car/messenger/core/ui/launcher/MessageLauncherActivity.java
index 0c114cc..beed82c 100644
--- a/src/com/android/car/messenger/core/ui/launcher/MessageLauncherActivity.java
+++ b/src/com/android/car/messenger/core/ui/launcher/MessageLauncherActivity.java
@@ -75,7 +75,7 @@
     @Override
     protected void onResume() {
         L.d("On Resume of Message Activity.");
-        AppFactory.get().getDataModel().refreshUserAccounts();
+        AppFactory.get().getDataModel().refresh();
         super.onResume();
     }
 
diff --git a/src/com/android/car/messenger/impl/datamodels/ConversationListLiveData.java b/src/com/android/car/messenger/impl/datamodels/ConversationListLiveData.java
index 5e71513..fa91f65 100644
--- a/src/com/android/car/messenger/impl/datamodels/ConversationListLiveData.java
+++ b/src/com/android/car/messenger/impl/datamodels/ConversationListLiveData.java
@@ -59,6 +59,8 @@
     ConversationListLiveData(@NonNull UserAccount userAccount) {
         super(Telephony.MmsSms.CONTENT_URI);
         mUserAccount = userAccount;
+        // source to refresh the data to avoid stale data when resuming from background
+        addSource(RefreshLiveData.getInstance(), it -> onDataChange());
     }
 
     @Override
diff --git a/src/com/android/car/messenger/impl/datamodels/RefreshLiveData.java b/src/com/android/car/messenger/impl/datamodels/RefreshLiveData.java
new file mode 100644
index 0000000..0ca75bd
--- /dev/null
+++ b/src/com/android/car/messenger/impl/datamodels/RefreshLiveData.java
@@ -0,0 +1,46 @@
+/*
+ * Copyright (C) 2021 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.car.messenger.impl.datamodels;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.lifecycle.LiveData;
+
+/**
+ * Refresh live data for other live data to force refresh. When a live data listens for updates, it
+ * can refresh its data and repost as needed. This typically occurs when the user revisits the
+ * activity from the background. The background has a number of states that leads to live datas not
+ * being updated. The content observer from the telephony database does not always update when in
+ * the background if something changes. Same thing occurs with shared preferences. By checking the
+ * data again for any stale data when we resume, we can ensure the latest data is provided.
+ */
+public class RefreshLiveData extends LiveData<Boolean> {
+
+    @Nullable private static RefreshLiveData sInstance;
+
+    /** Gets the instance of {@link RefreshLiveData} */
+    @NonNull
+    public static RefreshLiveData getInstance() {
+        if (sInstance == null) {
+            sInstance = new RefreshLiveData();
+        }
+        return sInstance;
+    }
+
+    public void refresh() {
+        postValue(true);
+    }
+}
diff --git a/src/com/android/car/messenger/impl/datamodels/TelephonyDataModel.java b/src/com/android/car/messenger/impl/datamodels/TelephonyDataModel.java
index 82a3d0a..4de3433 100644
--- a/src/com/android/car/messenger/impl/datamodels/TelephonyDataModel.java
+++ b/src/com/android/car/messenger/impl/datamodels/TelephonyDataModel.java
@@ -43,7 +43,6 @@
 
 /** Queries the telephony data model to retrieve the SMS/MMS messages */
 public class TelephonyDataModel implements DataModel {
-
     @NonNull
     @Override
     public LiveData<Collection<UserAccount>> getAccounts() {
@@ -52,8 +51,9 @@
     }
 
     @Override
-    public void refreshUserAccounts() {
+    public void refresh() {
         UserAccountLiveData.getInstance().refresh();
+        RefreshLiveData.getInstance().refresh();
     }
 
     @NonNull