diff options
author | Uchenna Okoye <uokoye@google.com> | 2021-10-12 13:08:43 -0700 |
---|---|---|
committer | Uchenna Okoye <uokoye@google.com> | 2021-10-12 20:14:03 +0000 |
commit | 53dcbec2a441db541fbb3932730ca16855a8410e (patch) | |
tree | 287c3bcfeedb459fbcd20532f2ebf245a7ea0132 | |
parent | 667eed8c115b2a47fba18f05c2a9029cbfb40d6a (diff) | |
download | Messenger-53dcbec2a441db541fbb3932730ca16855a8410e.tar.gz |
Message list should update if a message is sent while the main activity is paused.
Bug: 202322468, 197674787, 202427652
PiperOrigin-RevId: 402636807
Change-Id: I05e66762e9f9ed95be5e5d2dbf15aa5a5cbf4b6f
5 files changed, 54 insertions, 6 deletions
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 @@ public interface DataModel { 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 @@ public class MessageLauncherActivity extends FragmentActivity implements InsetsC @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 @@ class ConversationListLiveData extends ContentProviderLiveData<Collection<Conver 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 @@ import java.util.Set; /** 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 @@ public class TelephonyDataModel implements DataModel { } @Override - public void refreshUserAccounts() { + public void refresh() { UserAccountLiveData.getInstance().refresh(); + RefreshLiveData.getInstance().refresh(); } @NonNull |