1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-19 23:27:48 +01:00

Merge pull request #527 from ivanovlev/master

Fix issue #522 : Transliterate Caller Name
This commit is contained in:
Carsten Pfeiffer 2017-01-28 21:50:08 +01:00 committed by GitHub
commit 42031cb50f
5 changed files with 71 additions and 42 deletions

View File

@ -3,7 +3,9 @@ package nodomain.freeyourgadget.gadgetbridge.impl;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import java.util.ArrayList;
@ -21,6 +23,8 @@ import nodomain.freeyourgadget.gadgetbridge.model.WeatherSpec;
import nodomain.freeyourgadget.gadgetbridge.service.DeviceCommunicationService;
import nodomain.freeyourgadget.gadgetbridge.util.LanguageUtils;
import static nodomain.freeyourgadget.gadgetbridge.util.JavaExtensions.coalesce;
public class GBDeviceService implements DeviceService {
protected final Context mContext;
private final Class<? extends Service> mServiceClass;
@ -32,6 +36,7 @@ public class GBDeviceService implements DeviceService {
EXTRA_NOTIFICATION_BODY,
EXTRA_NOTIFICATION_SOURCENAME,
EXTRA_CALL_PHONENUMBER,
EXTRA_CALL_DISPLAYNAME,
EXTRA_MUSIC_ARTIST,
EXTRA_MUSIC_ALBUM,
EXTRA_MUSIC_TRACK,
@ -111,7 +116,7 @@ public class GBDeviceService implements DeviceService {
Intent intent = createIntent().setAction(ACTION_NOTIFICATION)
.putExtra(EXTRA_NOTIFICATION_FLAGS, notificationSpec.flags)
.putExtra(EXTRA_NOTIFICATION_PHONENUMBER, notificationSpec.phoneNumber)
.putExtra(EXTRA_NOTIFICATION_SENDER, notificationSpec.sender)
.putExtra(EXTRA_NOTIFICATION_SENDER, coalesce(notificationSpec.sender, getContactDisplayNameByNumber(notificationSpec.phoneNumber)))
.putExtra(EXTRA_NOTIFICATION_SUBJECT, notificationSpec.subject)
.putExtra(EXTRA_NOTIFICATION_TITLE, notificationSpec.title)
.putExtra(EXTRA_NOTIFICATION_BODY, notificationSpec.body)
@ -144,9 +149,9 @@ public class GBDeviceService implements DeviceService {
@Override
public void onSetCallState(CallSpec callSpec) {
// name is actually ignored and provided by the service itself...
Intent intent = createIntent().setAction(ACTION_CALLSTATE)
.putExtra(EXTRA_CALL_PHONENUMBER, callSpec.number)
.putExtra(EXTRA_CALL_DISPLAYNAME, coalesce(callSpec.name, getContactDisplayNameByNumber(callSpec.number)))
.putExtra(EXTRA_CALL_COMMAND, callSpec.command);
invokeService(intent);
}
@ -332,4 +337,29 @@ public class GBDeviceService implements DeviceService {
.putExtra(EXTRA_WEATHER_TOMORROWCONDITIONCODE, weatherSpec.tomorrowConditionCode);
invokeService(intent);
}
/**
* Returns contact DisplayName by call number
* @param number contact number
* @return contact DisplayName, if found it
*/
private String getContactDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = number;
if (number == null || number.equals("")) {
return name;
}
try (Cursor contactLookup = mContext.getContentResolver().query(uri, null, null, null, null)) {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
}
} catch (SecurityException e) {
// ignore, just return name below
}
return name;
}
}

View File

@ -60,6 +60,7 @@ public interface DeviceService extends EventHandler {
String EXTRA_VIBRATION_INTENSITY = "vibration_intensity";
String EXTRA_CALL_COMMAND = "call_command";
String EXTRA_CALL_PHONENUMBER = "call_phonenumber";
String EXTRA_CALL_DISPLAYNAME = "call_displayname";
String EXTRA_CANNEDMESSAGES = "cannedmessages";
String EXTRA_CANNEDMESSAGES_TYPE = "cannedmessages_type";
String EXTRA_MUSIC_ARTIST = "music_artist";

View File

@ -8,10 +8,8 @@ import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Toast;
@ -97,6 +95,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CAL
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALENDAREVENT_TITLE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALENDAREVENT_TYPE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_COMMAND;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_DISPLAYNAME;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CALL_PHONENUMBER;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_CANNEDMESSAGES_TYPE;
@ -332,8 +331,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
notificationSpec.flags = intent.getIntExtra(EXTRA_NOTIFICATION_FLAGS, 0);
if (notificationSpec.type == NotificationType.GENERIC_SMS && notificationSpec.phoneNumber != null) {
notificationSpec.sender = getContactDisplayNameByNumber(notificationSpec.phoneNumber);
notificationSpec.id = mRandom.nextInt(); // FIXME: add this in external SMS Receiver?
GBApplication.getIDSenderLookup().add(notificationSpec.id, notificationSpec.phoneNumber);
}
@ -412,18 +409,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
break;
}
case ACTION_CALLSTATE:
int command = intent.getIntExtra(EXTRA_CALL_COMMAND, CallSpec.CALL_UNDEFINED);
String phoneNumber = intent.getStringExtra(EXTRA_CALL_PHONENUMBER);
String callerName = null;
if (phoneNumber != null) {
callerName = getContactDisplayNameByNumber(phoneNumber);
}
CallSpec callSpec = new CallSpec();
callSpec.command = command;
callSpec.number = phoneNumber;
callSpec.name = callerName;
callSpec.command = intent.getIntExtra(EXTRA_CALL_COMMAND, CallSpec.CALL_UNDEFINED);
callSpec.number = intent.getStringExtra(EXTRA_CALL_PHONENUMBER);
callSpec.name = intent.getStringExtra(EXTRA_CALL_DISPLAYNAME);
mDeviceSupport.onSetCallState(callSpec);
break;
case ACTION_SETCANNEDMESSAGES:
@ -687,27 +676,6 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
return null;
}
private String getContactDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = number;
if (number == null || number.equals("")) {
return name;
}
try (Cursor contactLookup = getContentResolver().query(uri, null, null, null, null)) {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
}
} catch (SecurityException e) {
// ignore, just return name below
}
return name;
}
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
if (GBPrefs.AUTO_RECONNECT.equals(key)) {

View File

@ -0,0 +1,15 @@
package nodomain.freeyourgadget.gadgetbridge.util;
public class JavaExtensions {
/**
* Equivalent c# '??' operator
* @param one first value
* @param two second value
* @return first if not null, or second if first is null
*/
public static <T> T coalesce(T one, T two)
{
return one != null ? one : two;
}
}

View File

@ -23,13 +23,20 @@ public class LanguageUtils {
}
};
//check transliterate option status
/**
* Checks the status of transliteration option
* @return true if transliterate option is On, and false, if Off or not exist
*/
public static boolean transliterate()
{
return GBApplication.getPrefs().getBoolean("transliteration", false);
}
//replace unsupported symbols to english analog
/**
* Replaces unsupported symbols to english
* @param txt input text
* @return transliterated text
*/
public static String transliterate(String txt){
if (txt == null || txt.isEmpty()) {
return txt;
@ -47,7 +54,11 @@ public class LanguageUtils {
return flattenToAscii(message.toString());
}
//replace unsupported symbol to english analog text
/**
* Replaces unsupported symbol to english by {@code transliterateMap}
* @param c input char
* @return replacement text
*/
private static String transliterate(char c){
char lowerChar = Character.toLowerCase(c);
@ -65,7 +76,11 @@ public class LanguageUtils {
return String.valueOf(c);
}
//convert diacritic
/**
* Converts the diacritics
* @param string input text
* @return converted text
*/
private static String flattenToAscii(String string) {
string = Normalizer.normalize(string, Normalizer.Form.NFD);
return string.replaceAll("\\p{M}", "");