Skip to content

Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.

feat(react-native-sdk/android): force permissions approval in order to launch RNOngoingNotification (#15400)

Calinteodorb890aa33c35e
diff --git a/.gitignore b/.gitignore
index 1387dd5e90..fab8dfa58f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -101,6 +101,9 @@ tsconfig.json
react-native-sdk/*.tgz
react-native-sdk/android/src
!react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JitsiMeetReactNativePackage.java
+!react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JitsiMeetOngoingConferenceService.java
+!react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JMOngoingConferenceModule.java
+!react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/RNOngoingNotification.java
react-native-sdk/images
react-native-sdk/ios
react-native-sdk/lang
diff --git a/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JMOngoingConferenceModule.java b/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JMOngoingConferenceModule.java
new file mode 100644
index 0000000000..dd6f9f3a98
--- /dev/null
+++ b/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JMOngoingConferenceModule.java
@@ -0,0 +1,117 @@
+package org.jitsi.meet.sdk;
+
+import static android.Manifest.permission.POST_NOTIFICATIONS;
+import static android.Manifest.permission.RECORD_AUDIO;
+
+import android.content.Context;
+import android.content.pm.PackageManager;
+import android.os.Build;
+
+import androidx.annotation.NonNull;
+import androidx.annotation.RequiresApi;
+
+import com.facebook.react.ReactActivity;
+
+import com.facebook.react.bridge.ReactApplicationContext;
+import com.facebook.react.bridge.ReactContextBaseJavaModule;
+import com.facebook.react.bridge.ReactMethod;
+import com.facebook.react.module.annotations.ReactModule;
+import com.facebook.react.modules.core.PermissionListener;
+
+import org.jitsi.meet.sdk.log.JitsiMeetLogger;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+
+/**
+ * This class implements a ReactModule and it's
+ * responsible for launching/aborting a service when a conference is in progress.
+ */
+@ReactModule(name = JMOngoingConferenceModule.NAME)
+class JMOngoingConferenceModule extends ReactContextBaseJavaModule {
+
+ public static final String NAME = "JMOngoingConference";
+
+ private static final int PERMISSIONS_REQUEST_CODE = (int) (Math.random() * Short.MAX_VALUE);
+
+ public JMOngoingConferenceModule(ReactApplicationContext reactContext) {
+ super(reactContext);
+ }
+
+ @ReactMethod
+ public void launch() {
+ Context context = getReactApplicationContext();
+ ReactActivity reactActivity = (ReactActivity) getCurrentActivity();
+
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) {
+ JMOngoingConferenceService.launch(context);
+
+ JitsiMeetLogger.i(NAME + " launch");
+
+ return;
+ }
+
+ PermissionListener listener = new PermissionListener() {
+ @Override
+ public boolean onRequestPermissionsResult(int i, String[] strings, int[] results) {
+ JitsiMeetLogger.i(NAME + " Permission callback received");
+
+ if (results == null || results.length == 0) {
+ JitsiMeetLogger.w(NAME + " Permission results are null or empty");
+ return true;
+ }
+
+ int counter = 0;
+ for (int result : results) {
+ if (result == PackageManager.PERMISSION_GRANTED) {
+ counter++;
+ }
+ }
+
+ JitsiMeetLogger.i(NAME + " Permissions granted: " + counter + "/" + results.length);
+
+ if (counter == results.length) {
+ JitsiMeetLogger.i(NAME + " All permissions granted, launching service");
+ JMOngoingConferenceService.launch(context);
+ } else {
+ JitsiMeetLogger.w(NAME + " Not all permissions were granted");
+ }
+
+ return true;
+ }
+ };
+
+ JitsiMeetLogger.i(NAME + " Checking Tiramisu permissions");
+
+ List<String> permissionsList = new ArrayList<>();
+
+ permissionsList.add(POST_NOTIFICATIONS);
+ permissionsList.add(RECORD_AUDIO);
+
+ String[] permissionsArray = new String[ permissionsList.size() ];
+ permissionsArray = permissionsList.toArray( permissionsArray );
+
+ try {
+ JitsiMeetLogger.i(NAME + " Requesting permissions: " + Arrays.toString(permissionsArray));
+ reactActivity.requestPermissions(permissionsArray, PERMISSIONS_REQUEST_CODE, listener);
+ } catch (Exception e) {
+ JitsiMeetLogger.e(e, NAME + " Error requesting permissions");
+ }
+ }
+
+ @ReactMethod
+ public void abort() {
+ Context context = getReactApplicationContext();
+ JMOngoingConferenceService.abort(context);
+
+ JitsiMeetLogger.i(NAME + " abort");
+ }
+
+ @NonNull
+ @Override
+ public String getName() {
+ return NAME;
+ }
+}
diff --git a/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JMOngoingConferenceService.java b/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JMOngoingConferenceService.java
new file mode 100644
index 0000000000..fd73560ed0
--- /dev/null
+++ b/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JMOngoingConferenceService.java
@@ -0,0 +1,107 @@
+/*
+ * Copyright @ 2019-present 8x8, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jitsi.meet.sdk;
+
+import android.app.Notification;
+import android.app.Service;
+
+import android.content.Context;
+import android.content.Intent;
+
+import android.content.pm.ServiceInfo;
+
+import android.os.Build;
+import android.os.IBinder;
+
+import org.jitsi.meet.sdk.log.JitsiMeetLogger;
+
+import java.util.Random;
+
+/**
+ * This class implements an Android {@link Service}, a foreground one specifically, and it's
+ * responsible for presenting an ongoing notification when a conference is in progress.
+ * The service will help keep the app running while in the background.
+ *
+ * See: https://developer.android.com/guide/components/services
+ */
+public class JMOngoingConferenceService extends Service {
+ private static final String TAG = JMOngoingConferenceService.class.getSimpleName();
+
+ static final int NOTIFICATION_ID = new Random().nextInt(99999) + 10000;
+
+ public static void launch(Context context) {
+ try {
+ RNOngoingNotification.createOngoingConferenceNotificationChannel(context);
+ JitsiMeetLogger.i(TAG + " Notification channel creation completed");
+ } catch (Exception e) {
+ JitsiMeetLogger.e(e, TAG + " Error creating notification channel");
+ }
+
+ Intent intent = new Intent(context, JMOngoingConferenceService.class);
+
+ try {
+ context.startForegroundService(intent);
+ JitsiMeetLogger.i(TAG + " Starting foreground service");
+ } catch (RuntimeException e) {
+ // Avoid crashing due to ForegroundServiceStartNotAllowedException (API level 31).
+ // See: https://developer.android.com/guide/components/foreground-services#background-start-restrictions
+ JitsiMeetLogger.w(TAG + " Ongoing conference service not started", e);
+ }
+ }
+
+ public static void abort(Context context) {
+ Intent intent = new Intent(context, JMOngoingConferenceService.class);
+ context.stopService(intent);
+ }
+
+ @Override
+ public void onCreate() {
+ super.onCreate();
+ JitsiMeetLogger.i(TAG + " onCreate called");
+
+ try {
+ Notification notification = RNOngoingNotification.buildOngoingConferenceNotification(this);
+ JitsiMeetLogger.i(TAG + " Notification build result: " + (notification != null));
+
+ if (notification == null) {
+ stopSelf();
+ JitsiMeetLogger.w(TAG + " Couldn't start service, notification is null");
+ } else {
+ JitsiMeetLogger.i(TAG + " Starting service in foreground with notification");
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
+ startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK | ServiceInfo.FOREGROUND_SERVICE_TYPE_MICROPHONE);
+ } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.Q) {
+ startForeground(NOTIFICATION_ID, notification, ServiceInfo.FOREGROUND_SERVICE_TYPE_MEDIA_PLAYBACK);
+ } else {
+ startForeground(NOTIFICATION_ID, notification);
+ }
+ }
+ } catch (Exception e) {
+ JitsiMeetLogger.e(e, TAG + " Error in onCreate");
+ }
+ }
+
+ @Override
+ public IBinder onBind(Intent intent) {
+ return null;
+ }
+
+ @Override
+ public int onStartCommand(Intent intent, int flags, int startId) {
+ return START_NOT_STICKY;
+ }
+}
diff --git a/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JitsiMeetReactNativePackage.java b/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JitsiMeetReactNativePackage.java
index fb6f46d583..ff2c86f681 100644
--- a/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JitsiMeetReactNativePackage.java
+++ b/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JitsiMeetReactNativePackage.java
@@ -21,6 +21,7 @@ public class JitsiMeetReactNativePackage implements ReactPackage {
new AndroidSettingsModule(reactContext),
new AppInfoModule(reactContext),
new AudioModeModule(reactContext),
+ new JMOngoingConferenceModule(reactContext),
new JavaScriptSandboxModule(reactContext),
new LocaleDetector(reactContext),
new LogBridgeModule(reactContext),
diff --git a/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/RNOngoingNotification.java b/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/RNOngoingNotification.java
new file mode 100644
index 0000000000..2925588dda
--- /dev/null
+++ b/react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/RNOngoingNotification.java
@@ -0,0 +1,108 @@
+/*
+ * Copyright @ 2019-present 8x8, Inc.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jitsi.meet.sdk;
+
+import android.app.Notification;
+import android.app.NotificationChannel;
+import android.app.NotificationManager;
+
+import android.app.PendingIntent;
+import android.app.Service;
+import android.content.Context;
+
+import android.content.Intent;
+import android.os.Build;
+
+import androidx.core.app.NotificationCompat;
+
+import org.jitsi.meet.sdk.log.JitsiMeetLogger;
+
+
+/**
+ * Helper class for creating the ongoing notification which is used with
+ * {@link JMOngoingConferenceService}. It allows the user to easily get back to the app
+ * and to hangup from within the notification itself.
+ */
+class RNOngoingNotification {
+ private static final String TAG = RNOngoingNotification.class.getSimpleName();
+
+ static final String RN_ONGOING_CONFERENCE_CHANNEL_ID = "OngoingConferenceChannel";
+
+ static void createOngoingConferenceNotificationChannel(Context context) {
+ NotificationManager notificationManager = (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);
+ NotificationChannel channel = notificationManager.getNotificationChannel(RN_ONGOING_CONFERENCE_CHANNEL_ID);
+
+ if (channel != null) {
+ JitsiMeetLogger.i(TAG + " Notification channel already exists");
+ return;
+ }
+
+ channel = new NotificationChannel(
+ RN_ONGOING_CONFERENCE_CHANNEL_ID,
+ context.getString(R.string.ongoing_notification_channel_name),
+ NotificationManager.IMPORTANCE_DEFAULT
+ );
+
+ channel.enableVibration(true);
+ channel.setShowBadge(true);
+ channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
+
+ notificationManager.createNotificationChannel(channel);
+ JitsiMeetLogger.i(TAG + " Notification channel created with importance: " + channel.getImportance());
+ }
+
+ static Notification buildOngoingConferenceNotification(Context context) {
+ if (context == null) {
+ JitsiMeetLogger.w(TAG + " Cannot create notification: no current context");
+ return null;
+ }
+
+ JitsiMeetLogger.i(TAG + " Creating notification with context: " + context);
+
+ // Creating an intent to launch app's main activity
+ Intent intent = context.getPackageManager()
+ .getLaunchIntentForPackage(context.getPackageName());
+ assert intent != null;
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
+
+ // Creating PendingIntent
+ PendingIntent pendingIntent = PendingIntent.getActivity(
+ context,
+ 0,
+ intent,
+ PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
+ );
+
+ NotificationCompat.Builder builder = new NotificationCompat.Builder(context, RN_ONGOING_CONFERENCE_CHANNEL_ID);
+
+ builder
+ .setCategory(NotificationCompat.CATEGORY_CALL)
+ .setContentTitle(context.getString(R.string.ongoing_notification_title))
+ .setContentText(context.getString(R.string.ongoing_notification_text))
+ .setPriority(NotificationCompat.PRIORITY_HIGH)
+ .setOngoing(true)
+ .setWhen(System.currentTimeMillis())
+ .setUsesChronometer(true)
+ .setAutoCancel(false)
+ .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
+ .setOnlyAlertOnce(true)
+ .setSmallIcon(context.getResources().getIdentifier("ic_notification", "drawable", context.getPackageName()))
+ .setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE)
+ .setContentIntent(pendingIntent);
+
+ return builder.build();
+ }
+}
diff --git a/react-native-sdk/package.json b/react-native-sdk/package.json
index 29a04e149e..1c86def2b9 100644
--- a/react-native-sdk/package.json
+++ b/react-native-sdk/package.json
@@ -36,6 +36,7 @@
"moment-duration-format": "0.0.0",
"optional-require": "0.0.0",
"promise.allsettled": "0.0.0",
+ "promise.withresolvers": "0.0.0",
"punycode": "0.0.0",
"react-emoji-render": "0.0.0",
"react-i18next": "0.0.0",
diff --git a/react/features/mobile/react-native-sdk/middleware.js b/react/features/mobile/react-native-sdk/middleware.js
index a8922e7302..13bf6b3df6 100644
--- a/react/features/mobile/react-native-sdk/middleware.js
+++ b/react/features/mobile/react-native-sdk/middleware.js
@@ -1,3 +1,5 @@
+import { NativeModules } from 'react-native';
+
import { getAppProp } from '../../base/app/functions';
import {
CONFERENCE_BLURRED,
@@ -10,6 +12,7 @@ import {
import { SET_AUDIO_MUTED, SET_VIDEO_MUTED } from '../../base/media/actionTypes';
import { PARTICIPANT_JOINED, PARTICIPANT_LEFT } from '../../base/participants/actionTypes';
import MiddlewareRegistry from '../../base/redux/MiddlewareRegistry';
+import StateListenerRegistry from '../../base/redux/StateListenerRegistry';
import { READY_TO_CLOSE } from '../external-api/actionTypes';
import { participantToParticipantInfo } from '../external-api/functions';
import { ENTER_PICTURE_IN_PICTURE } from '../picture-in-picture/actionTypes';
@@ -17,6 +20,7 @@ import { ENTER_PICTURE_IN_PICTURE } from '../picture-in-picture/actionTypes';
import { isExternalAPIAvailable } from './functions';
const externalAPIEnabled = isExternalAPIAvailable();
+const { JMOngoingConference } = NativeModules;
/**
@@ -84,3 +88,21 @@ const externalAPIEnabled = isExternalAPIAvailable();
return result;
});
+
+/**
+ * Before enabling media projection service control on Android,
+ * we need to check if native modules are being used or not.
+ */
+JMOngoingConference && !externalAPIEnabled && StateListenerRegistry.register(
+ state => state['features/base/conference'].conference,
+ (conference, previousConference) => {
+ if (!conference) {
+ JMOngoingConference.abort();
+ } else if (conference && !previousConference) {
+ JMOngoingConference.launch();
+ } else if (conference !== previousConference) {
+ JMOngoingConference.abort();
+ JMOngoingConference.launch();
+ }
+ }
+);