瀏覽代碼

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
 
37
 
38
 ```gradle
38
 ```gradle
39
 android {
39
 android {
40
-    ... 
40
+    ...
41
     defaultConfig {
41
     defaultConfig {
42
         ndk {
42
         ndk {
43
             abiFilters "armeabi-v7a", "x86"
43
             abiFilters "armeabi-v7a", "x86"
75
 
75
 
76
 Alternatively, you can use the scripts located in the android/scripts directory to publish these dependencies to your Maven repo.
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
 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:
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
 
89
 
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.
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
     $ ./gradlew :react-native-webrtc:publish
93
     $ ./gradlew :react-native-webrtc:publish
94
 
94
 
95
 You build and publish the SDK itself in the same way:
95
 You build and publish the SDK itself in the same way:
184
 
184
 
185
     @Override
185
     @Override
186
     public void onBackPressed() {
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
     @Override
190
     @Override

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

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
  * Licensed under the Apache License, Version 2.0 (the "License");
5
  * Licensed under the Apache License, Version 2.0 (the "License");
5
  * you may not use this file except in compliance with the License.
6
  * you may not use this file except in compliance with the License.
28
  * during a conference by leaving the conference and (2) not handle the
29
  * during a conference by leaving the conference and (2) not handle the
29
  * invocation when not in a conference.
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
      * The {@code Activity} to which the default handling of the back button
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
 /*
1
 /*
2
- * Copyright @ 2017-present Atlassian Pty Ltd
2
+ * Copyright @ 2019-present 8x8, Inc.
3
+ * Copyright @ 2017-2018 Atlassian Pty Ltd
3
  *
4
  *
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
  * Licensed under the Apache License, Version 2.0 (the "License");
5
  * you may not use this file except in compliance with the License.
6
  * you may not use this file except in compliance with the License.
26
 import android.view.KeyEvent;
27
 import android.view.KeyEvent;
27
 
28
 
28
 import com.facebook.react.ReactInstanceManager;
29
 import com.facebook.react.ReactInstanceManager;
29
-import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
30
 import com.facebook.react.modules.core.PermissionListener;
30
 import com.facebook.react.modules.core.PermissionListener;
31
 
31
 
32
 import java.net.URL;
32
 import java.net.URL;
52
     private static final int OVERLAY_PERMISSION_REQUEST_CODE
52
     private static final int OVERLAY_PERMISSION_REQUEST_CODE
53
         = (int) (Math.random() * Short.MAX_VALUE);
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
      * The default base {@code URL} used to join a conference when a partial URL
56
      * The default base {@code URL} used to join a conference when a partial URL
63
      * (e.g. a room name only) is specified. The value is used only while
57
      * (e.g. a room name only) is specified. The value is used only while
185
 
179
 
186
     @Override
180
     @Override
187
     public void onBackPressed() {
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
     @Override
185
     @Override
279
     protected void onResume() {
261
     protected void onResume() {
280
         super.onResume();
262
         super.onResume();
281
 
263
 
282
-        defaultBackButtonImpl = new DefaultHardwareBackBtnHandlerImpl(this);
283
-        ReactActivityLifecycleCallbacks.onHostResume(this, defaultBackButtonImpl);
264
+        ReactActivityLifecycleCallbacks.onHostResume(this);
284
     }
265
     }
285
 
266
 
286
     @Override
267
     @Override
288
         super.onStop();
269
         super.onStop();
289
 
270
 
290
         ReactActivityLifecycleCallbacks.onHostPause(this);
271
         ReactActivityLifecycleCallbacks.onHostPause(this);
291
-        defaultBackButtonImpl = null;
292
     }
272
     }
293
 
273
 
294
     @Override
274
     @Override

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

1
 /*
1
 /*
2
- * Copyright @ 2018-present Atlassian Pty Ltd
2
+ * Copyright @ 2019-present 8x8, Inc.
3
+ * Copyright @ 2018 Atlassian Pty Ltd
3
  *
4
  *
4
  * Licensed under the Apache License, Version 2.0 (the "License");
5
  * Licensed under the Apache License, Version 2.0 (the "License");
5
  * you may not use this file except in compliance with the License.
6
  * you may not use this file except in compliance with the License.
24
 import com.calendarevents.CalendarEventsPackage;
25
 import com.calendarevents.CalendarEventsPackage;
25
 import com.facebook.react.ReactInstanceManager;
26
 import com.facebook.react.ReactInstanceManager;
26
 import com.facebook.react.bridge.Callback;
27
 import com.facebook.react.bridge.Callback;
27
-import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
28
 import com.facebook.react.modules.core.PermissionListener;
28
 import com.facebook.react.modules.core.PermissionListener;
29
 
29
 
30
 /**
30
 /**
73
      * otherwise. If {@code false}, the application should call the
73
      * otherwise. If {@code false}, the application should call the
74
      * {@code super}'s implementation.
74
      * {@code super}'s implementation.
75
      */
75
      */
76
-    public static boolean onBackPressed() {
76
+    public static void onBackPressed() {
77
         ReactInstanceManager reactInstanceManager
77
         ReactInstanceManager reactInstanceManager
78
             = ReactInstanceManagerHolder.getReactInstanceManager();
78
             = ReactInstanceManagerHolder.getReactInstanceManager();
79
 
79
 
80
-        if (reactInstanceManager == null) {
81
-            return false;
82
-        } else {
80
+        if (reactInstanceManager != null) {
83
             reactInstanceManager.onBackPressed();
81
             reactInstanceManager.onBackPressed();
84
-            return true;
85
         }
82
         }
86
     }
83
     }
87
 
84
 
123
      * @param activity {@code Activity} being resumed.
120
      * @param activity {@code Activity} being resumed.
124
      */
121
      */
125
     public static void onHostResume(Activity activity) {
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
         ReactInstanceManager reactInstanceManager
123
         ReactInstanceManager reactInstanceManager
141
             = ReactInstanceManagerHolder.getReactInstanceManager();
124
             = ReactInstanceManagerHolder.getReactInstanceManager();
142
 
125
 
143
         if (reactInstanceManager != null) {
126
         if (reactInstanceManager != null) {
144
-            reactInstanceManager.onHostResume(activity, defaultBackButtonImpl);
127
+            reactInstanceManager.onHostResume(activity, new DefaultHardwareBackBtnHandlerImpl(activity));
145
         }
128
         }
146
 
129
 
147
         if (permissionsCallback != null) {
130
         if (permissionsCallback != null) {

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

2
 
2
 
3
 import React, { Component } from 'react';
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
 import { connect as reactReduxConnect } from 'react-redux';
6
 import { connect as reactReduxConnect } from 'react-redux';
8
 
7
 
9
 import { appNavigate } from '../../app';
8
 import { appNavigate } from '../../app';
144
  * The conference page of the mobile (i.e. React Native) application.
143
  * The conference page of the mobile (i.e. React Native) application.
145
  */
144
  */
146
 class Conference extends Component<Props> {
145
 class Conference extends Component<Props> {
147
-    _backHandler: ?BackHandler;
148
-
149
     /**
146
     /**
150
      * Initializes a new Conference instance.
147
      * Initializes a new Conference instance.
151
      *
148
      *
157
 
154
 
158
         // Bind event handlers so they are only bound once per instance.
155
         // Bind event handlers so they are only bound once per instance.
159
         this._onClick = this._onClick.bind(this);
156
         this._onClick = this._onClick.bind(this);
160
-        this._onHardwareBackPress = this._onHardwareBackPress.bind(this);
161
     }
157
     }
162
 
158
 
163
     /**
159
     /**
170
     componentDidMount() {
166
     componentDidMount() {
171
         this.props._onConnect();
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
         // Show the toolbox if we are the only participant; otherwise, the whole
173
         // Show the toolbox if we are the only participant; otherwise, the whole
184
         // UI looks too unpopulated the LargeVideo visible.
174
         // UI looks too unpopulated the LargeVideo visible.
233
      */
223
      */
234
     componentWillUnmount() {
224
     componentWillUnmount() {
235
         // Tear handling any hardware button presses for back navigation down.
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
         this.props._onDisconnect();
230
         this.props._onDisconnect();
246
     }
231
     }
341
         this.props._setToolboxVisible(toolboxVisible);
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
      * Renders the conference notification badge if the feature is enabled.
330
      * Renders the conference notification badge if the feature is enabled.
359
      *
331
      *

Loading…
取消
儲存