Snap for 5688376 from 7be70840ccfc0c738ddb054bfdc02a780dfca124 to qt-release

Change-Id: Ibe219eb523f0f901a193b94418a34d4dcb7adc71
diff --git a/res/values/config.xml b/res/values/config.xml
new file mode 100644
index 0000000..56cb667
--- /dev/null
+++ b/res/values/config.xml
@@ -0,0 +1,21 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+    Copyright (C) 2019 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.
+-->
+<resources>
+    <!-- Whether existing messages should be loaded. Recommended to turn off if head-unit's and
+     BT-paired phone's clocks are not synced.-->
+    <bool name="config_loadExistingMessages">false</bool>
+</resources>
diff --git a/src/com/android/car/messenger/MessengerDelegate.java b/src/com/android/car/messenger/MessengerDelegate.java
index ed154c3..b2a5cda 100644
--- a/src/com/android/car/messenger/MessengerDelegate.java
+++ b/src/com/android/car/messenger/MessengerDelegate.java
@@ -11,6 +11,7 @@
 import android.content.ContentUris;
 import android.content.Context;
 import android.content.Intent;
+import android.content.res.Resources.NotFoundException;
 import android.database.Cursor;
 import android.graphics.Bitmap;
 import android.graphics.drawable.Drawable;
@@ -41,12 +42,10 @@
 
 import java.util.ArrayList;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
-import java.util.Set;
 import java.util.function.Predicate;
 
 /** Delegate class responsible for handling messaging service actions */
@@ -59,6 +58,7 @@
     private BluetoothMapClient mBluetoothMapClient;
     private NotificationManager mNotificationManager;
     private final SmsDatabaseHandler mSmsDatabaseHandler;
+    private boolean mShouldLoadExistingMessages;
 
     @VisibleForTesting
     final Map<MessageKey, MapMessage> mMessages = new HashMap<>();
@@ -74,8 +74,16 @@
 
         mNotificationManager =
                 (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
-
         mSmsDatabaseHandler = new SmsDatabaseHandler(mContext);
+
+        try {
+            mShouldLoadExistingMessages =
+                    mContext.getResources().getBoolean(R.bool.config_loadExistingMessages);
+        } catch(NotFoundException e) {
+            // Should only happen for robolectric unit tests;
+            L.e(TAG, e, "Disabling loading of existing messages");
+            mShouldLoadExistingMessages = false;
+        }
     }
 
     @Override
@@ -104,7 +112,7 @@
     public void onDeviceConnected(BluetoothDevice device) {
         L.d(TAG, "Device connected: \t%s", device.getAddress());
         mBTDeviceAddressToConnectionTimestamp.put(device.getAddress(), System.currentTimeMillis());
-        if (mBluetoothMapClient != null) {
+        if (mBluetoothMapClient != null && mShouldLoadExistingMessages) {
             mBluetoothMapClient.getUnreadMessages(device);
         } else {
             // onDeviceConnected should be sent by BluetoothMapClient, so log if we run into this