浏览代码

android: simplify handling of the back button

Provide a default and builtin default implementation which finishes the
Activity, same as before.

What this PR removes is the ability to provide a custom default handler because
applications can already take this decision when calling `onBackPressed`. In
addition, make `onBackPressed` return `void` because it's virtually impossible
for it to return `false` (that would mean that there is no
`ReactInstanceManager`, which means there is no app to begin with).

In addition, remove the use of `BackAndroid` since `BackHandler` contains an iOS
shim now.
master
Saúl Ibarra Corretgé 6 年前
父节点
当前提交
634f304815

+ 4
- 7
android/README.md 查看文件

@@ -37,7 +37,7 @@ Also, enable 32bit mode for react-native, since react-native only supports 32bit
37 37
 
38 38
 ```gradle
39 39
 android {
40
-    ... 
40
+    ...
41 41
     defaultConfig {
42 42
         ndk {
43 43
             abiFilters "armeabi-v7a", "x86"
@@ -75,7 +75,7 @@ In the same way, copy the JavaScriptCore dependency:
75 75
 
76 76
 Alternatively, you can use the scripts located in the android/scripts directory to publish these dependencies to your Maven repo.
77 77
 
78
-Third-party React Native _modules_, which Jitsi Meet SDK for Android depends on, are download by NPM in source code form. These need to be assembled into Maven artifacts, and then published to your local Maven repository. The SDK project facilitates this. 
78
+Third-party React Native _modules_, which Jitsi Meet SDK for Android depends on, are download by NPM in source code form. These need to be assembled into Maven artifacts, and then published to your local Maven repository. The SDK project facilitates this.
79 79
 
80 80
 To prepare, Configure the Maven repositories in which you are going to publish the SDK artifacts/binaries. In `android/sdk/build.gradle` as well as in `android/build.gradle` modify the lines that contain:
81 81
 
@@ -89,7 +89,7 @@ Make sure to do this in both files! Each file should require one line to be chan
89 89
 
90 90
 To create the release assembly for any _specific_ third-party React Native module that you need, you can execture the following commands, replace the module name in the examples below.
91 91
 
92
-    $ ./gradlew :react-native-webrtc:assembleRelease 
92
+    $ ./gradlew :react-native-webrtc:assembleRelease
93 93
     $ ./gradlew :react-native-webrtc:publish
94 94
 
95 95
 You build and publish the SDK itself in the same way:
@@ -184,10 +184,7 @@ public class MainActivity extends AppCompatActivity {
184 184
 
185 185
     @Override
186 186
     public void onBackPressed() {
187
-        if (!ReactActivityLifecycleCallbacks.onBackPressed()) {
188
-            // Invoke the default handler if it wasn't handled by React.
189
-            super.onBackPressed();
190
-        }
187
+        ReactActivityLifecycleCallbacks.onBackPressed();
191 188
     }
192 189
 
193 190
     @Override

+ 3
- 3
android/sdk/src/main/java/org/jitsi/meet/sdk/DefaultHardwareBackBtnHandlerImpl.java 查看文件

@@ -1,5 +1,6 @@
1 1
 /*
2
- * Copyright @ 2017-present Atlassian Pty Ltd
2
+ * Copyright @ 2019-present 8x8, Inc.
3
+ * Copyright @ 2017-2018 Atlassian Pty Ltd
3 4
  *
4 5
  * Licensed under the Apache License, Version 2.0 (the "License");
5 6
  * you may not use this file except in compliance with the License.
@@ -28,8 +29,7 @@ import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
28 29
  * during a conference by leaving the conference and (2) not handle the
29 30
  * invocation when not in a conference.
30 31
  */
31
-public class DefaultHardwareBackBtnHandlerImpl
32
-    implements DefaultHardwareBackBtnHandler {
32
+class DefaultHardwareBackBtnHandlerImpl implements DefaultHardwareBackBtnHandler {
33 33
 
34 34
     /**
35 35
      * The {@code Activity} to which the default handling of the back button

+ 4
- 24
android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetActivity.java 查看文件

@@ -1,5 +1,6 @@
1 1
 /*
2
- * Copyright @ 2017-present Atlassian Pty Ltd
2
+ * Copyright @ 2019-present 8x8, Inc.
3
+ * Copyright @ 2017-2018 Atlassian Pty Ltd
3 4
  *
4 5
  * Licensed under the Apache License, Version 2.0 (the "License");
5 6
  * you may not use this file except in compliance with the License.
@@ -26,7 +27,6 @@ import android.support.v7.app.AppCompatActivity;
26 27
 import android.view.KeyEvent;
27 28
 
28 29
 import com.facebook.react.ReactInstanceManager;
29
-import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
30 30
 import com.facebook.react.modules.core.PermissionListener;
31 31
 
32 32
 import java.net.URL;
@@ -52,12 +52,6 @@ public class JitsiMeetActivity
52 52
     private static final int OVERLAY_PERMISSION_REQUEST_CODE
53 53
         = (int) (Math.random() * Short.MAX_VALUE);
54 54
 
55
-    /**
56
-     * The default behavior of this {@code JitsiMeetActivity} upon invoking the
57
-     * back button if {@link #view} does not handle the invocation.
58
-     */
59
-    private DefaultHardwareBackBtnHandler defaultBackButtonImpl;
60
-
61 55
     /**
62 56
      * The default base {@code URL} used to join a conference when a partial URL
63 57
      * (e.g. a room name only) is specified. The value is used only while
@@ -185,19 +179,7 @@ public class JitsiMeetActivity
185 179
 
186 180
     @Override
187 181
     public void onBackPressed() {
188
-        if (!ReactActivityLifecycleCallbacks.onBackPressed()) {
189
-            // JitsiMeetView didn't handle the invocation of the back button.
190
-            // Generally, an Activity extender would very likely want to invoke
191
-            // Activity#onBackPressed(). For the sake of consistency with
192
-            // JitsiMeetView and within the Jitsi Meet SDK for Android though,
193
-            // JitsiMeetActivity does what JitsiMeetView would've done if it
194
-            // were able to handle the invocation.
195
-            if (defaultBackButtonImpl == null) {
196
-                super.onBackPressed();
197
-            } else {
198
-                defaultBackButtonImpl.invokeDefaultOnBackPressed();
199
-            }
200
-        }
182
+        ReactActivityLifecycleCallbacks.onBackPressed();
201 183
     }
202 184
 
203 185
     @Override
@@ -279,8 +261,7 @@ public class JitsiMeetActivity
279 261
     protected void onResume() {
280 262
         super.onResume();
281 263
 
282
-        defaultBackButtonImpl = new DefaultHardwareBackBtnHandlerImpl(this);
283
-        ReactActivityLifecycleCallbacks.onHostResume(this, defaultBackButtonImpl);
264
+        ReactActivityLifecycleCallbacks.onHostResume(this);
284 265
     }
285 266
 
286 267
     @Override
@@ -288,7 +269,6 @@ public class JitsiMeetActivity
288 269
         super.onStop();
289 270
 
290 271
         ReactActivityLifecycleCallbacks.onHostPause(this);
291
-        defaultBackButtonImpl = null;
292 272
     }
293 273
 
294 274
     @Override

+ 5
- 22
android/sdk/src/main/java/org/jitsi/meet/sdk/ReactActivityLifecycleCallbacks.java 查看文件

@@ -1,5 +1,6 @@
1 1
 /*
2
- * Copyright @ 2018-present Atlassian Pty Ltd
2
+ * Copyright @ 2019-present 8x8, Inc.
3
+ * Copyright @ 2018 Atlassian Pty Ltd
3 4
  *
4 5
  * Licensed under the Apache License, Version 2.0 (the "License");
5 6
  * you may not use this file except in compliance with the License.
@@ -24,7 +25,6 @@ import android.os.Build;
24 25
 import com.calendarevents.CalendarEventsPackage;
25 26
 import com.facebook.react.ReactInstanceManager;
26 27
 import com.facebook.react.bridge.Callback;
27
-import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
28 28
 import com.facebook.react.modules.core.PermissionListener;
29 29
 
30 30
 /**
@@ -73,15 +73,12 @@ public class ReactActivityLifecycleCallbacks {
73 73
      * otherwise. If {@code false}, the application should call the
74 74
      * {@code super}'s implementation.
75 75
      */
76
-    public static boolean onBackPressed() {
76
+    public static void onBackPressed() {
77 77
         ReactInstanceManager reactInstanceManager
78 78
             = ReactInstanceManagerHolder.getReactInstanceManager();
79 79
 
80
-        if (reactInstanceManager == null) {
81
-            return false;
82
-        } else {
80
+        if (reactInstanceManager != null) {
83 81
             reactInstanceManager.onBackPressed();
84
-            return true;
85 82
         }
86 83
     }
87 84
 
@@ -123,25 +120,11 @@ public class ReactActivityLifecycleCallbacks {
123 120
      * @param activity {@code Activity} being resumed.
124 121
      */
125 122
     public static void onHostResume(Activity activity) {
126
-        onHostResume(activity, new DefaultHardwareBackBtnHandlerImpl(activity));
127
-    }
128
-
129
-    /**
130
-     * {@link Activity} lifecycle method which should be called from
131
-     * {@code Activity#onResume} so we can do the required internal processing.
132
-     *
133
-     * @param activity {@code Activity} being resumed.
134
-     * @param defaultBackButtonImpl a {@link DefaultHardwareBackBtnHandler} to
135
-     * handle invoking the back button if no {@link BaseReactView} handles it.
136
-     */
137
-    public static void onHostResume(
138
-            Activity activity,
139
-            DefaultHardwareBackBtnHandler defaultBackButtonImpl) {
140 123
         ReactInstanceManager reactInstanceManager
141 124
             = ReactInstanceManagerHolder.getReactInstanceManager();
142 125
 
143 126
         if (reactInstanceManager != null) {
144
-            reactInstanceManager.onHostResume(activity, defaultBackButtonImpl);
127
+            reactInstanceManager.onHostResume(activity, new DefaultHardwareBackBtnHandlerImpl(activity));
145 128
         }
146 129
 
147 130
         if (permissionsCallback != null) {

+ 7
- 35
react/features/conference/components/Conference.native.js 查看文件

@@ -2,8 +2,7 @@
2 2
 
3 3
 import React, { Component } from 'react';
4 4
 
5
-// eslint-disable-next-line react-native/split-platform-components
6
-import { BackAndroid, BackHandler, StatusBar, View } from 'react-native';
5
+import { BackHandler, StatusBar, View } from 'react-native';
7 6
 import { connect as reactReduxConnect } from 'react-redux';
8 7
 
9 8
 import { appNavigate } from '../../app';
@@ -144,8 +143,6 @@ type Props = {
144 143
  * The conference page of the mobile (i.e. React Native) application.
145 144
  */
146 145
 class Conference extends Component<Props> {
147
-    _backHandler: ?BackHandler;
148
-
149 146
     /**
150 147
      * Initializes a new Conference instance.
151 148
      *
@@ -157,7 +154,6 @@ class Conference extends Component<Props> {
157 154
 
158 155
         // Bind event handlers so they are only bound once per instance.
159 156
         this._onClick = this._onClick.bind(this);
160
-        this._onHardwareBackPress = this._onHardwareBackPress.bind(this);
161 157
     }
162 158
 
163 159
     /**
@@ -170,15 +166,9 @@ class Conference extends Component<Props> {
170 166
     componentDidMount() {
171 167
         this.props._onConnect();
172 168
 
173
-        // Set handling any hardware button presses for back navigation up.
174
-        const backHandler = BackHandler || BackAndroid;
175
-
176
-        if (backHandler) {
177
-            this._backHandler = backHandler;
178
-            backHandler.addEventListener(
179
-                'hardwareBackPress',
180
-                this._onHardwareBackPress);
181
-        }
169
+        BackHandler.addEventListener(
170
+            'hardwareBackPress',
171
+            this.props._onHardwareBackPress);
182 172
 
183 173
         // Show the toolbox if we are the only participant; otherwise, the whole
184 174
         // UI looks too unpopulated the LargeVideo visible.
@@ -233,14 +223,9 @@ class Conference extends Component<Props> {
233 223
      */
234 224
     componentWillUnmount() {
235 225
         // Tear handling any hardware button presses for back navigation down.
236
-        const backHandler = this._backHandler;
237
-
238
-        if (backHandler) {
239
-            this._backHandler = undefined;
240
-            backHandler.removeEventListener(
241
-                'hardwareBackPress',
242
-                this._onHardwareBackPress);
243
-        }
226
+        BackHandler.removeEventListener(
227
+            'hardwareBackPress',
228
+            this.props._onHardwareBackPress);
244 229
 
245 230
         this.props._onDisconnect();
246 231
     }
@@ -341,19 +326,6 @@ class Conference extends Component<Props> {
341 326
         this.props._setToolboxVisible(toolboxVisible);
342 327
     }
343 328
 
344
-    _onHardwareBackPress: () => boolean;
345
-
346
-    /**
347
-     * Handles a hardware button press for back navigation.
348
-     *
349
-     * @returns {boolean} If the hardware button press for back navigation was
350
-     * handled by this {@code Conference}, then {@code true}; otherwise,
351
-     * {@code false}.
352
-     */
353
-    _onHardwareBackPress() {
354
-        return this._backHandler && this.props._onHardwareBackPress();
355
-    }
356
-
357 329
     /**
358 330
      * Renders the conference notification badge if the feature is enabled.
359 331
      *

正在加载...
取消
保存