This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
[Android] Return keyboard pressed state #41695
Merged
auto-submit
merged 3 commits into
flutter-team-archive:main
from
NevercodeHQ:android_listen_and_answer_to_keyboard_pressed_state_queries
May 18, 2023
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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."); | ||
|
bleroux marked this conversation as resolved.
Outdated
|
||
| 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.