forked from upstream-mirrors/quik
Merge pull request #205 from Allsimon/number2contact
Fix for qklabs/qksms#99
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
package com.moez.QKSMS.common.formatter;
|
||||
|
||||
public interface Formatter {
|
||||
String format(String text);
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.moez.QKSMS.common.formatter;
|
||||
|
||||
public class FormatterFactory {
|
||||
private static final Formatter[] FORMATTERS = {new NumberToContactFormatter()};
|
||||
|
||||
public static String format(String text) {
|
||||
for (Formatter formatter : FORMATTERS) {
|
||||
text = formatter.format(text);
|
||||
}
|
||||
return text;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.moez.QKSMS.common.formatter;
|
||||
|
||||
import android.content.Context;
|
||||
import android.telephony.TelephonyManager;
|
||||
|
||||
import com.google.i18n.phonenumbers.PhoneNumberMatch;
|
||||
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
||||
import com.google.i18n.phonenumbers.PhoneNumberUtil.PhoneNumberFormat;
|
||||
import com.moez.QKSMS.QKSMSAppBase;
|
||||
import com.moez.QKSMS.data.Contact;
|
||||
|
||||
public class NumberToContactFormatter implements Formatter {
|
||||
String mCountryIso;
|
||||
|
||||
@Override
|
||||
public String format(String text) {
|
||||
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
|
||||
Iterable<PhoneNumberMatch> matches = phoneNumberUtil.findNumbers(text, getCurrentCountryIso());
|
||||
for (PhoneNumberMatch match : matches) {
|
||||
Contact contact = Contact.get(match.rawString(), true);
|
||||
if (contact.isNamed()) {
|
||||
String nameAndNumber = phoneNumberUtil.format(match.number(), PhoneNumberFormat.NATIONAL)
|
||||
+ " (" + contact.getName() + ")";
|
||||
text = text.replace(match.rawString(), nameAndNumber);
|
||||
} // If the contact doesn't exist yet, leave the number as-is
|
||||
}
|
||||
return text;
|
||||
}
|
||||
|
||||
public String getCurrentCountryIso() {
|
||||
if (mCountryIso == null) {
|
||||
TelephonyManager tm = (TelephonyManager) QKSMSAppBase.getApplication().getSystemService(Context.TELEPHONY_SERVICE);
|
||||
mCountryIso = tm.getNetworkCountryIso();
|
||||
// Just in case the TelephonyManager method failed, fallback to US
|
||||
if (mCountryIso == null) {
|
||||
mCountryIso = "US";
|
||||
}
|
||||
mCountryIso = mCountryIso.toUpperCase();
|
||||
}
|
||||
return mCountryIso;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -261,6 +261,10 @@ public class Contact {
|
||||
}
|
||||
}
|
||||
|
||||
public synchronized boolean isNamed() {
|
||||
return !TextUtils.isEmpty(mName);
|
||||
}
|
||||
|
||||
public synchronized String getNameAndNumber() {
|
||||
return mNameAndNumber;
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ import com.google.android.mms.pdu_alt.PduPersister;
|
||||
import com.google.android.mms.pdu_alt.RetrieveConf;
|
||||
import com.moez.QKSMS.QKSMSApp;
|
||||
import com.moez.QKSMS.R;
|
||||
import com.moez.QKSMS.common.formatter.FormatterFactory;
|
||||
import com.moez.QKSMS.data.Contact;
|
||||
import com.moez.QKSMS.model.SlideModel;
|
||||
import com.moez.QKSMS.model.SlideshowModel;
|
||||
@@ -151,6 +152,7 @@ public class MessageItem {
|
||||
mContact = Contact.get(mAddress, canBlock).getName();
|
||||
}
|
||||
mBody = cursor.getString(columnsMap.mColumnSmsBody);
|
||||
mBody = FormatterFactory.format(mBody);
|
||||
|
||||
// Unless the message is currently in the progress of being sent, it gets a time stamp.
|
||||
if (!isOutgoingMessage()) {
|
||||
|
||||
Reference in New Issue
Block a user