From 050a943723ae50e5216cc92c6b0368fa5bc0ec5d Mon Sep 17 00:00:00 2001 From: Guillaume Beraudo Date: Wed, 17 Aug 2011 15:17:19 +0200 Subject: [PATCH] Specific function to get LC when LinphoneManager may be already destroyed. --- src/org/linphone/DialerActivity.java | 2 +- src/org/linphone/LinphoneManager.java | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/org/linphone/DialerActivity.java b/src/org/linphone/DialerActivity.java index 34de67cc9..7e99f58e0 100644 --- a/src/org/linphone/DialerActivity.java +++ b/src/org/linphone/DialerActivity.java @@ -385,7 +385,7 @@ public class DialerActivity extends SoftVolumeActivity implements LinphoneGuiLis public void onCallStateChanged(LinphoneCall call, State state, String message) { - LinphoneCore lc = LinphoneManager.getLc(); + LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); if (lc==null) { /* we are certainly exiting, ignore then.*/ return; diff --git a/src/org/linphone/LinphoneManager.java b/src/org/linphone/LinphoneManager.java index c3c74740c..6c1e35430 100644 --- a/src/org/linphone/LinphoneManager.java +++ b/src/org/linphone/LinphoneManager.java @@ -214,13 +214,16 @@ public final class LinphoneManager implements LinphoneCoreListener { public static synchronized final LinphoneManager getInstance() { if (instance != null) return instance; - if (!sExited) throw new RuntimeException("Linphone Manager should be created before accessed"); - return null; + if (sExited) { + throw new RuntimeException("Linphone Manager was already destroyed. " + + "Better use getLcIfManagerNotDestroyed and check returned value"); + } + + throw new RuntimeException("Linphone Manager should be created before accessed"); } public static synchronized final LinphoneCore getLc() { - LinphoneManager m=getInstance(); - return m!=null ? m.mLc : null; + return getInstance().mLc; } @@ -916,5 +919,14 @@ public final class LinphoneManager implements LinphoneCoreListener { return distanceInCm < threshold; } + public static synchronized LinphoneCore getLcIfManagerNotDestroyedOrNull() { + if (sExited) { + // Can occur if the UI thread play a posted event but in the meantime the LinphoneManager was destroyed + // Ex: stop call and quickly terminate application. + Log.w("Trying to get linphone core while LinphoneManager already destroyed"); + return null; + } + return getLc(); + } }