Skip to content

Commit

Permalink
实现短信黑名单功能
Browse files Browse the repository at this point in the history
  • Loading branch information
qwe7002 committed Apr 2, 2020
1 parent 7b2a6e0 commit 506e417
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -54,6 +57,7 @@ public class chat_command_service extends Service {
static Thread thread_main;
private boolean have_bot_username = false;
private boolean privacy_mode;
private SharedPreferences sharedPreferences;
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Notification notification = public_func.get_notification_obj(getApplicationContext(), getString(R.string.chat_command_service_name));
Expand All @@ -67,7 +71,7 @@ public void onCreate() {
super.onCreate();
context = getApplicationContext();
Paper.init(context);
SharedPreferences sharedPreferences = context.getSharedPreferences("data", MODE_PRIVATE);
sharedPreferences = context.getSharedPreferences("data", MODE_PRIVATE);
chat_id = sharedPreferences.getString("chat_id", "");
bot_token = sharedPreferences.getString("bot_token", "");
okhttp_client = public_func.get_okhttp_obj(sharedPreferences.getBoolean("doh_switch", true));
Expand Down Expand Up @@ -181,10 +185,14 @@ private void receive_handle(JsonObject result_obj) {
switch (command) {
case "/help":
case "/start":
request_body.text = getString(R.string.system_message_head) + "\n" + getString(R.string.available_command) + "\n" + getString(R.string.sendsms);
case "/commandlist":
request_body.text = getString(R.string.system_message_head) + "\n" + getString(R.string.available_command) + "\n" + getString(R.string.sendsms) + "\n" + getString(R.string.get_spam_sms);
if (!message_type_is_private && privacy_mode && !bot_username.equals("")) {
request_body.text = request_body.text.replace(" -", "@" + bot_username + " -");
}
if (command.equals("/commandlist")) {
request_body.text = request_body.text.replace("/", "");
}
has_command = true;
break;
case "/ping":
Expand All @@ -196,6 +204,43 @@ private void receive_handle(JsonObject result_obj) {
request_body.text = getString(R.string.system_message_head) + public_func.read_log(context, 10);
has_command = true;
break;
case "/getspamsms":
ArrayList<String> spam_sms_list = Paper.book().read("spam_sms_list", new ArrayList<>());
if (spam_sms_list.size() == 0) {
request_body.text = context.getString(R.string.system_message_head) + "\n" + getString(R.string.no_spam_history);
break;
}
new Thread(() -> {
if (public_func.check_network_status(context)) {
OkHttpClient okhttp_client = public_func.get_okhttp_obj(sharedPreferences.getBoolean("doh_switch", true));
for (String item : spam_sms_list) {
message_json send_sms_request_body = new message_json();
send_sms_request_body.chat_id = chat_id;
send_sms_request_body.text = item;
String request_uri = public_func.get_url(bot_token, "sendMessage");
String request_body_json = new Gson().toJson(send_sms_request_body);
RequestBody body = RequestBody.create(public_func.JSON, request_body_json);
Request request_obj = new Request.Builder().url(request_uri).method("POST", body).build();
Call call = okhttp_client.newCall(request_obj);
call.enqueue(new Callback() {
@Override
public void onFailure(@NotNull Call call, @NotNull IOException e) {
Log.d(TAG, "onFailure: ");
}

@Override
public void onResponse(@NotNull Call call, @NotNull Response response) {

}
});
ArrayList<String> resend_list_local = Paper.book().read("spam_sms_list", new ArrayList<>());
resend_list_local.remove(item);
Paper.book().write("spam_sms_list", resend_list_local);
}
}
public_func.write_log(context, "Send spam message is complete.");
}).start();
return;
case "/sendsms":
String[] msg_send_list = request_msg.split("\n");
if (msg_send_list.length > 2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.provider.Settings;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
Expand All @@ -34,6 +36,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

Expand Down Expand Up @@ -69,7 +72,6 @@ protected void onCreate(Bundle savedInstanceState) {
final Switch privacy_mode_switch = findViewById(R.id.privacy_switch);
final Button save_button = findViewById(R.id.save);
final Button get_id = findViewById(R.id.get_id);
final Button notify_app_set = findViewById(R.id.notify_app_set);

Paper.init(context);
sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
Expand Down Expand Up @@ -143,6 +145,7 @@ public void afterTextChanged(Editable s) {
chat_command.setOnClickListener(v -> set_privacy_mode_checkbox(chat_id, chat_command, privacy_mode_switch));
verification_code.setChecked(sharedPreferences.getBoolean("verification_code", false));
doh_switch.setChecked(sharedPreferences.getBoolean("doh_switch", true));

chat_id.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
Expand Down Expand Up @@ -360,19 +363,6 @@ public void onResponse(@NonNull Call call, @NonNull Response response) throws IO
}
});
});
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
notify_app_set.setVisibility(View.VISIBLE);
notify_app_set.setOnClickListener(v -> {
if (!public_func.is_notify_listener(context)) {
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
set_permission_back = true;
return;
}
startActivity(new Intent(main_activity.this, notify_apps_list_activity.class));
});
}

}

Expand Down Expand Up @@ -404,6 +394,11 @@ protected void onResume() {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
MenuItem mItem;
mItem = menu.getItem(R.id.spam_sms_keyword);
mItem.setVisible(true);
}
return true;
}

Expand Down Expand Up @@ -434,6 +429,7 @@ private void show_privacy_dialog() {
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
LayoutInflater inflater = this.getLayoutInflater();
switch (item.getItemId()) {
case R.id.scan:
Intent intent = new Intent(context, scanner_activity.class);
Expand All @@ -443,6 +439,46 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
Intent logcat_intent = new Intent(main_activity.this, logcat_activity.class);
startActivity(logcat_intent);
return true;
case R.id.set_notify:
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
if (!public_func.is_notify_listener(context)) {
Intent setting_intent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
setting_intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
} else {
setting_intent = new Intent("android.settings.NOTIFICATION_LISTENER_SETTINGS");
}
setting_intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setting_intent);
set_permission_back = true;
return false;
}
startActivity(new Intent(main_activity.this, notify_apps_list_activity.class));
}
return true;
case R.id.spam_sms_keyword:
View spam_dialog_view = inflater.inflate(R.layout.set_keyword_layout, null);
final EditText editText = spam_dialog_view.findViewById(R.id.spam_sms_keyword);
ArrayList<String> black_keyword_list_old = Paper.book().read("black_keyword_list", new ArrayList<>());
StringBuilder black_keyword_list_old_string = new StringBuilder();
int count = 0;
for (String list_item : black_keyword_list_old) {
if (count != 0) {
black_keyword_list_old_string.append(";");
}
++count;
black_keyword_list_old_string.append(list_item);
}
editText.setText(black_keyword_list_old_string);
new AlertDialog.Builder(this).setTitle(R.string.spam_keyword_dialog_title)
.setView(spam_dialog_view)
.setPositiveButton("OK", (dialog, which) -> {
String input = editText.getText().toString();
String[] black_keyword_list = input.split(";");
Paper.book().write("black_keyword_list", new ArrayList<>(Arrays.asList(black_keyword_list)));
})
.show();
return true;
}
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
import com.google.gson.Gson;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import io.paperdb.Paper;
import okhttp3.Call;
Expand Down Expand Up @@ -81,6 +85,7 @@ public void onReceive(final Context context, Intent intent) {
String message_body_html = message_body;
final String message_head = "[" + context.getString(R.string.receive_sms_head) + "]" + "\n" + context.getString(R.string.from) + message_address + "\n" + context.getString(R.string.content);
String raw_request_body_text = message_head + message_body;
boolean is_verification_code = false;
if (sharedPreferences.getBoolean("verification_code", false) && !is_trusted_phone) {
String verification = CodeauxLibStatic.parsecode(message_body);
if (verification != null) {
Expand All @@ -90,6 +95,7 @@ public void onReceive(final Context context, Intent intent) {
.replace(">", "&gt;")
.replace("&", "&amp;")
.replace(verification, "<code>" + verification + "</code>");
is_verification_code = true;
}
}
request_body.text = message_head + message_body_html;
Expand Down Expand Up @@ -117,6 +123,27 @@ public void onReceive(final Context context, Intent intent) {
return;
}
}

if (!is_verification_code && !is_trusted_phone) {
ArrayList<String> black_list_array = Paper.book().read("black_keyword_list");
for (String black_list_item : black_list_array) {
if (message_body.contains(black_list_item)) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(context.getString(R.string.time_format), Locale.UK);
String write_message = request_body.text + "\n" + context.getString(R.string.time) + simpleDateFormat.format(new Date(System.currentTimeMillis()));
ArrayList<String> spam_sms_list;
Paper.init(context);
spam_sms_list = Paper.book().read("spam_sms_list", new ArrayList<>());
if (spam_sms_list.size() >= 5) {
spam_sms_list.remove(0);
}
spam_sms_list.add(write_message);
Paper.book().write("spam_sms_list", spam_sms_list);
Log.i(TAG, "Detected message contains blacklist keywords, add spam list");
return;
}
}
}

String request_body_json = new Gson().toJson(request_body);
RequestBody body = RequestBody.create(public_func.JSON, request_body_json);
OkHttpClient okhttp_client = public_func.get_okhttp_obj(sharedPreferences.getBoolean("doh_switch", true));
Expand Down
27 changes: 27 additions & 0 deletions app/src/main/res/layout/set_keyword_layout.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:paddingLeft="20dp"
android:paddingRight="20dp">

<EditText
android:id="@+id/spam_sms_keyword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/keywords"
android:importantForAutofill="no"
android:inputType="text"
android:maxLines="1"
android:singleLine="true" />

</com.google.android.material.textfield.TextInputLayout>

</LinearLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/menu/main_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
android:icon="@drawable/ic_scan_code"
android:title="@string/scan_title"
app:showAsAction="ifRoom" />
<item
android:id="@+id/set_notify"
android:title="@string/set_notification_listener"
android:visible="false"
app:showAsAction="never" />
<item
android:id="@+id/spam_sms_keyword"
android:title="@string/spam_sms_keyword_title"
app:showAsAction="never" />
<item
android:id="@+id/logcat"
android:title="@string/logcat"
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/values-ja-rJP/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="time_format">"yyyy-MM-dd HH:mm:ss z"</string>
<string name="receive_sms_head">新着メール</string>
<string name="content">"本文: "</string>
<string name="from">"送信元: "</string>
Expand Down Expand Up @@ -78,4 +79,12 @@
<string name="decline">後で決めます</string>
<string name="visit_page">プライバシー規約ページにアクセス</string>
<string name="failed_resend">失敗したメッセージの再送信</string>
<string name="keywords">キーワード</string>
<string name="spam_sms_keyword_title">SMSブラックリスト</string>
<string name="spam_keyword_dialog_title">キーワードを分割するには「;」(半角字符)を使用してください。</string>
<string name="set_notification_listener">通知リスナー</string>
<string name="time">"時間: "</string>
<string name="get_spam_sms">/getspamsms - フィルタリングされたスパムメッセージを取得する</string>
<string name="spam_count_title">"フィルタリングされたスパム: "</string>
<string name="no_spam_history">スパム履歴なし</string>
</resources>
9 changes: 9 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="time_format">"yyyy-MM-dd HH:mm:ss z"</string>
<string name="content">"内容: "</string>
<string name="from">"来自: "</string>
<string name="to">"发送到: "</string>
Expand Down Expand Up @@ -78,4 +79,12 @@
<string name="decline">"我稍后再决定"</string>
<string name="visit_page">访问“隐私条款”页面</string>
<string name="failed_resend">重新发送</string>
<string name="keywords">关键词</string>
<string name="spam_sms_keyword_title">短信黑名单</string>
<string name="spam_keyword_dialog_title">请使用\"; \"分割关键字。</string>
<string name="set_notification_listener">通知侦听器</string>
<string name="time">"时间: "</string>
<string name="get_spam_sms">/getspamsms - 获取被过滤的垃圾短信</string>
<string name="spam_count_title">"过滤的垃圾短信: "</string>
<string name="no_spam_history">没有垃圾短信历史</string>
</resources>
9 changes: 9 additions & 0 deletions app/src/main/res/values-zh-rTW/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="time_format">"yyyy-MM-dd HH:mm:ss z"</string>
<string name="receive_sms_head">收到簡訊</string>
<string name="content">"內容: "</string>
<string name="from">"來自: "</string>
Expand Down Expand Up @@ -78,4 +79,12 @@
<string name="decline">我稍后再決定</string>
<string name="visit_page">訪問“隱私條款”頁面</string>
<string name="failed_resend">重新發送</string>
<string name="keywords">關鍵詞</string>
<string name="spam_sms_keyword_title">簡訊黑名單</string>
<string name="spam_keyword_dialog_title">請使用\"; \"分割關鍵字。</string>
<string name="set_notification_listener">通知偵聽器</string>
<string name="time">"時間: "</string>
<string name="get_spam_sms">/getspamsms - 獲取被過濾的垃圾簡訊</string>
<string name="spam_count_title">"過濾的垃圾簡訊: "</string>
<string name="no_spam_history">没有垃圾簡訊历史</string>
</resources>
9 changes: 9 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="time_format">dd/MMM/yyyy HH:mm:ss z</string>
<string name="content">"Content: "</string>
<string name="from">"From: "</string>
<string name="to">"To: "</string>
Expand Down Expand Up @@ -78,4 +79,12 @@
<string name="decline">"I'll decide later"</string>
<string name="visit_page">Visit the Privacy Terms page</string>
<string name="failed_resend">"Failed message resend "</string>
<string name="keywords">Keywords</string>
<string name="time">"Time: "</string>
<string name="spam_sms_keyword_title">SMS blacklist</string>
<string name="spam_keyword_dialog_title">Please use \";\" to split keywords.</string>
<string name="set_notification_listener">Notification listener</string>
<string name="get_spam_sms">/getspamsms - Get filtered spam messages</string>
<string name="spam_count_title">"Filtered spam: "</string>
<string name="no_spam_history">No spam history</string>
</resources>

0 comments on commit 506e417

Please sign in to comment.