Added method into LinphoneUtils to get displayable username from address and vice versa

This commit is contained in:
Sylvain Berfini 2016-07-21 11:56:46 +02:00
parent f049bff00c
commit b7c21b57f1
4 changed files with 54 additions and 59 deletions

View file

@ -17,7 +17,6 @@ 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 org.linphone.core.LinphoneCore;
import org.linphone.core.LinphoneProxyConfig;
import android.annotation.SuppressLint;
@ -47,22 +46,7 @@ public class ContactDetailsFragment extends Fragment implements OnClickListener
@Override
public void onClick(View v) {
if (LinphoneActivity.isInstanciated()) {
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc != null) {
LinphoneProxyConfig lpc = lc.getDefaultProxyConfig();
String to;
if (lpc != null) {
String address = v.getTag().toString();
if (!address.contains("@")) {
to = lpc.normalizePhoneNumber(address);
} else {
to = v.getTag().toString();
}
} else {
to = v.getTag().toString();
}
LinphoneActivity.instance().setAddresGoToDialerAndCall(to, contact.getFullName(), contact.getPhotoUri());
}
LinphoneActivity.instance().setAddresGoToDialerAndCall(v.getTag().toString(), contact.getFullName(), contact.getPhotoUri());
}
}
};
@ -125,13 +109,7 @@ public class ContactDetailsFragment extends Fragment implements OnClickListener
boolean skip = false;
View v = inflater.inflate(R.layout.contact_control_row, null);
String displayednumberOrAddress = noa.getValue();
if (displayednumberOrAddress.startsWith("sip:")) {
displayednumberOrAddress = displayednumberOrAddress.replace("sip:", "");
}
if (displayednumberOrAddress.contains("@")) {
displayednumberOrAddress = displayednumberOrAddress.split("@")[0];
}
String displayednumberOrAddress = LinphoneUtils.getDisplayableUsernameFromAddress(noa.getValue());
TextView label = (TextView) v.findViewById(R.id.address_label);
if (noa.isSIPAddress()) {
@ -156,15 +134,8 @@ public class ContactDetailsFragment extends Fragment implements OnClickListener
v.findViewById(R.id.contact_chat).setOnClickListener(chatListener);
LinphoneProxyConfig lpc = LinphoneManager.getLc().getDefaultProxyConfig();
if (lpc != null) {
displayednumberOrAddress = lpc.normalizePhoneNumber(displayednumberOrAddress);
String tag = noa.getValue();
if (!tag.startsWith("sip:")) {
tag = "sip:" + tag;
}
if (!tag.contains("@")) {
tag = tag + "@" + lpc.getDomain();
}
String username = lpc.normalizePhoneNumber(LinphoneUtils.getUsernameFromAddress(noa.getValue()));
String tag = LinphoneUtils.getFullAddressFromUsername(username);
v.findViewById(R.id.contact_chat).setTag(tag);
} else {
v.findViewById(R.id.contact_chat).setTag(noa.getValue());

View file

@ -25,8 +25,6 @@ import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.linphone.core.LinphoneCore;
import org.linphone.core.LinphoneProxyConfig;
import org.linphone.mediastream.Log;
import org.linphone.mediastream.Version;
@ -126,10 +124,6 @@ public class ContactEditorFragment extends Fragment {
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
LinphoneProxyConfig lpc = lc != null ? lc.getDefaultProxyConfig() : null;
String defaultDomain = lpc != null ? lpc.getDomain() : null;
if (isNewContact) {
boolean areAllFielsEmpty = true;
for (LinphoneNumberOrAddress nounoa : numbersAndAddresses) {
@ -150,12 +144,7 @@ public class ContactEditorFragment extends Fragment {
}
for (LinphoneNumberOrAddress noa : numbersAndAddresses) {
if (noa.isSIPAddress() && noa.getValue() != null) {
if (!noa.getValue().contains("@") && defaultDomain != null) {
noa.setValue(noa.getValue() + "@" + defaultDomain);
}
if (!noa.getValue().startsWith("sip:")) {
noa.setValue("sip:" + noa.getValue());
}
noa.setValue(LinphoneUtils.getFullAddressFromUsername(noa.getValue()));
}
contact.addOrUpdateNumberOrAddress(noa);
}
@ -484,10 +473,7 @@ public class ContactEditorFragment extends Fragment {
if (firstSipAddressIndex == -1) {
firstSipAddressIndex = controls.getChildCount();
}
numberOrAddress = numberOrAddress.replace("sip:", "");
if (numberOrAddress.contains("@")) {
numberOrAddress = numberOrAddress.split("@")[0];
}
numberOrAddress = LinphoneUtils.getDisplayableUsernameFromAddress(numberOrAddress);
}
if ((getResources().getBoolean(R.bool.hide_phone_numbers_in_editor) && !isSIP) || (getResources().getBoolean(R.bool.hide_sip_addresses_in_editor) && isSIP)) {
if (forceAddNumber)

View file

@ -478,5 +478,50 @@ public final class LinphoneUtils {
}
}
}
public static String getDisplayableUsernameFromAddress(String sipAddress) {
String username = sipAddress;
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc == null) return username;
if (username.startsWith("sip:")) {
username = username.substring(4);
}
if (username.contains("@")) {
String domain = username.split("@")[1];
LinphoneProxyConfig lpc = lc.getDefaultProxyConfig();
if (lpc != null) {
if (domain.equals(lpc.getDomain())) {
return username.split("@")[0];
}
} else {
if (domain.equals(LinphoneManager.getInstance().getContext().getString(R.string.default_domain))) {
return username.split("@")[0];
}
}
}
return username;
}
public static String getFullAddressFromUsername(String username) {
String sipAddress = username;
LinphoneCore lc = LinphoneManager.getLcIfManagerNotDestroyedOrNull();
if (lc == null) return sipAddress;
if (!sipAddress.startsWith("sip:")) {
sipAddress = "sip:" + sipAddress;
}
if (!sipAddress.contains("@")) {
LinphoneProxyConfig lpc = lc.getDefaultProxyConfig();
if (lpc != null) {
sipAddress = sipAddress + "@" + lpc.getDomain();
} else {
sipAddress = sipAddress + "@" + LinphoneManager.getInstance().getContext().getString(R.string.default_domain);
}
}
return sipAddress;
}
}

View file

@ -20,6 +20,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
import org.linphone.LinphoneActivity;
import org.linphone.LinphoneManager;
import org.linphone.LinphonePreferences;
import org.linphone.LinphoneUtils;
import org.linphone.LinphonePreferences.AccountBuilder;
import org.linphone.R;
import org.linphone.StatusFragment;
@ -366,16 +367,8 @@ private static AssistantActivity instance;
if (accountCreated)
return;
if(username.startsWith("sip:")) {
username = username.substring(4);
}
if (username.contains("@"))
username = username.split("@")[0];
if(domain.startsWith("sip:")) {
domain = domain.substring(4);
}
username = LinphoneUtils.getDisplayableUsernameFromAddress(username);
domain = LinphoneUtils.getDisplayableUsernameFromAddress(domain);
String identity = "sip:" + username + "@" + domain;
try {