1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-23 18:36:50 +01:00

Resolve caller names by phone number before sending to Pebble

This commit is contained in:
Andreas Shimokawa 2015-01-23 11:32:58 +01:00
parent bf6abe9672
commit 070c6db5ed
3 changed files with 38 additions and 1 deletions

View File

@ -4,6 +4,8 @@
<uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application <application
android:allowBackup="true" android:allowBackup="true"
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_launcher"

View File

@ -7,9 +7,13 @@ import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice; import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket; import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket; import android.bluetooth.BluetoothSocket;
import android.content.ContentResolver;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.IBinder; import android.os.IBinder;
import android.os.ParcelUuid; import android.os.ParcelUuid;
import android.provider.ContactsContract;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import android.util.Log; import android.util.Log;
import android.widget.Toast; import android.widget.Toast;
@ -107,6 +111,7 @@ public class BluetoothCommunicationService extends Service {
e.printStackTrace(); e.printStackTrace();
} }
try { try {
mBtSocket.close(); mBtSocket.close();
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
@ -127,8 +132,10 @@ public class BluetoothCommunicationService extends Service {
String phoneNumber = intent.getStringExtra("incomingcall_phonenumber"); String phoneNumber = intent.getStringExtra("incomingcall_phonenumber");
byte phoneState = intent.getByteExtra("incomingcall_state", (byte) 0); byte phoneState = intent.getByteExtra("incomingcall_state", (byte) 0);
String callerName = getContactDisplayNameByNumber(phoneNumber);
if (mBtSocketIoThread != null) { if (mBtSocketIoThread != null) {
byte[] msg = PebbleProtocol.encodeIncomingCall(phoneNumber, phoneNumber, phoneState); byte[] msg = PebbleProtocol.encodeIncomingCall(phoneNumber, callerName, phoneState);
mBtSocketIoThread.write(msg); mBtSocketIoThread.write(msg);
} }
} else if (intent.getAction().equals(ACTION_SETTIME)) { } else if (intent.getAction().equals(ACTION_SETTIME)) {
@ -150,6 +157,33 @@ public class BluetoothCommunicationService extends Service {
return null; return null;
} }
private String getContactDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
String name = "Unknown";
if (number == null || number.equals("")) {
return name;
}
ContentResolver contentResolver = getContentResolver();
Cursor contactLookup = contentResolver.query(uri, null, null, null, null);
try {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return name;
}
private class BtSocketAcceptThread extends Thread { private class BtSocketAcceptThread extends Thread {
private final BluetoothServerSocket mmServerSocket; private final BluetoothServerSocket mmServerSocket;

View File

@ -26,4 +26,5 @@ public class PhoneCallReceiver extends BroadcastReceiver {
context.startService(startIntent); context.startService(startIntent);
} }
} }
} }