Merge "Removing reflection from AlphabeticIndexCompat" into ub-launcher3-calgary-polish

This commit is contained in:
Sunny Goyal
2016-09-27 17:03:34 +00:00
committed by Android (Google) Code Review
@@ -1,12 +1,14 @@
package com.android.launcher3.compat; package com.android.launcher3.compat;
import android.annotation.TargetApi;
import android.content.Context; 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 android.util.Log;
import com.android.launcher3.Utilities; import com.android.launcher3.Utilities;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Locale; import java.util.Locale;
@@ -157,69 +159,39 @@ public class AlphabeticIndexCompat {
} }
/** /**
* Reflected android.icu.text.AlphabeticIndex implementation, falls back to the base * Implementation based on {@link AlphabeticIndex}.
* alphabetic index.
*/ */
@TargetApi(Build.VERSION_CODES.N)
private static class AlphabeticIndexVN extends BaseIndex { private static class AlphabeticIndexVN extends BaseIndex {
private Object mAlphabeticIndex; private final AlphabeticIndex.ImmutableIndex mAlphabeticIndex;
private Method mGetBucketIndexMethod;
private Method mGetBucketMethod; public AlphabeticIndexVN(Context context) {
private Method mGetLabelMethod; LocaleList locales = context.getResources().getConfiguration().getLocales();
int localeCount = locales.size();
public AlphabeticIndexVN(Context context) throws Exception { Locale primaryLocale = localeCount == 0 ? Locale.ENGLISH : locales.get(0);
// TODO: Replace this with locale list once available. AlphabeticIndex indexBuilder = new AlphabeticIndex(primaryLocale);
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);
for (int i = 1; i < localeCount; i++) { for (int i = 1; i < localeCount; i++) {
Locale l = (Locale) localeGetter.invoke(locales, i); indexBuilder.addLabels(locales.get(i));
addLocales.invoke(mAlphabeticIndex, new Object[]{ new Locale[] {l}});
} }
addLocales.invoke(mAlphabeticIndex, new Object[]{ new Locale[] {Locale.ENGLISH}}); indexBuilder.addLabels(Locale.ENGLISH);
mAlphabeticIndex = mAlphabeticIndex.getClass() mAlphabeticIndex = indexBuilder.buildImmutableIndex();
.getDeclaredMethod("buildImmutableIndex")
.invoke(mAlphabeticIndex);
mGetBucketIndexMethod = mAlphabeticIndex.getClass().getDeclaredMethod(
"getBucketIndex", CharSequence.class);
mGetBucketMethod = mAlphabeticIndex.getClass().getDeclaredMethod("getBucket", int.class);
mGetLabelMethod = mGetBucketMethod.getReturnType().getDeclaredMethod("getLabel");
} }
/** /**
* Returns the index of the bucket in which {@param s} should appear. * Returns the index of the bucket in which {@param s} should appear.
*/ */
protected int getBucketIndex(String s) { protected int getBucketIndex(String s) {
try { return mAlphabeticIndex.getBucketIndex(s);
return (Integer) mGetBucketIndexMethod.invoke(mAlphabeticIndex, s);
} catch (Exception e) {
e.printStackTrace();
}
return super.getBucketIndex(s);
} }
/** /**
* Returns the label for the bucket at the given index * Returns the label for the bucket at the given index
*/ */
protected String getBucketLabel(int index) { protected String getBucketLabel(int index) {
try { return mAlphabeticIndex.getBucket(index).getLabel();
return (String) mGetLabelMethod.invoke(
mGetBucketMethod.invoke(mAlphabeticIndex, index));
} catch (Exception e) {
e.printStackTrace();
}
return super.getBucketLabel(index);
} }
} }
} }