-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(react-native-sdk/android): force permissions approval in order to launch RNOngoingNotification #15400
Open
Calinteodor
wants to merge
5
commits into
jitsi:master
Choose a base branch
from
Calinteodor:fix-android-rnsdk-screenshare
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+328
−0
Open
feat(react-native-sdk/android): force permissions approval in order to launch RNOngoingNotification #15400
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
74f9017
feat(react-native-sdk/android): force permissions to launch RNOngoing…
Calinteodor 315ec10
feat(react-native-sdk/android): rename JitsiMeetOngoingConferenceService
Calinteodor 627dcbf
feat(react-native-sdk/android): added extra logs
Calinteodor a9a9eb4
feat(react-native-sdk): use context instead of activity for notificat…
Calinteodor 3b4b6c2
feat(react-native-sdk): fix builder .set method and module imports
Calinteodor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JMOngoingConferenceModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.jitsi.meet.sdk; | ||
import android.app.Activity; | ||
import android.content.Context; | ||
import androidx.annotation.NonNull; | ||
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 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. | ||
*/ | ||
@ReactModule(name = JMOngoingConferenceModule.NAME) | ||
class JMOngoingConferenceModule extends ReactContextBaseJavaModule { | ||
|
||
public static final String NAME = "JMOngoingConference"; | ||
|
||
public JMOngoingConferenceModule(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
@ReactMethod | ||
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 | ||
@Override | ||
public String getName() { | ||
return NAME; | ||
} | ||
} |
162 changes: 162 additions & 0 deletions
162
react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/JMOngoingConferenceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* 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 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.Context; | ||
import android.content.Intent; | ||
|
||
import android.content.pm.PackageManager; | ||
import android.content.pm.ServiceInfo; | ||
|
||
import android.os.Build; | ||
import android.os.IBinder; | ||
|
||
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; | ||
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(); | ||
|
||
private static final int PERMISSIONS_REQUEST_CODE = (int) (Math.random() * Short.MAX_VALUE); | ||
|
||
static final int NOTIFICATION_ID = new Random().nextInt(99999) + 10000; | ||
|
||
public static void doLaunch(Context context) { | ||
|
||
RNOngoingNotification.createOngoingConferenceNotificationChannel(context); | ||
|
||
Intent intent = new Intent(context, JMOngoingConferenceService.class); | ||
|
||
try { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { | ||
context.startForegroundService(intent); | ||
} else { | ||
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; | ||
} | ||
} | ||
|
||
public static void launch(Context context, Activity currentActivity) { | ||
List<String> permissionsList = new ArrayList<>(); | ||
|
||
PermissionListener listener = new PermissionListener() { | ||
@Override | ||
public boolean onRequestPermissionsResult(int i, String[] strings, int[] results) { | ||
int counter = 0; | ||
|
||
if (results.length > 0) { | ||
for (int result : results) { | ||
if (result == PackageManager.PERMISSION_GRANTED) { | ||
counter++; | ||
} | ||
} | ||
|
||
if (counter == results.length){ | ||
doLaunch(context); | ||
JitsiMeetLogger.w(TAG + " Service launched, permissions were granted"); | ||
} else { | ||
JitsiMeetLogger.w(TAG + " Couldn't launch service, permissions were not granted"); | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
}; | ||
|
||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { | ||
permissionsList.add(POST_NOTIFICATIONS); | ||
permissionsList.add(RECORD_AUDIO); | ||
} | ||
|
||
String[] permissionsArray = new String[ permissionsList.size() ]; | ||
permissionsArray = permissionsList.toArray( permissionsArray ); | ||
|
||
if (permissionsArray.length > 0) { | ||
try { | ||
currentActivity.requestPermissions(permissionsArray, PERMISSIONS_REQUEST_CODE); | ||
JitsiMeetLogger.w(TAG + " Requesting permissions: " + Arrays.toString(permissionsArray)); | ||
} catch (Exception e) { | ||
JitsiMeetLogger.e(e, "Error requesting permissions"); | ||
listener.onRequestPermissionsResult(PERMISSIONS_REQUEST_CODE, permissionsArray, new int[0]); | ||
} | ||
} else { | ||
doLaunch(context); | ||
JitsiMeetLogger.w(TAG + " No permissions needed, launching service"); | ||
} | ||
} | ||
|
||
public static void abort(Context context) { | ||
Intent intent = new Intent(context, JMOngoingConferenceService.class); | ||
context.stopService(intent); | ||
} | ||
|
||
@Override | ||
public void onCreate() { | ||
super.onCreate(); | ||
JitsiMeetLogger.w(TAG + " Building ongoing conference notification"); | ||
|
||
Notification notification = RNOngoingNotification.buildOngoingConferenceNotification(this); | ||
if (notification == null) { | ||
stopSelf(); | ||
JitsiMeetLogger.w(TAG + " Couldn't start service, notification is null"); | ||
} else { | ||
JitsiMeetLogger.w(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); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public IBinder onBind(Intent intent) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public int onStartCommand(Intent intent, int flags, int startId) { | ||
return START_NOT_STICKY; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
react-native-sdk/android/src/main/java/org/jitsi/meet/sdk/RNOngoingNotification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* | ||
* 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.content.Context; | ||
|
||
import android.os.Build; | ||
|
||
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 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(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 (context == null) { | ||
JitsiMeetLogger.w(TAG + " Cannot create notification channel: no current context"); | ||
return; | ||
} | ||
|
||
NotificationManager notificationManager | ||
= (NotificationManager) context.getSystemService(Service.NOTIFICATION_SERVICE); | ||
|
||
NotificationChannel channel | ||
= notificationManager.getNotificationChannel("OngoingConferenceChannel"); | ||
|
||
if (channel != null) { | ||
JitsiMeetLogger.w(TAG + " Notification channel already exists"); | ||
return; | ||
} | ||
|
||
channel = new NotificationChannel("OngoingConferenceChannel", context.getString(R.string.ongoing_notification_channel_name), NotificationManager.IMPORTANCE_DEFAULT); | ||
channel.enableLights(false); | ||
channel.enableVibration(false); | ||
channel.setShowBadge(false); | ||
|
||
notificationManager.createNotificationChannel(channel); | ||
JitsiMeetLogger.w(TAG + " Notification channel created successfully"); | ||
} | ||
static Notification buildOngoingConferenceNotification(Context context) { | ||
if (context == null) { | ||
JitsiMeetLogger.w(TAG + " Cannot create notification: no current context"); | ||
return null; | ||
} | ||
|
||
JitsiMeetLogger.w(TAG + " Creating notification with context: " + context); | ||
|
||
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "OngoingConferenceChannel"); | ||
|
||
builder | ||
.setCategory(NotificationCompat.CATEGORY_CALL) | ||
.setContentTitle(context.getString(R.string.ongoing_notification_title)) | ||
.setContentText(context.getString(R.string.ongoing_notification_text)) | ||
.setPriority(NotificationCompat.PRIORITY_DEFAULT) | ||
.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); | ||
|
||
return builder.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could check if
JMOngoingConference
is not undefined instead.