diff --git a/coreapi/linphonecore.c b/coreapi/linphonecore.c index cc3931c7b..49ccf8aee 100644 --- a/coreapi/linphonecore.c +++ b/coreapi/linphonecore.c @@ -6018,6 +6018,14 @@ void friends_config_uninit(LinphoneCore* lc) ms_message("Destroying friends done."); } +void linphone_core_enter_background(LinphoneCore *lc) { + L_GET_CPP_PTR_FROM_C_OBJECT(lc)->enterBackground(); +} + +void linphone_core_enter_foreground(LinphoneCore *lc) { + L_GET_CPP_PTR_FROM_C_OBJECT(lc)->enterForeground(); +} + LpConfig * linphone_core_get_config(const LinphoneCore *lc){ return lc->config; } diff --git a/include/linphone/core.h b/include/linphone/core.h index dee491297..4fdc0c26d 100644 --- a/include/linphone/core.h +++ b/include/linphone/core.h @@ -4026,6 +4026,18 @@ LINPHONE_PUBLIC void *linphone_core_get_user_data(const LinphoneCore *lc); **/ LINPHONE_PUBLIC void linphone_core_set_user_data(LinphoneCore *lc, void *userdata); +/** + * This method is called by the application to notify the linphone core library when it enters background mode. + * @ingroup misc + */ +LINPHONE_PUBLIC void linphone_core_enter_background(LinphoneCore *lc); + +/** + * This method is called by the application to notify the linphone core library when it enters foreground mode. + * @ingroup misc + */ +LINPHONE_PUBLIC void linphone_core_enter_foreground(LinphoneCore *lc); + /** * Returns the LpConfig object used to manage the storage (config) file. * @param[in] lc #LinphoneCore object diff --git a/src/conference/handlers/remote-conference-event-handler-p.h b/src/conference/handlers/remote-conference-event-handler-p.h index 54eaf2280..84903a293 100644 --- a/src/conference/handlers/remote-conference-event-handler-p.h +++ b/src/conference/handlers/remote-conference-event-handler-p.h @@ -41,6 +41,8 @@ private: // CoreListener void onNetworkReachable (bool sipNetworkReachable, bool mediaNetworkReachable) override; void onRegistrationStateChanged (LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const std::string &message) override; + void onEnteringBackground () override; + void onEnteringForeground () override; ChatRoomId chatRoomId; diff --git a/src/conference/handlers/remote-conference-event-handler.cpp b/src/conference/handlers/remote-conference-event-handler.cpp index 69142d6f3..bc97ca183 100644 --- a/src/conference/handlers/remote-conference-event-handler.cpp +++ b/src/conference/handlers/remote-conference-event-handler.cpp @@ -238,6 +238,14 @@ void RemoteConferenceEventHandlerPrivate::onRegistrationStateChanged (LinphonePr subscribe(); } +void RemoteConferenceEventHandlerPrivate::onEnteringBackground () { + unsubscribe(); +} + +void RemoteConferenceEventHandlerPrivate::onEnteringForeground () { + subscribe(); +} + // ----------------------------------------------------------------------------- RemoteConferenceEventHandler::RemoteConferenceEventHandler (RemoteConference *remoteConference) : diff --git a/src/conference/handlers/remote-conference-list-event-handler.cpp b/src/conference/handlers/remote-conference-list-event-handler.cpp index ea0035e58..1c77a4636 100644 --- a/src/conference/handlers/remote-conference-list-event-handler.cpp +++ b/src/conference/handlers/remote-conference-list-event-handler.cpp @@ -245,4 +245,12 @@ void RemoteConferenceListEventHandler::onRegistrationStateChanged (LinphoneProxy subscribe(); } +void RemoteConferenceListEventHandler::onEnteringBackground () { + unsubscribe(); +} + +void RemoteConferenceListEventHandler::onEnteringForeground () { + subscribe(); +} + LINPHONE_END_NAMESPACE diff --git a/src/conference/handlers/remote-conference-list-event-handler.h b/src/conference/handlers/remote-conference-list-event-handler.h index e7d504c86..c93dc4ca5 100644 --- a/src/conference/handlers/remote-conference-list-event-handler.h +++ b/src/conference/handlers/remote-conference-list-event-handler.h @@ -61,6 +61,8 @@ private: // CoreListener void onNetworkReachable (bool sipNetworkReachable, bool mediaNetworkReachable) override; void onRegistrationStateChanged (LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const std::string &message) override; + void onEnteringBackground () override; + void onEnteringForeground () override; }; LINPHONE_END_NAMESPACE diff --git a/src/core/core-listener.h b/src/core/core-listener.h index b26d2de8d..53784f59e 100644 --- a/src/core/core-listener.h +++ b/src/core/core-listener.h @@ -33,6 +33,8 @@ public: virtual void onGlobalStateChanged (LinphoneGlobalState state) {} virtual void onNetworkReachable (bool sipNetworkReachable, bool mediaNetworkReachable) {} virtual void onRegistrationStateChanged (LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const std::string &message) {} + virtual void onEnteringBackground () {} + virtual void onEnteringForeground () {} }; LINPHONE_END_NAMESPACE diff --git a/src/core/core-p.h b/src/core/core-p.h index c1667f0b1..d740ff4be 100644 --- a/src/core/core-p.h +++ b/src/core/core-p.h @@ -44,6 +44,8 @@ public: void notifyGlobalStateChanged (LinphoneGlobalState state); void notifyNetworkReachable (bool sipNetworkReachable, bool mediaNetworkReachable); void notifyRegistrationStateChanged (LinphoneProxyConfig *cfg, LinphoneRegistrationState state, const std::string &message); + void notifyEnteringBackground (); + void notifyEnteringForeground (); int addCall (const std::shared_ptr &call); bool canWeAddCall () const; @@ -72,6 +74,8 @@ public: std::unique_ptr localListEventHandler; private: + bool isInBackground = false; + std::list listeners; std::list> calls; diff --git a/src/core/core.cpp b/src/core/core.cpp index f9a82dfcc..834d74d45 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -113,6 +113,26 @@ void CorePrivate::notifyRegistrationStateChanged (LinphoneProxyConfig *cfg, Linp listener->onRegistrationStateChanged(cfg, state, message); } +void CorePrivate::notifyEnteringBackground () { + if (isInBackground) + return; + + isInBackground = true; + auto listenersCopy = listeners; // Allow removable of a listener in its own call + for (const auto &listener : listenersCopy) + listener->onEnteringBackground(); +} + +void CorePrivate::notifyEnteringForeground () { + if (!isInBackground) + return; + + isInBackground = false; + auto listenersCopy = listeners; // Allow removable of a listener in its own call + for (const auto &listener : listenersCopy) + listener->onEnteringForeground(); +} + // ============================================================================= Core::Core () : Object(*new CorePrivate) { @@ -131,6 +151,24 @@ shared_ptr Core::create (LinphoneCore *cCore) { return core; } +// --------------------------------------------------------------------------- +// Application lifecycle. +// --------------------------------------------------------------------------- + +void Core::enterBackground () { + L_D(); + d->notifyEnteringBackground(); +} + +void Core::enterForeground () { + L_D(); + d->notifyEnteringForeground(); +} + +// --------------------------------------------------------------------------- +// C-Core. +// --------------------------------------------------------------------------- + LinphoneCore *Core::getCCore () const { return L_GET_C_BACK_PTR(this); } diff --git a/src/core/core.h b/src/core/core.h index c8de9d2fe..ba9356e55 100644 --- a/src/core/core.h +++ b/src/core/core.h @@ -71,6 +71,13 @@ public: // Return a new Core instance. Entry point of Linphone. static std::shared_ptr create (LinphoneCore *cCore); + // --------------------------------------------------------------------------- + // Application lifecycle. + // --------------------------------------------------------------------------- + + void enterBackground (); + void enterForeground (); + // --------------------------------------------------------------------------- // C-Core. // ---------------------------------------------------------------------------