-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Android] Return keyboard pressed state
- Loading branch information
Showing
13 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
shell/platform/android/io/flutter/embedding/engine/systemchannels/KeyboardChannel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.embedding.engine.systemchannels; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import io.flutter.Log; | ||
import io.flutter.embedding.engine.dart.DartExecutor; | ||
import io.flutter.plugin.common.MethodCall; | ||
import io.flutter.plugin.common.MethodChannel; | ||
import io.flutter.plugin.common.StandardMethodCodec; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* Event message channel for keyboard events to/from the Flutter framework. | ||
* | ||
* <p>Receives asynchronous messages from the framework to query the engine known pressed state. | ||
*/ | ||
public class KeyboardChannel { | ||
private static final String TAG = "KeyboardChannel"; | ||
|
||
public final MethodChannel channel; | ||
private KeyboardMethodHandler keyboardMethodHandler; | ||
|
||
@NonNull | ||
public final MethodChannel.MethodCallHandler parsingMethodHandler = | ||
new MethodChannel.MethodCallHandler() { | ||
@Override | ||
public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) { | ||
if (keyboardMethodHandler == null) { | ||
Log.v(TAG, "No KeyboardMethodHandler registered, call not forwarded to keyboard API."); | ||
return; | ||
} | ||
String method = call.method; | ||
Log.v(TAG, "Received '" + method + "' message."); | ||
switch (method) { | ||
case "getKeyboardState": | ||
Map<Long, Long> pressedState = new HashMap<>(); | ||
try { | ||
pressedState = keyboardMethodHandler.getKeyboardState(); | ||
} catch (IllegalStateException exception) { | ||
result.error("error", exception.getMessage(), null); | ||
} | ||
result.success(pressedState); | ||
break; | ||
default: | ||
result.notImplemented(); | ||
break; | ||
} | ||
} | ||
}; | ||
|
||
public KeyboardChannel(@NonNull DartExecutor dartExecutor) { | ||
channel = new MethodChannel(dartExecutor, "flutter/keyboard", StandardMethodCodec.INSTANCE); | ||
channel.setMethodCallHandler(parsingMethodHandler); | ||
} | ||
|
||
/** | ||
* Sets the {@link KeyboardMethodHandler} which receives all requests to query the keyboard state. | ||
*/ | ||
public void setKeyboardMethodHandler(@Nullable KeyboardMethodHandler keyboardMethodHandler) { | ||
this.keyboardMethodHandler = keyboardMethodHandler; | ||
} | ||
|
||
public interface KeyboardMethodHandler { | ||
/** | ||
* Returns the keyboard pressed states. | ||
* | ||
* @return A map which keys are physical keyboard key ID and values the corresponding logical | ||
* keyboard key ID. | ||
*/ | ||
Map<Long, Long> getKeyboardState(); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
shell/platform/android/io/flutter/plugin/keyboard/KeyboardPlugin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
package io.flutter.plugin.keyboard; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.VisibleForTesting; | ||
import io.flutter.embedding.android.KeyboardManager; | ||
import io.flutter.embedding.engine.systemchannels.KeyboardChannel; | ||
import io.flutter.plugin.common.MethodChannel; | ||
import java.util.Map; | ||
|
||
/** | ||
* {@link KeyboardPlugin} is the implementation of all functionalities needed for querying keyboard | ||
* pressed state. | ||
* | ||
* <p>The plugin handles requests for querying keyboard pressed states by the {@link | ||
* io.flutter.embedding.engine.systemchannels.KeyboardChannel} via returning the {@link | ||
* io.flutter.embedding.android.KeyEmbedderResponder} pressed keys. | ||
*/ | ||
public class KeyboardPlugin implements KeyboardChannel.KeyboardMethodHandler { | ||
|
||
private final KeyboardChannel mKeyboardChannel; | ||
private final KeyboardManager mKeyboardManager; | ||
|
||
@VisibleForTesting MethodChannel.Result pendingResult; | ||
|
||
public KeyboardPlugin( | ||
@NonNull KeyboardManager keyboardManager, @NonNull KeyboardChannel keyboardChannel) { | ||
mKeyboardManager = keyboardManager; | ||
mKeyboardChannel = keyboardChannel; | ||
|
||
mKeyboardChannel.setKeyboardMethodHandler(this); | ||
} | ||
|
||
/** | ||
* Unregisters this {@code KeyboardPlugin} as the {@code KeyboardChannel.KeyboardMethodHandler}, | ||
* for the {@link io.flutter.embedding.engine.systemchannels.KeyboardChannel}. | ||
* | ||
* <p>Do not invoke any methods on a {@code KeyboardPlugin} after invoking this method. | ||
*/ | ||
public void destroy() { | ||
mKeyboardChannel.setKeyboardMethodHandler(null); | ||
} | ||
|
||
/** | ||
* Returns the keyboard pressed state. | ||
* | ||
* @return A map which keys are physical keyboard key ID and values the corresponding logical | ||
* keyboard key ID. | ||
*/ | ||
@Override | ||
public Map<Long, Long> getKeyboardState() { | ||
return mKeyboardManager.getPressedState(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.