Jitsi Meet - Secure, Simple and Scalable Video Conferences that you use as a standalone app or embed in your web application.
TypeScript debiandeep-videojitsijitsi-meetscalable-video-conferencessfuvideovideo-communicationvideo-conferencingwebrtc
android: add notification while there is an ongoing meeting
Saúl Ibarra Corretgé714e0e045dfc
diff --git a/android/app/src/main/res/drawable-hdpi/ic_notification.png b/android/app/src/main/res/drawable-hdpi/ic_notification.pngnew file mode 100644index 0000000000..b3e86fe68aBinary files /dev/null and b/android/app/src/main/res/drawable-hdpi/ic_notification.png differdiff --git a/android/app/src/main/res/drawable-mdpi/ic_notification.png b/android/app/src/main/res/drawable-mdpi/ic_notification.pngnew file mode 100644index 0000000000..d6787b1db8Binary files /dev/null and b/android/app/src/main/res/drawable-mdpi/ic_notification.png differdiff --git a/android/app/src/main/res/drawable-xhdpi/ic_notification.png b/android/app/src/main/res/drawable-xhdpi/ic_notification.pngnew file mode 100644index 0000000000..c34043b86dBinary files /dev/null and b/android/app/src/main/res/drawable-xhdpi/ic_notification.png differdiff --git a/android/app/src/main/res/drawable-xxhdpi/ic_notification.png b/android/app/src/main/res/drawable-xxhdpi/ic_notification.pngnew file mode 100644index 0000000000..b9b4f516d5Binary files /dev/null and b/android/app/src/main/res/drawable-xxhdpi/ic_notification.png differdiff --git a/android/app/src/main/res/drawable-xxxhdpi/ic_notification.png b/android/app/src/main/res/drawable-xxxhdpi/ic_notification.pngnew file mode 100644index 0000000000..84bb3cb31fBinary files /dev/null and b/android/app/src/main/res/drawable-xxxhdpi/ic_notification.png differdiff --git a/android/sdk/src/main/AndroidManifest.xml b/android/sdk/src/main/AndroidManifest.xmlindex eae4471a43..b69676395a 100644--- a/android/sdk/src/main/AndroidManifest.xml+++ b/android/sdk/src/main/AndroidManifest.xml@@ -12,6 +12,7 @@<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /><uses-permission android:name="android.permission.WAKE_LOCK" /><uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />+ <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/><uses-featureandroid:glEsVersion="0x00020000"@@ -44,6 +45,8 @@<action android:name="android.telecom.ConnectionService" /></intent-filter></service>++ <service android:name="org.jitsi.meet.sdk.JitsiMeetOngoingConferenceService" /></application></manifest>\ No newline at end of filediff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/ExternalAPIModule.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/ExternalAPIModule.javaindex 9629b31ba1..4ac27de02b 100644--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/ExternalAPIModule.java+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/ExternalAPIModule.java@@ -68,7 +68,7 @@ class ExternalAPIModule@ReactMethodpublic void sendEvent(String name, ReadableMap data, String scope) {// Keep track of the current ongoing conference.- OngoingConferenceTracker.onExternalAPIEvent(name, data);+ OngoingConferenceTracker.getInstance().onExternalAPIEvent(name, data);// The JavaScript App needs to provide uniquely identifying information// to the native ExternalAPI module so that the latter may match thediff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeet.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeet.javaindex 68267f1c8a..e88279a7e9 100644--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeet.java+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeet.java@@ -42,7 +42,7 @@ public class JitsiMeet {* @return the current conference URL.*/public static String getCurrentConference() {- return OngoingConferenceTracker.getCurrentConference();+ return OngoingConferenceTracker.getInstance().getCurrentConference();}/**diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetActivity.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetActivity.javaindex d88f780adf..368816d45c 100644--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetActivity.java+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetActivity.java@@ -73,7 +73,18 @@ public class JitsiMeetActivity extends FragmentActivity@Overridepublic void onDestroy() {+ // Here we are trying to handle the following corner case: an application using the SDK+ // is using this Activity for displaying meetings, but there is another "main" Activity+ // with other content. If this Activity is "swiped out" from the recent list we will get+ // Activity#onDestroy() called without warning. At this point we can try to leave the+ // current meeting, but when our view is detached from React the JS <-> Native bridge won't+ // be operational so the external API won't be able to notify the native side that the+ // conference terminated. Thus, try our best to clean up.leave();+ if (AudioModeModule.useConnectionService()) {+ ConnectionService.abortConnections();+ }+ JitsiMeetOngoingConferenceService.abort(this);super.onDestroy();}@@ -198,6 +209,8 @@ public class JitsiMeetActivity extends FragmentActivity@Overridepublic void onConferenceJoined(Map<String, Object> data) {Log.d(TAG, "Conference joined: " + data);+ // Launch the service for the ongoing notification.+ JitsiMeetOngoingConferenceService.launch(this);}@Overridediff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetOngoingConferenceService.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetOngoingConferenceService.javanew file mode 100644index 0000000000..01abff5140--- /dev/null+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetOngoingConferenceService.java@@ -0,0 +1,109 @@+/*+ * 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.ComponentName;+import android.content.Context;+import android.content.Intent;+import android.os.IBinder;+import android.util.Log;+++/**+ * 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 JitsiMeetOngoingConferenceService extends Service+ implements OngoingConferenceTracker.OngoingConferenceListener {+ private static final String TAG = JitsiMeetOngoingConferenceService.class.getSimpleName();++ static final class Actions {+ static final String START = TAG + ":START";+ static final String HANGUP = TAG + ":HANGUP";+ }++ static void launch(Context context) {+ OngoingNotification.createOngoingConferenceNotificationChannel();++ Intent intent = new Intent(context, JitsiMeetOngoingConferenceService.class);+ intent.setAction(Actions.START);++ ComponentName componentName = context.startService(intent);+ if (componentName == null) {+ Log.w(TAG, "Ongoing conference service not started");+ }+ }++ static void abort(Context context) {+ Intent intent = new Intent(context, JitsiMeetOngoingConferenceService.class);+ context.stopService(intent);+ }++ @Override+ public void onCreate() {+ super.onCreate();++ OngoingConferenceTracker.getInstance().addListener(this);+ }++ @Override+ public void onDestroy() {+ OngoingConferenceTracker.getInstance().removeListener(this);++ super.onDestroy();+ }++ @Override+ public IBinder onBind(Intent intent) {+ return null;+ }++ @Override+ public int onStartCommand(Intent intent, int flags, int startId) {+ final String action = intent.getAction();+ if (action.equals(Actions.START)) {+ Notification notification = OngoingNotification.buildOngoingConferenceNotification();+ startForeground(OngoingNotification.NOTIFICATION_ID, notification);+ Log.i(TAG, "Service started");+ } else if (action.equals(Actions.HANGUP)) {+ Log.i(TAG, "Hangup requested");+ // Abort all ongoing calls+ if (AudioModeModule.useConnectionService()) {+ ConnectionService.abortConnections();+ }+ stopSelf();+ } else {+ Log.w(TAG, "Unknown action received: " + action);+ stopSelf();+ }++ return START_NOT_STICKY;+ }++ @Override+ public void onCurrentConferenceChanged(String conferenceUrl) {+ if (conferenceUrl == null) {+ stopSelf();+ Log.i(TAG, "Service stopped");+ }+ }+}diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.javaindex d9fb4e79c0..e5978c6649 100644--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/JitsiMeetView.java@@ -108,12 +108,12 @@ public class JitsiMeetView extends BaseReactView<JitsiMeetViewListener>throw new RuntimeException("Enclosing Activity must implement JitsiMeetActivityInterface");}- OngoingConferenceTracker.addListener(this);+ OngoingConferenceTracker.getInstance().addListener(this);}@Overridepublic void dispose() {- OngoingConferenceTracker.removeListener(this);+ OngoingConferenceTracker.getInstance().removeListener(this);super.dispose();}diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/OngoingConferenceTracker.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/OngoingConferenceTracker.javaindex ab0a1ce073..4e6f661ecc 100644--- a/android/sdk/src/main/java/org/jitsi/meet/sdk/OngoingConferenceTracker.java+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/OngoingConferenceTracker.java@@ -22,47 +22,71 @@ import java.util.Collection;import java.util.Collections;import java.util.HashSet;++/**+ * Helper class to keep track of what the current conference is.+ */class OngoingConferenceTracker {- private static final Collection<OngoingConferenceListener> listeners =+ private static final OngoingConferenceTracker instance = new OngoingConferenceTracker();++ private static final String CONFERENCE_WILL_JOIN = "CONFERENCE_WILL_JOIN";+ private static final String CONFERENCE_TERMINATED = "CONFERENCE_TERMINATED";++ private final Collection<OngoingConferenceListener> listeners =Collections.synchronizedSet(new HashSet<OngoingConferenceListener>());- private static String currentConference;+ private String currentConference;++ public OngoingConferenceTracker() {+ }++ public static OngoingConferenceTracker getInstance() {+ return instance;+ }- static synchronized String getCurrentConference() {+ /**+ * Gets the current active conference URL.+ *+ * @return - The current conference URL as a String.+ */+ synchronized String getCurrentConference() {return currentConference;}- static synchronized void onExternalAPIEvent(String name, ReadableMap data) {+ synchronized void onExternalAPIEvent(String name, ReadableMap data) {if (!data.hasKey("url")) {return;}String url = data.getString("url");+ if (url == null) {+ return;+ }switch(name) {- case "CONFERENCE_WILL_JOIN":+ case CONFERENCE_WILL_JOIN:currentConference = url;- updateCurrentConference();+ updateListeners();break;- case "CONFERENCE_TERMINATED":- if (currentConference != null && url.equals(currentConference)) {+ case CONFERENCE_TERMINATED:+ if (url.equals(currentConference)) {currentConference = null;- updateCurrentConference();+ updateListeners();}break;}}- static void addListener(OngoingConferenceListener listener) {+ void addListener(OngoingConferenceListener listener) {listeners.add(listener);}- static void removeListener(OngoingConferenceListener listener) {+ void removeListener(OngoingConferenceListener listener) {listeners.remove(listener);}- private static synchronized void updateCurrentConference() {- for (OngoingConferenceListener listener: listeners) {+ private void updateListeners() {+ for (OngoingConferenceListener listener : listeners) {listener.onCurrentConferenceChanged(currentConference);}}diff --git a/android/sdk/src/main/java/org/jitsi/meet/sdk/OngoingNotification.java b/android/sdk/src/main/java/org/jitsi/meet/sdk/OngoingNotification.javanew file mode 100644index 0000000000..7265a893ae--- /dev/null+++ b/android/sdk/src/main/java/org/jitsi/meet/sdk/OngoingNotification.java@@ -0,0 +1,118 @@+/*+ * 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.content.Context;+import android.content.Intent;+import android.os.Build;+import android.support.v4.app.NotificationCompat;+import android.util.Log;++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+ * and to hangup from within the notification itself.+ */+class OngoingNotification {+ private static final String TAG = OngoingNotification.class.getSimpleName();++ private static final String CHANNEL_ID = "JitsiNotificationChannel";+ private static final String CHANNEL_NAME = "Ongoing Conference Notifications";++ static final int NOTIFICATION_ID = new Random().nextInt(99999) + 10000;+++ static void createOngoingConferenceNotificationChannel() {+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {+ return;+ }++ Context context = ReactInstanceManagerHolder.getCurrentActivity();+ if (context == null) {+ Log.w(TAG, "Cannot create notification channel: no current context");+ return;+ }++ NotificationManager notificationManager+ = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);++ NotificationChannel channel+ = notificationManager.getNotificationChannel(CHANNEL_ID);+ if (channel != null) {+ // The channel was already created, no need to do it again.+ return;+ }++ channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT);+ channel.enableLights(false);+ channel.enableVibration(false);+ channel.setShowBadge(false);++ notificationManager.createNotificationChannel(channel);+ }++ static Notification buildOngoingConferenceNotification() {+ Context context = ReactInstanceManagerHolder.getCurrentActivity();+ if (context == null) {+ Log.w(TAG, "Cannot create notification: no current context");+ return null;+ }++ Intent notificationIntent = new Intent(context, context.getClass());+ PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);++ NotificationCompat.Builder builder;+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {+ builder = new NotificationCompat.Builder(context, CHANNEL_ID);+ } else {+ builder = new NotificationCompat.Builder(context);+ }++ 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)+ .setContentIntent(pendingIntent)+ .setOngoing(true)+ .setAutoCancel(false)+ .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)+ .setUsesChronometer(true)+ .setOnlyAlertOnce(true)+ .setSmallIcon(context.getResources().getIdentifier("ic_notification", "drawable", context.getPackageName()));++ // Add a "hang-up" action only if we are using ConnectionService.+ if (AudioModeModule.useConnectionService()) {+ Intent hangupIntent = new Intent(context, JitsiMeetOngoingConferenceService.class);+ hangupIntent.setAction(JitsiMeetOngoingConferenceService.Actions.HANGUP);+ PendingIntent hangupPendingIntent+ = PendingIntent.getService(context, 0, hangupIntent, PendingIntent.FLAG_UPDATE_CURRENT);+ NotificationCompat.Action hangupAction = new NotificationCompat.Action(0, "Hang up", hangupPendingIntent);++ builder.addAction(hangupAction);+ }++ return builder.build();+ }+}diff --git a/android/sdk/src/main/res/values/strings.xml b/android/sdk/src/main/res/values/strings.xmlindex c9d1ebcb92..3bd33c20e6 100644--- a/android/sdk/src/main/res/values/strings.xml+++ b/android/sdk/src/main/res/values/strings.xml@@ -1,4 +1,6 @@<resources><string name="app_name">Jitsi Meet SDK</string><string name="dropbox_app_key"></string>+ <string name="ongoing_notification_title">Ongoing meeting</string>+ <string name="ongoing_notification_text">You are currently in a meeting. Tap to return to it.</string></resources>