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 .api .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,19 @@ 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 mKeyEventRelayServiceWrapperRelease ;
223+ private KeyEventRelayServiceWrapperImpl mKeyEventRelayServiceWrapperDebug ;
224+ private KeyEventRelayServiceWrapperImpl mKeyEventRelayServiceWrapperCi ;
225+ private IKeyEventRelayServiceCallback mKeyEventRelayServiceCallback = new IKeyEventRelayServiceCallback . Stub () {
227226 @ Override
228- public void onServiceConnected (ComponentName name , IBinder service ) {
229- synchronized (mKeyEventReceiverLock ) {
230- mKeyEventReceiverBinder = IKeyEventReceiver .Stub .asInterface (service );
231- }
232- }
227+ public boolean onKeyEvent (KeyEvent event , String sourcePackageName ) {
228+ InputConnection ic = getCurrentInputConnection ();
233229
234- @ Override
235- public void onServiceDisconnected (ComponentName name ) {
236- synchronized (mKeyEventReceiverLock ) {
237- mKeyEventReceiverBinder = null ;
230+ if (ic == null ) {
231+ return false ;
238232 }
233+
234+ return ic .sendKeyEvent (event );
239235 }
240236 };
241237
@@ -438,7 +434,7 @@ public void handleMessage(final Message msg) {
438434 latinIme .deallocateMemory ();
439435 break ;
440436 case MSG_SWITCH_LANGUAGE_AUTOMATICALLY :
441- latinIme .switchLanguage ((InputMethodSubtype )msg .obj );
437+ latinIme .switchLanguage ((InputMethodSubtype ) msg .obj );
442438 break ;
443439 case MSG_UPDATE_CLIPBOARD_PINNED_CLIPS :
444440 @ SuppressWarnings ("unchecked" )
@@ -782,14 +778,27 @@ public void onCreate() {
782778
783779 registerReceiver (mKeyMapperBroadcastReceiver , keyMapperIntentFilter );
784780
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- }
781+ // Connect to the different key mapper build types.
782+ mKeyEventRelayServiceWrapperRelease =
783+ new KeyEventRelayServiceWrapperImpl (
784+ getApplicationContext (),
785+ "io.github.sds100.keymapper" ,
786+ mKeyEventRelayServiceCallback );
787+ mKeyEventRelayServiceWrapperRelease .bind ();
788+
789+ mKeyEventRelayServiceWrapperDebug =
790+ new KeyEventRelayServiceWrapperImpl (
791+ getApplicationContext (),
792+ "io.github.sds100.keymapper.debug" ,
793+ mKeyEventRelayServiceCallback );
794+ mKeyEventRelayServiceWrapperDebug .bind ();
795+
796+ mKeyEventRelayServiceWrapperCi =
797+ new KeyEventRelayServiceWrapperImpl (
798+ getApplicationContext (),
799+ "io.github.sds100.keymapper.ci" ,
800+ mKeyEventRelayServiceCallback );
801+ mKeyEventRelayServiceWrapperCi .bind ();
793802
794803 StatsUtils .onCreate (mSettings .getCurrent (), mRichImm );
795804 }
@@ -903,7 +912,9 @@ public void onDestroy() {
903912 unregisterReceiver (mRestartAfterDeviceUnlockReceiver );
904913 unregisterReceiver (mKeyMapperBroadcastReceiver );
905914 mStatsUtilsManager .onDestroy (this /* context */ );
906- unbindService (mKeyEventReceiverConnection );
915+ mKeyEventRelayServiceWrapperRelease .unbind ();
916+ mKeyEventRelayServiceWrapperDebug .unbind ();
917+ mKeyEventRelayServiceWrapperCi .unbind ();
907918 super .onDestroy ();
908919 }
909920
@@ -1483,7 +1494,7 @@ public int getCurrentRecapitalizeState() {
14831494
14841495 /**
14851496 * @param codePoints code points to get coordinates for.
1486- * @return x,y coordinates for this keyboard, as a flattened array.
1497+ * @return x, y coordinates for this keyboard, as a flattened array.
14871498 */
14881499 public int [] getCoordinatesForCurrentKeyboard (final int [] codePoints ) {
14891500 final Keyboard keyboard = mKeyboardSwitcher .getKeyboard ();
@@ -1528,7 +1539,7 @@ public void onMovePointer(int steps) {
15281539 // for RTL languages we want to invert pointer movement
15291540 if (mRichImm .getCurrentSubtype ().isRtlSubtype ())
15301541 steps = -steps ;
1531-
1542+
15321543 mInputLogic .finishInput ();
15331544 if (steps < 0 ) {
15341545 int availableCharacters = mInputLogic .mConnection .getTextBeforeCursor (64 , 0 ).length ();
@@ -1687,6 +1698,7 @@ public void onCancelBatchInput() {
16871698 * IME for the full gesture, possibly updating the TextView to reflect the first suggestion.
16881699 * <p>
16891700 * This method must be run on the UI Thread.
1701+ *
16901702 * @param suggestedWords suggested words by the IME for the full gesture.
16911703 */
16921704 public void onTailBatchInputResultShown (final SuggestedWords suggestedWords ) {
@@ -1836,6 +1848,7 @@ void loadKeyboard() {
18361848 * After an input transaction has been executed, some state must be updated. This includes
18371849 * the shift state of the keyboard and suggestions. This method looks at the finished
18381850 * inputTransaction to find out what is necessary and updates the state accordingly.
1851+ *
18391852 * @param inputTransaction The transaction that has been executed.
18401853 */
18411854 private void updateStateAfterInputTransaction (final InputTransaction inputTransaction ) {
@@ -1923,13 +1936,9 @@ private HardwareEventDecoder getHardwareKeyEventDecoder(final int deviceId) {
19231936 @ Override
19241937 public boolean onKeyDown (final int keyCode , final KeyEvent keyEvent ) {
19251938
1926- if (mKeyEventReceiverBinder != null ) {
1927- try {
1928- if (mKeyEventReceiverBinder .onKeyEvent (keyEvent )) {
1929- return true ;
1930- }
1931- } catch (RemoteException e ) {
1932-
1939+ if (mKeyEventRelayServiceWrapperDebug != null ) {
1940+ if (mKeyEventRelayServiceWrapperDebug .sendKeyEvent (keyEvent , "io.github.sds100.keymapper" )) {
1941+ return true ;
19331942 }
19341943 }
19351944
@@ -1959,13 +1968,9 @@ public boolean onKeyDown(final int keyCode, final KeyEvent keyEvent) {
19591968
19601969 @ Override
19611970 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-
1971+ if (mKeyEventRelayServiceWrapperDebug != null ) {
1972+ if (mKeyEventRelayServiceWrapperDebug .sendKeyEvent (keyEvent , "io.github.sds100.keymapper" )) {
1973+ return true ;
19691974 }
19701975 }
19711976
@@ -2024,7 +2029,7 @@ private void showSubtypeSelectorAndSettings() {
20242029 final CharSequence title = getString (R .string .english_ime_input_options );
20252030 // TODO: Should use new string "Select active input modes".
20262031 final CharSequence languageSelectionTitle = getString (R .string .language_selection_title );
2027- final CharSequence [] items = new CharSequence [] {
2032+ final CharSequence [] items = new CharSequence []{
20282033 languageSelectionTitle ,
20292034 getString (ApplicationUtils .getActivityTitleResId (this , SettingsActivity .class ))
20302035 };
0 commit comments