1
0
mirror of https://github.com/tijder/SmsMatrix.git synced 2024-09-28 21:50:43 +00:00

Add Android call events as notice in the contact room #10

This commit is contained in:
Gerben Droogers 2017-10-12 22:16:03 +02:00
parent 3f8141c2dc
commit c7d2a14801
6 changed files with 92 additions and 59 deletions

View File

@ -23,10 +23,13 @@
</intent-filter> </intent-filter>
</activity> </activity>
<receiver android:name="eu.droogers.smsmatrix.SmsListener"> <receiver android:name="eu.droogers.smsmatrix.ReceiverListener">
<intent-filter> <intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" /> <action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver> </receiver>
<service android:name="eu.droogers.smsmatrix.MatrixService" /> <service android:name="eu.droogers.smsmatrix.MatrixService" />

View File

@ -141,7 +141,7 @@ public class Matrix {
} }
} }
public void sendMessage(final String phoneNumber, final String body) { public void sendMessage(final String phoneNumber, final String body, final String type) {
if (session != null && session.isAlive()) { if (session != null && session.isAlive()) {
Room room = getRoomByPhonenumber(phoneNumber); Room room = getRoomByPhonenumber(phoneNumber);
if (room == null) { if (room == null) {
@ -153,16 +153,16 @@ public class Matrix {
session.getRoomsApiClient().updateTopic(info, phoneNumber, new SimpleApiCallback<Void>()); session.getRoomsApiClient().updateTopic(info, phoneNumber, new SimpleApiCallback<Void>());
changeDisplayname(info, getContactName(phoneNumber, context)); changeDisplayname(info, getContactName(phoneNumber, context));
Room room = store.getRoom(info); Room room = store.getRoom(info);
SendMesageToRoom(room, body); SendMesageToRoom(room, body, type);
} }
}); });
} else { } else {
changeDisplayname(room.getRoomId(), getContactName(phoneNumber, context)); changeDisplayname(room.getRoomId(), getContactName(phoneNumber, context));
SendMesageToRoom(room, body); SendMesageToRoom(room, body, type);
} }
} else { } else {
Log.e(tag, "Error with sending message"); Log.e(tag, "Error with sending message");
notSendMesages.add(new NotSendMesage(phoneNumber, body)); notSendMesages.add(new NotSendMesage(phoneNumber, body, type));
} }
} }
@ -173,10 +173,10 @@ public class Matrix {
session.getRoomsApiClient().sendStateEvent(roomId, "m.room.member", session.getMyUserId(), params, new SimpleApiCallback<Void>()); session.getRoomsApiClient().sendStateEvent(roomId, "m.room.member", session.getMyUserId(), params, new SimpleApiCallback<Void>());
} }
public void SendMesageToRoom(Room room, String body) { public void SendMesageToRoom(Room room, String body, String type) {
Message msg = new Message(); Message msg = new Message();
msg.body = body; msg.body = body;
msg.msgtype = "m.text"; msg.msgtype = type;
session.getRoomsApiClient().sendMessage(String.valueOf(transaction), room.getRoomId(), msg, new SimpleApiCallback<Event>()); session.getRoomsApiClient().sendMessage(String.valueOf(transaction), room.getRoomId(), msg, new SimpleApiCallback<Event>());
transaction++; transaction++;
} }
@ -213,7 +213,7 @@ public class Matrix {
public void sendMessageList(List<NotSendMesage> messages) { public void sendMessageList(List<NotSendMesage> messages) {
for (NotSendMesage ms : messages) { for (NotSendMesage ms : messages) {
sendMessage(ms.getPhone(), ms.getBody()); sendMessage(ms.getPhone(), ms.getBody(), ms.getType());
} }
} }

View File

@ -46,8 +46,9 @@ public class MatrixService extends Service {
String phone = intent.getStringExtra("SendSms_phone"); String phone = intent.getStringExtra("SendSms_phone");
String body = intent.getStringExtra("SendSms_body"); String body = intent.getStringExtra("SendSms_body");
String type = intent.getStringExtra("SendSms_type");
if (phone != null) { if (phone != null) {
mx.sendMessage(phone, body); mx.sendMessage(phone, body, type);
} }
return START_NOT_STICKY; return START_NOT_STICKY;

View File

@ -5,12 +5,14 @@ package eu.droogers.smsmatrix;
*/ */
class NotSendMesage { class NotSendMesage {
private String type;
private String phone; private String phone;
private String body; private String body;
public NotSendMesage(String phone, String body) { public NotSendMesage(String phone, String body, String type) {
this.phone = phone; this.phone = phone;
this.body = body; this.body = body;
this.type = type;
} }
public String getPhone() { public String getPhone() {
@ -20,4 +22,8 @@ class NotSendMesage {
public String getBody() { public String getBody() {
return body; return body;
} }
public String getType() {
return type;
}
} }

View File

@ -0,0 +1,72 @@
package eu.droogers.smsmatrix;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.telephony.TelephonyManager;
import android.util.Log;
/**
* Created by gerben on 6-10-17.
*/
public class ReceiverListener extends BroadcastReceiver {
private static final String TAG = "ReceiverListener";
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
handleIncomingSMS(context, intent);
} else if (intent.getAction().equals("android.intent.action.PHONE_STATE")) {
handleIncomingCall(context, intent);
}
}
private void handleIncomingSMS(Context context, Intent intent) {
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
sendMatrix(context, msgBody, msg_from, "m.text");
}
}catch(Exception e){
Log.d("Exception caught",e.getMessage());
}
}
}
private void handleIncomingCall(Context context, Intent intent) {
String cal_state = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
String cal_from = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
String body = cal_from;
switch(cal_state){
case "IDLE":
body += " end call";
break;
case "OFFHOOK":
body += " answered call";
break;
case "RINGING":
body += " is calling";
break;
}
sendMatrix(context, body, cal_from, "m.notice");
}
private void sendMatrix(Context context, String body, String phone, String type) {
Intent intent = new Intent(context, MatrixService.class);
intent.putExtra("SendSms_phone", phone);
intent.putExtra("SendSms_body", body);
intent.putExtra("SendSms_type", type);
context.startService(intent);
}
}

View File

@ -1,49 +0,0 @@
package eu.droogers.smsmatrix;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
/**
* Created by gerben on 6-10-17.
*/
public class SmsListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
String msg_from;
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
String msgBody = msgs[i].getMessageBody();
System.out.println(msg_from + ": " + msgBody);
// Matrix mx = new Matrix(context, "https://matrix.org");
sendMatrix(context, msgBody, msg_from);
}
}catch(Exception e){
Log.d("Exception caught",e.getMessage());
}
}
}
}
private void sendMatrix(Context context, String body, String phone) {
Intent intent = new Intent(context, MatrixService.class);
intent.putExtra("SendSms_phone", phone);
intent.putExtra("SendSms_body", body);
context.startService(intent);
// MainActivity.mx.sendMessage( + ": " + );
}
}