Change index of carrier_settings_euicc_key

Since we need to user EuiccManager#ACTION_MANAGE_EMBEDDED_SUBSCRIPTIONS
to start the activity of this key, this CL moves the index of this key
from queryXmlResources to queryRawData.

Bug: 36525840
Test: TeleServiceTests
Merged-In: I2912a1d221eaaff1e8e56f290c0ce3ce9d5e6653
Change-Id: I2912a1d221eaaff1e8e56f290c0ce3ce9d5e6653
diff --git a/res/values/strings.xml b/res/values/strings.xml
index d091a67..5863909 100644
--- a/res/values/strings.xml
+++ b/res/values/strings.xml
@@ -499,6 +499,8 @@
     <string name="data_usage_template"><xliff:g name="amount" example="200 MB">%1$s</xliff:g> mobile data used between <xliff:g name="date_range" example="Jan 1 -- Feb 2">%2$s</xliff:g></string>
     <!-- Mobile network settings screen, name of the option to manage carrier profiles on devices which support embedded carrier profiles -->
     <string name="carrier_settings_euicc">Carrier</string>
+    <!-- Keywords used to search the carrier menu for managing carrier profiles on devices which support embedded carrier profiles. eSIM/eUICC is embedded SIM card used to store this data.  -->
+    <string name="keywords_carrier_settings_euicc">carrier, esim, sim, euicc</string>
     <!-- Mobile network settings screen, summary of the option to manage carrier profiles on devices which support embedded carrier profiles -->
     <string name="carrier_settings_euicc_summary"><xliff:g id="carrier_name">%1$s</xliff:g> &#8212; <xliff:g id="phone_number">%2$s</xliff:g></string>
     <!-- Mobile network settings screen, title of Mobile Data switch preference -->
diff --git a/src/com/android/phone/MobileNetworkSettings.java b/src/com/android/phone/MobileNetworkSettings.java
index 0f83332..65287a2 100644
--- a/src/com/android/phone/MobileNetworkSettings.java
+++ b/src/com/android/phone/MobileNetworkSettings.java
@@ -111,6 +111,26 @@
         return super.onOptionsItemSelected(item);
     }
 
+
+    /**
+     * Whether to show the entry point to eUICC settings.
+     *
+     * <p>We show the entry point on any device which supports eUICC as long as either the eUICC
+     * was ever provisioned (that is, at least one profile was ever downloaded onto it), or if
+     * the user has enabled development mode.
+     */
+    public static boolean showEuiccSettings(Context context) {
+        EuiccManager euiccManager =
+                (EuiccManager) context.getSystemService(Context.EUICC_SERVICE);
+        if (!euiccManager.isEnabled()) {
+            return false;
+        }
+        ContentResolver cr = context.getContentResolver();
+        return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0
+                || Settings.Global.getInt(
+                cr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
+    }
+
     public static class MobileNetworkFragment extends PreferenceFragment implements
             Preference.OnPreferenceChangeListener, RoamingDialogFragment.RoamingDialogListener {
 
@@ -588,25 +608,6 @@
             return mActiveSubInfos.size() > 0;
         }
 
-        /**
-         * Whether to show the entry point to eUICC settings.
-         *
-         * <p>We show the entry point on any device which supports eUICC as long as either the eUICC
-         * was ever provisioned (that is, at least one profile was ever downloaded onto it), or if
-         * the user has enabled development mode.
-         */
-        private boolean showEuiccSettings() {
-            EuiccManager euiccManager =
-                    (EuiccManager) getActivity().getSystemService(Context.EUICC_SERVICE);
-            if (!euiccManager.isEnabled()) {
-                return false;
-            }
-            ContentResolver cr = getActivity().getContentResolver();
-            return Settings.Global.getInt(cr, Settings.Global.EUICC_PROVISIONED, 0) != 0
-                    || Settings.Global.getInt(
-                            cr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) != 0;
-        }
-
         private void updateBody() {
             final Activity activity = getActivity();
             if (activity == null || activity.isDestroyed()) {
@@ -628,7 +629,7 @@
                 prefSet.addPreference(mButtonPreferredNetworkMode);
                 prefSet.addPreference(mButtonEnabledNetworks);
                 prefSet.addPreference(mButton4glte);
-                if (showEuiccSettings()) {
+                if (showEuiccSettings(getActivity())) {
                     prefSet.addPreference(mEuiccSettingsPref);
                     if (TextUtils.isEmpty(mTelephonyManager.getLine1Number())) {
                         mEuiccSettingsPref.setSummary(null);
diff --git a/src/com/android/phone/PhoneSearchIndexablesProvider.java b/src/com/android/phone/PhoneSearchIndexablesProvider.java
index 171f74f..209d100 100644
--- a/src/com/android/phone/PhoneSearchIndexablesProvider.java
+++ b/src/com/android/phone/PhoneSearchIndexablesProvider.java
@@ -16,28 +16,31 @@
 
 package com.android.phone;
 
+import static android.provider.SearchIndexablesContract.COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE;
+import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_CLASS_NAME;
+import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_ICON_RESID;
+import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_ACTION;
+import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_TARGET_CLASS;
+import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE;
+import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_RANK;
+import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_RESID;
+import static android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS;
+import static android.provider.SearchIndexablesContract.INDEXABLES_XML_RES_COLUMNS;
+import static android.provider.SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS;
+
 import android.content.Context;
 import android.database.Cursor;
 import android.database.MatrixCursor;
 import android.os.UserManager;
 import android.provider.SearchIndexableResource;
+import android.provider.SearchIndexablesContract.RawData;
 import android.provider.SearchIndexablesProvider;
-
-import static android.provider.SearchIndexablesContract.COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE;
-import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_RANK;
-import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_RESID;
-import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_CLASS_NAME;
-import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_ICON_RESID;
-import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_ACTION;
-import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_TARGET_PACKAGE;
-import static android.provider.SearchIndexablesContract.COLUMN_INDEX_XML_RES_INTENT_TARGET_CLASS;
-
-import static android.provider.SearchIndexablesContract.INDEXABLES_RAW_COLUMNS;
-import static android.provider.SearchIndexablesContract.INDEXABLES_XML_RES_COLUMNS;
-import static android.provider.SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS;
+import android.support.annotation.VisibleForTesting;
+import android.telephony.euicc.EuiccManager;
 
 public class PhoneSearchIndexablesProvider extends SearchIndexablesProvider {
     private static final String TAG = "PhoneSearchIndexablesProvider";
+    private UserManager mUserManager;
 
     private static SearchIndexableResource[] INDEXABLE_RES = new SearchIndexableResource[] {
             new SearchIndexableResource(1, R.xml.network_setting_fragment,
@@ -47,6 +50,7 @@
 
     @Override
     public boolean onCreate() {
+        mUserManager = (UserManager) getContext().getSystemService(Context.USER_SERVICE);
         return true;
     }
 
@@ -71,25 +75,50 @@
     @Override
     public Cursor queryRawData(String[] projection) {
         MatrixCursor cursor = new MatrixCursor(INDEXABLES_RAW_COLUMNS);
+        Context context = getContext();
+        String title = context.getString(R.string.carrier_settings_euicc);
+        cursor.newRow()
+                .add(RawData.COLUMN_RANK, 0)
+                .add(RawData.COLUMN_TITLE, title)
+                .add(
+                        RawData.COLUMN_KEYWORDS,
+                        context.getString(R.string.keywords_carrier_settings_euicc))
+                .add(RawData.COLUMN_SCREEN_TITLE, title)
+                .add(RawData.COLUMN_KEY, "esim_list_profile")
+                .add(
+                        RawData.COLUMN_INTENT_ACTION,
+                        EuiccManager.ACTION_MANAGE_EMBEDDED_SUBSCRIPTIONS)
+                .add(
+                        RawData.COLUMN_INTENT_TARGET_PACKAGE,
+                        context.getPackageName());
         return cursor;
     }
 
     @Override
     public Cursor queryNonIndexableKeys(String[] projection) {
         MatrixCursor cursor = new MatrixCursor(NON_INDEXABLES_KEYS_COLUMNS);
-        final UserManager userManager = (UserManager) getContext().getSystemService(
-                Context.USER_SERVICE);
-        if (!userManager.isAdminUser()) {
+        if (!mUserManager.isAdminUser()) {
             final String[] values = new String[]{"preferred_network_mode_key", "button_roaming_key",
                     "cdma_lte_data_service_key", "enabled_networks_key", "enhanced_4g_lte",
                     "button_apn_key", "button_carrier_sel_key", "carrier_settings_key",
-                    "cdma_system_select_key", "carrier_settings_euicc_key"};
+                    "cdma_system_select_key", "esim_list_profile"};
             for (String nik : values) {
-                final Object[] ref = new Object[NON_INDEXABLES_KEYS_COLUMNS.length];
-                ref[COLUMN_INDEX_NON_INDEXABLE_KEYS_KEY_VALUE] = nik;
-                cursor.addRow(ref);
+                cursor.addRow(createNonIndexableRow(nik));
             }
+        } else if (isEuiccSettingsHidden()) {
+            cursor.addRow(createNonIndexableRow("esim_list_profile" /* key */));
         }
+        cursor.addRow(createNonIndexableRow("carrier_settings_euicc_key" /* key */));
         return cursor;
     }
+
+    @VisibleForTesting boolean isEuiccSettingsHidden() {
+        return !MobileNetworkSettings.showEuiccSettings(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;
+        return ref;
+    }
 }
diff --git a/tests/src/com/android/phone/PhoneSearchIndexablesProviderTest.java b/tests/src/com/android/phone/PhoneSearchIndexablesProviderTest.java
new file mode 100644
index 0000000..2573780
--- /dev/null
+++ b/tests/src/com/android/phone/PhoneSearchIndexablesProviderTest.java
@@ -0,0 +1,124 @@
+/*
+ * Copyright (C) 2017 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.phone;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import static org.mockito.Mockito.when;
+
+import android.content.ContentResolver;
+import android.content.Context;
+import android.content.pm.ApplicationInfo;
+import android.content.pm.ProviderInfo;
+import android.content.res.Resources;
+import android.database.Cursor;
+import android.os.UserManager;
+import android.provider.SearchIndexablesContract;
+import android.provider.Settings;
+import android.support.test.runner.AndroidJUnit4;
+import android.telephony.euicc.EuiccManager;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.MockitoAnnotations;
+
+/** Unit tests for {@link PhoneSearchIndexablesProvider}. */
+@RunWith(AndroidJUnit4.class)
+public final class PhoneSearchIndexablesProviderTest {
+    private PhoneSearchIndexablesTestProvider mProvider;
+    @Mock private ApplicationInfo mApplicationInfo;
+    @Mock private Context mContext;
+    @Mock private Resources mResources;
+    @Mock private UserManager mUserManager;
+    @Mock private EuiccManager mEuiccManager;
+    @Mock private ContentResolver mCr;
+
+    private class PhoneSearchIndexablesTestProvider extends PhoneSearchIndexablesProvider {
+        private boolean mIsEuiccSettingsHidden = false;
+
+        @Override boolean isEuiccSettingsHidden() {
+            return mIsEuiccSettingsHidden;
+        }
+
+        public void setIsEuiccSettingsHidden(boolean isEuiccSettingsHidden) {
+            mIsEuiccSettingsHidden = isEuiccSettingsHidden;
+        }
+    }
+
+    @Before
+    public void setUp() {
+        MockitoAnnotations.initMocks(this);
+        when(mResources.getString(com.android.phone.R.string.carrier_settings_euicc))
+                .thenReturn("");
+        when(mResources.getString(com.android.phone.R.string.keywords_carrier_settings_euicc))
+                .thenReturn("");
+
+        when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
+        when(mContext.getSystemService(Context.EUICC_SERVICE)).thenReturn(mEuiccManager);
+        when(mContext.getResources()).thenReturn(mResources);
+        when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
+        when(mContext.getPackageName()).thenReturn("PhoneSearchIndexablesProviderTest");
+        when(mContext.getContentResolver()).thenReturn(mCr);
+        when(mCr.getPackageName()).thenReturn("com.android.phone");
+
+        final ProviderInfo providerInfo = new ProviderInfo();
+        providerInfo.authority = Settings.AUTHORITY;
+        providerInfo.exported = true;
+        providerInfo.grantUriPermissions = true;
+        providerInfo.readPermission = android.Manifest.permission.READ_SEARCH_INDEXABLES;
+        mProvider = new PhoneSearchIndexablesTestProvider();
+        mProvider.attachInfo(mContext, providerInfo);
+    }
+
+    @Test
+    public void testQueryRawData() {
+        when(mUserManager.isAdminUser()).thenReturn(true);
+        when(mEuiccManager.isEnabled()).thenReturn(true);
+        Settings.Global.putInt(mCr, Settings.Global.EUICC_PROVISIONED, 1);
+        Settings.Global.getInt(mCr, Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 1);
+
+        Cursor cursor = mProvider.queryRawData(SearchIndexablesContract.INDEXABLES_RAW_COLUMNS);
+        assertThat(cursor.getColumnNames()).isEqualTo(
+                SearchIndexablesContract.INDEXABLES_RAW_COLUMNS);
+        assertThat(cursor.getCount()).isEqualTo(1);
+        cursor.moveToNext();
+        assertThat(cursor.getString(SearchIndexablesContract.COLUMN_INDEX_RAW_KEY))
+                .isEqualTo("esim_list_profile");
+    }
+
+    @Test
+    public void testQueryNonIndexableKeys() {
+        when(mUserManager.isAdminUser()).thenReturn(false);
+        mProvider.setIsEuiccSettingsHidden(false /* isEuiccSettingsHidden */);
+        Cursor cursor1 = mProvider.queryNonIndexableKeys(
+                SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
+        assertThat(cursor1.getColumnNames()).isEqualTo(
+                SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
+        assertThat(cursor1.getCount()).isEqualTo(11);
+
+        when(mUserManager.isAdminUser()).thenReturn(true);
+        Cursor cursor2 = mProvider
+                .queryNonIndexableKeys(SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
+        assertThat(cursor2.getCount()).isEqualTo(1);
+
+        mProvider.setIsEuiccSettingsHidden(true /* isEuiccSettingsHidden */);
+        Cursor cursor3 = mProvider
+                .queryNonIndexableKeys(SearchIndexablesContract.NON_INDEXABLES_KEYS_COLUMNS);
+        assertThat(cursor3.getCount()).isEqualTo(2);
+    }
+}