Some code refactoring

- use SearchIndexableResources.NO_DATA_RES_ID
- extract code for getting a SearchIndexProvider

Change-Id: I3d31cc58f70b9b0abfa6684d9d20b19534ec5e45
diff --git a/src/com/android/settings/search/Index.java b/src/com/android/settings/search/Index.java
index 1247c3b..3281b8a 100644
--- a/src/com/android/settings/search/Index.java
+++ b/src/com/android/settings/search/Index.java
@@ -530,7 +530,7 @@
 
     private void indexOneResource(SQLiteDatabase database, String localeStr,
                                   SearchIndexableResource sir) {
-        if (sir.xmlResId > 0) {
+        if (sir.xmlResId > SearchIndexableResources.NO_DATA_RES_ID) {
             indexFromResource(sir.context, database, localeStr,
                     sir.xmlResId, sir.className, sir.iconResId, sir.rank,
                     sir.intentAction, sir.intentTargetPackage, sir.intentTargetClass);
@@ -646,72 +646,80 @@
                 raw.key);
     }
 
-    private void indexFromLocalProvider(SQLiteDatabase database, String localeStr,
-                                        SearchIndexableResource sir) {
+    private Indexable.SearchIndexProvider getSearchIndexProvider(String className) {
         try {
-            final Class<?> clazz = Class.forName(sir.className);
+            final Class<?> clazz = Class.forName(className);
             if (Indexable.class.isAssignableFrom(clazz)) {
                 final Field f = clazz.getField(FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER);
-                final Indexable.SearchIndexProvider provider =
-                        (Indexable.SearchIndexProvider) f.get(null);
-
-                final List<SearchIndexableRaw> rawList =
-                        provider.getRawDataToIndex(sir.context, sir.enabled);
-                if (rawList != null) {
-                    final int rawSize = rawList.size();
-                    for (int i = 0; i < rawSize; i++) {
-                        SearchIndexableRaw raw = rawList.get(i);
-
-                        // Should be the same locale as the one we are processing
-                        if (!raw.locale.toString().equalsIgnoreCase(localeStr)) {
-                            continue;
-                        }
-
-                        updateOneRowWithFilteredData(database, localeStr,
-                                raw.title,
-                                raw.summaryOn,
-                                raw.summaryOff,
-                                raw.entries,
-                                sir.className,
-                                raw.screenTitle,
-                                sir.iconResId,
-                                sir.rank,
-                                raw.keywords,
-                                raw.intentAction,
-                                raw.intentTargetPackage,
-                                raw.intentTargetClass,
-                                raw.enabled,
-                                raw.key);
-                    }
-                }
-
-                final List<SearchIndexableResource> resList =
-                        provider.getXmlResourcesToIndex(sir.context, sir.enabled);
-                if (resList != null) {
-                    final int resSize = resList.size();
-                    for (int i = 0; i < resSize; i++) {
-                        SearchIndexableResource item = resList.get(i);
-
-                        // Should be the same locale as the one we are processing
-                        if (!item.locale.toString().equalsIgnoreCase(localeStr)) {
-                            continue;
-                        }
-
-                        indexFromResource(sir.context, database, localeStr,
-                                item.xmlResId, item.className, item.iconResId, item.rank,
-                                item.intentAction, item.intentTargetPackage,
-                                item.intentTargetClass);
-                    }
-                }
+                return (Indexable.SearchIndexProvider) f.get(null);
             }
         } catch (ClassNotFoundException e) {
-            Log.e(LOG_TAG, "Cannot find class: " + sir.className, e);
+            Log.e(LOG_TAG, "Cannot find class: " + className, e);
         } catch (NoSuchFieldException e) {
             Log.e(LOG_TAG, "Cannot find field '" + FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER + "'", e);
         } catch (IllegalAccessException e) {
             Log.e(LOG_TAG,
                     "Illegal access to field '" + FIELD_NAME_SEARCH_INDEX_DATA_PROVIDER + "'", e);
         }
+        return null;
+    }
+
+    private void indexFromLocalProvider(SQLiteDatabase database, String localeStr,
+                                        SearchIndexableResource sir) {
+        final Indexable.SearchIndexProvider provider = getSearchIndexProvider(sir.className);
+        if (provider == null) {
+            Log.w(LOG_TAG, "Cannot find provider: " + sir.className);
+            return;
+        }
+
+        final List<SearchIndexableRaw> rawList =
+                provider.getRawDataToIndex(sir.context, sir.enabled);
+        if (rawList != null) {
+            final int rawSize = rawList.size();
+            for (int i = 0; i < rawSize; i++) {
+                SearchIndexableRaw raw = rawList.get(i);
+
+                // Should be the same locale as the one we are processing
+                if (!raw.locale.toString().equalsIgnoreCase(localeStr)) {
+                    continue;
+                }
+
+                updateOneRowWithFilteredData(database, localeStr,
+                        raw.title,
+                        raw.summaryOn,
+                        raw.summaryOff,
+                        raw.entries,
+                        sir.className,
+                        raw.screenTitle,
+                        sir.iconResId,
+                        sir.rank,
+                        raw.keywords,
+                        raw.intentAction,
+                        raw.intentTargetPackage,
+                        raw.intentTargetClass,
+                        raw.enabled,
+                        raw.key);
+            }
+        }
+
+        final List<SearchIndexableResource> resList =
+                provider.getXmlResourcesToIndex(sir.context, sir.enabled);
+        if (resList != null) {
+            final int resSize = resList.size();
+            for (int i = 0; i < resSize; i++) {
+                SearchIndexableResource item = resList.get(i);
+
+                // Should be the same locale as the one we are processing
+                if (!item.locale.toString().equalsIgnoreCase(localeStr)) {
+                    continue;
+                }
+
+                indexFromResource(sir.context, database, localeStr,
+                        item.xmlResId, item.className, item.iconResId, item.rank,
+                        item.intentAction, item.intentTargetPackage,
+                        item.intentTargetClass);
+            }
+        }
     }
 
     private void updateOneRowWithFilteredData(SQLiteDatabase database, String locale,
diff --git a/src/com/android/settings/search/SearchIndexableResources.java b/src/com/android/settings/search/SearchIndexableResources.java
index 6ade187..c7914c8 100644
--- a/src/com/android/settings/search/SearchIndexableResources.java
+++ b/src/com/android/settings/search/SearchIndexableResources.java
@@ -47,7 +47,7 @@
 
 public final class SearchIndexableResources {
 
-    private static int NO_DATA_RES_ID = 0;
+    public static int NO_DATA_RES_ID = 0;
 
     private static final int RANK_WIFI = 1;
     private static final int RANK_BT = 2;