Merge "Refresh Dialer when switch phones" into qt-qpr1-dev
diff --git a/src/com/android/car/dialer/ui/TelecomActivity.java b/src/com/android/car/dialer/ui/TelecomActivity.java
index 8368a9e..5092450 100644
--- a/src/com/android/car/dialer/ui/TelecomActivity.java
+++ b/src/com/android/car/dialer/ui/TelecomActivity.java
@@ -17,6 +17,7 @@
 package com.android.car.dialer.ui;
 
 import android.app.SearchManager;
+import android.bluetooth.BluetoothDevice;
 import android.content.Intent;
 import android.content.SharedPreferences;
 import android.graphics.drawable.Drawable;
@@ -76,6 +77,7 @@
     // View objects for this activity.
     private TelecomPageTab.Factory mTabFactory;
     private Toolbar mCarUiToolbar;
+    private BluetoothDevice mBluetoothDevice;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -96,6 +98,7 @@
                 dialerAppState -> updateCurrentFragment(dialerAppState));
         MutableLiveData<Integer> toolbarTitleMode = viewModel.getToolbarTitleMode();
         toolbarTitleMode.setValue(Themes.getAttrInteger(this, R.attr.toolbarTitleMode));
+        viewModel.getRefreshTabsLiveData().observe(this, this::refreshTabs);
 
         InCallViewModel inCallViewModel = ViewModelProviders.of(this).get(InCallViewModel.class);
         mOngoingCallListLiveData = inCallViewModel.getOngoingCallList();
@@ -105,6 +108,13 @@
         handleIntent();
     }
 
+    private void refreshTabs(boolean refreshTabs) {
+        L.v(TAG, "hfp connected device list Changes.");
+        if (refreshTabs) {
+            setupTabLayout();
+        }
+    }
+
     @Override
     protected void onNewIntent(Intent i) {
         super.onNewIntent(i);
@@ -245,6 +255,7 @@
     private void setupTabLayout() {
         boolean wasContentFragmentRestored = false;
         mTabFactory = new TelecomPageTab.Factory(this, getSupportFragmentManager());
+        mCarUiToolbar.clearAllTabs();
         for (int i = 0; i < mTabFactory.getTabCount(); i++) {
             TelecomPageTab tab = mTabFactory.createTab(getBaseContext(), i);
             mCarUiToolbar.addTab(tab);
diff --git a/src/com/android/car/dialer/ui/TelecomActivityViewModel.java b/src/com/android/car/dialer/ui/TelecomActivityViewModel.java
index bf6a051..cd16402 100644
--- a/src/com/android/car/dialer/ui/TelecomActivityViewModel.java
+++ b/src/com/android/car/dialer/ui/TelecomActivityViewModel.java
@@ -27,11 +27,13 @@
 import androidx.lifecycle.LiveData;
 import androidx.lifecycle.MediatorLiveData;
 import androidx.lifecycle.MutableLiveData;
+import androidx.lifecycle.Transformations;
 
 import com.android.car.dialer.R;
 import com.android.car.dialer.livedata.BluetoothHfpStateLiveData;
 import com.android.car.dialer.livedata.BluetoothPairListLiveData;
 import com.android.car.dialer.livedata.BluetoothStateLiveData;
+import com.android.car.dialer.livedata.HfpDeviceListLiveData;
 import com.android.car.dialer.log.L;
 import com.android.car.dialer.telecom.UiBluetoothMonitor;
 
@@ -52,10 +54,13 @@
     private final Context mApplicationContext;
     private final LiveData<String> mErrorStringLiveData;
     private final MutableLiveData<Integer> mDialerAppStateLiveData;
+    private final LiveData<Boolean> mRefreshTabsLiveData;
 
     private final ToolbarTitleLiveData mToolbarTitleLiveData;
     private final MutableLiveData<Integer> mToolbarTitleMode;
 
+    private BluetoothDevice mBluetoothDevice;
+
     /**
      * App state indicates if bluetooth is connected or it should just show the content fragments.
      */
@@ -90,6 +95,22 @@
         }
 
         mDialerAppStateLiveData = new DialerAppStateLiveData(mErrorStringLiveData);
+
+        HfpDeviceListLiveData hfpDeviceListLiveData = new HfpDeviceListLiveData(getApplication());
+        mRefreshTabsLiveData = Transformations.map(hfpDeviceListLiveData, (hfpDeviceList) -> {
+            if (hfpDeviceList != null && !hfpDeviceList.isEmpty()) {
+                if (!hfpDeviceList.contains(mBluetoothDevice)) {
+                    mBluetoothDevice = hfpDeviceList.get(0);
+                    return true;
+                }
+            } else {
+                if (mBluetoothDevice != null) {
+                    mBluetoothDevice = null;
+                    return true;
+                }
+            }
+            return false;
+        });
     }
 
     /**
@@ -108,6 +129,9 @@
         return mToolbarTitleMode;
     }
 
+    /**
+     * Returns the state of Dialer App.
+     */
     public MutableLiveData<Integer> getDialerAppState() {
         return mDialerAppStateLiveData;
     }
@@ -120,6 +144,13 @@
         return mErrorStringLiveData;
     }
 
+    /**
+     * Returns the live data which monitors whether to refresh Dialer.
+     */
+    public LiveData<Boolean> getRefreshTabsLiveData() {
+        return mRefreshTabsLiveData;
+    }
+
     private static class DialerAppStateLiveData extends MediatorLiveData<Integer> {
         private final LiveData<String> mErrorStringLiveData;