package org.linphone; /* LinphonePreferences.java Copyright (C) 2013 Belledonne Communications, Grenoble, France This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import org.linphone.core.LinphoneAddress; import org.linphone.core.LinphoneAddress.TransportType; import org.linphone.core.LinphoneAuthInfo; import org.linphone.core.LinphoneCore; import org.linphone.core.LinphoneCore.AdaptiveRateAlgorithm; import org.linphone.core.LinphoneCore.FirewallPolicy; import org.linphone.core.LinphoneCore.MediaEncryption; import org.linphone.core.LinphoneCore.Transports; import org.linphone.core.LinphoneCoreException; import org.linphone.core.LinphoneCoreFactory; import org.linphone.core.LinphoneProxyConfig; import org.linphone.core.LpConfig; import org.linphone.core.TunnelConfig; import org.linphone.mediastream.Log; import android.content.Context; /** * @author Sylvain Berfini */ public class LinphonePreferences { private static final int LINPHONE_CORE_RANDOM_PORT = -1; private static LinphonePreferences instance; private Context mContext; public static final synchronized LinphonePreferences instance() { if (instance == null) { instance = new LinphonePreferences(); } return instance; } private LinphonePreferences() { } public void setContext(Context c) { mContext = c; } private String getString(int key) { if (mContext == null && LinphoneManager.isInstanciated()) { mContext = LinphoneManager.getInstance().getContext(); } return mContext.getString(key); } private LinphoneCore getLc() { if (!LinphoneManager.isInstanciated()) return null; return LinphoneManager.getLcIfManagerNotDestroyedOrNull(); } public LpConfig getConfig() { LinphoneCore lc = getLc(); if (lc != null) { return lc.getConfig(); } if (!LinphoneManager.isInstanciated()) { File linphonerc = new File(mContext.getFilesDir().getAbsolutePath() + "/.linphonerc"); if (linphonerc.exists()) { return LinphoneCoreFactory.instance().createLpConfig(linphonerc.getAbsolutePath()); } else { InputStream inputStream = mContext.getResources().openRawResource(R.raw.linphonerc_default); InputStreamReader inputreader = new InputStreamReader(inputStream); BufferedReader buffreader = new BufferedReader(inputreader); StringBuilder text = new StringBuilder(); String line; try { while ((line = buffreader.readLine()) != null) { text.append(line); text.append('\n'); } } catch (IOException ioe) { } return LinphoneCoreFactory.instance().createLpConfigFromString(text.toString()); } } return LinphoneCoreFactory.instance().createLpConfig(LinphoneManager.getInstance().mLinphoneConfigFile); } public void removePreviousVersionAuthInfoRemoval() { getConfig().setBool("sip", "store_auth_info", true); } // App settings public boolean isFirstLaunch() { return getConfig().getBool("app", "first_launch", true); } public void firstLaunchSuccessful() { getConfig().setBool("app", "first_launch", false); } public String getRingtone(String defaultRingtone) { String ringtone = getConfig().getString("app", "ringtone", defaultRingtone); if (ringtone == null || ringtone.length() == 0) ringtone = defaultRingtone; return ringtone; } public void setRingtone(String ringtonePath) { getConfig().setString("app", "ringtone", ringtonePath); } public boolean shouldAutomaticallyAcceptFriendsRequests() { return false; //TODO } // End of app settings // Accounts settings private LinphoneProxyConfig getProxyConfig(int n) { LinphoneProxyConfig[] prxCfgs = getLc().getProxyConfigList(); if (n < 0 || n >= prxCfgs.length) return null; return prxCfgs[n]; } private LinphoneAuthInfo getAuthInfo(int n) { LinphoneProxyConfig prxCfg = getProxyConfig(n); try { LinphoneAddress addr = LinphoneCoreFactory.instance().createLinphoneAddress(prxCfg.getIdentity()); LinphoneAuthInfo authInfo = getLc().findAuthInfo(addr.getUserName(), null, addr.getDomain()); return authInfo; } catch (LinphoneCoreException e) { Log.e(e); } return null; } /** * Removes a authInfo from the core and returns a copy of it. * Useful to edit a authInfo (you should call saveAuthInfo after the modifications to save them). */ private LinphoneAuthInfo getClonedAuthInfo(int n) { LinphoneAuthInfo authInfo = getAuthInfo(n); if (authInfo == null) return null; LinphoneAuthInfo cloneAuthInfo = authInfo.clone(); getLc().removeAuthInfo(authInfo); return cloneAuthInfo; } /** * Saves a authInfo into the core. * Useful to save the changes made to a cloned authInfo. */ private void saveAuthInfo(LinphoneAuthInfo authInfo) { getLc().addAuthInfo(authInfo); } public static class AccountBuilder { private LinphoneCore lc; private String tempUsername; private String tempDisplayName; private String tempUserId; private String tempPassword; private String tempDomain; private String tempProxy; private String tempRealm; private boolean tempOutboundProxy; private String tempContactsParams; private String tempExpire; private TransportType tempTransport; private boolean tempAvpfEnabled = false; private int tempAvpfRRInterval = 0; private String tempQualityReportingCollector; private boolean tempQualityReportingEnabled = false; private int tempQualityReportingInterval = 0; private boolean tempEnabled = true; private boolean tempNoDefault = false; public AccountBuilder(LinphoneCore lc) { this.lc = lc; } public AccountBuilder setTransport(TransportType transport) { tempTransport = transport; return this; } public AccountBuilder setUsername(String username) { tempUsername = username; return this; } public AccountBuilder setDisplayName(String displayName) { tempDisplayName = displayName; return this; } public AccountBuilder setPassword(String password) { tempPassword = password; return this; } public AccountBuilder setDomain(String domain) { tempDomain = domain; return this; } public AccountBuilder setProxy(String proxy) { tempProxy = proxy; return this; } public AccountBuilder setOutboundProxyEnabled(boolean enabled) { tempOutboundProxy = enabled; return this; } public AccountBuilder setContactParameters(String contactParams) { tempContactsParams = contactParams; return this; } public AccountBuilder setExpires(String expire) { tempExpire = expire; return this; } public AccountBuilder setUserId(String userId) { tempUserId = userId; return this; } public AccountBuilder setAvpfEnabled(boolean enable) { tempAvpfEnabled = enable; return this; } public AccountBuilder setAvpfRRInterval(int interval) { tempAvpfRRInterval = interval; return this; } public AccountBuilder setRealm(String realm) { tempRealm = realm; return this; } public AccountBuilder setQualityReportingCollector(String collector) { tempQualityReportingCollector = collector; return this; } public AccountBuilder setQualityReportingEnabled(boolean enable) { tempQualityReportingEnabled = enable; return this; } public AccountBuilder setQualityReportingInterval(int interval) { tempQualityReportingInterval = interval; return this; } public AccountBuilder setEnabled(boolean enable) { tempEnabled = enable; return this; } public AccountBuilder setNoDefault(boolean yesno) { tempNoDefault = yesno; return this; } /** * Creates a new account * @throws LinphoneCoreException */ public void saveNewAccount() throws LinphoneCoreException { if (tempUsername == null || tempUsername.length() < 1 || tempDomain == null || tempDomain.length() < 1) { Log.w("Skipping account save: username or domain not provided"); return; } String identity = "sip:" + tempUsername + "@" + tempDomain; String proxy = "sip:"; if (tempProxy == null) { proxy += tempDomain; } else { if (!tempProxy.startsWith("sip:") && !tempProxy.startsWith(" 0) { getLc().setFirewallPolicy(FirewallPolicy.UseStun); } else { getLc().setFirewallPolicy(FirewallPolicy.NoFirewall); } } } public void setUpnpEnabled(boolean enabled) { if (enabled) { if (isIceEnabled()) { Log.e("Cannot have both ice and upnp enabled, disabling upnp"); } else { getLc().setFirewallPolicy(FirewallPolicy.UseUpnp); } } else { String stun = getStunServer(); if (stun != null && stun.length() > 0) { getLc().setFirewallPolicy(FirewallPolicy.UseStun); } else { getLc().setFirewallPolicy(FirewallPolicy.NoFirewall); } } } public void useRandomPort(boolean enabled) { useRandomPort(enabled, true); } public void useRandomPort(boolean enabled, boolean apply) { getConfig().setBool("app", "random_port", enabled); if (apply) { if (enabled) { setSipPort(LINPHONE_CORE_RANDOM_PORT); } else { setSipPort(5060); } } } public boolean isUsingRandomPort() { return getConfig().getBool("app", "random_port", true); } public String getSipPort() { Transports transports = getLc().getSignalingTransportPorts(); int port; if (transports.udp > 0) port = transports.udp; else port = transports.tcp; return String.valueOf(port); } public void setSipPort(int port) { Transports transports = getLc().getSignalingTransportPorts(); transports.udp = port; transports.tcp = port; transports.tls = LINPHONE_CORE_RANDOM_PORT; getLc().setSignalingTransportPorts(transports); } public boolean isUpnpEnabled() { return getLc().upnpAvailable() && getLc().getFirewallPolicy() == FirewallPolicy.UseUpnp; } public boolean isIceEnabled() { return getLc().getFirewallPolicy() == FirewallPolicy.UseIce; } public MediaEncryption getMediaEncryption() { return getLc().getMediaEncryption(); } public void setMediaEncryption(MediaEncryption menc) { if (menc == null) return; getLc().setMediaEncryption(menc); } public void setPushNotificationEnabled(boolean enable) { getConfig().setBool("app", "push_notification", enable); if (enable) { // Add push infos to exisiting proxy configs String regId = getPushNotificationRegistrationID(); String appId = getString(R.string.push_sender_id); if (regId != null && getLc().getProxyConfigList().length > 0) { for (LinphoneProxyConfig lpc : getLc().getProxyConfigList()) { String contactInfos = "app-id=" + appId + ";pn-type=google;pn-tok=" + regId; lpc.edit(); lpc.setContactUriParameters(contactInfos); lpc.done(); Log.d("Push notif infos added to proxy config"); } getLc().refreshRegisters(); } } else { if (getLc().getProxyConfigList().length > 0) { for (LinphoneProxyConfig lpc : getLc().getProxyConfigList()) { lpc.edit(); lpc.setContactUriParameters(null); lpc.done(); Log.d("Push notif infos removed from proxy config"); } getLc().refreshRegisters(); } } } public boolean isPushNotificationEnabled() { return getConfig().getBool("app", "push_notification", false); } public void setPushNotificationRegistrationID(String regId) { getConfig().setString("app", "push_notification_regid", regId); setPushNotificationEnabled(isPushNotificationEnabled()); } public String getPushNotificationRegistrationID() { return getConfig().getString("app", "push_notification_regid", null); } public void useIpv6(Boolean enable) { getLc().enableIpv6(enable); } public boolean isUsingIpv6() { return getLc().isIpv6Enabled(); } // End of network settings // Advanced settings public void setDebugEnabled(boolean enabled) { getConfig().setBool("app", "debug", enabled); LinphoneCoreFactory.instance().enableLogCollection(enabled); LinphoneCoreFactory.instance().setDebugMode(enabled, getString(R.string.app_name)); } public boolean isDebugEnabled() { return getConfig().getBool("app", "debug", false); } public void setBackgroundModeEnabled(boolean enabled) { getConfig().setBool("app", "background_mode", enabled); } public boolean isBackgroundModeEnabled() { return getConfig().getBool("app", "background_mode", true); } public void setAnimationsEnabled(boolean enabled) { getConfig().setBool("app", "animations", enabled); } public boolean areAnimationsEnabled() { return getConfig().getBool("app", "animations", false); } public boolean isAutoStartEnabled() { return getConfig().getBool("app", "auto_start", false); } public void setAutoStart(boolean autoStartEnabled) { getConfig().setBool("app", "auto_start", autoStartEnabled); } public String getSharingPictureServerUrl() { return getLc().getFileTransferServer(); } public void setSharingPictureServerUrl(String url) { getLc().setFileTransferServer(url); } public void setRemoteProvisioningUrl(String url) { if (url != null && url.length() == 0) { url = null; } getLc().setProvisioningUri(url); } public String getRemoteProvisioningUrl() { return getLc().getProvisioningUri(); } public void setDefaultDisplayName(String displayName) { getLc().setPrimaryContact(displayName, getDefaultUsername()); } public String getDefaultDisplayName() { return getLc().getPrimaryContactDisplayName(); } public void setDefaultUsername(String username) { getLc().setPrimaryContact(getDefaultDisplayName(), username); } public String getDefaultUsername() { return getLc().getPrimaryContactUsername(); } // End of advanced settings // Tunnel settings private TunnelConfig tunnelConfig = null; public TunnelConfig getTunnelConfig() { if(getLc().isTunnelAvailable()) { if(tunnelConfig == null) { TunnelConfig servers[] = getLc().tunnelGetServers(); if(servers.length > 0) { tunnelConfig = servers[0]; } else { tunnelConfig = LinphoneCoreFactory.instance().createTunnelConfig(); } } return tunnelConfig; } else { return null; } } public String getTunnelHost() { TunnelConfig config = getTunnelConfig(); if(config != null) { return config.getHost(); } else { return null; } } public void setTunnelHost(String host) { TunnelConfig config = getTunnelConfig(); if(config != null) { config.setHost(host); LinphoneManager.getInstance().initTunnelFromConf(); } } public int getTunnelPort() { TunnelConfig config = getTunnelConfig(); if(config != null) { return config.getPort(); } else { return -1; } } public void setTunnelPort(int port) { TunnelConfig config = getTunnelConfig(); if(config != null) { config.setPort(port); LinphoneManager.getInstance().initTunnelFromConf(); } } public String getTunnelMode() { return getConfig().getString("app", "tunnel", null); } public void setTunnelMode(String mode) { getConfig().setString("app", "tunnel", mode); LinphoneManager.getInstance().initTunnelFromConf(); } // End of tunnel settings public boolean isProvisioningLoginViewEnabled() { return getConfig().getBool("app", "show_login_view", false); } public void disableProvisioningLoginView() { if (isProvisioningLoginViewEnabled()) { // Only do it if it was previously enabled getConfig().setBool("app", "show_login_view", false); } else { Log.w("Remote provisioning login view wasn't enabled, ignoring"); } } public void firstRemoteProvisioningSuccessful() { getConfig().setBool("app", "first_remote_provisioning", false); } public boolean isFirstRemoteProvisioning() { return getConfig().getBool("app", "first_remote_provisioning", true); } public boolean isAdaptiveRateControlEnabled() { return getLc().isAdaptiveRateControlEnabled(); } public void enableAdaptiveRateControl(boolean enabled) { getLc().enableAdaptiveRateControl(enabled); } public AdaptiveRateAlgorithm getAdaptiveRateAlgorithm() { return getLc().getAdaptiveRateAlgorithm(); } public void setAdaptiveRateAlgorithm(AdaptiveRateAlgorithm alg) { getLc().setAdaptiveRateAlgorithm(alg); } public int getCodecBitrateLimit() { return getConfig().getInt("audio", "codec_bitrate_limit", 36); } public void setCodecBitrateLimit(int bitrate) { getConfig().setInt("audio", "codec_bitrate_limit", bitrate); } public void contactsMigrationDone(){ getConfig().setBool("app", "contacts_migration_done", true); } public boolean isContactsMigrationDone(){ return getConfig().getBool("app", "contacts_migration_done",false); } public String getXmlRpcServerUrl() { return getConfig().getString("app", "server_url", null); } public String getDebugPopupAddress(){ return getConfig().getString("app", "debug_popup_magic", null); } public Boolean audioPermAsked(){ return getConfig().getBool("app", "audio_perm", false); } public void neverAskAudioPerm(){ getConfig().setBool("app", "audio_perm", true); } public Boolean cameraPermAsked(){ return getConfig().getBool("app", "camera_perm", false); } public void neverAskCameraPerm(){ getConfig().setBool("app", "camera_perm", true); } public String getActivityToLaunchOnIncomingReceived() { return getConfig().getString("app", "incoming_call_activity", "org.linphone.LinphoneActivity"); } public void setActivityToLaunchOnIncomingReceived(String name) { getConfig().setString("app", "incoming_call_activity", name); } }