Merge "Support system locale as the locale of the spell checkers"

This commit is contained in:
satok
2011-08-26 02:55:06 -07:00
committed by Android (Google) Code Review
2 changed files with 51 additions and 19 deletions

View File

@@ -72,27 +72,48 @@ public final class TextServicesManager {
* languages in settings will be returned. * languages in settings will be returned.
* @return the spell checker session of the spell checker * @return the spell checker session of the spell checker
*/ */
// TODO: Add a method to get enabled spell checkers.
// TODO: Handle referToSpellCheckerLanguageSettings
public SpellCheckerSession newSpellCheckerSession(Bundle bundle, Locale locale, public SpellCheckerSession newSpellCheckerSession(Bundle bundle, Locale locale,
SpellCheckerSessionListener listener, boolean referToSpellCheckerLanguageSettings) { SpellCheckerSessionListener listener, boolean referToSpellCheckerLanguageSettings) {
if (listener == null) { if (listener == null) {
throw new NullPointerException(); throw new NullPointerException();
} }
// TODO: set a proper locale instead of the dummy locale
final String localeString = locale == null ? "en" : locale.toString();
final SpellCheckerInfo sci; final SpellCheckerInfo sci;
try { try {
sci = sService.getCurrentSpellChecker(localeString); sci = sService.getCurrentSpellChecker(null);
} catch (RemoteException e) { } catch (RemoteException e) {
return null; return null;
} }
if (sci == null) { if (sci == null) {
return null; return null;
} }
SpellCheckerSubtype subtypeInUse = null;
if (referToSpellCheckerLanguageSettings) {
subtypeInUse = getCurrentSpellCheckerSubtype(true);
if (subtypeInUse == null) {
return null;
}
if (locale != null) {
final String subtypeLocale = subtypeInUse.getLocale();
final String inputLocale = locale.toString();
if (subtypeLocale.length() < 2 || inputLocale.length() < 2
|| !subtypeLocale.substring(0, 2).equals(inputLocale.substring(0, 2))) {
return null;
}
}
} else {
for (int i = 0; i < sci.getSubtypeCount(); ++i) {
final SpellCheckerSubtype subtype = sci.getSubtypeAt(i);
if (subtype.getLocale().equals(locale)) {
subtypeInUse = subtype;
}
}
}
if (subtypeInUse == null) {
return null;
}
final SpellCheckerSession session = new SpellCheckerSession(sci, sService, listener); final SpellCheckerSession session = new SpellCheckerSession(sci, sService, listener);
try { try {
sService.getSpellCheckerService(sci.getId(), localeString, sService.getSpellCheckerService(sci.getId(), subtypeInUse.getLocale(),
session.getTextServicesSessionListener(), session.getTextServicesSessionListener(),
session.getSpellCheckerSessionListener(), bundle); session.getSpellCheckerSessionListener(), bundle);
} catch (RemoteException e) { } catch (RemoteException e) {

View File

@@ -210,29 +210,40 @@ public class TextServicesManagerService extends ITextServicesManager.Stub {
} }
return null; return null;
} }
if (TextUtils.isEmpty(subtypeHashCodeStr)) { final int hashCode;
if (DBG) { if (!TextUtils.isEmpty(subtypeHashCodeStr)) {
Slog.w(TAG, "Return first subtype in " + sci.getId()); hashCode = Integer.valueOf(subtypeHashCodeStr);
} } else {
return null; hashCode = 0;
} }
final int hashCode = Integer.valueOf(subtypeHashCodeStr); if (hashCode == 0 && !allowImplicitlySelectedSubtype) {
if (hashCode == 0) {
return null; return null;
} }
final String systemLocale =
mContext.getResources().getConfiguration().locale.toString();
SpellCheckerSubtype candidate = null;
for (int i = 0; i < sci.getSubtypeCount(); ++i) { for (int i = 0; i < sci.getSubtypeCount(); ++i) {
final SpellCheckerSubtype scs = sci.getSubtypeAt(i); final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
if (scs.hashCode() == hashCode) { if (hashCode == 0) {
if (systemLocale.equals(locale)) {
return scs;
} else if (candidate == null) {
final String scsLocale = scs.getLocale();
if (systemLocale.length() >= 2
&& scsLocale.length() >= 2
&& systemLocale.substring(0, 2).equals(
scsLocale.substring(0, 2))) {
candidate = scs;
}
}
} else if (scs.hashCode() == hashCode) {
if (DBG) { if (DBG) {
Slog.w(TAG, "Return subtype " + scs.hashCode()); Slog.w(TAG, "Return subtype " + scs.hashCode());
} }
return scs; return scs;
} }
} }
if (DBG) { return candidate;
Slog.w(TAG, "Return first subtype in " + sci.getId());
}
return null;
} }
} }