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
This commit is contained in:
Svetoslav Ganov
2012-06-11 12:08:18 -07:00
parent 4206ee2b68
commit bbd31559f3
2 changed files with 16 additions and 16 deletions

View File

@@ -70,20 +70,19 @@ public final class AccessibilityIterators {
implements ComponentCallbacks {
private static CharacterTextSegmentIterator sInstance;
private final Context mAppContext;
private Locale mLocale;
protected BreakIterator mImpl;
public static CharacterTextSegmentIterator getInstance(Context context) {
public static CharacterTextSegmentIterator getInstance(Locale locale) {
if (sInstance == null) {
sInstance = new CharacterTextSegmentIterator(context);
sInstance = new CharacterTextSegmentIterator(locale);
}
return sInstance;
}
private CharacterTextSegmentIterator(Context context) {
mAppContext = context.getApplicationContext();
Locale locale = mAppContext.getResources().getConfiguration().locale;
private CharacterTextSegmentIterator(Locale locale) {
mLocale = locale;
onLocaleChanged(locale);
ViewRootImpl.addConfigCallback(this);
}
@@ -148,10 +147,9 @@ public final class AccessibilityIterators {
@Override
public void onConfigurationChanged(Configuration newConfig) {
Configuration oldConfig = mAppContext.getResources().getConfiguration();
final int changed = oldConfig.diff(newConfig);
if ((changed & ActivityInfo.CONFIG_LOCALE) != 0) {
Locale locale = newConfig.locale;
Locale locale = newConfig.locale;
if (!mLocale.equals(locale)) {
mLocale = locale;
onLocaleChanged(locale);
}
}
@@ -169,15 +167,15 @@ public final class AccessibilityIterators {
static class WordTextSegmentIterator extends CharacterTextSegmentIterator {
private static WordTextSegmentIterator sInstance;
public static WordTextSegmentIterator getInstance(Context context) {
public static WordTextSegmentIterator getInstance(Locale locale) {
if (sInstance == null) {
sInstance = new WordTextSegmentIterator(context);
sInstance = new WordTextSegmentIterator(locale);
}
return sInstance;
}
private WordTextSegmentIterator(Context context) {
super(context);
private WordTextSegmentIterator(Locale locale) {
super(locale);
}
@Override

View File

@@ -6957,7 +6957,8 @@ public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Cal
CharSequence text = getIterableTextForAccessibility();
if (text != null && text.length() > 0) {
CharacterTextSegmentIterator iterator =
CharacterTextSegmentIterator.getInstance(mContext);
CharacterTextSegmentIterator.getInstance(
mContext.getResources().getConfiguration().locale);
iterator.initialize(text.toString());
return iterator;
}
@@ -6966,7 +6967,8 @@ public class View implements Drawable.Callback, Drawable.Callback2, KeyEvent.Cal
CharSequence text = getIterableTextForAccessibility();
if (text != null && text.length() > 0) {
WordTextSegmentIterator iterator =
WordTextSegmentIterator.getInstance(mContext);
WordTextSegmentIterator.getInstance(
mContext.getResources().getConfiguration().locale);
iterator.initialize(text.toString());
return iterator;
}