diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java index 6f217b5d1..c8f839267 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/impl/GBDeviceService.java @@ -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 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; + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java index a7be20f89..9177b077b 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/model/DeviceService.java @@ -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"; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index 336e0a927..1dd3934c3 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -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)) { diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/JavaExtensions.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/JavaExtensions.java new file mode 100644 index 000000000..cd4b37cc2 --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/JavaExtensions.java @@ -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 coalesce(T one, T two) + { + return one != null ? one : two; + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java index 59d7aa817..a19d79d98 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/util/LanguageUtils.java @@ -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}", "");