mirror of
https://github.com/tijder/SmsMatrix.git
synced 2024-10-27 18:24:01 +00:00
Read contact name #2
This commit is contained in:
parent
55949e9097
commit
bacd9e10da
@ -37,7 +37,7 @@
|
||||
<ConfirmationsSetting value="0" id="Add" />
|
||||
<ConfirmationsSetting value="0" id="Remove" />
|
||||
</component>
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/build/classes" />
|
||||
</component>
|
||||
<component name="ProjectType">
|
||||
|
@ -55,7 +55,7 @@ public class EventListener implements IMXEventListener {
|
||||
// mx.getUnreadEvents();
|
||||
mx.sendEvent(event);
|
||||
}
|
||||
Log.e(TAG, "onLiveEvent: " + event + " " + event.getSender() + " : " + event.getContent());
|
||||
Log.e(TAG, "onLiveEvent: " + event);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,11 +1,17 @@
|
||||
package eu.droogers.smsmatrix;
|
||||
|
||||
import android.content.Context;
|
||||
import android.database.Cursor;
|
||||
import android.net.Uri;
|
||||
import android.provider.ContactsContract;
|
||||
import android.telephony.SmsManager;
|
||||
import android.util.Log;
|
||||
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import org.matrix.androidsdk.HomeServerConnectionConfig;
|
||||
import org.matrix.androidsdk.MXDataHandler;
|
||||
@ -24,7 +30,9 @@ import org.matrix.androidsdk.rest.model.login.Credentials;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static android.content.ContentValues.TAG;
|
||||
|
||||
@ -43,6 +51,8 @@ public class Matrix {
|
||||
private IMXEventListener evLis;
|
||||
IMXStore store;
|
||||
String deviceName;
|
||||
private String botUsername;
|
||||
private String botHSUrl;
|
||||
|
||||
private String realUserid;
|
||||
|
||||
@ -52,6 +62,8 @@ public class Matrix {
|
||||
|
||||
realUserid = username;
|
||||
deviceName = device;
|
||||
this.botUsername = botUsername;
|
||||
botHSUrl = url;
|
||||
|
||||
login(botUsername, botPassword);
|
||||
}
|
||||
@ -139,10 +151,13 @@ public class Matrix {
|
||||
public void onSuccess(String info) {
|
||||
super.onSuccess(info);
|
||||
session.getRoomsApiClient().updateTopic(info, phoneNumber, new SimpleApiCallback<Void>());
|
||||
SendMesageToRoom(store.getRoom(info), body);
|
||||
changeDisplayname(info, getContactName(phoneNumber, context));
|
||||
Room room = store.getRoom(info);
|
||||
SendMesageToRoom(room, body);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
changeDisplayname(room.getRoomId(), getContactName(phoneNumber, context));
|
||||
SendMesageToRoom(room, body);
|
||||
}
|
||||
} else {
|
||||
@ -151,6 +166,18 @@ public class Matrix {
|
||||
}
|
||||
}
|
||||
|
||||
private void changeDisplayname(String roomId, String displayname) {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("displayname", displayname);
|
||||
params.put("membership", "join");
|
||||
session.getRoomsApiClient().sendStateEvent(roomId, "m.room.member", botId(), params, new SimpleApiCallback<Void>());
|
||||
}
|
||||
|
||||
private String botId() {
|
||||
String baseUrl = botHSUrl.replaceFirst("^(http(?>s)://www\\.|http(?>s)://|www\\.)","");
|
||||
return "@" + botUsername + ":" + baseUrl;
|
||||
}
|
||||
|
||||
public void SendMesageToRoom(Room room, String body) {
|
||||
Message msg = new Message();
|
||||
msg.body = body;
|
||||
@ -195,7 +222,7 @@ public class Matrix {
|
||||
}
|
||||
}
|
||||
|
||||
public Room getRoomByPhonenumber (String number) {
|
||||
private Room getRoomByPhonenumber (String number) {
|
||||
Collection<Room> rooms = store.getRooms();
|
||||
Log.e(TAG, "getRoomByPhonenumber: " + number );
|
||||
Log.e(TAG, "getRoomByPhonenumber: " + rooms.size() );
|
||||
@ -208,6 +235,29 @@ public class Matrix {
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getContactName(final String phoneNumber, Context context)
|
||||
{
|
||||
Uri uri=Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,Uri.encode(phoneNumber));
|
||||
|
||||
String[] projection = new String[]{ContactsContract.PhoneLookup.DISPLAY_NAME};
|
||||
|
||||
String contactName="";
|
||||
Cursor cursor=context.getContentResolver().query(uri,projection,null,null,null);
|
||||
|
||||
if (cursor != null) {
|
||||
if(cursor.moveToFirst()) {
|
||||
contactName=cursor.getString(0);
|
||||
}
|
||||
cursor.close();
|
||||
}
|
||||
|
||||
if (contactName.isEmpty()) {
|
||||
contactName = phoneNumber;
|
||||
}
|
||||
|
||||
return contactName;
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
session.stopEventStream();
|
||||
dh.removeListener(evLis);
|
||||
|
Loading…
Reference in New Issue
Block a user