Using new addListener/removeListener instead of deprecated setListener for LoggingService, Player and FriendList

This commit is contained in:
Sylvain Berfini 2019-02-20 11:31:19 +01:00
parent 29670d63ff
commit 49cee682dc
9 changed files with 67 additions and 85 deletions

View file

@ -1855,13 +1855,13 @@ public class LinphoneManager implements CoreListener, SensorEventListener, Accou
@Override @Override
public void onFriendListCreated(Core lc, FriendList list) { public void onFriendListCreated(Core lc, FriendList list) {
if (LinphoneService.isReady()) { if (LinphoneService.isReady()) {
list.setListener(ContactsManager.getInstance()); list.addListener(ContactsManager.getInstance());
} }
} }
@Override @Override
public void onFriendListRemoved(Core lc, FriendList list) { public void onFriendListRemoved(Core lc, FriendList list) {
list.setListener(null); list.removeListener(ContactsManager.getInstance());
} }
@Override @Override

View file

@ -43,6 +43,9 @@ import org.linphone.core.Core;
import org.linphone.core.CoreListenerStub; import org.linphone.core.CoreListenerStub;
import org.linphone.core.Factory; import org.linphone.core.Factory;
import org.linphone.core.GlobalState; import org.linphone.core.GlobalState;
import org.linphone.core.LogLevel;
import org.linphone.core.LoggingService;
import org.linphone.core.LoggingServiceListener;
import org.linphone.core.ProxyConfig; import org.linphone.core.ProxyConfig;
import org.linphone.core.RegistrationState; import org.linphone.core.RegistrationState;
import org.linphone.mediastream.Log; import org.linphone.mediastream.Log;
@ -86,6 +89,36 @@ public final class LinphoneService extends Service {
private String mIncomingReceivedActivityName; private String mIncomingReceivedActivityName;
private Class<? extends Activity> mIncomingReceivedActivity = LinphoneActivity.class; private Class<? extends Activity> mIncomingReceivedActivity = LinphoneActivity.class;
private LoggingServiceListener mJavaLoggingService =
new LoggingServiceListener() {
@Override
public void onLogMessageWritten(
LoggingService logService, String domain, LogLevel lev, String message) {
switch (lev) {
case Debug:
android.util.Log.d(domain, message);
break;
case Message:
android.util.Log.i(domain, message);
break;
case Warning:
android.util.Log.w(domain, message);
break;
case Error:
android.util.Log.e(domain, message);
break;
case Fatal:
default:
android.util.Log.wtf(domain, message);
break;
}
}
};
public LoggingServiceListener getJavaLoggingService() {
return mJavaLoggingService;
}
public static boolean isReady() { public static boolean isReady() {
return sInstance != null && sInstance.mTestDelayElapsed; return sInstance != null && sInstance.mTestDelayElapsed;
} }
@ -270,7 +303,11 @@ public final class LinphoneService extends Service {
LinphonePreferences.instance().setContext(getBaseContext()); LinphonePreferences.instance().setContext(getBaseContext());
Factory.instance().setLogCollectionPath(getFilesDir().getAbsolutePath()); Factory.instance().setLogCollectionPath(getFilesDir().getAbsolutePath());
boolean isDebugEnabled = LinphonePreferences.instance().isDebugEnabled(); boolean isDebugEnabled = LinphonePreferences.instance().isDebugEnabled();
LinphoneUtils.initLoggingService(isDebugEnabled, getString(R.string.app_name)); LinphoneUtils.configureLoggingService(isDebugEnabled, getString(R.string.app_name));
// LinphoneService isn't ready yet so we have to manually set up the Java logging service
if (LinphonePreferences.instance().useJavaLogger()) {
Factory.instance().getLoggingService().addListener(mJavaLoggingService);
}
// Dump some debugging information to the logs // Dump some debugging information to the logs
Log.i(START_LINPHONE_LOGS); Log.i(START_LINPHONE_LOGS);
@ -395,6 +432,8 @@ public final class LinphoneService extends Service {
LinphoneActivity.instance().finish(); LinphoneActivity.instance().finish();
} }
LinphoneUtils.configureLoggingService(false, getString(R.string.app_name));
super.onDestroy(); super.onDestroy();
} }

View file

@ -75,7 +75,7 @@ class AsyncContactsLoader extends AsyncTask<Void, Void, AsyncContactsLoader.Asyn
|| !list.getRlsAddress().asStringUriOnly().equals(rls))) { || !list.getRlsAddress().asStringUriOnly().equals(rls))) {
list.setRlsUri(rls); list.setRlsUri(rls);
} }
list.setListener(ContactsManager.getInstance()); list.addListener(ContactsManager.getInstance());
} }
} }
} }
@ -215,8 +215,6 @@ class AsyncContactsLoader extends AsyncTask<Void, Void, AsyncContactsLoader.Asyn
contact.createOrUpdateFriendFromNativeContact(); contact.createOrUpdateFriendFromNativeContact();
} }
ContactsManager.getInstance().clearGroupChatContacts();
ContactsManager.getInstance().clearLimeX3dhContacts();
// Now that contact fetching is asynchronous, this is required to ensure // Now that contact fetching is asynchronous, this is required to ensure
// presence subscription event will be sent with all friends // presence subscription event will be sent with all friends
for (FriendList list : LinphoneManager.getLc().getFriendsLists()) { for (FriendList list : LinphoneManager.getLc().getFriendsLists()) {

View file

@ -50,7 +50,6 @@ import org.linphone.compatibility.Compatibility;
import org.linphone.core.Address; import org.linphone.core.Address;
import org.linphone.core.Core; import org.linphone.core.Core;
import org.linphone.core.Friend; import org.linphone.core.Friend;
import org.linphone.core.FriendCapability;
import org.linphone.core.FriendList; import org.linphone.core.FriendList;
import org.linphone.core.FriendListListener; import org.linphone.core.FriendListListener;
import org.linphone.core.MagicSearch; import org.linphone.core.MagicSearch;
@ -61,7 +60,7 @@ import org.linphone.settings.LinphonePreferences;
public class ContactsManager extends ContentObserver implements FriendListListener { public class ContactsManager extends ContentObserver implements FriendListListener {
private static ContactsManager sInstance; private static ContactsManager sInstance;
private List<LinphoneContact> mContacts, mSipContacts, mGroupChatContacts, mLimeX3dhContacts; private List<LinphoneContact> mContacts, mSipContacts;
private ArrayList<ContactsUpdatedListener> mContactsUpdatedListeners; private ArrayList<ContactsUpdatedListener> mContactsUpdatedListeners;
private MagicSearch mMagicSearch; private MagicSearch mMagicSearch;
private final Bitmap mDefaultAvatar; private final Bitmap mDefaultAvatar;
@ -83,8 +82,6 @@ public class ContactsManager extends ContentObserver implements FriendListListen
mContactsUpdatedListeners = new ArrayList<>(); mContactsUpdatedListeners = new ArrayList<>();
mContacts = new ArrayList<>(); mContacts = new ArrayList<>();
mSipContacts = new ArrayList<>(); mSipContacts = new ArrayList<>();
mGroupChatContacts = new ArrayList<>();
mLimeX3dhContacts = new ArrayList<>();
if (LinphoneManager.getLcIfManagerNotDestroyedOrNull() != null) { if (LinphoneManager.getLcIfManagerNotDestroyedOrNull() != null) {
mMagicSearch = LinphoneManager.getLcIfManagerNotDestroyedOrNull().createMagicSearch(); mMagicSearch = LinphoneManager.getLcIfManagerNotDestroyedOrNull().createMagicSearch();
@ -114,10 +111,6 @@ public class ContactsManager extends ContentObserver implements FriendListListen
fetchContactsAsync(); fetchContactsAsync();
} }
public synchronized boolean hasContacts() {
return mContacts.size() > 0;
}
public synchronized List<LinphoneContact> getContacts() { public synchronized List<LinphoneContact> getContacts() {
return mContacts; return mContacts;
} }
@ -134,27 +127,11 @@ public class ContactsManager extends ContentObserver implements FriendListListen
mSipContacts = c; mSipContacts = c;
} }
public synchronized List<LinphoneContact> getGroupChatContacts() {
return mGroupChatContacts;
}
synchronized void clearGroupChatContacts() {
mGroupChatContacts.clear();
}
public synchronized List<LinphoneContact> getLimeX3dhContacts() {
return mLimeX3dhContacts;
}
synchronized void clearLimeX3dhContacts() {
mLimeX3dhContacts.clear();
}
public void destroy() { public void destroy() {
Core lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); Core lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc != null) { if (lc != null) {
for (FriendList list : lc.getFriendsLists()) { for (FriendList list : lc.getFriendsLists()) {
list.setListener(null); list.removeListener(this);
} }
} }
mDefaultAvatar.recycle(); mDefaultAvatar.recycle();
@ -440,19 +417,6 @@ public class ContactsManager extends ContentObserver implements FriendListListen
private synchronized boolean refreshSipContact(Friend lf) { private synchronized boolean refreshSipContact(Friend lf) {
LinphoneContact contact = (LinphoneContact) lf.getUserData(); LinphoneContact contact = (LinphoneContact) lf.getUserData();
if (contact != null) { if (contact != null) {
if (lf.hasCapability(FriendCapability.GroupChat)
&& !mGroupChatContacts.contains(contact)) {
mGroupChatContacts.add(contact);
Log.i("[Contacts Manager] Contact " + contact + " has group chat capability");
// Contact may only have LimeX3DH capability if it already has GroupChat capability
if (lf.hasCapability(FriendCapability.LimeX3Dh)
&& !mLimeX3dhContacts.contains(contact)) {
mLimeX3dhContacts.add(contact);
Log.i("[Contacts Manager] Contact " + contact + " has lime x3dh capability");
}
}
if (!mSipContacts.contains(contact)) { if (!mSipContacts.contains(contact)) {
mSipContacts.add(contact); mSipContacts.add(contact);
return true; return true;
@ -518,8 +482,6 @@ public class ContactsManager extends ContentObserver implements FriendListListen
if (updated) { if (updated) {
Collections.sort(mSipContacts); Collections.sort(mSipContacts);
Collections.sort(mGroupChatContacts);
Collections.sort(mLimeX3dhContacts);
} }
for (ContactsUpdatedListener listener : mContactsUpdatedListeners) { for (ContactsUpdatedListener listener : mContactsUpdatedListeners) {

View file

@ -41,7 +41,7 @@ public class DozeReceiver extends android.content.BroadcastReceiver {
if (!LinphoneService.isReady()) return; if (!LinphoneService.isReady()) return;
boolean isDebugEnabled = LinphonePreferences.instance().isDebugEnabled(); boolean isDebugEnabled = LinphonePreferences.instance().isDebugEnabled();
LinphoneUtils.initLoggingService(isDebugEnabled, context.getString(R.string.app_name)); LinphoneUtils.configureLoggingService(isDebugEnabled, context.getString(R.string.app_name));
Core lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); Core lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc == null) return; if (lc == null) return;

View file

@ -41,7 +41,8 @@ public class KeepAliveReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) { public void onReceive(Context context, Intent intent) {
if (LinphoneService.isReady()) { if (LinphoneService.isReady()) {
boolean isDebugEnabled = LinphonePreferences.instance().isDebugEnabled(); boolean isDebugEnabled = LinphonePreferences.instance().isDebugEnabled();
LinphoneUtils.initLoggingService(isDebugEnabled, context.getString(R.string.app_name)); LinphoneUtils.configureLoggingService(
isDebugEnabled, context.getString(R.string.app_name));
Core lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull(); Core lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc == null) return; if (lc == null) return;

View file

@ -72,7 +72,7 @@ class Recording implements PlayerListener, Comparable<Recording> {
}; };
mPlayer = LinphoneManager.getLc().createLocalPlayer(null, null, null); mPlayer = LinphoneManager.getLc().createLocalPlayer(null, null, null);
mPlayer.setListener(this); mPlayer.addListener(this);
} }
public String getRecordPath() { public String getRecordPath() {
@ -135,6 +135,7 @@ class Recording implements PlayerListener, Comparable<Recording> {
} }
public void close() { public void close() {
mPlayer.removeListener(this);
mPlayer.close(); mPlayer.close();
} }

View file

@ -1034,12 +1034,13 @@ public class LinphonePreferences {
// Advanced settings // Advanced settings
public void setDebugEnabled(boolean enabled) { public void setDebugEnabled(boolean enabled) {
getConfig().setBool("app", "debug", enabled); getConfig().setBool("app", "debug", enabled);
LinphoneUtils.initLoggingService(enabled, mContext.getString(R.string.app_name)); LinphoneUtils.configureLoggingService(enabled, mContext.getString(R.string.app_name));
} }
public void setJavaLogger(boolean enabled) { public void setJavaLogger(boolean enabled) {
getConfig().setBool("app", "java_logger", enabled); getConfig().setBool("app", "java_logger", enabled);
LinphoneUtils.initLoggingService(isDebugEnabled(), mContext.getString(R.string.app_name)); LinphoneUtils.configureLoggingService(
isDebugEnabled(), mContext.getString(R.string.app_name));
} }
public boolean useJavaLogger() { public boolean useJavaLogger() {

View file

@ -52,9 +52,6 @@ import org.linphone.core.Call.State;
import org.linphone.core.Core; import org.linphone.core.Core;
import org.linphone.core.Factory; import org.linphone.core.Factory;
import org.linphone.core.LogCollectionState; import org.linphone.core.LogCollectionState;
import org.linphone.core.LogLevel;
import org.linphone.core.LoggingService;
import org.linphone.core.LoggingServiceListener;
import org.linphone.core.ProxyConfig; import org.linphone.core.ProxyConfig;
import org.linphone.mediastream.Log; import org.linphone.mediastream.Log;
import org.linphone.settings.LinphonePreferences; import org.linphone.settings.LinphonePreferences;
@ -66,7 +63,7 @@ public final class LinphoneUtils {
private LinphoneUtils() {} private LinphoneUtils() {}
public static void initLoggingService(boolean isDebugEnabled, String appName) { public static void configureLoggingService(boolean isDebugEnabled, String appName) {
if (!LinphonePreferences.instance().useJavaLogger()) { if (!LinphonePreferences.instance().useJavaLogger()) {
Factory.instance().enableLogCollection(LogCollectionState.Enabled); Factory.instance().enableLogCollection(LogCollectionState.Enabled);
Factory.instance().setDebugMode(isDebugEnabled, appName); Factory.instance().setDebugMode(isDebugEnabled, appName);
@ -74,36 +71,19 @@ public final class LinphoneUtils {
Factory.instance().setDebugMode(isDebugEnabled, appName); Factory.instance().setDebugMode(isDebugEnabled, appName);
Factory.instance() Factory.instance()
.enableLogCollection(LogCollectionState.EnabledWithoutPreviousLogHandler); .enableLogCollection(LogCollectionState.EnabledWithoutPreviousLogHandler);
Factory.instance() if (isDebugEnabled) {
.getLoggingService() if (LinphoneService.isReady()) {
.setListener( Factory.instance()
new LoggingServiceListener() { .getLoggingService()
@Override .addListener(LinphoneService.instance().getJavaLoggingService());
public void onLogMessageWritten( }
LoggingService logService, } else {
String domain, if (LinphoneService.isReady()) {
LogLevel lev, Factory.instance()
String message) { .getLoggingService()
switch (lev) { .removeListener(LinphoneService.instance().getJavaLoggingService());
case Debug: }
android.util.Log.d(domain, message); }
break;
case Message:
android.util.Log.i(domain, message);
break;
case Warning:
android.util.Log.w(domain, message);
break;
case Error:
android.util.Log.e(domain, message);
break;
case Fatal:
default:
android.util.Log.wtf(domain, message);
break;
}
}
});
} }
} }