Hide wifi calling index if disabled

Hide the wifi calling index from the setting search if wifi-calling is
not available.

Bug: 111373252
Test: atest TeleServiceTests:PhoneSearchIndexablesProviderTest#testQueryNonIndexableKeys
Merged-In: I69b793df481bd9b293bcefb9b424017f70d798dc
Change-Id: I69b793df481bd9b293bcefb9b424017f70d798dc
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index 0c4cffc..840cbfe 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -149,6 +149,45 @@
         return super.onOptionsItemSelected(item);
     }
 
+
+    /**
+     * Returns true if Wifi calling is enabled for at least one phone.
+     */
+    public static boolean isWifiCallingEnabled(Context context) {
+        int phoneCount = TelephonyManager.from(context).getPhoneCount();
+        for (int i = 0; i < phoneCount; i++) {
+            if (isWifiCallingEnabled(context, i)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * Returns true if Wifi calling is enabled for the specific phone with id {@code phoneId}.
+     */
+    public static boolean isWifiCallingEnabled(Context context, int phoneId) {
+        final PhoneAccountHandle simCallManager =
+                TelecomManager.from(context).getSimCallManager();
+
+        boolean isWifiCallingEnabled;
+        if (simCallManager != null) {
+            Intent intent = PhoneAccountSettingsFragment
+                    .buildPhoneAccountConfigureIntent(context, simCallManager);
+            PackageManager pm = context.getPackageManager();
+            isWifiCallingEnabled = intent != null
+                    && !pm.queryIntentActivities(intent, 0 /* flags */).isEmpty();
+        } else {
+            ImsManager imsMgr = ImsManager.getInstance(context, phoneId);
+            isWifiCallingEnabled = imsMgr != null
+                    && imsMgr.isWfcEnabledByPlatform()
+                    && imsMgr.isWfcProvisionedOnDevice()
+                    && isImsServiceStateReady(imsMgr);
+        }
+
+        return isWifiCallingEnabled;
+    }
+
     /**
      * Whether to show the entry point to eUICC settings.
      *
@@ -236,6 +275,22 @@
         }
     }
 
+    private static boolean isImsServiceStateReady(ImsManager imsMgr) {
+        boolean isImsServiceStateReady = false;
+
+        try {
+            if (imsMgr != null && imsMgr.getImsServiceState() == ImsFeature.STATE_READY) {
+                isImsServiceStateReady = true;
+            }
+        } catch (ImsException ex) {
+            Log.e(MobileNetworkFragment.LOG_TAG,
+                    "Exception when trying to get ImsServiceStatus: " + ex);
+        }
+
+        Log.d(MobileNetworkFragment.LOG_TAG, "isImsServiceStateReady=" + isImsServiceStateReady);
+        return isImsServiceStateReady;
+    }
+
     public static class MobileNetworkFragment extends PreferenceFragment implements
             Preference.OnPreferenceChangeListener, RoamingDialogFragment.RoamingDialogListener {
 
@@ -1759,31 +1814,23 @@
                 return;
             }
 
-            boolean removePref = false;
+            // Removes the preference if the wifi calling is disabled.
+            if (!isWifiCallingEnabled(getContext(), mPhone.getPhoneId())) {
+                mCallingCategory.removePreference(mWiFiCallingPref);
+                return;
+            }
+
             final PhoneAccountHandle simCallManager =
                     TelecomManager.from(getContext()).getSimCallManager();
 
             if (simCallManager != null) {
                 Intent intent = PhoneAccountSettingsFragment.buildPhoneAccountConfigureIntent(
                         getContext(), simCallManager);
-                if (intent != null) {
-                    PackageManager pm = mPhone.getContext().getPackageManager();
-                    List<ResolveInfo> resolutions = pm.queryIntentActivities(intent, 0);
-                    if (!resolutions.isEmpty()) {
-                        mWiFiCallingPref.setTitle(resolutions.get(0).loadLabel(pm));
-                        mWiFiCallingPref.setSummary(null);
-                        mWiFiCallingPref.setIntent(intent);
-                    } else {
-                        removePref = true;
-                    }
-                } else {
-                    removePref = true;
-                }
-            } else if (mImsMgr == null
-                    || !mImsMgr.isWfcEnabledByPlatform()
-                    || !mImsMgr.isWfcProvisionedOnDevice()
-                    || !isImsServiceStateReady()) {
-                removePref = true;
+                PackageManager pm = mPhone.getContext().getPackageManager();
+                List<ResolveInfo> resolutions = pm.queryIntentActivities(intent, 0);
+                mWiFiCallingPref.setTitle(resolutions.get(0).loadLabel(pm));
+                mWiFiCallingPref.setSummary(null);
+                mWiFiCallingPref.setIntent(intent);
             } else {
                 int resId = com.android.internal.R.string.wifi_calling_off_summary;
                 if (mImsMgr.isWfcEnabledByUser()) {
@@ -1808,13 +1855,9 @@
                 mWiFiCallingPref.setSummary(resId);
             }
 
-            if (removePref) {
-                mCallingCategory.removePreference(mWiFiCallingPref);
-            } else {
-                mCallingCategory.addPreference(mWiFiCallingPref);
-                mWiFiCallingPref.setEnabled(mTelephonyManager.getCallState(mPhone.getSubId())
-                        == TelephonyManager.CALL_STATE_IDLE && hasActiveSubscriptions());
-            }
+            mCallingCategory.addPreference(mWiFiCallingPref);
+            mWiFiCallingPref.setEnabled(mTelephonyManager.getCallState(mPhone.getSubId())
+                    == TelephonyManager.CALL_STATE_IDLE && hasActiveSubscriptions());
         }
 
         private void updateEnhanced4gLteState() {
@@ -1828,7 +1871,7 @@
             if ((mImsMgr == null
                     || !mImsMgr.isVolteEnabledByPlatform()
                     || !mImsMgr.isVolteProvisionedOnDevice()
-                    || !isImsServiceStateReady()
+                    || !isImsServiceStateReady(mImsMgr)
                     || carrierConfig.getBoolean(
                     CarrierConfigManager.KEY_HIDE_ENHANCED_4G_LTE_BOOL))) {
                 getPreferenceScreen().removePreference(mButton4glte);
@@ -1852,7 +1895,7 @@
             if (mImsMgr != null
                     && mImsMgr.isVtEnabledByPlatform()
                     && mImsMgr.isVtProvisionedOnDevice()
-                    && isImsServiceStateReady()
+                    && isImsServiceStateReady(mImsMgr)
                     && (carrierConfig.getBoolean(
                         CarrierConfigManager.KEY_IGNORE_DATA_ENABLED_CHANGED_FOR_VIDEO_CALLS)
                         || mPhone.mDcTracker.isDataEnabled())) {
@@ -2121,21 +2164,6 @@
                 mCdmaOptions.update(phone);
             }
         }
-
-        private boolean isImsServiceStateReady() {
-            boolean isImsServiceStateReady = false;
-
-            try {
-                if (mImsMgr != null && mImsMgr.getImsServiceState() == ImsFeature.STATE_READY) {
-                    isImsServiceStateReady = true;
-                }
-            } catch (ImsException ex) {
-                loge("Exception when trying to get ImsServiceStatus: " + ex);
-            }
-
-            log("isImsServiceStateReady=" + isImsServiceStateReady);
-            return isImsServiceStateReady;
-        }
     }
 }
 
diff --git a/src/com/android/phone/PhoneSearchIndexablesProvider.java b/src/com/android/phone/PhoneSearchIndexablesProvider.java
index 57a5b8a..339602d 100644
--- a/src/com/android/phone/PhoneSearchIndexablesProvider.java
+++ b/src/com/android/phone/PhoneSearchIndexablesProvider.java
@@ -42,6 +42,11 @@
 
 public class PhoneSearchIndexablesProvider extends SearchIndexablesProvider {
     private static final String TAG = "PhoneSearchIndexablesProvider";
+
+    private static final String ESIM_LIST_PROFILE_KEY = "esim_list_profile";
+    private static final String ENHANCED_4G_LTE_KEY = "enhanced_4g_lte";
+    private static final String WIFI_CALLING_KEY = "wifi_calling_key";
+
     private UserManager mUserManager;
 
     private static SearchIndexableResource[] INDEXABLE_RES = new SearchIndexableResource[] {
@@ -113,10 +118,15 @@
                 }
             } else {
                 if (isEuiccSettingsHidden()) {
-                    cursor.addRow(createNonIndexableRow("esim_list_profile" /* key */));
+                    cursor.addRow(createNonIndexableRow(ESIM_LIST_PROFILE_KEY));
                 }
+
                 if (isEnhanced4gLteHidden()) {
-                    cursor.addRow(createNonIndexableRow("enhanced_4g_lte" /* key */));
+                    cursor.addRow(createNonIndexableRow(ENHANCED_4G_LTE_KEY));
+                }
+
+                if (isWifiCallingHidden()) {
+                    cursor.addRow(createNonIndexableRow(WIFI_CALLING_KEY));
                 }
             }
 
@@ -141,6 +151,10 @@
         return MobileNetworkSettings.hideEnhanced4gLteSettings(getContext());
     }
 
+    boolean isWifiCallingHidden() {
+        return !MobileNetworkSettings.isWifiCallingEnabled(getContext());
+    }
+
     private Object[] createNonIndexableRow(String key) {
         final Object[] ref = new Object[NON_INDEXABLES_KEYS_COLUMNS.length];
         ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] = key;
diff --git a/tests/src/com/android/phone/PhoneSearchIndexablesProviderTest.java b/tests/src/com/android/phone/PhoneSearchIndexablesProviderTest.java
index 6b7f825..34e3937 100644
--- a/tests/src/com/android/phone/PhoneSearchIndexablesProviderTest.java
+++ b/tests/src/com/android/phone/PhoneSearchIndexablesProviderTest.java
@@ -51,6 +51,7 @@
     private class PhoneSearchIndexablesTestProvider extends PhoneSearchIndexablesProvider {
         private boolean mIsEuiccSettingsHidden = false;
         private boolean mIsEnhanced4gLteHidden = false;
+        private boolean mIsWifiCallingHidden = false;
 
         @Override boolean isEuiccSettingsHidden() {
             return mIsEuiccSettingsHidden;
@@ -60,6 +61,10 @@
             return mIsEnhanced4gLteHidden;
         }
 
+        @Override boolean isWifiCallingHidden() {
+            return mIsWifiCallingHidden;
+        }
+
         public void setIsEuiccSettingsHidden(boolean isEuiccSettingsHidden) {
             mIsEuiccSettingsHidden = isEuiccSettingsHidden;
         }
@@ -67,6 +72,10 @@
         public void setIsEnhanced4gLteHidden(boolean isEnhanced4gLteHidden) {
             mIsEnhanced4gLteHidden = isEnhanced4gLteHidden;
         }
+
+        public void setIsWifiCallingHidden(boolean isWifiCallingHidden) {
+            mIsWifiCallingHidden = isWifiCallingHidden;
+        }
     }
 
     @Before
@@ -115,25 +124,31 @@
         mProvider.setIsEnhanced4gLteHidden(false /* isEnhanced4gLteHidden */);
         mProvider.setIsEuiccSettingsHidden(false /* isEuiccSettingsHiden */);
         when(mUserManager.isAdminUser()).thenReturn(false);
-        Cursor cursor1 = mProvider.queryNonIndexableKeys(
+        Cursor cursor;
+        cursor = mProvider.queryNonIndexableKeys(
                 SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
-        assertThat(cursor1.getColumnNames()).isEqualTo(
+        assertThat(cursor.getColumnNames()).isEqualTo(
                 SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
-        assertThat(cursor1.getCount()).isEqualTo(16);
+        assertThat(cursor.getCount()).isEqualTo(16);
 
         when(mUserManager.isAdminUser()).thenReturn(true);
-        Cursor cursor2 = mProvider
+        cursor = mProvider
                 .queryNonIndexableKeys(SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
-        assertThat(cursor2.getCount()).isEqualTo(3);
+        assertThat(cursor.getCount()).isEqualTo(3);
 
         mProvider.setIsEuiccSettingsHidden(true /* isEuiccSettingsHidden */);
-        Cursor cursor3 = mProvider
+        cursor = mProvider
                 .queryNonIndexableKeys(SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
-        assertThat(cursor3.getCount()).isEqualTo(4);
+        assertThat(cursor.getCount()).isEqualTo(4);
 
         mProvider.setIsEnhanced4gLteHidden(true /* isEnhanced4gLteHidden */);
-        Cursor cursor4 = mProvider
+        cursor = mProvider
                 .queryNonIndexableKeys(SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
-        assertThat(cursor4.getCount()).isEqualTo(5);
+        assertThat(cursor.getCount()).isEqualTo(5);
+
+        mProvider.setIsWifiCallingHidden(true /* isWifiCallingHidden */);
+        cursor = mProvider
+                .queryNonIndexableKeys(SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
+        assertThat(cursor.getCount()).isEqualTo(6);
     }
 }