Skip to content

Commit cfc0f2c

Browse files
satok16Android (Google) Code Review
authored andcommitted
Merge "Add functions to set / get SpellCheckerSubtype"
2 parents bc714a3 + ada8c4e commit cfc0f2c

File tree

4 files changed

+118
-8
lines changed

4 files changed

+118
-8
lines changed

core/java/android/provider/Settings.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3768,12 +3768,21 @@ public static final String getBluetoothInputDevicePriorityKey(String address) {
37683768

37693769

37703770
/**
3771-
* The {@link ComponentName} string of the service to be used as the spell checker
3771+
* The {@link ComponentName} string of the selected spell checker service which is
3772+
* one of the services managed by the text service manager.
3773+
*
3774+
* @hide
3775+
*/
3776+
public static final String SELECTED_SPELL_CHECKER = "selected_spell_checker";
3777+
3778+
/**
3779+
* The {@link ComponentName} string of the selected subtype of the selected spell checker
37723780
* service which is one of the services managed by the text service manager.
37733781
*
37743782
* @hide
37753783
*/
3776-
public static final String SPELL_CHECKER_SERVICE = "spell_checker_service";
3784+
public static final String SELECTED_SPELL_CHECKER_SUBTYPE =
3785+
"selected_spell_checker_subtype";
37773786

37783787
/**
37793788
* What happens when the user presses the Power button while in-call

core/java/android/view/textservice/TextServicesManager.java

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,40 @@ public SpellCheckerInfo getCurrentSpellChecker() {
135135
public void setCurrentSpellChecker(SpellCheckerInfo sci) {
136136
try {
137137
if (sci == null) {
138-
throw new NullPointerException("SpellCheckerInfo is null");
138+
throw new NullPointerException("SpellCheckerInfo is null.");
139139
}
140-
sService.setCurrentSpellChecker(sci.getId());
140+
sService.setCurrentSpellChecker(null, sci.getId());
141141
} catch (RemoteException e) {
142142
Log.e(TAG, "Error in setCurrentSpellChecker: " + e);
143143
}
144144
}
145+
146+
/**
147+
* @hide
148+
*/
149+
public SpellCheckerSubtype getCurrentSpellCheckerSubtype() {
150+
try {
151+
// Passing null as a locale for ICS
152+
return sService.getCurrentSpellCheckerSubtype(null);
153+
} catch (RemoteException e) {
154+
Log.e(TAG, "Error in getCurrentSpellCheckerSubtype: " + e);
155+
return null;
156+
}
157+
}
158+
159+
/**
160+
* @hide
161+
*/
162+
public void setSpellCheckerSubtype(SpellCheckerSubtype subtype) {
163+
try {
164+
if (subtype == null) {
165+
throw new NullPointerException("SpellCheckerSubtype is null.");
166+
}
167+
sService.setCurrentSpellCheckerSubtype(null, subtype.hashCode());
168+
} catch (RemoteException e) {
169+
Log.e(TAG, "Error in setSpellCheckerSubtype:" + e);
170+
}
171+
}
172+
173+
145174
}

core/java/com/android/internal/textservice/ITextServicesManager.aidl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,20 @@ import com.android.internal.textservice.ITextServicesSessionListener;
2222
import android.content.ComponentName;
2323
import android.os.Bundle;
2424
import android.view.textservice.SpellCheckerInfo;
25+
import android.view.textservice.SpellCheckerSubtype;
2526

2627
/**
2728
* Interface to the text service manager.
2829
* @hide
2930
*/
3031
interface ITextServicesManager {
3132
SpellCheckerInfo getCurrentSpellChecker(String locale);
33+
SpellCheckerSubtype getCurrentSpellCheckerSubtype(String locale);
3234
oneway void getSpellCheckerService(String sciId, in String locale,
3335
in ITextServicesSessionListener tsListener,
3436
in ISpellCheckerSessionListener scListener, in Bundle bundle);
3537
oneway void finishSpellCheckerService(in ISpellCheckerSessionListener listener);
36-
oneway void setCurrentSpellChecker(String sciId);
38+
oneway void setCurrentSpellChecker(String locale, String sciId);
39+
oneway void setCurrentSpellCheckerSubtype(String locale, int hashCode);
3740
SpellCheckerInfo[] getEnabledSpellCheckers();
3841
}

services/java/com/android/server/TextServicesManagerService.java

Lines changed: 72 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import android.text.TextUtils;
4242
import android.util.Slog;
4343
import android.view.textservice.SpellCheckerInfo;
44+
import android.view.textservice.SpellCheckerSubtype;
4445

4546
import java.io.IOException;
4647
import java.util.ArrayList;
@@ -174,7 +175,7 @@ public SpellCheckerInfo getCurrentSpellChecker(String locale) {
174175
synchronized (mSpellCheckerMap) {
175176
String curSpellCheckerId =
176177
Settings.Secure.getString(mContext.getContentResolver(),
177-
Settings.Secure.SPELL_CHECKER_SERVICE);
178+
Settings.Secure.SELECTED_SPELL_CHECKER);
178179
if (DBG) {
179180
Slog.w(TAG, "getCurrentSpellChecker: " + curSpellCheckerId);
180181
}
@@ -185,6 +186,35 @@ public SpellCheckerInfo getCurrentSpellChecker(String locale) {
185186
}
186187
}
187188

189+
// TODO: Save SpellCheckerSubtype by supported languages.
190+
@Override
191+
public SpellCheckerSubtype getCurrentSpellCheckerSubtype(String locale) {
192+
synchronized (mSpellCheckerMap) {
193+
final String subtypeHashCodeStr =
194+
Settings.Secure.getString(mContext.getContentResolver(),
195+
Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE);
196+
if (DBG) {
197+
Slog.w(TAG, "getCurrentSpellChecker: " + subtypeHashCodeStr);
198+
}
199+
final SpellCheckerInfo sci = getCurrentSpellChecker(null);
200+
if (sci.getSubtypeCount() == 0) {
201+
return null;
202+
}
203+
if (TextUtils.isEmpty(subtypeHashCodeStr)) {
204+
// Return the first Subtype if there is no settings for the current subtype.
205+
return sci.getSubtypeAt(0);
206+
}
207+
final int hashCode = Integer.valueOf(subtypeHashCodeStr);
208+
for (int i = 0; i < sci.getSubtypeCount(); ++i) {
209+
final SpellCheckerSubtype scs = sci.getSubtypeAt(i);
210+
if (scs.hashCode() == hashCode) {
211+
return scs;
212+
}
213+
}
214+
return sci.getSubtypeAt(0);
215+
}
216+
}
217+
188218
@Override
189219
public void getSpellCheckerService(String sciId, String locale,
190220
ITextServicesSessionListener tsListener, ISpellCheckerSessionListener scListener,
@@ -301,7 +331,7 @@ public void finishSpellCheckerService(ISpellCheckerSessionListener listener) {
301331
}
302332

303333
@Override
304-
public void setCurrentSpellChecker(String sciId) {
334+
public void setCurrentSpellChecker(String locale, String sciId) {
305335
synchronized(mSpellCheckerMap) {
306336
if (mContext.checkCallingOrSelfPermission(
307337
android.Manifest.permission.WRITE_SECURE_SETTINGS)
@@ -314,6 +344,20 @@ public void setCurrentSpellChecker(String sciId) {
314344
}
315345
}
316346

347+
@Override
348+
public void setCurrentSpellCheckerSubtype(String locale, int hashCode) {
349+
synchronized(mSpellCheckerMap) {
350+
if (mContext.checkCallingOrSelfPermission(
351+
android.Manifest.permission.WRITE_SECURE_SETTINGS)
352+
!= PackageManager.PERMISSION_GRANTED) {
353+
throw new SecurityException(
354+
"Requires permission "
355+
+ android.Manifest.permission.WRITE_SECURE_SETTINGS);
356+
}
357+
setCurrentSpellCheckerLocked(hashCode);
358+
}
359+
}
360+
317361
private void setCurrentSpellCheckerLocked(String sciId) {
318362
if (DBG) {
319363
Slog.w(TAG, "setCurrentSpellChecker: " + sciId);
@@ -322,7 +366,32 @@ private void setCurrentSpellCheckerLocked(String sciId) {
322366
final long ident = Binder.clearCallingIdentity();
323367
try {
324368
Settings.Secure.putString(mContext.getContentResolver(),
325-
Settings.Secure.SPELL_CHECKER_SERVICE, sciId);
369+
Settings.Secure.SELECTED_SPELL_CHECKER, sciId);
370+
} finally {
371+
Binder.restoreCallingIdentity(ident);
372+
}
373+
}
374+
375+
private void setCurrentSpellCheckerLocked(int hashCode) {
376+
if (DBG) {
377+
Slog.w(TAG, "setCurrentSpellCheckerSubtype: " + hashCode);
378+
}
379+
final SpellCheckerInfo sci = getCurrentSpellChecker(null);
380+
if (sci == null) return;
381+
boolean found = false;
382+
for (int i = 0; i < sci.getSubtypeCount(); ++i) {
383+
if(sci.getSubtypeAt(i).hashCode() == hashCode) {
384+
found = true;
385+
break;
386+
}
387+
}
388+
if (!found) {
389+
return;
390+
}
391+
final long ident = Binder.clearCallingIdentity();
392+
try {
393+
Settings.Secure.putString(mContext.getContentResolver(),
394+
Settings.Secure.SELECTED_SPELL_CHECKER_SUBTYPE, String.valueOf(hashCode));
326395
} finally {
327396
Binder.restoreCallingIdentity(ident);
328397
}

0 commit comments

Comments
 (0)