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

247 lines
9.7 KiB
Java

package com.appetize.remotesms;
import android.content.pm.PackageManager;
import android.graphics.drawable.Animatable;
import android.os.Bundle;
import android.telephony.SubscriptionInfo;
import android.telephony.SubscriptionManager;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import com.pengrad.telegrambot.Callback;
import com.pengrad.telegrambot.TelegramBot;
import com.pengrad.telegrambot.model.Message;
import com.pengrad.telegrambot.model.Update;
import com.pengrad.telegrambot.request.GetUpdates;
import com.pengrad.telegrambot.request.SendMessage;
import com.pengrad.telegrambot.response.GetUpdatesResponse;
import com.pengrad.telegrambot.response.SendResponse;
import java.io.IOException;
import java.util.List;
import java.util.Random;
public class AddDevice extends AppCompatActivity {
private int page_no;
private long chat_id;
private final String code = randCode(4, 4);
FrameLayout p1;
FrameLayout p2;
Button btn_nxt;
Button btn_back;
SubscriptionManager subscriptionManager;
CheckBox[] boxes;
TelegramBot bot;
List<SubscriptionInfo> sims;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_device);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
page_no = 0;
p1 = (FrameLayout) findViewById(R.id.p1);
p2 = (FrameLayout) findViewById(R.id.p2);
btn_nxt = (Button) findViewById(R.id.btn_nxt);
btn_back = (Button) findViewById(R.id.btn_back);
((TextView) findViewById(R.id.conn_code)).setText(code);
bot = new TelegramBot(this.getString(R.string.bot_token));
subscriptionManager = SubscriptionManager.from(this);
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
sims = subscriptionManager.getActiveSubscriptionInfoList();
Log.d("subscription", "onCreate: "+ sims.get(0).getSubscriptionId());
LinearLayout ll = findViewById(R.id.ll_sims);
new Thread(new Runnable() {
@Override
public void run() {
boxes = new CheckBox[sims.size()];
for(int i = 0; i < sims.size(); i++){
boxes[i] = new CheckBox(AddDevice.this);
boxes[i].setText("SIM " + sims.get(i).getNumber());
int finalI = i;
runOnUiThread(new Runnable() {
@Override
public void run() {
ll.addView(boxes[finalI]);
}
});
}
}
}).start();
}
public void back(View view) {
switch (page_no){
case 0:
finish();
break;
case 1:
page_no = 0;
p1.setVisibility(View.VISIBLE);
Animation a1 = AnimationUtils.loadAnimation(this, R.anim.slide_in_left);
p1.startAnimation(a1);
Animation a2 = AnimationUtils.loadAnimation(this,R.anim.slide_out_right);
a2.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
p2.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
p2.startAnimation(a2);
((Button)findViewById(R.id.btn_nxt)).setText(R.string.next);
((Button)findViewById(R.id.btn_back)).setText(R.string.cancel);
break;
}
}
public void nxt(View view) {
switch (page_no){
case 0:
page_no=1;
Animation a1 = AnimationUtils.loadAnimation(this, R.anim.slide_out_left);
a1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
p1.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
p1.startAnimation(a1);
p2.setVisibility(View.VISIBLE);
Animation a2 = AnimationUtils.loadAnimation(this,R.anim.slide_in_right);
p2.startAnimation(a2);
btn_nxt.setText(R.string.finish);
btn_back.setText(R.string.back);
break;
case 1:
StringBuilder backsims = new StringBuilder();
for(int i=0; i < boxes.length; i++){
if(boxes[i].isChecked()){
if(!backsims.toString().isEmpty())
backsims.append('#');
backsims.append(sims.get(i).getSubscriptionId());
}
}
dbViewmodel dbv = new dbViewmodel(this);
Rule rule = new Rule();
rule.name = ((TextView)findViewById(R.id.txt_blk_name)).getText().toString();
rule.sims = backsims.toString();
rule.chat_id = chat_id;
dbv.add_rule(rule);
SendMessage request = new SendMessage(chat_id, "Congratulations!\nYour Telegram account is now verified as a SMS receiver account of your target device. From now on, all the incoming SMSs will be forwarded here to you. ;)");
bot.execute(request);
finish();
break;
}
}
static String randCode(int length, int section){
StringBuilder buffer = new StringBuilder((length*section)+section-1);
for(int i = 1; i<=section; i++){
for(int j=0;j<length;j++){
int randomInt = new Random().nextInt(9);
buffer.append(String.valueOf(randomInt));
}
if(i < section)
buffer.append('-');
}
return buffer.toString();
}
public void updateChatID(long chatID){
chat_id = chatID;
TextView chid = findViewById(R.id.chat_id);
chid.setText("Chat ID: " + String.valueOf(chatID));
chid.setVisibility(View.VISIBLE);
((Button)findViewById(R.id.btn_nxt)).setEnabled(true);
}
public void verify(View view) {
GetUpdates getUpdates = new GetUpdates().limit(100).offset(0).timeout(0);
bot.execute(getUpdates, new Callback<GetUpdates, GetUpdatesResponse>() {
@Override
public void onResponse(GetUpdates request, GetUpdatesResponse response) {
List<Update> updates = response.updates();
boolean verified = false;
for (Update update : updates){
if(update.message().text().equals(code)){
verified = true;
runOnUiThread(new Runnable() {
@Override
public void run() {
updateChatID(update.message().chat().id());
Toast.makeText(AddDevice.this, "Verified", Toast.LENGTH_LONG).show();
}
});
}
}
if(!verified){
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AddDevice.this, "Could not verify", Toast.LENGTH_SHORT).show();
}
});
}
}
@Override
public void onFailure(GetUpdates request, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(AddDevice.this, "Could not verify", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}