删除:主动轮询远程 SmsHub Api(仅保留本地 HttpServer)

This commit is contained in:
pppscn 2022-04-03 17:17:40 +08:00
parent 31017609f9
commit f34b404adb
24 changed files with 129 additions and 494 deletions

View File

@ -88,20 +88,14 @@
- [x] 转发规则上支持配置正则替换内容
- [x] 转发到 Gotify发送通道自主推送通知服务
- [x] 被动接收本地 HttpServer
- [x] 主动轮询远程 SmsHub Api
- [x] 主动轮询远程 SmsHub Apiv2.5.0+已删除)
- [x] 适配暗夜模式
--------
### 应用截图:
| 前台服务常驻状态栏 | 应用主界面 | 发送通道 | 转发规则 |
| :--: | :--: | :--: | :--: |
| ![前台服务常驻状态栏](pic/taskbar.jpg "前台服务常驻状态栏") | ![应用主界面](pic/main.jpg "应用主界面") | ![发送通道](pic/sender.png "发送通道") | ![转发规则](pic/rule.jpg "转发规则") |
| 转发规则--短信转发 | 转发规则--通话记录 | 转发规则--APP通知 | 转发日志详情 |
| ![短信转发](pic/rule_sms.jpg "短信转发") | ![通话转发](pic/rule_call.jpg "通话转发") | ![通知转发](pic/rule_app.jpg "通知转发") | ![转发日志详情](pic/maindetail.jpg "转发日志详情") |
| 设置界面--总开关 | 设置界面--电量监控&保活措施 | 设置界面--个性设置 | 一键克隆(配置导出导入) |
| ![设置界面--总开关](pic/setting_1.jpg "设置界面--总开关") | ![设置界面--电量监控&保活措施](pic/setting_2.jpg "设置界面--电量监控&保活措施") | ![设置界面--个性设置](pic/setting_3.jpg "设置界面--个性设置") | ![配置导出导入功能(一键克隆)](pic/clone.jpg "配置导出导入功能(一键克隆)") |
| 前台服务常驻状态栏 | 应用主界面 | 发送通道 | 转发规则 | | :--: | :--: | :--: | :--: | | ![前台服务常驻状态栏](pic/taskbar.jpg "前台服务常驻状态栏") | ![应用主界面](pic/main.jpg "应用主界面") | ![发送通道](pic/sender.png "发送通道") | ![转发规则](pic/rule.jpg "转发规则") | | 转发规则--短信转发 | 转发规则--通话记录 | 转发规则--APP通知 | 转发日志详情 | | ![短信转发](pic/rule_sms.jpg "短信转发") | ![通话转发](pic/rule_call.jpg "通话转发") | ![通知转发](pic/rule_app.jpg "通知转发") | ![转发日志详情](pic/maindetail.jpg "转发日志详情") | | 设置界面--总开关 | 设置界面--电量监控&保活措施 | 设置界面--个性设置 | 一键克隆(配置导出导入) | | ![设置界面--总开关](pic/setting_1.jpg "设置界面--总开关") | ![设置界面--电量监控&保活措施](pic/setting_2.jpg "设置界面--电量监控&保活措施") | ![设置界面--个性设置](pic/setting_3.jpg "设置界面--个性设置") | ![配置导出导入功能(一键克隆)](pic/clone.jpg "配置导出导入功能(一键克隆)") |
更多截图参见 https://github.com/pppscn/SmsForwarder/wiki
@ -127,7 +121,7 @@
+ https://github.com/alibaba/fastjson (Json解析)
+ https://github.com/getActivity/XXPermissions (权限请求框架)
+ https://github.com/Xcreen/RestSMS 被动接收本地API方案
+ https://github.com/juancrescente/SMSHub 主动轮询远程API方案
+ ~~https://github.com/juancrescente/SMSHub 主动轮询远程API方案v2.5.0+删除~~
+ [<img src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg?_ga=2.126618957.1361252949.1638261367-1417196221.1635638144&_gl=1*1pfl3dq*_ga*MTQxNzE5NjIyMS4xNjM1NjM4MTQ0*_ga_V0XZL7QHEB*MTYzODMzMjA4OC43LjAuMTYzODMzMjA5Ny4w" alt="GitHub license" style="zoom:50%;" />](https://jb.gg/OpenSourceSupport) (License Certificate for JetBrains All Products Pack)
--------

View File

@ -1,7 +1,7 @@
![SmsForwarder](pic/SmsForwarder.png)
# SmsForwarder
[中文版](README.md)
[![GitHub release](https://img.shields.io/github/release/pppscn/SmsForwarder.svg)](https://github.com/pppscn/SmsForwarder/releases) [![GitHub stars](https://img.shields.io/github/stars/pppscn/SmsForwarder)](https://github.com/pppscn/SmsForwarder/stargazers) [![GitHub forks](https://img.shields.io/github/forks/pppscn/SmsForwarder)](https://github.com/pppscn/SmsForwarder/network/members) [![GitHub issues](https://img.shields.io/github/issues/pppscn/SmsForwarder)](https://github.com/pppscn/SmsForwarder/issues) [![GitHub license](https://img.shields.io/github/license/pppscn/SmsForwarder)](https://github.com/pppscn/SmsForwarder/blob/main/LICENSE)
@ -90,19 +90,14 @@ Benefit by simplicity:
- [x] Support for variables in regular expression of forwarding rules;
- [x] 转发到 Gotify发送通道自主推送通知服务
- [x] 被动接收本地 HttpServer
- [x] 主动轮询远程 SmsHub Api
- [x] 主动轮询远程 SmsHub Apiv2.5.0+已删除)
- [x] 适配暗夜模式
--------
### Screenshots :
| 前台服务常驻状态栏 | 应用主界面 | 发送通道 | 转发规则 |
| :--: | :--: | :--: | :--: |
| ![前台服务常驻状态栏](pic/taskbar.jpg "前台服务常驻状态栏") | ![应用主界面](pic/main.jpg "应用主界面") | ![发送通道](pic/sender.png "发送通道") | ![转发规则](pic/rule.jpg "转发规则") |
| 转发规则--短信转发 | 转发规则--通话记录 | 转发规则--APP通知 | 转发日志详情 |
| ![短信转发](pic/rule_sms.jpg "短信转发") | ![通话转发](pic/rule_call.jpg "通话转发") | ![通知转发](pic/rule_app.jpg "通知转发") | ![转发日志详情](pic/maindetail.jpg "转发日志详情") |
| 设置界面--总开关 | 设置界面--电量监控&保活措施 | 设置界面--个性设置 | 一键克隆(配置导出导入) |
| ![设置界面--总开关](pic/setting_1.jpg "设置界面--总开关") | ![设置界面--电量监控&保活措施](pic/setting_2.jpg "设置界面--电量监控&保活措施") | ![设置界面--个性设置](pic/setting_3.jpg "设置界面--个性设置") | ![配置导出导入功能(一键克隆)](pic/clone.jpg "配置导出导入功能(一键克隆)") |
| 前台服务常驻状态栏 | 应用主界面 | 发送通道 | 转发规则 | | :--: | :--: | :--: | :--: | | ![前台服务常驻状态栏](pic/taskbar.jpg "前台服务常驻状态栏") | ![应用主界面](pic/main.jpg "应用主界面") | ![发送通道](pic/sender.png "发送通道") | ![转发规则](pic/rule.jpg "转发规则") | | 转发规则--短信转发 | 转发规则--通话记录 | 转发规则--APP通知 | 转发日志详情 | | ![短信转发](pic/rule_sms.jpg "短信转发") | ![通话转发](pic/rule_call.jpg "通话转发") | ![通知转发](pic/rule_app.jpg "通知转发") | ![转发日志详情](pic/maindetail.jpg "转发日志详情") | | 设置界面--总开关 | 设置界面--电量监控&保活措施 | 设置界面--个性设置 | 一键克隆(配置导出导入) | | ![设置界面--总开关](pic/setting_1.jpg "设置界面--总开关") | ![设置界面--电量监控&保活措施](pic/setting_2.jpg "设置界面--电量监控&保活措施") | ![设置界面--个性设置](pic/setting_3.jpg "设置界面--个性设置") | ![配置导出导入功能(一键克隆)](pic/clone.jpg "配置导出导入功能(一键克隆)") |
更多截图参见 https://github.com/pppscn/SmsForwarder/wiki
@ -128,7 +123,7 @@ Benefit by simplicity:
+ https://github.com/alibaba/fastjson (json parsing)
+ https://github.com/getActivity/XXPermissions (permission requiring)
+ https://github.com/Xcreen/RestSMS被动接收本地API方案
+ https://github.com/juancrescente/SMSHub主动轮询远程API方案
+ ~~https://github.com/juancrescente/SMSHub主动轮询远程API方案v2.5.0+删除~~
+ [<img src="https://resources.jetbrains.com/storage/products/company/brand/logos/jb_beam.svg?_ga=2.126618957.1361252949.1638261367-1417196221.1635638144&_gl=1*1pfl3dq*_ga*MTQxNzE5NjIyMS4xNjM1NjM4MTQ0*_ga_V0XZL7QHEB*MTYzODMzMjA4OC43LjAuMTYzODMzMjA5Ny4w" alt="GitHub license" style="zoom:50%;" />](https://jb.gg/OpenSourceSupport) (License Certificate for JetBrains All Products Pack)
--------

View File

@ -36,7 +36,6 @@ import com.idormy.sms.forwarder.sender.BatteryReportCronTask;
import com.idormy.sms.forwarder.sender.HttpServer;
import com.idormy.sms.forwarder.sender.SendUtil;
import com.idormy.sms.forwarder.sender.SenderUtil;
import com.idormy.sms.forwarder.sender.SmsHubApiTask;
import com.idormy.sms.forwarder.service.BatteryService;
import com.idormy.sms.forwarder.service.FrontService;
import com.idormy.sms.forwarder.service.MusicService;
@ -139,16 +138,6 @@ public class MainActivity extends AppCompatActivity implements RefreshListView.I
}
}
//启用SmsHubApiTask
if (SettingUtil.getSwitchEnableSmsHubApi()) {
SmsHubApiTask.init(this);
try {
SmsHubApiTask.updateTimer();
} catch (Exception e) {
Log.e(TAG, "SmsHubApiTask:", e);
}
}
//电池状态定时推送
if (SettingUtil.getSwitchEnableBatteryCron()) {
try {

View File

@ -39,7 +39,6 @@ import com.idormy.sms.forwarder.receiver.RebootBroadcastReceiver;
import com.idormy.sms.forwarder.sender.BatteryReportCronTask;
import com.idormy.sms.forwarder.sender.HttpServer;
import com.idormy.sms.forwarder.sender.SenderUtil;
import com.idormy.sms.forwarder.sender.SmsHubApiTask;
import com.idormy.sms.forwarder.service.MusicService;
import com.idormy.sms.forwarder.utils.CommonUtil;
import com.idormy.sms.forwarder.utils.DbHelper;
@ -50,7 +49,6 @@ import com.idormy.sms.forwarder.utils.LogUtil;
import com.idormy.sms.forwarder.utils.OnePixelManager;
import com.idormy.sms.forwarder.utils.RuleUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.view.ClearEditText;
import com.idormy.sms.forwarder.view.StepBar;
import java.lang.reflect.Method;
@ -97,8 +95,6 @@ public class SettingActivity extends AppCompatActivity {
//HttpServer
switchEnableHttpServer(findViewById(R.id.switch_enable_http_server));
//SmsHubApiTask
editSmsHubConfig(findViewById(R.id.switch_enable_sms_hub), findViewById(R.id.editText_text_sms_hub_url));
//监听电池状态变化
switchBatteryReceiver(findViewById(R.id.switch_battery_receiver));
@ -352,26 +348,6 @@ public class SettingActivity extends AppCompatActivity {
}
}
//SmsHubApiTask
@SuppressLint("UseSwitchCompatOrMaterialCode")
private void editSmsHubConfig(Switch switch_enable_send_sms, ClearEditText editText_text_send_sms) {
switch_enable_send_sms.setChecked(SettingUtil.getSwitchEnableSmsHubApi());
switch_enable_send_sms.setOnCheckedChangeListener((buttonView, isChecked) -> {
String webServer = editText_text_send_sms.getText().trim();
if (isChecked && !CommonUtil.checkUrl(webServer, false)) {
HttpUtil.Toast(TAG, getString(R.string.invalid_webserver));
switch_enable_send_sms.setChecked(false);
return;
}
SettingUtil.switchEnableSmsHubApi(isChecked);
Log.d(TAG, "switchEnableSendApi:" + isChecked);
SmsHubApiTask.updateTimer();
});
editText_text_send_sms.setText(SettingUtil.getSmsHubApiUrl());
editText_text_send_sms.setOnEditInputListener(content -> SettingUtil.smsHubApiUrl(content.trim()));
}
//HttpServer
@SuppressLint("UseSwitchCompatOrMaterialCode")
private void switchEnableHttpServer(Switch switch_enable_http_server) {

View File

@ -24,7 +24,6 @@ public class CloneInfoVo implements Serializable {
private boolean callType3;
private boolean enableAppNotify;
private boolean cancelAppNotify;
private String smsHubApiUrl;
private int batteryLevelAlarmMin;
private int batteryLevelAlarmMax;
private boolean batteryLevelAlarmOnce;
@ -48,7 +47,6 @@ public class CloneInfoVo implements Serializable {
", callType3=" + callType3 +
", enableAppNotify=" + enableAppNotify +
", cancelAppNotify=" + cancelAppNotify +
", smsHubApiUrl=" + smsHubApiUrl +
", batteryLevelAlarmMin=" + batteryLevelAlarmMin +
", batteryLevelAlarmMax=" + batteryLevelAlarmMax +
", batteryLevelAlarmOnce=" + batteryLevelAlarmOnce +

View File

@ -10,7 +10,6 @@ public class ResVo<T> {
private int code;
private String msg;
private String auth;
private SmsHubVo heartbeat;
private T data;
public ResVo<T> setSuessces(T data) {

View File

@ -3,7 +3,6 @@ package com.idormy.sms.forwarder.model.vo;
import androidx.annotation.NonNull;
import com.alibaba.fastjson.JSON;
import com.idormy.sms.forwarder.sender.SmsHubApiTask;
import com.idormy.sms.forwarder.utils.PhoneUtils;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SimUtil;
@ -87,7 +86,6 @@ public class SmsHubVo implements Serializable {
deviInfoMap.put("imei", PhoneUtils.getIMEI());
deviInfoMap.put("SDKVersion", PhoneUtils.getSDKVersion() + "");
deviInfoMap.put("Version", SettingUtil.getVersionName());
deviInfoMap.put("heartbeat", SmsHubApiTask.DELAY_SECONDS + "");
cache.put(key, deviInfoMap);
return deviInfoMap;
}

View File

@ -7,11 +7,15 @@ import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.util.IOUtils;
import com.idormy.sms.forwarder.model.LogModel;
import com.idormy.sms.forwarder.model.vo.ResVo;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.utils.CloneUtils;
import com.idormy.sms.forwarder.utils.HttpUtil;
import com.idormy.sms.forwarder.utils.LogUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import com.idormy.sms.forwarder.utils.SimUtil;
import com.idormy.sms.forwarder.utils.SmsUtil;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
@ -38,47 +42,21 @@ import javax.servlet.http.HttpServletResponse;
@MultipartConfig
public class BaseServlet extends HttpServlet {
public static final int BUFFER_SIZE = 1 << 12;
public static final String CLONE_PATH = "/clone";
public static final String SMSHUB_PATH = "/send_api";
private static final long serialVersionUID = 1L;
private static final String TAG = "BaseServlet";
public static final SmsHubActionHandler.SmsHubMode smsHubMode = SmsHubActionHandler.SmsHubMode.server;
public BaseServlet(String path, Context context) {
this.path = path;
this.context = context;
SettingUtil.init(context);
}
public static final int BUFFER_SIZE = 1 << 12;
private static final long serialVersionUID = 1L;
public static final long RULE_ID = -999L;
public static final String SMSHUB_PATH = "/send_api";
public static final String CLONE_PATH = "/clone";
private final String path;
@SuppressLint("StaticFieldLeak")
private final Context context;
public static void addServlet(Server jettyServer, Context context) {
ServletContextHandler contextHandler = new ServletContextHandler();
addHolder(contextHandler, new BaseServlet(BaseServlet.CLONE_PATH, context));
addHolder(contextHandler, new BaseServlet(BaseServlet.SMSHUB_PATH, context));
// addholder(contextHandler, new BaseServlet("/", context));
jettyServer.setHandler(contextHandler);
}
public static String read(Reader reader) throws IOException {
char[] buffer = new char[BUFFER_SIZE];
int size;
StringBuilder sb = new StringBuilder();
while ((size = reader.read(buffer)) != -1) {
char[] chars = new char[size];
System.arraycopy(buffer, 0, chars, 0, size);
sb.append(chars);
}
return sb.toString();
}
private static void addHolder(ServletContextHandler servletContextHandler, BaseServlet baseServlet) {
ServletHolder servletHolder = new ServletHolder(baseServlet);
servletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement(baseServlet.getContext().getCacheDir().getAbsolutePath() + File.pathSeparator + "jettyServer"));
servletContextHandler.addServlet(servletHolder, baseServlet.getPath());
public BaseServlet(String path, Context context) {
this.path = path;
this.context = context;
SettingUtil.init(context);
}
public Context getContext() {
@ -89,6 +67,14 @@ public class BaseServlet extends HttpServlet {
return path;
}
public static void addServlet(Server jettyServer, Context context) {
ServletContextHandler contextHandler = new ServletContextHandler();
addHolder(contextHandler, new BaseServlet(BaseServlet.CLONE_PATH, context));
addHolder(contextHandler, new BaseServlet(BaseServlet.SMSHUB_PATH, context));
// addholder(contextHandler, new BaseServlet("/", context));
jettyServer.setHandler(contextHandler);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
String msg = "HTTP method POST is not supported by this URL";
@ -123,18 +109,16 @@ public class BaseServlet extends HttpServlet {
try {
String read = read(reader);
Log.i(TAG, "Request message:" + read);
SmsHubVo smsHubVo = SmsHubVo.heartbeatInstance(SmsHubActionHandler.getData(smsHubMode));
ResVo<List<SmsHubVo>> result = ResVo.suessces(null);
if (StringUtil.isNotBlank(read)) {
ResVo<List<SmsHubVo>> obj = JSON.parseObject(read, new TypeReference<ResVo<List<SmsHubVo>>>() {
}.getType());
List<SmsHubVo> data = obj.getData();
for (SmsHubVo vo : data) {
SmsHubActionHandler.handle(TAG, vo);
sendSms(TAG, vo);
}
result.setData(data);
}
result.setHeartbeat(smsHubVo);
resp.setContentType("application/json;charset=utf-8");
StringBuilder sb = new StringBuilder();
int i = 0;
@ -158,14 +142,6 @@ public class BaseServlet extends HttpServlet {
}
}
private void printErrMsg(HttpServletResponse resp, PrintWriter writer, Exception e) {
String text = "Internal server error: " + e.getMessage();
Log.e(TAG, text);
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("application/json;charset=utf-8");
writer.println(JSON.toJSONString(ResVo.error(text)));
}
//一键克隆查询接口
private void clone_api(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setCharacterEncoding("utf-8");
@ -202,4 +178,83 @@ public class BaseServlet extends HttpServlet {
}
}
//短信列表api
//联系人api
//查询通话记录api
//查询电量api
//规则列表api增删改查
//发送通道api增删改查
/////////////////////////////////////////////////////////////////////////////////////////////////////////
private static String read(Reader reader) throws IOException {
char[] buffer = new char[BUFFER_SIZE];
int size;
StringBuilder sb = new StringBuilder();
while ((size = reader.read(buffer)) != -1) {
char[] chars = new char[size];
System.arraycopy(buffer, 0, chars, 0, size);
sb.append(chars);
}
return sb.toString();
}
private static void addHolder(ServletContextHandler servletContextHandler, BaseServlet baseServlet) {
ServletHolder servletHolder = new ServletHolder(baseServlet);
servletHolder.getRegistration().setMultipartConfig(new MultipartConfigElement(baseServlet.getContext().getCacheDir().getAbsolutePath() + File.pathSeparator + "jettyServer"));
servletContextHandler.addServlet(servletHolder, baseServlet.getPath());
}
private void printErrMsg(HttpServletResponse resp, PrintWriter writer, Exception e) {
String text = "Internal server error: " + e.getMessage();
Log.e(TAG, text);
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType("application/json;charset=utf-8");
writer.println(JSON.toJSONString(ResVo.error(text)));
}
private static void sendSms(String tag, SmsHubVo vo) {
boolean failure = true;
String msg = "";
Long logId = null;
try {
if (SmsHubVo.Action.send.code().equals(vo.getAction())) {
vo.setType(SmsHubVo.Type.sms.code());
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
int subscriptionIdBySimId = SimUtil.getSubscriptionIdBySimId(Integer.parseInt(vo.getChannel()) - 1);
msg = SmsUtil.sendSms(subscriptionIdBySimId, vo.getTarget(), vo.getContent());
String simInfo = "SIM" + (subscriptionIdBySimId + 1);
vo.setChannel(simInfo);
logId = LogUtil.addLog(new LogModel(vo.getType(), vo.getTarget(), vo.getContent(), simInfo, RULE_ID));
if (msg == null) {
failure = false;
HttpUtil.Toast(tag, "短信发送成功");
Log.i(tag, "短信发送成功");
vo.setAction(SmsHubVo.Action.suessces.code());
LogUtil.updateLog(logId, 2, SmsHubVo.Action.suessces.code());
}
} else {
msg = "api<22";
}
}
} catch (Exception e) {
msg += e.getMessage();
e.printStackTrace();
}
if (failure) {
msg = "短信发送失败:" + msg;
HttpUtil.Toast(tag, msg);
Log.i(tag, msg);
vo.setAction(SmsHubVo.Action.failure.code());
vo.setErrMsg(msg);
if (logId != null) {
LogUtil.updateLog(logId, 0, msg);
}
}
}
}

View File

@ -10,7 +10,6 @@ import android.util.Log;
import com.idormy.sms.forwarder.R;
import com.idormy.sms.forwarder.model.CallInfo;
import com.idormy.sms.forwarder.model.PhoneBookEntity;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.model.vo.SmsVo;
import com.idormy.sms.forwarder.sender.SendUtil;
import com.idormy.sms.forwarder.utils.CommonUtil;
@ -18,7 +17,6 @@ import com.idormy.sms.forwarder.utils.ContactHelper;
import com.idormy.sms.forwarder.utils.PhoneUtils;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SimUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -130,11 +128,6 @@ public class PhoneStateReceiver extends BroadcastReceiver {
SmsVo smsVo = new SmsVo(phoneNumber, getTypeText(context, type, name, viaNumber), new Date(), simInfo);
Log.d(TAG, "send_msg" + smsVo);
SendUtil.send_msg(context, smsVo, simId, "call");
//SmsHubApi
if (SettingUtil.getSwitchEnableSmsHubApi()) {
SmsHubActionHandler.putData(new SmsHubVo(SmsHubVo.Type.phone, simId, getTypeText(context, type, name, viaNumber), phoneNumber));
}
}
//获取通话类型1.呼入 2.呼出 3.未接

View File

@ -10,12 +10,10 @@ import android.util.Log;
import androidx.annotation.RequiresApi;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.model.vo.SmsVo;
import com.idormy.sms.forwarder.sender.SendUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SimUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import java.util.ArrayList;
import java.util.Date;
@ -93,15 +91,6 @@ public class SmsBroadcastReceiver extends BroadcastReceiver {
Log.d(TAG, "SMS: " + smsVoList);
SendUtil.send_msg_list(context, smsVoList, simId, "sms");
//SmsHubApi
if (SettingUtil.getSwitchEnableSmsHubApi()) {
List<SmsHubVo> smsHubVos = new ArrayList<>();
for (String mobile : mobileToContent.keySet()) {
smsHubVos.add(new SmsHubVo(SmsHubVo.Type.sms, simId, mobileToContent.get(mobile), mobile));
}
SmsHubActionHandler.putData(smsHubVos.toArray(new SmsHubVo[0]));
}
}
} catch (Throwable throwable) {

View File

@ -6,11 +6,9 @@ import android.content.IntentFilter;
import android.util.Log;
import com.idormy.sms.forwarder.MyApplication;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.model.vo.SmsVo;
import com.idormy.sms.forwarder.utils.BatteryUtils;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import java.util.Calendar;
import java.util.Date;
@ -91,11 +89,6 @@ public class BatteryReportCronTask {
try {
SmsVo smsVo = new SmsVo("88888888", msg, new Date(), "电池状态定时推送");
SendUtil.send_msg(context, smsVo, 1, "app");
//SmsHubApi
if (SettingUtil.getSwitchEnableSmsHubApi()) {
SmsHubActionHandler.putData(new SmsHubVo(SmsHubVo.Type.phone, null, msg, "电池状态定时推送"));
}
} catch (Exception e) {
Log.e(TAG, "sendMessage e:" + e.getMessage());
}

View File

@ -11,11 +11,9 @@ import com.idormy.sms.forwarder.receiver.BaseServlet;
import com.idormy.sms.forwarder.utils.Define;
import com.idormy.sms.forwarder.utils.NetUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import org.eclipse.jetty.server.Server;
public class HttpServer {
private static Boolean hasInit = false;
private static Server jettyServer;
@ -23,7 +21,6 @@ public class HttpServer {
private static Context context;
private static long ts = 0L;
@SuppressLint("HandlerLeak")
public static void init(Context context) {
//noinspection SynchronizeOnNonFinalField
@ -32,7 +29,6 @@ public class HttpServer {
hasInit = true;
HttpServer.context = context;
SmsHubActionHandler.init(context);
jettyServer = new Server(Define.HTTP_SERVER_PORT);
BaseServlet.addServlet(jettyServer, context);
}
@ -49,6 +45,17 @@ public class HttpServer {
return false;
}
/**
* Checks if Jetty is stopping
* boolean - True when server is stopping
*/
private synchronized static Boolean asStopp() {
if (jettyServer != null) {
return !(jettyServer.isRunning() || jettyServer.isStopping());
}
return true;
}
public synchronized static boolean update() {
//非WiFi网络下不可启用
if (NetUtil.NETWORK_WIFI != NetUtil.getNetWorkStatus()) {
@ -76,42 +83,25 @@ public class HttpServer {
return true;
}
/**
* Checks if Jetty is stopping
* boolean - True when server is stopping
*/
private synchronized static Boolean asStopp() {
if (jettyServer != null) {
return !(jettyServer.isRunning() || jettyServer.isStopping());
} else {
return true;
}
}
private static void start() {
stop();
Log.i("HttpServer", "start");
//new Thread(() -> {
try {
//Start Jetty
jettyServer.start();
//jettyServer.join();
} catch (Exception e) {
e.printStackTrace();
}
//}).start();
}
private static void stop() {
if (Boolean.FALSE.equals(asStopp())) {
try {
if (jettyServer != null) {
jettyServer.stop();
//jettyServer = new Server(port);
}
} catch (Exception e) {
e.printStackTrace();
if (asStopp()) return;
try {
if (jettyServer != null) {
jettyServer.stop();
}
} catch (Exception e) {
e.printStackTrace();
}
}

View File

@ -1,105 +0,0 @@
package com.idormy.sms.forwarder.sender;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.utils.HttpUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import java.util.List;
import java.util.Objects;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* 主动发送短信轮询任务
*
* @author xxc
* 2022/1/10 9:53
*/
@SuppressWarnings("CodeBlock2Expr")
public class SmsHubApiTask extends TimerTask {
private static Boolean hasInit = false;
public static final long DELAY_SECONDS = 30;
private static final String TAG = "SmsHubApiTask";
private static Timer sendApiTimer;
@SuppressLint("StaticFieldLeak")
private static Context context;
private static final SmsHubActionHandler.SmsHubMode smsHubMode = SmsHubActionHandler.SmsHubMode.client;
@SuppressLint("HandlerLeak")
public static void init(Context context) {
//noinspection SynchronizeOnNonFinalField
synchronized (hasInit) {
if (hasInit) return;
hasInit = true;
SmsHubApiTask.context = context;
SmsHubActionHandler.init(SmsHubApiTask.context);
}
}
@Override
public void run() {
List<SmsHubVo> data = SmsHubActionHandler.getData(smsHubMode);
SmsHubVo smsHubVo = SmsHubVo.heartbeatInstance(data);
String url = SettingUtil.getSmsHubApiUrl();
boolean asRetry = data != null && data.size() > 0;
AtomicBoolean isSusess = new AtomicBoolean(false);
Runnable runnable = () -> {
HttpUtil.asyncPostJson(TAG, url, smsHubVo, response -> {
//HttpUtil.Toast(TAG, "Response" + response.code() + "" + responseStr);
if (response.code() == 200) {
isSusess.set(true);
String responseStr = Objects.requireNonNull(response.body()).string();
List<SmsHubVo> vos = JSON.parseArray(responseStr, SmsHubVo.class);
for (SmsHubVo vo : vos) {
SmsHubActionHandler.handle(TAG, vo);
}
SmsHubActionHandler.putData(smsHubMode, vos.toArray(new SmsHubVo[0]));
}
}, null, asRetry);
};
if (asRetry) {
new Thread(runnable).start();
} else {
runnable.run();
}
}
public static void updateTimer() {
cancelTimer();
if (SettingUtil.getSwitchEnableSmsHubApi()) {
SmsHubVo.getDevInfoMap(true);
startTimer();
} else {
Log.d(TAG, "Cancel SmsHubApiTaskTimer");
HttpUtil.Toast(TAG, "Cancel SmsHubApiTaskTimer");
}
}
private static void cancelTimer() {
if (sendApiTimer != null) {
sendApiTimer.cancel();
sendApiTimer = null;
}
}
private static void startTimer() {
Log.d(TAG, "Start SmsHubApiTimer");
if (SettingUtil.getSwitchEnableSmsHubApi()) {
long seconds = SmsHubApiTask.DELAY_SECONDS;
Log.d(TAG, "SmsHubApiTimer started " + seconds);
sendApiTimer = new Timer("SmsHubApiTimer", true);
sendApiTimer.schedule(new SmsHubApiTask(), 3000, seconds * 1000);
}
}
}

View File

@ -10,12 +10,10 @@ import android.os.IBinder;
import android.util.Log;
import com.idormy.sms.forwarder.MyApplication;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.model.vo.SmsVo;
import com.idormy.sms.forwarder.sender.SendUtil;
import com.idormy.sms.forwarder.utils.BatteryUtils;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import java.util.Date;
@ -119,11 +117,6 @@ public class BatteryService extends Service {
SmsVo smsVo = new SmsVo("88888888", msg, new Date(), "电池状态监听");
Log.d(TAG, "send_msg" + smsVo);
SendUtil.send_msg(context, smsVo, 1, "app");
//SmsHubApi
if (SettingUtil.getSwitchEnableSmsHubApi()) {
SmsHubActionHandler.putData(new SmsHubVo(SmsHubVo.Type.phone, null, msg, "电池状态监听"));
}
} catch (Exception e) {
Log.e(TAG, "getLog e:" + e.getMessage());
}

View File

@ -9,12 +9,10 @@ import android.util.Log;
import androidx.annotation.RequiresApi;
import com.idormy.sms.forwarder.MyApplication;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.model.vo.SmsVo;
import com.idormy.sms.forwarder.sender.SendUtil;
import com.idormy.sms.forwarder.utils.CommonUtil;
import com.idormy.sms.forwarder.utils.SettingUtil;
import com.idormy.sms.forwarder.utils.SmsHubActionHandler;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -94,11 +92,6 @@ public class NotifyService extends NotificationListenerService {
SmsVo smsVo = new SmsVo(packageName, text, new Date(), title);
Log.d(TAG, "send_msg" + smsVo);
SendUtil.send_msg(this, smsVo, 1, "app");
//SmsHubApi
if (SettingUtil.getSwitchEnableSmsHubApi()) {
SmsHubActionHandler.putData(new SmsHubVo(SmsHubVo.Type.app, null, text, packageName));
}
} catch (Exception e) {
Log.e(TAG, "onNotificationPosted:", e);
}

View File

@ -26,7 +26,6 @@ public class CloneUtils {
cloneInfo.setCallType3(SettingUtil.getSwitchCallType3());
cloneInfo.setEnableAppNotify(SettingUtil.getSwitchEnableAppNotify());
cloneInfo.setCancelAppNotify(SettingUtil.getSwitchCancelAppNotify());
cloneInfo.setSmsHubApiUrl(SettingUtil.getSmsHubApiUrl());
cloneInfo.setBatteryLevelAlarmMin(SettingUtil.getBatteryLevelAlarmMin());
cloneInfo.setBatteryLevelAlarmMax(SettingUtil.getBatteryLevelAlarmMax());
cloneInfo.setBatteryLevelAlarmOnce(SettingUtil.getBatteryLevelAlarmOnce());
@ -56,7 +55,6 @@ public class CloneUtils {
SettingUtil.switchCallType3(cloneInfoVo.isCallType3());
SettingUtil.switchEnableAppNotify(cloneInfoVo.isEnableAppNotify());
SettingUtil.switchCancelAppNotify(cloneInfoVo.isCancelAppNotify());
SettingUtil.smsHubApiUrl(cloneInfoVo.getSmsHubApiUrl());
SettingUtil.setBatteryLevelAlarmMin(cloneInfoVo.getBatteryLevelAlarmMin());
SettingUtil.setBatteryLevelAlarmMax(cloneInfoVo.getBatteryLevelAlarmMax());
SettingUtil.switchBatteryLevelAlarmOnce(cloneInfoVo.isBatteryLevelAlarmOnce());

View File

@ -30,8 +30,6 @@ public class Define {
public static final String SP_MSG_KEY_STRING_BATTERY_LEVEL_ONCE = "tsms_msg_key_string_battery_level_once";
public static final String SP_MSG_KEY_STRING_RETRY_TIMES = "tsms_msg_key_retry_times";
public static final String SP_MSG_KEY_STRING_DELAY_TIME = "tsms_msg_key_delay_time";
public static final String SP_MSG_KEY_STRING_ENABLE_SMSHUB_API = "tsms_msg_key_string_enable_smshub_api";
public static final String SP_MSG_KEY_STRING_SMSHUB_API_URL = "tsms_msg_key_string_smshub_api_url";
public static final String SP_MSG_KEY_STRING_ENABLE_HTTP_SERVER = "tsms_msg_key_string_enable_http_server";
public static final String SP_MSG_KEY_STRING_ENABLE_CALL_TYPE_1 = "tsms_msg_key_string_enable_call_type_1";
public static final String SP_MSG_KEY_STRING_ENABLE_CALL_TYPE_2 = "tsms_msg_key_string_enable_call_type_2";

View File

@ -3,6 +3,7 @@ package com.idormy.sms.forwarder.utils;
import java.util.Objects;
public class Lamda {
@SuppressWarnings("RedundantThrows")
public interface Consumer<T> extends Func<T, T> {
void accept(T t) throws Exception;

View File

@ -226,14 +226,6 @@ public class SettingUtil {
sp_setting.edit().putString(key, value).apply();
}
public static void switchEnableSmsHubApi(Boolean enable) {
sp_setting.edit().putBoolean(Define.SP_MSG_KEY_STRING_ENABLE_SMSHUB_API, enable).apply();
}
public static boolean getSwitchEnableSmsHubApi() {
return sp_setting.getBoolean(Define.SP_MSG_KEY_STRING_ENABLE_SMSHUB_API, false);
}
public static void switchEnableHttpServer(Boolean enable) {
sp_setting.edit().putBoolean(Define.SP_MSG_KEY_STRING_ENABLE_HTTP_SERVER, enable).apply();
}
@ -242,14 +234,6 @@ public class SettingUtil {
return sp_setting.getBoolean(Define.SP_MSG_KEY_STRING_ENABLE_HTTP_SERVER, false);
}
public static void smsHubApiUrl(String url) {
sp_setting.edit().putString(Define.SP_MSG_KEY_STRING_SMSHUB_API_URL, url).apply();
}
public static String getSmsHubApiUrl() {
return sp_setting.getString(Define.SP_MSG_KEY_STRING_SMSHUB_API_URL, "");
}
public static void switchCallType1(Boolean switchCallType) {
sp_setting.edit().putBoolean(Define.SP_MSG_KEY_STRING_ENABLE_CALL_TYPE_1, switchCallType).apply();
}

View File

@ -1,144 +0,0 @@
package com.idormy.sms.forwarder.utils;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import com.alibaba.fastjson.JSON;
import com.idormy.sms.forwarder.model.LogModel;
import com.idormy.sms.forwarder.model.vo.SmsHubVo;
import com.idormy.sms.forwarder.sender.SmsHubApiTask;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
public class SmsHubActionHandler {
public static final long RULE_ID = -999L;
private static Boolean hasInit = false;
private static ConcurrentHashMap<SmsHubMode, List<SmsHubVo>> cache;
public enum SmsHubMode {
server, client
}
@SuppressLint("StaticFieldLeak")
private static Context context;
@SuppressLint("HandlerLeak")
public static void init(Context context) {
//noinspection SynchronizeOnNonFinalField
synchronized (hasInit) {
if (hasInit) return;
hasInit = true;
SmsHubActionHandler.context = context;
cache = new ConcurrentHashMap<>();
for (SmsHubMode smsHubMode : SmsHubMode.values()) {
cache.put(smsHubMode, new ArrayList<>());
}
}
}
public static synchronized int size(SmsHubMode smsHubMode) {
return Objects.requireNonNull(cache.get(smsHubMode)).size();
}
public static synchronized List<SmsHubVo> getData(SmsHubMode smsHubMode) {
List<SmsHubVo> smsHubVoList = cache.get(smsHubMode);
assert smsHubVoList != null;
if (smsHubVoList.size() > 0) {
cache.put(smsHubMode, new ArrayList<>());
return smsHubVoList;
} else {
return null;
}
}
private static long falg = System.currentTimeMillis();
public static synchronized void putData(SmsHubMode smsHubMode, SmsHubVo... smsHubVos) {
if (isEnable(smsHubMode)) {
if (smsHubMode == SmsHubMode.server) {
long l = falg;
falg = System.currentTimeMillis();
if (System.currentTimeMillis() - l > SmsHubApiTask.DELAY_SECONDS * 3) {
return;
}
}
Objects.requireNonNull(cache.get(smsHubMode)).addAll(Arrays.asList(smsHubVos));
}
}
public static synchronized void putData(SmsHubVo... smsHubVos) {
for (SmsHubMode smsHubMode : SmsHubMode.values()) {
putData(smsHubMode, smsHubVos);
}
}
private static boolean isEnable(SmsHubMode smsHubMode) {
boolean enable = false;
if (smsHubMode == SmsHubMode.client) {
enable = SettingUtil.getSwitchEnableSmsHubApi();
} else if (smsHubMode == SmsHubMode.server) {
enable = SettingUtil.getSwitchEnableHttpServer();
}
return enable;
}
public static void handle(String tag, SmsHubVo vo) {
Log.i(tag, JSON.toJSONString(vo));
String action = vo.getAction();
if (SmsHubVo.Action.send.code().equals(action)) {
send(tag, vo);
} else {
String errMsg = "暂不支持的action[" + action + "]";
vo.setErrMsg(errMsg);
vo.setAction(SmsHubVo.Action.failure.code());
}
vo.setTs(Long.toString(System.currentTimeMillis()));
}
public static void send(String tag, SmsHubVo vo) {
boolean failure = true;
String msg = "";
Long logId = null;
try {
if (SmsHubVo.Action.send.code().equals(vo.getAction())) {
vo.setType(SmsHubVo.Type.sms.code());
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
int subscriptionIdBySimId = SimUtil.getSubscriptionIdBySimId(Integer.parseInt(vo.getChannel()) - 1);
msg = SmsUtil.sendSms(subscriptionIdBySimId, vo.getTarget(), vo.getContent());
String simInfo = "SIM" + (subscriptionIdBySimId + 1);
vo.setChannel(simInfo);
logId = LogUtil.addLog(new LogModel(vo.getType(), vo.getTarget(), vo.getContent(), simInfo, RULE_ID));
if (msg == null) {
failure = false;
HttpUtil.Toast(tag, "短信发送成功");
Log.i(tag, "短信发送成功");
vo.setAction(SmsHubVo.Action.suessces.code());
LogUtil.updateLog(logId, 2, SmsHubVo.Action.suessces.code());
}
} else {
msg = "api<22";
}
}
} catch (Exception e) {
msg += e.getMessage();
e.printStackTrace();
}
if (failure) {
msg = "短信发送失败:" + msg;
HttpUtil.Toast(tag, msg);
Log.i(tag, msg);
vo.setAction(SmsHubVo.Action.failure.code());
vo.setErrMsg(msg);
if (logId != null) {
LogUtil.updateLog(logId, 0, msg);
}
}
}
}

View File

@ -18,7 +18,6 @@
android:orientation="horizontal">
<RadioButton
android:id="@+id/btnTypeNetwork"
style="@style/select_style"
android:checked="true"
android:minWidth="0dp"

View File

@ -312,53 +312,6 @@
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:background="@color/setting_bar_color"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="15dp">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/send_sms_config_title"
android:textStyle="bold"
tools:ignore="RelativeOverlap" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/send_sms_config_title2"
android:textSize="9sp"
tools:ignore="SmallSp" />
<com.idormy.sms.forwarder.view.ClearEditText
android:id="@+id/editText_text_sms_hub_url"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:hint="http://xxx.com/send_api"
app:showEye="true" />
</LinearLayout>
<Switch
android:id="@+id/switch_enable_sms_hub"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:textSize="16sp"
tools:ignore="UseSwitchCompatOrMaterialXml" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

View File

@ -290,8 +290,6 @@
<string name="proxy_authenticator">Proxy Authenticator</string>
<string name="username">Username</string>
<string name="password">Password</string>
<string name="send_sms_config_title">SmsHub client Mode</string>
<string name="send_sms_config_title2">Execute the operation returned by the interface</string>
<string name="privacy_policy">Privacy Policy</string>
<string name="agree">Agree</string>
<string name="refuse">Refuse</string>

View File

@ -289,8 +289,6 @@
<string name="proxy_authenticator">代理身份验证</string>
<string name="username">用户</string>
<string name="password">密码</string>
<string name="send_sms_config_title">主动轮询远程 SmsHubApi</string>
<string name="send_sms_config_title2">请先填写服务端地址再启动服务APP轮询执行接口返回的操作</string>
<string name="privacy_policy">隐私政策</string>
<string name="agree">同意</string>
<string name="refuse">拒绝</string>