Compare commits
3 Commits
976bb6316b
...
ebd667f391
| Author | SHA1 | Date |
|---|---|---|
|
|
ebd667f391 | |
|
|
a5b306a4a2 | |
|
|
acf592153e |
|
|
@ -10,35 +10,24 @@ import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import androidx.core.app.NotificationCompat;
|
import androidx.core.app.NotificationCompat;
|
||||||
|
|
||||||
import com.google.gson.Gson;
|
|
||||||
import com.google.gson.JsonSyntaxException;
|
|
||||||
import com.nbee.echolink.R;
|
import com.nbee.echolink.R;
|
||||||
import com.nbee.echolink.broadcast.HeartbeatAlarmReceiver;
|
import com.nbee.echolink.broadcast.HeartbeatAlarmReceiver;
|
||||||
import com.nbee.echolink.model.CallInfo;
|
import com.nbee.echolink.model.CallInfo;
|
||||||
import com.nbee.echolink.model.DeviceInfo;
|
|
||||||
import com.nbee.echolink.model.SMSInfo;
|
import com.nbee.echolink.model.SMSInfo;
|
||||||
import com.nbee.echolink.model.WeChatMsg;
|
import com.nbee.echolink.model.WeChatMsg;
|
||||||
import com.nbee.echolink.response.ApiResponse;
|
import com.nbee.echolink.utils.NetworkUtils;
|
||||||
import com.nbee.echolink.utils.DeviceInfoUtils;
|
|
||||||
import com.nbee.echolink.utils.NetworkUtil;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import okhttp3.MediaType;
|
|
||||||
import okhttp3.OkHttpClient;
|
|
||||||
import okhttp3.Request;
|
|
||||||
import okhttp3.RequestBody;
|
|
||||||
import timber.log.Timber;
|
import timber.log.Timber;
|
||||||
|
|
||||||
public class MonitorService extends Service {
|
public class MonitorService extends Service {
|
||||||
private NetworkUtil networkUtil;
|
private NetworkUtils networkUtils;
|
||||||
private static final String TAG = "MonitorService";
|
private static final String TAG = "MonitorService";
|
||||||
private String lastPhoneNumber = null;
|
private String lastPhoneNumber = null;
|
||||||
private long lastCallTime = 0;
|
private long lastCallTime = 0;
|
||||||
|
|
@ -48,7 +37,7 @@ public class MonitorService extends Service {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
Timber.d("监控服务 onCreate");
|
Timber.d("监控服务 onCreate");
|
||||||
startForegroundService();
|
startForegroundService();
|
||||||
networkUtil = new NetworkUtil(this);
|
networkUtils = new NetworkUtils(this);
|
||||||
scheduleHeartbeat(this); // 启动心跳定时任务
|
scheduleHeartbeat(this); // 启动心跳定时任务
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import android.util.Log;
|
||||||
|
|
||||||
import com.nbee.echolink.model.WeChatMsg;
|
import com.nbee.echolink.model.WeChatMsg;
|
||||||
import com.nbee.echolink.utils.HandleNoticeUtils;
|
import com.nbee.echolink.utils.HandleNoticeUtils;
|
||||||
import com.nbee.echolink.utils.NetworkUtil;
|
import com.nbee.echolink.utils.NetworkUtils;
|
||||||
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
@ -27,7 +27,7 @@ public class NotificationListener extends NotificationListenerService {
|
||||||
private static final List<String> notAllowList = Arrays.asList("com.github.kr328.clash","com.google.android.dialer",
|
private static final List<String> notAllowList = Arrays.asList("com.github.kr328.clash","com.google.android.dialer",
|
||||||
"com.google.android.apps.messaging");
|
"com.google.android.apps.messaging");
|
||||||
private HandleNoticeUtils handleNoticeUtils;
|
private HandleNoticeUtils handleNoticeUtils;
|
||||||
private NetworkUtil networkUtil;
|
private NetworkUtils networkUtil;
|
||||||
private HashMap<String, Long> recentLogs = new HashMap<>();
|
private HashMap<String, Long> recentLogs = new HashMap<>();
|
||||||
private Handler logCleanerHandler = new Handler(Looper.getMainLooper());
|
private Handler logCleanerHandler = new Handler(Looper.getMainLooper());
|
||||||
|
|
||||||
|
|
@ -35,7 +35,7 @@ public class NotificationListener extends NotificationListenerService {
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
handleNoticeUtils = new HandleNoticeUtils(this);
|
handleNoticeUtils = new HandleNoticeUtils(this);
|
||||||
networkUtil = new NetworkUtil(this);
|
networkUtil = new NetworkUtils(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,105 @@
|
||||||
|
package com.nbee.echolink.utils;
|
||||||
|
import android.content.Context;
|
||||||
|
import android.os.Handler;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.nbee.echolink.R;
|
||||||
|
import com.nbee.echolink.model.CallInfo;
|
||||||
|
import com.nbee.echolink.model.SMSInfo;
|
||||||
|
import com.nbee.echolink.model.WeChatMsg;
|
||||||
|
import com.nbee.echolink.response.ApiResponse;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import okhttp3.MediaType;
|
||||||
|
import okhttp3.OkHttpClient;
|
||||||
|
import okhttp3.Request;
|
||||||
|
import okhttp3.RequestBody;
|
||||||
|
import timber.log.Timber;
|
||||||
|
|
||||||
|
public class NetworkUtils {
|
||||||
|
private final String TAG = "NetworkUtils";
|
||||||
|
private final String callApiUrl;
|
||||||
|
private final String smsApiUrl;
|
||||||
|
private final String wechatApiUrl;
|
||||||
|
private final String accessToken;
|
||||||
|
private final Handler handler = new Handler();
|
||||||
|
private static final int MAX_RETRIES = 3;
|
||||||
|
private static final long RETRY_DELAY_MS = 2000; // 重试延迟,例如2秒
|
||||||
|
// OkHttpClient的单例
|
||||||
|
private static final OkHttpClient client = new OkHttpClient();
|
||||||
|
|
||||||
|
// API URL
|
||||||
|
public NetworkUtils(Context context) {
|
||||||
|
callApiUrl = context.getResources().getString(R.string.call_api_url);
|
||||||
|
smsApiUrl = context.getResources().getString(R.string.message_api_url);
|
||||||
|
wechatApiUrl= context.getResources().getString(R.string.send_wechat_msg_api_url);
|
||||||
|
accessToken = context.getResources().getString(R.string.access_token);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> void postRequest(T dataObject) {
|
||||||
|
// 使用单例的OkHttpClient
|
||||||
|
if (dataObject instanceof CallInfo) {
|
||||||
|
postRequestWithRetry(dataObject, 0, callApiUrl);
|
||||||
|
} else if (dataObject instanceof SMSInfo) {
|
||||||
|
postRequestWithRetry(dataObject, 0, smsApiUrl);
|
||||||
|
} else if (dataObject instanceof WeChatMsg){
|
||||||
|
postRequestWithRetry(dataObject,0,wechatApiUrl);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T> void postRequestWithRetry(T dataObject, int retryCount,String apiUrl) {
|
||||||
|
// ... 构建请求 ...
|
||||||
|
Request request = buildRequest(dataObject,apiUrl);
|
||||||
|
Timber.d("Sending request to " + apiUrl + " (Retry count: " + retryCount + ")");
|
||||||
|
|
||||||
|
client.newCall(request).enqueue(new okhttp3.Callback() {
|
||||||
|
@Override
|
||||||
|
public void onFailure(okhttp3.Call call, IOException e) {
|
||||||
|
Timber.d("Request to " + apiUrl + " failed on attempt " + retryCount+ e);
|
||||||
|
if (retryCount < MAX_RETRIES) {
|
||||||
|
handler.postDelayed(() -> postRequestWithRetry(dataObject, retryCount + 1,apiUrl), RETRY_DELAY_MS);
|
||||||
|
} else {
|
||||||
|
Timber.e("onFailure: Failed after " + MAX_RETRIES + " attempts" + e);
|
||||||
|
// 超出重试次数,处理失败情况
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
|
||||||
|
if (!response.isSuccessful()) {
|
||||||
|
// 在这里处理响应错误
|
||||||
|
Timber.d("Request to " + apiUrl + " returned error: " + response.code() + ", " + response.message());
|
||||||
|
throw new IOException("Unexpected code " + response);
|
||||||
|
}
|
||||||
|
// ... 处理响应 ...
|
||||||
|
Gson gson1 = new Gson();
|
||||||
|
ApiResponse apiResponse = gson1.fromJson(response.body().string(), ApiResponse.class);
|
||||||
|
Timber.d("ApiResponse" + apiResponse);
|
||||||
|
if (apiResponse.getCode().equals(0)){
|
||||||
|
Timber.d("Received response from " + apiUrl + ": " + apiResponse.getCode() + " - " + apiResponse.getMsg());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private <T>Request buildRequest(T dataObject,String apiUrl){
|
||||||
|
Gson gson = new Gson();
|
||||||
|
// 将对象转换为JSON字符串
|
||||||
|
String json = gson.toJson(dataObject);
|
||||||
|
Timber.d("Building request to " + apiUrl + " with data: " + json);
|
||||||
|
// 创建请求体
|
||||||
|
MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||||
|
RequestBody body = RequestBody.create(json, JSON);
|
||||||
|
|
||||||
|
// 构建请求,并设置请求头
|
||||||
|
Request request = new Request.Builder()
|
||||||
|
.url(apiUrl)
|
||||||
|
.addHeader("accessToken", accessToken) // 使用加载的accessToken
|
||||||
|
.addHeader("Content-Type", "application/json")
|
||||||
|
.post(body)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
return request;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue