1616
1717package io .github .sds100 .keymapper .inputmethod .latin ;
1818
19- import static io .github .sds100 .keymapper .inputmethod .latin .common .Constants .ImeOption .FORCE_ASCII ;
20- import static io .github .sds100 .keymapper .inputmethod .latin .common .Constants .ImeOption .NO_MICROPHONE ;
21- import static io .github .sds100 .keymapper .inputmethod .latin .common .Constants .ImeOption .NO_MICROPHONE_COMPAT ;
22-
2319import android .app .AlertDialog ;
2420import android .content .BroadcastReceiver ;
25- import android .content .ComponentName ;
2621import android .content .Context ;
2722import android .content .DialogInterface ;
2823import android .content .DialogInterface .OnClickListener ;
2924import android .content .Intent ;
3025import android .content .IntentFilter ;
31- import android .content .ServiceConnection ;
3226import android .content .res .Configuration ;
3327import android .content .res .Resources ;
3428import android .graphics .Color ;
3933import android .os .IBinder ;
4034import android .os .Message ;
4135import android .os .Process ;
42- import android .os .RemoteException ;
4336import android .text .InputType ;
4437import android .util .Log ;
4538import android .util .PrintWriterPrinter ;
6558
6659import javax .annotation .Nonnull ;
6760
68- import io .github .sds100 .keymapper .api . IKeyEventReceiver ;
61+ import io .github .sds100 .keymapper .IKeyEventRelayServiceCallback ;
6962import io .github .sds100 .keymapper .inputmethod .accessibility .AccessibilityUtils ;
7063import io .github .sds100 .keymapper .inputmethod .annotations .UsedForTesting ;
7164import io .github .sds100 .keymapper .inputmethod .compat .EditorInfoCompatUtils ;
108101import io .github .sds100 .keymapper .inputmethod .latin .utils .SubtypeLocaleUtils ;
109102import io .github .sds100 .keymapper .inputmethod .latin .utils .ViewLayoutUtils ;
110103
104+ import static io .github .sds100 .keymapper .inputmethod .latin .common .Constants .ImeOption .FORCE_ASCII ;
105+ import static io .github .sds100 .keymapper .inputmethod .latin .common .Constants .ImeOption .NO_MICROPHONE ;
106+ import static io .github .sds100 .keymapper .inputmethod .latin .common .Constants .ImeOption .NO_MICROPHONE_COMPAT ;
107+
111108/**
112109 * Input method implementation for Qwerty'ish keyboard.
113110 */
@@ -167,7 +164,8 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
167164 private SuggestionStripView mSuggestionStripView ;
168165
169166 private RichInputMethodManager mRichImm ;
170- @ UsedForTesting final KeyboardSwitcher mKeyboardSwitcher ;
167+ @ UsedForTesting
168+ final KeyboardSwitcher mKeyboardSwitcher ;
171169 private final SubtypeState mSubtypeState = new SubtypeState ();
172170 private EmojiAltPhysicalKeyDetector mEmojiAltPhysicalKeyDetector ;
173171 private StatsUtilsManager mStatsUtilsManager ;
@@ -199,6 +197,7 @@ public void onReceive(Context context, Intent intent) {
199197 }
200198 }
201199 }
200+
202201 final HideSoftInputReceiver mHideSoftInputReceiver = new HideSoftInputReceiver (this );
203202
204203 final static class RestartAfterDeviceUnlockReceiver extends BroadcastReceiver {
@@ -220,22 +219,17 @@ public void onReceive(Context context, Intent intent) {
220219
221220 final RestartAfterDeviceUnlockReceiver mRestartAfterDeviceUnlockReceiver = new RestartAfterDeviceUnlockReceiver ();
222221
223- private final Object mKeyEventReceiverLock = new Object ();
224- private IKeyEventReceiver mKeyEventReceiverBinder = null ;
225-
226- private final ServiceConnection mKeyEventReceiverConnection = new ServiceConnection () {
222+ private KeyEventRelayServiceWrapperImpl mKeyEventRelayServiceWrapper ;
223+ private IKeyEventRelayServiceCallback mKeyEventRelayServiceCallback = new IKeyEventRelayServiceCallback .Stub () {
227224 @ Override
228- public void onServiceConnected (ComponentName name , IBinder service ) {
229- synchronized (mKeyEventReceiverLock ) {
230- mKeyEventReceiverBinder = IKeyEventReceiver .Stub .asInterface (service );
231- }
232- }
225+ public boolean onKeyEvent (KeyEvent event , String sourcePackageName ) {
226+ InputConnection ic = getCurrentInputConnection ();
233227
234- @ Override
235- public void onServiceDisconnected (ComponentName name ) {
236- synchronized (mKeyEventReceiverLock ) {
237- mKeyEventReceiverBinder = null ;
228+ if (ic == null ) {
229+ return false ;
238230 }
231+
232+ return ic .sendKeyEvent (event );
239233 }
240234 };
241235
@@ -438,7 +432,7 @@ public void handleMessage(final Message msg) {
438432 latinIme .deallocateMemory ();
439433 break ;
440434 case MSG_SWITCH_LANGUAGE_AUTOMATICALLY :
441- latinIme .switchLanguage ((InputMethodSubtype )msg .obj );
435+ latinIme .switchLanguage ((InputMethodSubtype ) msg .obj );
442436 break ;
443437 case MSG_UPDATE_CLIPBOARD_PINNED_CLIPS :
444438 @ SuppressWarnings ("unchecked" )
@@ -782,14 +776,10 @@ public void onCreate() {
782776
783777 registerReceiver (mKeyMapperBroadcastReceiver , keyMapperIntentFilter );
784778
785- try {
786- ComponentName keyEventReceiverComponent = new ComponentName ("io.github.sds100.keymapper" , "io.github.sds100.keymapper.api.KeyEventReceiver" );
787- Intent keyEventReceiverServiceIntent = new Intent ();
788- keyEventReceiverServiceIntent .setComponent (keyEventReceiverComponent );
789- bindService (keyEventReceiverServiceIntent , mKeyEventReceiverConnection , 0 );
790- } catch (SecurityException e ) {
791- Log .e (TAG , e .toString ());
792- }
779+ mKeyEventRelayServiceWrapper =
780+ new KeyEventRelayServiceWrapperImpl (getApplicationContext (), mKeyEventRelayServiceCallback );
781+
782+ mKeyEventRelayServiceWrapper .bind ();
793783
794784 StatsUtils .onCreate (mSettings .getCurrent (), mRichImm );
795785 }
@@ -903,7 +893,7 @@ public void onDestroy() {
903893 unregisterReceiver (mRestartAfterDeviceUnlockReceiver );
904894 unregisterReceiver (mKeyMapperBroadcastReceiver );
905895 mStatsUtilsManager .onDestroy (this /* context */ );
906- unbindService ( mKeyEventReceiverConnection );
896+ mKeyEventRelayServiceWrapper . unbind ( );
907897 super .onDestroy ();
908898 }
909899
@@ -1483,7 +1473,7 @@ public int getCurrentRecapitalizeState() {
14831473
14841474 /**
14851475 * @param codePoints code points to get coordinates for.
1486- * @return x,y coordinates for this keyboard, as a flattened array.
1476+ * @return x, y coordinates for this keyboard, as a flattened array.
14871477 */
14881478 public int [] getCoordinatesForCurrentKeyboard (final int [] codePoints ) {
14891479 final Keyboard keyboard = mKeyboardSwitcher .getKeyboard ();
@@ -1528,7 +1518,7 @@ public void onMovePointer(int steps) {
15281518 // for RTL languages we want to invert pointer movement
15291519 if (mRichImm .getCurrentSubtype ().isRtlSubtype ())
15301520 steps = -steps ;
1531-
1521+
15321522 mInputLogic .finishInput ();
15331523 if (steps < 0 ) {
15341524 int availableCharacters = mInputLogic .mConnection .getTextBeforeCursor (64 , 0 ).length ();
@@ -1687,6 +1677,7 @@ public void onCancelBatchInput() {
16871677 * IME for the full gesture, possibly updating the TextView to reflect the first suggestion.
16881678 * <p>
16891679 * This method must be run on the UI Thread.
1680+ *
16901681 * @param suggestedWords suggested words by the IME for the full gesture.
16911682 */
16921683 public void onTailBatchInputResultShown (final SuggestedWords suggestedWords ) {
@@ -1836,6 +1827,7 @@ void loadKeyboard() {
18361827 * After an input transaction has been executed, some state must be updated. This includes
18371828 * the shift state of the keyboard and suggestions. This method looks at the finished
18381829 * inputTransaction to find out what is necessary and updates the state accordingly.
1830+ *
18391831 * @param inputTransaction The transaction that has been executed.
18401832 */
18411833 private void updateStateAfterInputTransaction (final InputTransaction inputTransaction ) {
@@ -1923,13 +1915,9 @@ private HardwareEventDecoder getHardwareKeyEventDecoder(final int deviceId) {
19231915 @ Override
19241916 public boolean onKeyDown (final int keyCode , final KeyEvent keyEvent ) {
19251917
1926- if (mKeyEventReceiverBinder != null ) {
1927- try {
1928- if (mKeyEventReceiverBinder .onKeyEvent (keyEvent )) {
1929- return true ;
1930- }
1931- } catch (RemoteException e ) {
1932-
1918+ if (mKeyEventRelayServiceWrapper != null ) {
1919+ if (mKeyEventRelayServiceWrapper .sendKeyEvent (keyEvent , "io.github.sds100.keymapper" )) {
1920+ return true ;
19331921 }
19341922 }
19351923
@@ -1959,13 +1947,9 @@ public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent) {
19591947
19601948 @ Override
19611949 public boolean onKeyUp (final int keyCode , final KeyEvent keyEvent ) {
1962- if (mKeyEventReceiverBinder != null ) {
1963- try {
1964- if (mKeyEventReceiverBinder .onKeyEvent (keyEvent )) {
1965- return true ;
1966- }
1967- } catch (RemoteException e ) {
1968-
1950+ if (mKeyEventRelayServiceWrapper != null ) {
1951+ if (mKeyEventRelayServiceWrapper .sendKeyEvent (keyEvent , "io.github.sds100.keymapper" )) {
1952+ return true ;
19691953 }
19701954 }
19711955
@@ -2024,7 +2008,7 @@ private void showSubtypeSelectorAndSettings() {
20242008 final CharSequence title = getString (R .string .english_ime_input_options );
20252009 // TODO: Should use new string "Select active input modes".
20262010 final CharSequence languageSelectionTitle = getString (R .string .language_selection_title );
2027- final CharSequence [] items = new CharSequence [] {
2011+ final CharSequence [] items = new CharSequence []{
20282012 languageSelectionTitle ,
20292013 getString (ApplicationUtils .getActivityTitleResId (this , SettingsActivity .class ))
20302014 };
0 commit comments