Android-x86
Fork
Faire un don

  • R/O
  • HTTP
  • SSH
  • HTTPS

packages-apps-Trebuchet: Commit

packages/apps/Trebuchet


Commit MetaInfo

Révision3d8069053e9b5c17ab25cbbbeb916c233f6da24b (tree)
l'heure2017-11-25 20:06:14
AuteurGabriele M <moto.falcon.git@gmai...>
CommiterGabriele M

Message de Log

Trebuchet: Use ICU4J public APIs

The private implementation of AlphabeticIndexCompat in libcore.icu
has been removed, so Trebuchet always uses BaseAlphabeticIndex,
which doesn't work properly with all the alphabets. Since Android
7.0, the Android platform exposes a subset of the ICU4J APIs, so
switch to that and drop the fallback code.

Change-Id: Id8e5538f70d074d5ffeea0c23ef3f2a72ec0f3bf

Change Summary

Modification

--- a/src/com/android/launcher3/compat/AlphabeticIndexCompat.java
+++ b/src/com/android/launcher3/compat/AlphabeticIndexCompat.java
@@ -1,119 +1,29 @@
11 package com.android.launcher3.compat;
22
33 import android.content.Context;
4+import android.icu.text.AlphabeticIndex;
5+
46 import com.android.launcher3.Utilities;
57
6-import java.lang.reflect.Constructor;
7-import java.lang.reflect.Method;
8-import java.text.Normalizer;
98 import java.util.Locale;
109
11-/**
12- * Fallback class to support Alphabetic indexing if not supported by the framework.
13- * TODO(winsonc): disable for non-english locales
14- */
15-class BaseAlphabeticIndex {
16-
17- private static final String BUCKETS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ-";
18- private static final int UNKNOWN_BUCKET_INDEX = BUCKETS.length() - 1;
19-
20- public BaseAlphabeticIndex() {}
21-
22- /**
23- * Sets the max number of the label buckets in this index.
24- */
25- public void setMaxLabelCount(int count) {
26- // Not currently supported
27- }
28-
29- /**
30- * Returns the index of the bucket in which the given string should appear.
31- */
32- protected int getBucketIndex(String s) {
33- if (s.isEmpty()) {
34- return UNKNOWN_BUCKET_INDEX;
35- }
36- String asciiName = Normalizer.normalize(s, Normalizer.Form.NFD)
37- .replaceAll("[^\\p{ASCII}]", "");
38- if (asciiName.isEmpty()) {
39- return UNKNOWN_BUCKET_INDEX;
40- }
41- int index = BUCKETS.indexOf(asciiName.substring(0, 1).toUpperCase());
42- if (index != -1) {
43- return index;
44- }
45- return UNKNOWN_BUCKET_INDEX;
46- }
47-
48- /**
49- * Returns the label for the bucket at the given index (as returned by getBucketIndex).
50- */
51- protected String getBucketLabel(int index) {
52- return BUCKETS.substring(index, index + 1);
53- }
54-}
55-
56-/**
57- * Reflected libcore.icu.AlphabeticIndex implementation, falls back to the base alphabetic index.
58- */
59-public class AlphabeticIndexCompat extends BaseAlphabeticIndex {
10+public class AlphabeticIndexCompat {
6011
6112 private static final String MID_DOT = "\u2219";
6213
63- private Object mAlphabeticIndex;
64- private Method mAddLabelsMethod;
65- private Method mSetMaxLabelCountMethod;
66- private Method mGetBucketIndexMethod;
67- private Method mGetBucketLabelMethod;
68- private boolean mHasValidAlphabeticIndex;
14+ private AlphabeticIndex.ImmutableIndex mAlphabeticIndex;
6915 private String mDefaultMiscLabel;
7016
7117 public AlphabeticIndexCompat(Context context) {
72- super();
73- try {
74- Locale curLocale = context.getResources().getConfiguration().locale;
75- Class clazz = Class.forName("libcore.icu.AlphabeticIndex");
76- Constructor ctor = clazz.getConstructor(Locale.class);
77- mAddLabelsMethod = clazz.getDeclaredMethod("addLabels", Locale.class);
78- mSetMaxLabelCountMethod = clazz.getDeclaredMethod("setMaxLabelCount", int.class);
79- mGetBucketIndexMethod = clazz.getDeclaredMethod("getBucketIndex", String.class);
80- mGetBucketLabelMethod = clazz.getDeclaredMethod("getBucketLabel", int.class);
81- mAlphabeticIndex = ctor.newInstance(curLocale);
82- try {
83- // Ensure we always have some base English locale buckets
84- if (!curLocale.getLanguage().equals(Locale.ENGLISH.getLanguage())) {
85- mAddLabelsMethod.invoke(mAlphabeticIndex, Locale.ENGLISH);
86- }
87- } catch (Exception e) {
88- e.printStackTrace();
89- }
90- if (curLocale.getLanguage().equals(Locale.JAPANESE.getLanguage())) {
91- // Japanese character 他 ("misc")
92- mDefaultMiscLabel = "\u4ed6";
93- // TODO(winsonc, omakoto): We need to handle Japanese sections better, especially the kanji
94- } else {
95- // Dot
96- mDefaultMiscLabel = MID_DOT;
97- }
98- mHasValidAlphabeticIndex = true;
99- } catch (Exception e) {
100- mHasValidAlphabeticIndex = false;
101- }
102- }
103-
104- /**
105- * Sets the max number of the label buckets in this index.
106- * (ICU 51 default is 99)
107- */
108- public void setMaxLabelCount(int count) {
109- if (mHasValidAlphabeticIndex) {
110- try {
111- mSetMaxLabelCountMethod.invoke(mAlphabeticIndex, count);
112- } catch (Exception e) {
113- e.printStackTrace();
114- }
18+ Locale curLocale = context.getResources().getConfiguration().getLocales().get(0);
19+ mAlphabeticIndex = new AlphabeticIndex(curLocale).buildImmutableIndex();
20+ if (curLocale.getLanguage().equals(Locale.JAPANESE.getLanguage())) {
21+ // Japanese character 他 ("misc")
22+ mDefaultMiscLabel = "\u4ed6";
23+ // TODO(winsonc, omakoto): We need to handle Japanese sections better, especially the kanji
11524 } else {
116- super.setMaxLabelCount(count);
25+ // Dot
26+ mDefaultMiscLabel = MID_DOT;
11727 }
11828 }
11929
@@ -122,7 +32,8 @@ public class AlphabeticIndexCompat extends BaseAlphabeticIndex {
12232 */
12333 public String computeSectionName(CharSequence cs) {
12434 String s = Utilities.trim(cs);
125- String sectionName = getBucketLabel(getBucketIndex(s));
35+ int bucketIndex = mAlphabeticIndex.getBucketIndex(s);
36+ String sectionName = mAlphabeticIndex.getBucket(bucketIndex).getLabel();
12637 if (Utilities.trim(sectionName).isEmpty()) {
12738 if (s.length() > 0) {
12839 int c = s.codePointAt(0);
@@ -149,34 +60,4 @@ public class AlphabeticIndexCompat extends BaseAlphabeticIndex {
14960 }
15061 return sectionName;
15162 }
152-
153- /**
154- * Returns the index of the bucket in which {@param s} should appear.
155- * Function is synchronized because underlying routine walks an iterator
156- * whose state is maintained inside the index object.
157- */
158- protected int getBucketIndex(String s) {
159- if (mHasValidAlphabeticIndex) {
160- try {
161- return (Integer) mGetBucketIndexMethod.invoke(mAlphabeticIndex, s);
162- } catch (Exception e) {
163- e.printStackTrace();
164- }
165- }
166- return super.getBucketIndex(s);
167- }
168-
169- /**
170- * Returns the label for the bucket at the given index (as returned by getBucketIndex).
171- */
172- protected String getBucketLabel(int index) {
173- if (mHasValidAlphabeticIndex) {
174- try {
175- return (String) mGetBucketLabelMethod.invoke(mAlphabeticIndex, index);
176- } catch (Exception e) {
177- e.printStackTrace();
178- }
179- }
180- return super.getBucketLabel(index);
181- }
18263 }
Afficher sur ancien navigateur de dépôt.