Skip to content

Commit bbd3155

Browse files
committed
NPE when iterating by character and word in Launcher widgets.
1. The character and word iterators were use the application context to keep track of locale changes. However, for widgets the context from which the app context is obtained is custom created therefore the app context is null and the iterators code does not expect that. Now we are caching the locale and update it when the configuration changes. bug:6642281 Change-Id: I3fd201ab9e4efd79e3bdc8afd8ee644e4354a7fb
1 parent 4206ee2 commit bbd3155

File tree

2 files changed

+16
-16
lines changed

2 files changed

+16
-16
lines changed

core/java/android/view/AccessibilityIterators.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,19 @@ static class CharacterTextSegmentIterator extends AbstractTextSegmentIterator
7070
implements ComponentCallbacks {
7171
private static CharacterTextSegmentIterator sInstance;
7272

73-
private final Context mAppContext;
73+
private Locale mLocale;
7474

7575
protected BreakIterator mImpl;
7676

77-
public static CharacterTextSegmentIterator getInstance(Context context) {
77+
public static CharacterTextSegmentIterator getInstance(Locale locale) {
7878
if (sInstance == null) {
79-
sInstance = new CharacterTextSegmentIterator(context);
79+
sInstance = new CharacterTextSegmentIterator(locale);
8080
}
8181
return sInstance;
8282
}
8383

84-
private CharacterTextSegmentIterator(Context context) {
85-
mAppContext = context.getApplicationContext();
86-
Locale locale = mAppContext.getResources().getConfiguration().locale;
84+
private CharacterTextSegmentIterator(Locale locale) {
85+
mLocale = locale;
8786
onLocaleChanged(locale);
8887
ViewRootImpl.addConfigCallback(this);
8988
}
@@ -148,10 +147,9 @@ public int[] preceding(int offset) {
148147

149148
@Override
150149
public void onConfigurationChanged(Configuration newConfig) {
151-
Configuration oldConfig = mAppContext.getResources().getConfiguration();
152-
final int changed = oldConfig.diff(newConfig);
153-
if ((changed & ActivityInfo.CONFIG_LOCALE) != 0) {
154-
Locale locale = newConfig.locale;
150+
Locale locale = newConfig.locale;
151+
if (!mLocale.equals(locale)) {
152+
mLocale = locale;
155153
onLocaleChanged(locale);
156154
}
157155
}
@@ -169,15 +167,15 @@ protected void onLocaleChanged(Locale locale) {
169167
static class WordTextSegmentIterator extends CharacterTextSegmentIterator {
170168
private static WordTextSegmentIterator sInstance;
171169

172-
public static WordTextSegmentIterator getInstance(Context context) {
170+
public static WordTextSegmentIterator getInstance(Locale locale) {
173171
if (sInstance == null) {
174-
sInstance = new WordTextSegmentIterator(context);
172+
sInstance = new WordTextSegmentIterator(locale);
175173
}
176174
return sInstance;
177175
}
178176

179-
private WordTextSegmentIterator(Context context) {
180-
super(context);
177+
private WordTextSegmentIterator(Locale locale) {
178+
super(locale);
181179
}
182180

183181
@Override

core/java/android/view/View.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6957,7 +6957,8 @@ public TextSegmentIterator getIteratorForGranularity(int granularity) {
69576957
CharSequence text = getIterableTextForAccessibility();
69586958
if (text != null && text.length() > 0) {
69596959
CharacterTextSegmentIterator iterator =
6960-
CharacterTextSegmentIterator.getInstance(mContext);
6960+
CharacterTextSegmentIterator.getInstance(
6961+
mContext.getResources().getConfiguration().locale);
69616962
iterator.initialize(text.toString());
69626963
return iterator;
69636964
}
@@ -6966,7 +6967,8 @@ public TextSegmentIterator getIteratorForGranularity(int granularity) {
69666967
CharSequence text = getIterableTextForAccessibility();
69676968
if (text != null && text.length() > 0) {
69686969
WordTextSegmentIterator iterator =
6969-
WordTextSegmentIterator.getInstance(mContext);
6970+
WordTextSegmentIterator.getInstance(
6971+
mContext.getResources().getConfiguration().locale);
69706972
iterator.initialize(text.toString());
69716973
return iterator;
69726974
}

0 commit comments

Comments
 (0)