Skip to content

Commit

Permalink
feat(react-native-sdk): use context instead of activity for notificat…
Browse files Browse the repository at this point in the history
…ion channel creation
  • Loading branch information
Calinteodor committed Dec 23, 2024
1 parent 627dcbf commit a9a9eb4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.module.annotations.ReactModule;

import org.jitsi.meet.sdk.log.JitsiMeetLogger;


/**
* This class implements a ReactModule and it's
* responsible for launching/aborting a service when a conference is in progress.
Expand All @@ -25,12 +28,14 @@ public void launch() {
Context context = getReactApplicationContext();
Activity currentActivity = getCurrentActivity();
JMOngoingConferenceService.launch(context, currentActivity);
JitsiMeetLogger.w(NAME + " Module launch");
}

@ReactMethod
public void abort() {
Context context = getReactApplicationContext();
JMOngoingConferenceService.abort(context);
JitsiMeetLogger.w(NAME + " Module abort");
}

@NonNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,20 @@
* limitations under the License.
*/
package org.jitsi.meet.sdk;

import static android.Manifest.permission.POST_NOTIFICATIONS;
import static android.Manifest.permission.RECORD_AUDIO;

import android.app.Activity;
import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;

import android.content.Context;
import android.content.Intent;

import android.content.pm.PackageManager;
import android.content.pm.ServiceInfo;

import android.os.Build;
import android.os.IBinder;

Expand All @@ -51,29 +54,24 @@ public class JMOngoingConferenceService extends Service {

static final int NOTIFICATION_ID = new Random().nextInt(99999) + 10000;

public static void doLaunch(Context context, Activity currentActivity) {
public static void doLaunch(Context context) {

RNOngoingNotification.createOngoingConferenceNotificationChannel(currentActivity);
RNOngoingNotification.createOngoingConferenceNotificationChannel(context);

Intent intent = new Intent(context, JMOngoingConferenceService.class);

ComponentName componentName;

try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
componentName = context.startForegroundService(intent);
context.startForegroundService(intent);
} else {
componentName = context.startService(intent);
context.startService(intent);
}
} 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);
return;
}
if (componentName == null) {
JitsiMeetLogger.w(TAG + " Ongoing conference service not started");
}
}

public static void launch(Context context, Activity currentActivity) {
Expand All @@ -92,7 +90,7 @@ public boolean onRequestPermissionsResult(int i, String[] strings, int[] results
}

if (counter == results.length){
doLaunch(context, currentActivity);
doLaunch(context);
JitsiMeetLogger.w(TAG + " Service launched, permissions were granted");
} else {
JitsiMeetLogger.w(TAG + " Couldn't launch service, permissions were not granted");
Expand Down Expand Up @@ -120,7 +118,7 @@ public boolean onRequestPermissionsResult(int i, String[] strings, int[] results
listener.onRequestPermissionsResult(PERMISSIONS_REQUEST_CODE, permissionsArray, new int[0]);
}
} else {
doLaunch(context, currentActivity);
doLaunch(context);
JitsiMeetLogger.w(TAG + " No permissions needed, launching service");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,41 @@
* limitations under the License.
*/
package org.jitsi.meet.sdk;
import android.app.Activity;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;

import android.content.Context;
import android.content.Intent;

import android.os.Build;
import androidx.annotation.StringRes;

import androidx.core.app.NotificationCompat;

import org.jitsi.meet.sdk.log.JitsiMeetLogger;

import java.util.Random;

/**
* Helper class for creating the ongoing notification which is used with
* {@link JitsiMeetOngoingConferenceService}. It allows the user to easily get back to the app
* {@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 void createOngoingConferenceNotificationChannel(Activity currentActivity) {
JitsiMeetLogger.w(TAG + " Checking Android version and activity context");

static void createOngoingConferenceNotificationChannel(Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
JitsiMeetLogger.w(TAG + " Android version is lower than Oreo, skipping notification channel creation");
return;
}
if (currentActivity == null) {
if (context == null) {
JitsiMeetLogger.w(TAG + " Cannot create notification channel: no current context");
return;
}

NotificationManager notificationManager
= (NotificationManager) currentActivity.getSystemService(Context.NOTIFICATION_SERVICE);
= (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE);

NotificationChannel channel
= notificationManager.getNotificationChannel("OngoingConferenceChannel");
Expand All @@ -57,7 +58,7 @@ static void createOngoingConferenceNotificationChannel(Activity currentActivity)
return;
}

channel = new NotificationChannel("OngoingConferenceChannel", currentActivity.getString(R.string.ongoing_notification_channel_name), NotificationManager.IMPORTANCE_DEFAULT);
channel = new NotificationChannel("OngoingConferenceChannel", context.getString(R.string.ongoing_notification_channel_name), NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(false);
channel.enableVibration(false);
channel.setShowBadge(false);
Expand Down Expand Up @@ -86,7 +87,8 @@ static Notification buildOngoingConferenceNotification(Context context) {
.setAutoCancel(false)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setOnlyAlertOnce(true)
.setSmallIcon(context.getResources().getIdentifier("ic_notification", "drawable", context.getPackageName()));
.setSmallIcon(context.getResources().getIdentifier("ic_notification", "drawable", context.getPackageName())),
.setForegroundServiceBehavior(NotificationCompat.FOREGROUND_SERVICE_IMMEDIATE);

return builder.build();
}
Expand Down

0 comments on commit a9a9eb4

Please sign in to comment.