Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
BackGestureChannel.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.embedding.engine.systemchannels;
6
7import android.annotation.TargetApi;
8import android.window.BackEvent;
9import androidx.annotation.NonNull;
10import androidx.annotation.Nullable;
11import androidx.annotation.RequiresApi;
12import io.flutter.Build.API_LEVELS;
13import io.flutter.Log;
14import io.flutter.embedding.engine.dart.DartExecutor;
15import io.flutter.plugin.common.MethodCall;
16import io.flutter.plugin.common.MethodChannel;
17import io.flutter.plugin.common.StandardMethodCodec;
18import java.util.Arrays;
19import java.util.HashMap;
20import java.util.Map;
21
22/**
23 * A {@link MethodChannel} for communicating back gesture events to the Flutter framework.
24 *
25 * <p>The BackGestureChannel facilitates communication between the platform-specific Android back
26 * gesture handling code and the Flutter framework. It enables the dispatch of back gesture events
27 * such as start, progress, commit, and cancellation from the platform to the Flutter application.
28 */
29public class BackGestureChannel {
30 private static final String TAG = "BackGestureChannel";
31
32 @NonNull public final MethodChannel channel;
33
34 /**
35 * Constructs a BackGestureChannel.
36 *
37 * @param dartExecutor The DartExecutor used to establish communication with the Flutter
38 * framework.
39 */
40 public BackGestureChannel(@NonNull DartExecutor dartExecutor) {
41 this.channel =
42 new MethodChannel(dartExecutor, "flutter/backgesture", StandardMethodCodec.INSTANCE);
43 channel.setMethodCallHandler(defaultHandler);
44 }
45
46 // Provide a default handler that returns an empty response to any messages
47 // on this channel.
48 private final MethodChannel.MethodCallHandler defaultHandler =
49 new MethodChannel.MethodCallHandler() {
50 @Override
51 public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result) {
52 result.success(null);
53 }
54 };
55
56 /**
57 * Initiates a back gesture event.
58 *
59 * <p>This method should be called when the back gesture is initiated by the user.
60 *
61 * @param backEvent The BackEvent object containing information about the touch.
62 */
63 @TargetApi(API_LEVELS.API_34)
64 @RequiresApi(API_LEVELS.API_34)
65 public void startBackGesture(@NonNull BackEvent backEvent) {
66 Log.v(TAG, "Sending message to start back gesture");
67 channel.invokeMethod("startBackGesture", backEventToJsonMap(backEvent));
68 }
69
70 /**
71 * Updates the progress of a back gesture event.
72 *
73 * <p>This method should be called to update the progress of an ongoing back gesture event.
74 *
75 * @param backEvent An BackEvent object describing the progress event.
76 */
77 @TargetApi(API_LEVELS.API_34)
78 @RequiresApi(API_LEVELS.API_34)
79 public void updateBackGestureProgress(@NonNull BackEvent backEvent) {
80 Log.v(TAG, "Sending message to update back gesture progress");
81 channel.invokeMethod("updateBackGestureProgress", backEventToJsonMap(backEvent));
82 }
83
84 /**
85 * Commits the back gesture event.
86 *
87 * <p>This method should be called to signify the completion of a back gesture event and commit
88 * the navigation action initiated by the gesture.
89 */
90 @TargetApi(API_LEVELS.API_34)
91 @RequiresApi(API_LEVELS.API_34)
92 public void commitBackGesture() {
93 Log.v(TAG, "Sending message to commit back gesture");
94 channel.invokeMethod("commitBackGesture", null);
95 }
96
97 /**
98 * Cancels the back gesture event.
99 *
100 * <p>This method should be called when a back gesture is cancelled or the back button is pressed.
101 */
102 @TargetApi(API_LEVELS.API_34)
103 @RequiresApi(API_LEVELS.API_34)
104 public void cancelBackGesture() {
105 Log.v(TAG, "Sending message to cancel back gesture");
106 channel.invokeMethod("cancelBackGesture", null);
107 }
108
109 /**
110 * Sets a method call handler for the channel.
111 *
112 * @param handler The handler to set for the channel.
113 */
114 public void setMethodCallHandler(@Nullable MethodChannel.MethodCallHandler handler) {
115 channel.setMethodCallHandler(handler);
116 }
117
118 @TargetApi(API_LEVELS.API_34)
119 @RequiresApi(API_LEVELS.API_34)
120 private Map<String, Object> backEventToJsonMap(@NonNull BackEvent backEvent) {
121 Map<String, Object> message = new HashMap<>(3);
122 final float x = backEvent.getTouchX();
123 final float y = backEvent.getTouchY();
124 final Object touchOffset = (Float.isNaN(x) || Float.isNaN(y)) ? null : Arrays.asList(x, y);
125 message.put("touchOffset", touchOffset);
126 message.put("progress", backEvent.getProgress());
127 message.put("swipeEdge", backEvent.getSwipeEdge());
128
129 return message;
130 }
131}
static void v(@NonNull String tag, @NonNull String message)
Definition Log.java:40
void setMethodCallHandler(@Nullable MethodChannel.MethodCallHandler handler)
static ::testing::Matcher< GBytes * > MethodCall(const std::string &name, ::testing::Matcher< FlValue * > args)
GAsyncResult * result
Win32Message message
double y
double x
#define TAG()