Selaa lähdekoodia

[Android] Implement DefaultHardwareBackBtnHandler

* Regardless of whether the SDK client/consumer employs
  JitsiMeetActivity or JitsiMeetView, default to finishing the
  associated Activity upon invoking the back button (which is what
  Activity#onBackPressed() is documented to do).

* Do not break the public API of JitsiMeetView and, thus, Jitsi Meet SDK
  for Android.
master
Lyubo Marinov 8 vuotta sitten
vanhempi
commit
ec58aa9959

+ 65
- 0
android/sdk/src/main/java/org/jitsi/meet/sdk/DefaultHardwareBackBtnHandlerImpl.java Näytä tiedosto

@@ -0,0 +1,65 @@
1
+/*
2
+ * Copyright @ 2017-present Atlassian Pty Ltd
3
+ *
4
+ * 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 obtain a copy of the License at
7
+ *
8
+ *     http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+package org.jitsi.meet.sdk;
18
+
19
+import android.app.Activity;
20
+
21
+import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
22
+
23
+/**
24
+ * Defines the default behavior of <tt>JitsiMeetActivity</tt> and
25
+ * <tt>JitsiMeetView</tt> upon invoking the back button if no
26
+ * <tt>JitsiMeetView</tt> handles the invocation. For example, a
27
+ * <tt>JitsiMeetView</tt> may (1) handle the invocation of the back button
28
+ * during a conference by leaving the conference and (2) not handle the
29
+ * invocation when not in a conference.
30
+ */
31
+public class DefaultHardwareBackBtnHandlerImpl
32
+    implements DefaultHardwareBackBtnHandler {
33
+
34
+    /**
35
+     * The <tt>Activity</tt> to which the default handling of the back button
36
+     * is being provided by this instance.
37
+     */
38
+    private final Activity activity;
39
+
40
+    /**
41
+     * Initializes a new <tt>DefaultHardwareBackBtnHandlerImpl</tt> instance to
42
+     * provide the default handling of the back button to a specific
43
+     * <tt>Activity</tt>.
44
+     *
45
+     * @param activity - the <tt>Activity</tt> to which the new instance is to
46
+     * provide the default behavior of the back button
47
+     */
48
+    public DefaultHardwareBackBtnHandlerImpl(Activity activity) {
49
+        this.activity = activity;
50
+    }
51
+
52
+    /**
53
+     * {@inheritDoc}
54
+     *
55
+     * Finishes the associated <tt>Activity</tt>.
56
+     */
57
+    @Override
58
+    public void invokeDefaultOnBackPressed() {
59
+        // Technically, we'd like to invoke Activity#onBackPressed().
60
+        // Practically, it's not possible. Fortunately, the documentation of
61
+        // Activity#onBackPressed() specifies that "[t]he default implementation
62
+        // simply finishes the current activity,"
63
+        activity.finish();
64
+    }
65
+}

+ 21
- 14
android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetActivity.java Näytä tiedosto

@@ -40,8 +40,7 @@ import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
40 40
  * <tt>JKConferenceView</tt> static methods.
41 41
  */
42 42
 public class JitsiMeetActivity
43
-    extends AppCompatActivity
44
-    implements DefaultHardwareBackBtnHandler {
43
+    extends AppCompatActivity {
45 44
 
46 45
     /**
47 46
      * The request code identifying requests for the permission to draw on top
@@ -50,6 +49,12 @@ public class JitsiMeetActivity
50 49
     private static final int OVERLAY_PERMISSION_REQUEST_CODE
51 50
         = (int) (Math.random() * Short.MAX_VALUE);
52 51
 
52
+    /**
53
+     * The default behavior of this <tt>JitsiMeetActivity</tt> upon invoking the
54
+     * back button if {@link #view} does not handle the invocation.
55
+     */
56
+    private DefaultHardwareBackBtnHandler defaultBackButtonImpl;
57
+
53 58
     /**
54 59
      * Instance of the {@link JitsiMeetView} which this activity will display.
55 60
      */
@@ -133,23 +138,23 @@ public class JitsiMeetActivity
133 138
         }
134 139
     }
135 140
 
136
-    /**
137
-     * This method is called if the JS part does not handle the physical back
138
-     * button press.
139
-     */
140
-    @Override
141
-    public void invokeDefaultOnBackPressed() {
142
-        super.onBackPressed();
143
-    }
144
-
145 141
     /**
146 142
      * {@inheritDoc}
147 143
      */
148 144
     @Override
149 145
     public void onBackPressed() {
150 146
         if (!JitsiMeetView.onBackPressed()) {
151
-            // Invoke the default handler if it wasn't handled by React.
152
-            super.onBackPressed();
147
+            // JitsiMeetView didn't handle the invocation of the back button.
148
+            // Generally, an Activity extender would very likely want to invoke
149
+            // Activity#onBackPressed(). For the sake of consistency with
150
+            // JitsiMeetView and within the Jitsi Meet SDK for Android though,
151
+            // JitsiMeetActivity does what JitsiMeetView would've done if it
152
+            // were able to handle the invocation.
153
+            if (defaultBackButtonImpl == null) {
154
+                super.onBackPressed();
155
+            } else {
156
+                defaultBackButtonImpl.invokeDefaultOnBackPressed();
157
+            }
153 158
         }
154 159
     }
155 160
 
@@ -206,6 +211,7 @@ public class JitsiMeetActivity
206 211
         super.onPause();
207 212
 
208 213
         JitsiMeetView.onHostPause(this);
214
+        defaultBackButtonImpl = null;
209 215
     }
210 216
 
211 217
     /**
@@ -215,7 +221,8 @@ public class JitsiMeetActivity
215 221
     protected void onResume() {
216 222
         super.onResume();
217 223
 
218
-        JitsiMeetView.onHostResume(this, this);
224
+        defaultBackButtonImpl = new DefaultHardwareBackBtnHandlerImpl(this);
225
+        JitsiMeetView.onHostResume(this, defaultBackButtonImpl);
219 226
     }
220 227
 
221 228
     /**

+ 20
- 10
android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java Näytä tiedosto

@@ -27,7 +27,6 @@ import android.support.annotation.Nullable;
27 27
 import android.widget.FrameLayout;
28 28
 
29 29
 import com.facebook.react.ReactInstanceManager;
30
-import com.facebook.react.ReactInstanceManagerBuilder;
31 30
 import com.facebook.react.ReactRootView;
32 31
 import com.facebook.react.bridge.NativeModule;
33 32
 import com.facebook.react.bridge.ReactApplicationContext;
@@ -148,11 +147,11 @@ public class JitsiMeetView extends FrameLayout {
148 147
      * implementation.
149 148
      */
150 149
     public static boolean onBackPressed() {
151
-        if (reactInstanceManager != null) {
150
+        if (reactInstanceManager == null) {
151
+            return false;
152
+        } else {
152 153
             reactInstanceManager.onBackPressed();
153 154
             return true;
154
-        } else {
155
-            return false;
156 155
         }
157 156
     }
158 157
 
@@ -186,14 +185,25 @@ public class JitsiMeetView extends FrameLayout {
186 185
      * <tt>Activity.onResume</tt> so we can do the required internal processing.
187 186
      *
188 187
      * @param activity - <tt>Activity</tt> being resumed.
189
-     * @param backHandler - a <tt>DefaultHardwareBackBtnHandler</tt> which
190
-     * will handle the back button press in case there's nothing to handle on
191
-     * the JS side.
192 188
      */
193
-    public static void onHostResume(Activity activity,
194
-                                    DefaultHardwareBackBtnHandler backHandler) {
189
+    public static void onHostResume(Activity activity) {
190
+        onHostResume(activity, new DefaultHardwareBackBtnHandlerImpl(activity));
191
+    }
192
+
193
+    /**
194
+     * Activity lifecycle method which should be called from
195
+     * <tt>Activity.onResume</tt> so we can do the required internal processing.
196
+     *
197
+     * @param activity - <tt>Activity</tt> being resumed.
198
+     * @param defaultBackButtonImpl - a <tt>DefaultHardwareBackBtnHandler</tt>
199
+     * to handle invoking the back button if no <tt>JitsiMeetView</tt> handles
200
+     * it.
201
+     */
202
+    public static void onHostResume(
203
+            Activity activity,
204
+            DefaultHardwareBackBtnHandler defaultBackButtonImpl) {
195 205
         if (reactInstanceManager != null) {
196
-            reactInstanceManager.onHostResume(activity, backHandler);
206
+            reactInstanceManager.onHostResume(activity, defaultBackButtonImpl);
197 207
         }
198 208
     }
199 209
 

Loading…
Peruuta
Tallenna