Remote-SMS-via-Telegram/app/src/main/java/com/appetize/remotesms/fgService.java

205 lines
9.8 KiB
Java

package com.appetize.remotesms;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.os.IBinder;
import android.telephony.SmsMessage;
import android.util.Log;
import androidx.core.app.NotificationCompat;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.request.ForceReply;
import com.pengrad.telegrambot.model.request.ParseMode;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.response.SendResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class fgService extends Service {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private static final String CONNECTIVITY_CHANGE = "android.net.conn.CONNECTIVITY_CHANGE";
public static boolean isServiceRunning;
private String CHANNEL_ID = "FG_SERVICE_CHANNEL";
public fgService() {
isServiceRunning = false;
}
@Override
public void onCreate() {
super.onCreate();
createNotificationChannel();
isServiceRunning = true;
IntentFilter filter = new IntentFilter();
filter.addAction(SMS_RECEIVED);
filter.addAction(CONNECTIVITY_CHANGE);
registerReceiver(receiver, filter);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, Home.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this, CHANNEL_ID)
.setContentTitle("Remote SMS service is running")
.setContentText("Monitoring for new SMS")
.setSmallIcon(R.drawable.common_google_signin_btn_icon_dark)
.setContentIntent(pendingIntent)
.setColor(getResources().getColor(R.color.design_default_color_primary))
.build();
startForeground(1, notification);
return START_STICKY;
}
private void createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
String appName = getString(R.string.app_name);
NotificationChannel serviceChannel = new NotificationChannel(CHANNEL_ID, appName, NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager manager = getSystemService(NotificationManager.class);
manager.createNotificationChannel(serviceChannel);
}
}
@Override
public void onDestroy() {
isServiceRunning = false;
stopForeground(true);
//Intent broadcastIntent = new Intent(this, SMSReciever.class);
//sendBroadcast(broadcastIntent);
unregisterReceiver(receiver);
super.onDestroy();
}
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
dbViewmodel dbv = new dbViewmodel(context);
new Thread(new Runnable() {
@Override
public void run() {
TelegramBot bot = new TelegramBot(getString(R.string.bot_token));
List<Rule> rules = dbv.get_i_rules();
if(intent.getAction().equals(SMS_RECEIVED)) {
Log.d("Foreground", "onReceive: Recieve korsi");
Bundle bundle = intent.getExtras();
if(bundle != null){
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for(int i=0; i<pdus.length;i++){
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
}
int simid = capturedSIM(bundle);
Log.d("Foreground", "simid: " + String.valueOf(simid));
StringBuilder body = new StringBuilder();
for(SmsMessage message:messages)
body.append(message.getMessageBody());
for(Rule rule:rules){
String[] sims = rule.sims.split("#");
for(String rsim:sims){
if(rsim.equals(String.valueOf(simid))){
try {
SendMessage request = new SendMessage(rule.chat_id, "From:\n" + messages[0].getOriginatingAddress() + "\n\nBody:\n" + body.toString() + "\n\nDatetime:\n" + new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z").format(new Date(messages[0].getTimestampMillis())));
Log.d("Foreground", "From:\n" + messages[0].getOriginatingAddress() + "\n\nBody:\n" + body.toString() + "\n\nDatetime:\n" + new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z").format(new Date(messages[0].getTimestampMillis())));
SendResponse sendResponse = bot.execute(request);
if (!sendResponse.isOk()) {
SMS sms = new SMS();
sms.body = body.toString();
sms.datetime = messages[0].getTimestampMillis();
sms.sender = messages[0].getOriginatingAddress();
sms.sim_no = simid;
dbv.add_sms(sms);
}
}catch (Exception e){
Log.e("Foreground Error", e.getMessage() );
SMS sms = new SMS();
sms.body = body.toString();
sms.datetime = messages[0].getTimestampMillis();
sms.sender = messages[0].getOriginatingAddress();
sms.sim_no = simid;
dbv.add_sms(sms);
}
}
}
}
}
} else if (intent.getAction().equals(CONNECTIVITY_CHANGE) && NetworkUtil.getConnectivityStatusString(context) != NetworkUtil.NETWORK_STATUS_NOT_CONNECTED) {
List<SMS> smsList = dbv.get_smsList();
for (SMS sms : smsList){
for(Rule rule:rules){
String[] sims = rule.sims.split("#");
for(String rsim:sims){
if(rsim.equals(String.valueOf(sms.sim_no))){
try {
SendMessage request = new SendMessage(rule.chat_id, "From:\n" + sms.sender + "\n\nBody:\n" + sms.body + "\n\nDatetime:\n" + new SimpleDateFormat("dd MMM yyyy HH:mm:ss Z").format(new Date(sms.datetime)));
SendResponse sendResponse = bot.execute(request);
if (sendResponse.isOk()) {
dbv.rem_SMS(sms);
}
}
catch (Exception e){
Log.e("Foreground Error", e.getMessage() );
}
}
}
}
}
}
}
}).start();
}
int capturedSIM(Bundle bundle){
String TAG = "SIM ID";
int simid = -1;
if(bundle.containsKey("subscription")){
simid = bundle.getInt("subscription");
Log.d(TAG, "capturedSIM: " + simid + " #subscription");
}
if(simid >= 0 && simid < 5){
return simid;
}
else{
if(bundle.containsKey("simId")){
simid = bundle.getInt("simId");
Log.d(TAG, "capturedSIM: " + simid + " #simid");
}
else if(bundle.containsKey("com.android.phone.extra.slot")){
simid = bundle.getInt("com.android.phone.extra.slot");
Log.d(TAG, "capturedSIM: " + simid + " #com.android.phone.extra.slot");
}
else if(bundle.containsKey("slot")){
simid = bundle.getInt("slot");
Log.d(TAG, "capturedSIM: " + simid + " #slolt");
}
else{
String keyName = "";
for(String key: bundle.keySet()){
if(key.contains("sim"))
keyName = key;
}
simid = bundle.getInt(keyName);
Log.d(TAG, "capturedSIM: " + simid + " #" + keyName);
}
return simid;
}
}
};
}