Removing reflection from AlphabeticIndexCompat

Bug: 31596662
Change-Id: I23da4a9747daa3cee6cec961fc590d18bf05791a
diff --git a/src/com/android/launcher3/compat/AlphabeticIndexCompat.java b/src/com/android/launcher3/compat/AlphabeticIndexCompat.java
index ec65c3e..c7a529d 100644
--- a/src/com/android/launcher3/compat/AlphabeticIndexCompat.java
+++ b/src/com/android/launcher3/compat/AlphabeticIndexCompat.java
@@ -1,12 +1,14 @@
 package com.android.launcher3.compat;
 
+import android.annotation.TargetApi;
 import android.content.Context;
-import android.content.res.Configuration;
+import android.icu.text.AlphabeticIndex;
+import android.os.Build;
+import android.os.LocaleList;
 import android.util.Log;
 
 import com.android.launcher3.Utilities;
 
-import java.lang.reflect.Constructor;
 import java.lang.reflect.Method;
 import java.util.Locale;
 
@@ -157,69 +159,39 @@
     }
 
     /**
-     * Reflected android.icu.text.AlphabeticIndex implementation, falls back to the base
-     * alphabetic index.
+     * Implementation based on {@link AlphabeticIndex}.
      */
+    @TargetApi(Build.VERSION_CODES.N)
     private static class AlphabeticIndexVN extends BaseIndex {
 
-        private Object mAlphabeticIndex;
-        private Method mGetBucketIndexMethod;
+        private final AlphabeticIndex.ImmutableIndex mAlphabeticIndex;
 
-        private Method mGetBucketMethod;
-        private Method mGetLabelMethod;
+        public AlphabeticIndexVN(Context context) {
+            LocaleList locales = context.getResources().getConfiguration().getLocales();
+            int localeCount = locales.size();
 
-        public AlphabeticIndexVN(Context context) throws Exception {
-            // TODO: Replace this with locale list once available.
-            Object locales = Configuration.class.getDeclaredMethod("getLocales").invoke(
-                    context.getResources().getConfiguration());
-            int localeCount = (Integer) locales.getClass().getDeclaredMethod("size").invoke(locales);
-            Method localeGetter = locales.getClass().getDeclaredMethod("get", int.class);
-            Locale primaryLocale = localeCount == 0 ? Locale.ENGLISH :
-                    (Locale) localeGetter.invoke(locales, 0);
-
-            Class clazz = Class.forName("android.icu.text.AlphabeticIndex");
-            mAlphabeticIndex = clazz.getConstructor(Locale.class).newInstance(primaryLocale);
-
-            Method addLocales = clazz.getDeclaredMethod("addLabels", Locale[].class);
+            Locale primaryLocale = localeCount == 0 ? Locale.ENGLISH : locales.get(0);
+            AlphabeticIndex indexBuilder = new AlphabeticIndex(primaryLocale);
             for (int i = 1; i < localeCount; i++) {
-                Locale l = (Locale) localeGetter.invoke(locales, i);
-                addLocales.invoke(mAlphabeticIndex, new Object[]{ new Locale[] {l}});
+                indexBuilder.addLabels(locales.get(i));
             }
-            addLocales.invoke(mAlphabeticIndex, new Object[]{ new Locale[] {Locale.ENGLISH}});
+            indexBuilder.addLabels(Locale.ENGLISH);
 
-            mAlphabeticIndex = mAlphabeticIndex.getClass()
-                    .getDeclaredMethod("buildImmutableIndex")
-                    .invoke(mAlphabeticIndex);
-
-            mGetBucketIndexMethod = mAlphabeticIndex.getClass().getDeclaredMethod(
-                    "getBucketIndex", CharSequence.class);
-            mGetBucketMethod = mAlphabeticIndex.getClass().getDeclaredMethod("getBucket", int.class);
-            mGetLabelMethod = mGetBucketMethod.getReturnType().getDeclaredMethod("getLabel");
+            mAlphabeticIndex = indexBuilder.buildImmutableIndex();
         }
 
         /**
          * Returns the index of the bucket in which {@param s} should appear.
          */
         protected int getBucketIndex(String s) {
-            try {
-                return (Integer) mGetBucketIndexMethod.invoke(mAlphabeticIndex, s);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-            return super.getBucketIndex(s);
+            return mAlphabeticIndex.getBucketIndex(s);
         }
 
         /**
          * Returns the label for the bucket at the given index
          */
         protected String getBucketLabel(int index) {
-            try {
-                return (String) mGetLabelMethod.invoke(
-                        mGetBucketMethod.invoke(mAlphabeticIndex, index));
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-            return super.getBucketLabel(index);
+            return mAlphabeticIndex.getBucket(index).getLabel();
         }
     }
 }