summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSunny Goyal <sunnygoyal@google.com>2016-09-26 16:46:38 -0700
committerSunny Goyal <sunnygoyal@google.com>2016-09-27 09:50:22 -0700
commit70fea7877d8d967b0d8861a1a99b87a065e5a42b (patch)
tree29e8424e81e8df66c5a61afaf1543681db30f770
parent00341907b7326fbdc4c2a39e8b6cda16b7074daf (diff)
downloadandroid_packages_apps_Trebuchet-70fea7877d8d967b0d8861a1a99b87a065e5a42b.tar.gz
android_packages_apps_Trebuchet-70fea7877d8d967b0d8861a1a99b87a065e5a42b.tar.bz2
android_packages_apps_Trebuchet-70fea7877d8d967b0d8861a1a99b87a065e5a42b.zip
Removing reflection from AlphabeticIndexCompat
Bug: 31596662 Change-Id: I23da4a9747daa3cee6cec961fc590d18bf05791a
-rw-r--r--src/com/android/launcher3/compat/AlphabeticIndexCompat.java62
1 files changed, 17 insertions, 45 deletions
diff --git a/src/com/android/launcher3/compat/AlphabeticIndexCompat.java b/src/com/android/launcher3/compat/AlphabeticIndexCompat.java
index ec65c3e8a..c7a529d07 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 @@ public class AlphabeticIndexCompat {
}
/**
- * 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 Method mGetBucketMethod;
- private Method mGetLabelMethod;
-
- 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);
+ private final AlphabeticIndex.ImmutableIndex mAlphabeticIndex;
- Class clazz = Class.forName("android.icu.text.AlphabeticIndex");
- mAlphabeticIndex = clazz.getConstructor(Locale.class).newInstance(primaryLocale);
+ public AlphabeticIndexVN(Context context) {
+ LocaleList locales = context.getResources().getConfiguration().getLocales();
+ int localeCount = locales.size();
- 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();
}
}
}