Compare commits
49 Commits
edef32dc07
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 302a7dd438 | |||
| 269cbb6736 | |||
| 7297b8f1c4 | |||
| 48be0900e1 | |||
| 620235c089 | |||
| b3752e4716 | |||
| ffc88920db | |||
| 492cafb51a | |||
| bfc5e4905a | |||
| e55cba4b63 | |||
| 59eae71d70 | |||
| 4463c24107 | |||
| c416971d67 | |||
| 21dc14ba5f | |||
| b4b02d0b1c | |||
| 443b41fde6 | |||
| 09908fcdc7 | |||
| 10b7fc57c1 | |||
| 55b181cba7 | |||
| f0a49ef542 | |||
| a398e59655 | |||
| d8523a3f3c | |||
| 7681db0596 | |||
| d500f8ee41 | |||
| 81af975717 | |||
| db097f7dd9 | |||
| 2314852b1c | |||
| 308ceedbe5 | |||
| 90e705b043 | |||
| a451f4ffc2 | |||
| ebd667f391 | |||
| a5b306a4a2 | |||
| acf592153e | |||
| 976bb6316b | |||
| e39170ef45 | |||
| 9ed7c9bcbb | |||
| f17489bdbb | |||
| 3e284d528e | |||
| 172a26896d | |||
| acdd23567e | |||
| e5d8b244d1 | |||
| b64cb19e17 | |||
| a5e081a0a1 | |||
| 5fd4acf9fb | |||
| 1ecf3998ad | |||
| d52b837175 | |||
| 82d21d98be | |||
| cc4ef4939c | |||
| e59de5764e |
22
.gitignore
vendored
22
.gitignore
vendored
@@ -2,4 +2,24 @@
|
||||
.idea
|
||||
.DS_Store
|
||||
local.properties
|
||||
app/debug/app-debug.apk
|
||||
# Built application files
|
||||
*.apk
|
||||
*.ap_
|
||||
|
||||
# Generated files
|
||||
bin/
|
||||
gen/
|
||||
out/
|
||||
|
||||
# Gradle files
|
||||
build/
|
||||
|
||||
# Keystore files
|
||||
*.jks
|
||||
*.keystore
|
||||
|
||||
app/release
|
||||
app/release/*
|
||||
app/debug
|
||||
app/debug/*
|
||||
app/debug/output-metadata.json
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
{
|
||||
"version": 3,
|
||||
"artifactType": {
|
||||
"type": "APK",
|
||||
"kind": "Directory"
|
||||
},
|
||||
"applicationId": "com.nbee.echolink",
|
||||
"variantName": "debug",
|
||||
"elements": [
|
||||
{
|
||||
"type": "SINGLE",
|
||||
"filters": [],
|
||||
"attributes": [],
|
||||
"versionCode": 1,
|
||||
"versionName": "1.0.1",
|
||||
"outputFile": "app-debug.apk"
|
||||
}
|
||||
],
|
||||
"elementType": "File"
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
|
||||
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
|
||||
<uses-permission android:name="android.permission.ACTION_NOTIFICATION_LISTENER_SETTINGS" />
|
||||
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
|
||||
<uses-permission android:name="android.permission.ANSWER_PHONE_CALLS" />
|
||||
@@ -67,6 +68,14 @@
|
||||
<action android:name="android.intent.action.BOOT_COMPLETED"/>
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<service android:name=".service.MonitorService" />
|
||||
<service android:name=".service.MonitorService"/>
|
||||
<service android:name=".service.NotificationListener"
|
||||
android:label="NotificationListener"
|
||||
android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE"
|
||||
android:exported="true">
|
||||
<intent-filter>
|
||||
<action android:name="android.service.notification.NotificationListenerService" />
|
||||
</intent-filter>
|
||||
</service>
|
||||
</application>
|
||||
</manifest>
|
||||
@@ -1,9 +1,9 @@
|
||||
package com.nbee.echolink;
|
||||
|
||||
import android.app.Application;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
public class EchoLink extends Application {
|
||||
@Override
|
||||
public void onCreate() {
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.nbee.echolink;
|
||||
import android.content.Context;
|
||||
import android.util.Log;
|
||||
import timber.log.Timber;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
|
||||
@@ -5,49 +5,112 @@ import android.content.Intent;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.os.PowerManager;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.TextView;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.nbee.echolink.async.BatteryOptimizationTask;
|
||||
import com.nbee.echolink.service.MonitorService;
|
||||
import com.nbee.echolink.service.NotificationListener;
|
||||
import com.nbee.echolink.utils.BatteryOptimizationUtil;
|
||||
import com.nbee.echolink.utils.DeviceInfoUtils;
|
||||
import com.nbee.echolink.utils.PermissionUtils;
|
||||
import com.nbee.echolink.utils.SharedPreferencesManager;
|
||||
import com.nbee.echolink.utils.ShellUtils;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private final String TAG = "MainActivity";
|
||||
private final String TAG = "MainActivity";
|
||||
private PermissionUtils permissionUtils;
|
||||
private SharedPreferencesManager spManager;
|
||||
private TextView logTextView; // 将类型从 View 更改为 TextView
|
||||
|
||||
/**
|
||||
* 当活动被创建时调用。
|
||||
* 主要负责初始化界面、检查权限、设置初始数据、启动服务等操作。
|
||||
*
|
||||
* @param savedInstanceState 如果活动之前被销毁,这参数包含之前的状态。如果活动没被销毁之前,这参数是null。
|
||||
*/
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
// 设置对应的布局文件
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
// 初始化“清除日志”按钮
|
||||
Button clearAllLogs = findViewById(R.id.clearLogsButton);
|
||||
|
||||
// 初始化日志文本视图并显示初始化信息
|
||||
logTextView = findViewById(R.id.logTextView);
|
||||
updateLog("初始化日志...");
|
||||
|
||||
// 初始化SharedPreferences管理器
|
||||
spManager = new SharedPreferencesManager(this);
|
||||
|
||||
// 检查是否是首次运行,是则进行初始数据设置
|
||||
if (isFirstRun()) {
|
||||
setupInitialDataAsync(); // 异步设置初始数据
|
||||
spManager.putBoolean("isFirstRun", false); // 标记不再首次运行
|
||||
Timber.i("首次运行,设置初始数据...");
|
||||
Timber.d("android_ID: " + DeviceInfoUtils.getAndroidID(this));
|
||||
}
|
||||
|
||||
// 请求忽略电池优化设置
|
||||
requestIgnoreBatteryAsync();
|
||||
|
||||
// 初始化权限工具类
|
||||
permissionUtils = new PermissionUtils(this);
|
||||
permissionUtils.checkPermissions();
|
||||
|
||||
// 启动MonitorService服务
|
||||
// 检查通知权限是否开启
|
||||
if (permissionUtils.isNotificationServiceEnabled(this)) {
|
||||
// 权限已开启,可以执行相关操作
|
||||
} else {
|
||||
// 权限未开启,引导用户去设置页面开启
|
||||
Intent intent = new Intent("android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
|
||||
startActivity(intent);
|
||||
}
|
||||
|
||||
// 启动MonitorService服务
|
||||
Intent serviceIntent = new Intent(this, MonitorService.class);
|
||||
startService(serviceIntent);
|
||||
|
||||
// 启动通知监听服务
|
||||
Intent serviceIntent1 = new Intent(this, NotificationListener.class);
|
||||
startService(serviceIntent1);
|
||||
|
||||
// 设置点击“清除日志”按钮的监听器
|
||||
clearAllLogs.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
clearLogs(); // 调用清空日志的方法
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 处理权限请求的结果。
|
||||
* 当用户对应用的权限请求做出响应时,系统会调用此方法。
|
||||
*
|
||||
* @param requestCode 请求码,用于标识哪个权限请求被响应。
|
||||
* @param permissions 请求的权限数组。
|
||||
* @param grantResults 请求结果的整型数组,每个元素对应一个权限的请求结果。
|
||||
*/
|
||||
@Override
|
||||
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
// 向PermissionUtils传递权限请求的结果,进行进一步处理。
|
||||
permissionUtils.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
||||
}
|
||||
|
||||
@@ -57,42 +120,131 @@ public class MainActivity extends AppCompatActivity {
|
||||
readLogFileAsync();
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查应用是否是首次运行。
|
||||
* <p>此方法通过读取SharedPreferences中的“isFirstRun”键值对来判断。如果该键不存在或者值为true,表示是首次运行。</p>
|
||||
*
|
||||
* @return boolean 如果是首次运行返回true,否则返回false。
|
||||
*/
|
||||
private boolean isFirstRun() {
|
||||
// 从SharedPreferences中获取“isFirstRun”键对应的值,如果不存在则默认返回true
|
||||
return spManager.getBoolean("isFirstRun", true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步设置初始数据,主要用于配置通知监控的包名。
|
||||
* 该方法会开启一个新的线程,在其中进行SharedPreferences的配置,
|
||||
* 包括设置特定包名及其对应的应用名称。
|
||||
* 完成后,可根据需要在该线程中执行其他操作,如发送广播或更新UI等,
|
||||
* 但若涉及UI操作,需确保在主线程中进行。
|
||||
*/
|
||||
private void setupInitialDataAsync() {
|
||||
new Thread(() -> {
|
||||
// 设置SharedPreferences中的包名和应用名示例
|
||||
spManager.putString("com.tencent.mm", "微信");
|
||||
// 日志输出,表示SharedPreferences设置成功
|
||||
Timber.d("写入SharedPreferences成功。");
|
||||
// 在这里可以执行其他操作,如需要更新UI,请确保在主线程中进行
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 异步读取日志文件的函数。
|
||||
* 该函数内部使用了AsyncTask来在后台读取日志文件,并且在读取完成后更新日志显示。
|
||||
* 无参数。
|
||||
* 无返回值。
|
||||
*/
|
||||
private void readLogFileAsync() {
|
||||
new AsyncTask<Void, Void, String>() {
|
||||
@Override
|
||||
protected String doInBackground(Void... voids) {
|
||||
// 在后台执行任务,读取日志文件内容
|
||||
return readLogFile();
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(String logContent) {
|
||||
// 在UI线程上执行,将读取到的日志内容更新到UI
|
||||
updateLog(logContent);
|
||||
}
|
||||
}.execute();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 异步请求忽略电池优化的函数。
|
||||
* 这个函数不会接收任何参数,也不会返回任何结果。
|
||||
* 它主要通过创建并执行一个名为BatteryOptimizationTask的异步任务,
|
||||
* 来请求系统忽略应用的电池优化设置。
|
||||
*/
|
||||
private void requestIgnoreBatteryAsync() {
|
||||
// 创建并执行BatteryOptimizationTask异步任务
|
||||
new BatteryOptimizationTask(this).execute();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 更新日志文本视图的内容。
|
||||
*
|
||||
* @param newLog 新的日志内容。
|
||||
* 调用此方法会将日志文本视图的内容更新为传入的新日志内容。
|
||||
*/
|
||||
private void updateLog(String newLog) {
|
||||
logTextView.setText(newLog);
|
||||
logTextView.setText(newLog); // 更新日志文本视图的内容为新的日志字符串
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 清除日志文件的内容。
|
||||
* 该方法首先检查是否存在名为"echoLink.log"的日志文件。
|
||||
* 如果存在,则重写该文件以清空其内容,并更新日志状态。
|
||||
* 如果文件不存在,则直接更新日志状态表示无需清空。
|
||||
* 该方法不接受任何参数且无返回值。
|
||||
*/
|
||||
private void clearLogs() {
|
||||
File logFile = new File(getFilesDir(), "logs/echoLink.log");
|
||||
if (logFile.exists()) {
|
||||
try {
|
||||
// 使用空字符串重写文件来清空内容
|
||||
PrintWriter writer = new PrintWriter(logFile);
|
||||
writer.print("");
|
||||
writer.close();
|
||||
updateLog("日志已清空。"); // 更新日志状态,提示日志已清空
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
updateLog("清空日志文件时出错。"); // 更新日志状态,提示清空日志文件时出现错误
|
||||
}
|
||||
} else {
|
||||
updateLog("日志文件不存在,无需清空。"); // 更新日志状态,表示无需清空日志文件
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取日志文件的内容。
|
||||
* <p>此方法不接受任何参数,它会尝试读取名为"echoLink.log"的日志文件。</p>
|
||||
* <p>返回值为日志文件的字符串内容,如果文件不存在,则返回"日志文件不存在";
|
||||
* 如果在读取过程中发生IO异常,则返回"读取日志文件时出错"。</p>
|
||||
*
|
||||
* @return 返回日志文件的内容,或者错误信息。
|
||||
*/
|
||||
private String readLogFile() {
|
||||
try {
|
||||
File logFile = new File(getFilesDir(), "logs/echoLink.log"); // 日志文件的路径
|
||||
// 创建日志文件的路径
|
||||
File logFile = new File(getFilesDir(), "logs/echoLink.log");
|
||||
if (!logFile.exists()) {
|
||||
return "日志文件不存在";
|
||||
}
|
||||
|
||||
// 用于存储日志文件内容的StringBuilder
|
||||
StringBuilder logContent = new StringBuilder();
|
||||
try (BufferedReader reader = new BufferedReader(new FileReader(logFile))) {
|
||||
String line;
|
||||
// 逐行读取日志文件,并将其添加到logContent中
|
||||
while ((line = reader.readLine()) != null) {
|
||||
logContent.append(line).append("\n");
|
||||
}
|
||||
}
|
||||
// 将logContent转换为字符串并返回
|
||||
return logContent.toString();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -7,10 +7,17 @@ import android.content.Intent;
|
||||
import com.nbee.echolink.service.MonitorService;
|
||||
|
||||
public class BootCompletedReceiver extends BroadcastReceiver {
|
||||
/**
|
||||
* 当接收到开机启动完成的广播时启动监控服务。
|
||||
*
|
||||
* @param context 上下文对象,提供了调用环境的信息。
|
||||
* @param intent 携带了广播的内容。
|
||||
*/
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
// 检查接收到的广播是否为系统启动完成的广播
|
||||
if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())) {
|
||||
// 启动您的服务或活动
|
||||
// 如果是系统启动完成的广播,则启动监控服务
|
||||
Intent serviceIntent = new Intent(context, MonitorService.class);
|
||||
context.startService(serviceIntent);
|
||||
}
|
||||
|
||||
@@ -21,26 +21,43 @@ import okhttp3.RequestBody;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class HeartbeatAlarmReceiver extends BroadcastReceiver {
|
||||
private Context context;
|
||||
private Context thisContext;
|
||||
private String accessToken;
|
||||
private String SN;
|
||||
|
||||
/**
|
||||
* 当接收到特定广播时,执行心跳发送逻辑的函数。
|
||||
*
|
||||
* @param context 上下文对象,用于访问应用全局功能。
|
||||
* @param intent 携带了广播的内容。
|
||||
*/
|
||||
@Override
|
||||
public void onReceive(Context context1, Intent intent) {
|
||||
// 在这里执行发送心跳的逻辑
|
||||
context = context1;
|
||||
// 例如,你可以启动一个服务或者直接在这里执行网络请求
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
// 初始化上下文对象,为后续操作提供context
|
||||
thisContext = context;
|
||||
// 执行发送心跳信号的逻辑
|
||||
sendHeartbeatSignal();
|
||||
// 重新设置下一个闹钟
|
||||
MonitorService.scheduleHeartbeat(context);
|
||||
// 在发送心跳后,重新设置下一次心跳发送的时间
|
||||
MonitorService.scheduleHeartbeat(thisContext);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送心跳信号的函数。
|
||||
* 该函数不会返回任何值,用于向服务器发送一个心跳请求,以保持客户端的活动状态。
|
||||
* 该请求为异步请求,请求失败或成功都会在回调函数中进行处理。
|
||||
*/
|
||||
private void sendHeartbeatSignal() {
|
||||
String heartBeatURL = context.getResources().getString(R.string.heart_beat_url); // 获取URL
|
||||
// 获取心跳请求的URL
|
||||
String heartBeatURL = thisContext.getResources().getString(R.string.heart_beat_url);
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
Request request = buildRequest(heartBeatURL);
|
||||
|
||||
// 发送异步请求
|
||||
client.newCall(request).enqueue(new okhttp3.Callback() {
|
||||
@Override
|
||||
public void onFailure(okhttp3.Call call, IOException e) {
|
||||
// 请求失败时的处理逻辑
|
||||
Timber.e(e, "请求 " + heartBeatURL + "失败");
|
||||
}
|
||||
|
||||
@@ -48,55 +65,68 @@ public class HeartbeatAlarmReceiver extends BroadcastReceiver {
|
||||
public void onResponse(okhttp3.Call call, okhttp3.Response response) {
|
||||
try {
|
||||
if (!response.isSuccessful()) {
|
||||
// 在这里处理响应错误
|
||||
// 处理响应错误的情况
|
||||
Timber.d("Request to " + heartBeatURL + " returned error: " + response.code() + ", " + response.message());
|
||||
} else {
|
||||
// 处理成功的响应
|
||||
String responseBody = response.body().string(); // 获取响应体
|
||||
// 处理成功响应的逻辑
|
||||
String responseBody = response.body().string(); // 获取响应体内容
|
||||
Gson gson = new Gson();
|
||||
ApiResponse apiResponse = gson.fromJson(responseBody, ApiResponse.class);
|
||||
if (apiResponse == null){
|
||||
if (apiResponse == null) {
|
||||
// 处理api响应为空的情况
|
||||
Timber.d("apiResponse is null.");
|
||||
}
|
||||
if (!apiResponse.getCode().equals("0")) {
|
||||
// 处理响应状态码错误的情况
|
||||
Timber.d("请求返回状态码错误,返回内容: " + apiResponse);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// 处理响应处理过程中的IO异常
|
||||
Timber.e(e, "IOException during handling response");
|
||||
} catch (JsonSyntaxException e) {
|
||||
// 处理响应解析过程中的JSON格式异常
|
||||
Timber.e(e, "JsonSyntaxException during parsing response");
|
||||
} finally {
|
||||
response.close(); // 确保响应体被关闭
|
||||
// 确保响应体被关闭,避免资源泄露
|
||||
response.close();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private <T>Request buildRequest(String apiUrl){
|
||||
SN = context.getResources().getString(R.string.SN);
|
||||
accessToken = context.getResources().getString(R.string.access_token);
|
||||
|
||||
/**
|
||||
* 构建一个网络请求。
|
||||
*
|
||||
* @param apiUrl 请求的API地址。
|
||||
* @param <T> 请求返回的类型。
|
||||
* @return 返回构建好的Request对象。
|
||||
*/
|
||||
private <T> Request buildRequest(String apiUrl) {
|
||||
// 从资源文件中读取序列号和访问令牌
|
||||
//SN = thisContext.getResources().getString(R.string.SN);
|
||||
SN = DeviceInfoUtils.getAndroidID(thisContext);
|
||||
accessToken = thisContext.getResources().getString(R.string.access_token);
|
||||
// 创建设备信息对象并设置设备相关属性
|
||||
DeviceInfo deviceInfo = new DeviceInfo();
|
||||
deviceInfo.setDeviceBrand(DeviceInfoUtils.getDeviceBrand());
|
||||
deviceInfo.setDeviceModel(DeviceInfoUtils.getDeviceModel());
|
||||
deviceInfo.setAndroidVersion("Android " + DeviceInfoUtils.getDeviceAndroidVersion());
|
||||
deviceInfo.setSn(SN);
|
||||
// 使用Gson将设备信息对象转换为JSON字符串
|
||||
Gson gson = new Gson();
|
||||
// 将对象转换为JSON字符串
|
||||
String json = gson.toJson(deviceInfo);
|
||||
// Timber.d("Building request to " + apiUrl + " with data: " + json);
|
||||
// 创建请求体
|
||||
// 创建请求体,使用JSON格式
|
||||
MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||
RequestBody body = RequestBody.create(json, JSON);
|
||||
|
||||
// 构建请求,并设置请求头
|
||||
// 构建请求对象,并设置请求头,包括accessToken和Content-Type
|
||||
Request request = new Request.Builder()
|
||||
.url(apiUrl)
|
||||
.addHeader("accessToken", accessToken) // 使用加载的accessToken
|
||||
.addHeader("accessToken", accessToken)
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.post(body)
|
||||
.build();
|
||||
|
||||
return request;
|
||||
}
|
||||
}
|
||||
|
||||
82
app/src/main/java/com/nbee/echolink/model/WeChatMsg.java
Normal file
82
app/src/main/java/com/nbee/echolink/model/WeChatMsg.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package com.nbee.echolink.model;
|
||||
|
||||
public class WeChatMsg {
|
||||
private String packageName;
|
||||
private String appName;
|
||||
private String title;
|
||||
private String sender;
|
||||
private String message;
|
||||
private String currentTime;
|
||||
|
||||
public String getCurrentTime() {
|
||||
return currentTime;
|
||||
}
|
||||
|
||||
public void setCurrentTime(String currentTime) {
|
||||
this.currentTime = currentTime;
|
||||
}
|
||||
|
||||
public String getPackageName() {
|
||||
return packageName;
|
||||
}
|
||||
|
||||
public void setPackageName(String packageName) {
|
||||
this.packageName = packageName;
|
||||
}
|
||||
|
||||
public String getAppName() {
|
||||
return appName;
|
||||
}
|
||||
|
||||
public void setAppName(String appName) {
|
||||
this.appName = appName;
|
||||
}
|
||||
|
||||
public String getTitle() {
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title) {
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getSender() {
|
||||
return sender;
|
||||
}
|
||||
|
||||
public void setSender(String sender) {
|
||||
this.sender = sender;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public WeChatMsg() {
|
||||
}
|
||||
|
||||
public WeChatMsg(String packageName, String appName, String title, String sender, String message,String currentTime) {
|
||||
this.packageName = packageName;
|
||||
this.appName = appName;
|
||||
this.title = title;
|
||||
this.sender = sender;
|
||||
this.message = message;
|
||||
this.currentTime = currentTime;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "WeChatMsg{" +
|
||||
"packageName='" + packageName + '\'' +
|
||||
", appName='" + appName + '\'' +
|
||||
", title='" + title + '\'' +
|
||||
", sender='" + sender + '\'' +
|
||||
", message='" + message + '\'' +
|
||||
", currentTime='" + currentTime + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -10,63 +10,80 @@ import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Build;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.core.app.NotificationCompat;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.nbee.echolink.R;
|
||||
import com.nbee.echolink.broadcast.HeartbeatAlarmReceiver;
|
||||
import com.nbee.echolink.model.CallInfo;
|
||||
import com.nbee.echolink.model.DeviceInfo;
|
||||
import com.nbee.echolink.model.SMSInfo;
|
||||
import com.nbee.echolink.response.ApiResponse;
|
||||
import com.nbee.echolink.utils.DeviceInfoUtils;
|
||||
import com.nbee.echolink.utils.NetworkUtil;
|
||||
import com.nbee.echolink.model.WeChatMsg;
|
||||
import com.nbee.echolink.utils.NetworkUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class MonitorService extends Service {
|
||||
private NetworkUtil networkUtil;
|
||||
private NetworkUtils networkUtils;
|
||||
private static final String TAG = "MonitorService";
|
||||
private String lastPhoneNumber = null;
|
||||
private long lastCallTime = 0;
|
||||
private static final long HEARTBEAT_INTERVAL_MINUTES = 10; // 10分钟
|
||||
|
||||
/**
|
||||
* 服务创建时调用的函数。
|
||||
* 该函数在服务创建时被系统自动调用,用于初始化服务的相关操作。
|
||||
* 无参数和返回值。
|
||||
*/
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
Timber.d("监控服务 onCreate");
|
||||
startForegroundService();
|
||||
networkUtil = new NetworkUtil(this);
|
||||
scheduleHeartbeat(this); // 启动心跳定时任务
|
||||
}
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (intent != null) {
|
||||
if (intent.hasExtra("incomingNumber")) {
|
||||
String incomingNumber = intent.getStringExtra("incomingNumber");
|
||||
Timber.d("获取到来电信息,号码: "+ incomingNumber);
|
||||
sendCallInfoToServer(incomingNumber);
|
||||
} else if (intent.hasExtra("sender") && intent.hasExtra("messageBody")) {
|
||||
String sender = intent.getStringExtra("sender");
|
||||
String messageBody = intent.getStringExtra("messageBody");
|
||||
Timber.d("获取到短信信息,号码: "+ sender);
|
||||
sendSmsInfoToServer(sender, messageBody);
|
||||
}
|
||||
}
|
||||
return START_STICKY;
|
||||
super.onCreate(); // 调用父类的onCreate方法进行初始化
|
||||
Timber.d("监控服务 onCreate"); // 记录日志,表示监控服务创建
|
||||
startForegroundService(); // 启动前台服务,保证服务不会被系统轻易杀死
|
||||
networkUtils = new NetworkUtils(this); // 初始化网络工具类
|
||||
scheduleHeartbeat(this); // 启动心跳定时任务,用于保持服务的活跃状态或与服务器保持连接
|
||||
}
|
||||
|
||||
/**
|
||||
* 服务启动时的命令处理。
|
||||
*
|
||||
* @param intent 携带启动服务的意图,可能包含来电或短信的信息。
|
||||
* @param flags 启动标志,提供额外数据。
|
||||
* @param startId 一个唯一的整数,标识此启动请求。
|
||||
* @return 返回START_STICKY,如果系统在服务终止后杀死,则重新创建服务并调用onStartCommand(),但不重新传递最后的意图。
|
||||
*/
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
if (intent != null) { // 检查传入的Intent是否非空
|
||||
if (intent.hasExtra("incomingNumber")) { // 检查Intent是否包含来电号码
|
||||
String incomingNumber = intent.getStringExtra("incomingNumber");
|
||||
Timber.d("获取到来电信息,号码: " + incomingNumber);
|
||||
sendCallInfoToServer(incomingNumber); // 将来电信息发送到服务器
|
||||
} else if (intent.hasExtra("sender") && intent.hasExtra("messageBody")) { // 检查Intent是否包含短信信息
|
||||
String sender = intent.getStringExtra("sender");
|
||||
String messageBody = intent.getStringExtra("messageBody");
|
||||
Timber.d("获取到短信信息,号码: " + sender);
|
||||
sendSmsInfoToServer(sender, messageBody); // 将短信信息发送到服务器
|
||||
}
|
||||
}
|
||||
return START_STICKY; // 返回服务启动策略
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将来电信息发送到服务器。
|
||||
*
|
||||
* @param incomingNumber 接收到的电话号码。
|
||||
* 该方法首先检查传入的电话号码是否为"null"或null,
|
||||
* 如果是,则不执行任何操作。
|
||||
* 其次,如果电话号码与上一个来电号码相同,并且时间间隔小于70秒,
|
||||
* 也不会执行发送操作,以避免频繁发送相同信息。
|
||||
* 如果通过所有检查,将创建一个包含来电信息的CallInfo对象,
|
||||
* 并使用网络工具将其发送到服务器。
|
||||
*/
|
||||
private void sendCallInfoToServer(String incomingNumber) {
|
||||
Timber.d("sendCallInfoToServer: 处理来电信息");
|
||||
if (incomingNumber.equals("null") || incomingNumber == null) {
|
||||
@@ -85,54 +102,101 @@ public class MonitorService extends Service {
|
||||
lastCallTime = currentTimeMillis;
|
||||
String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||
|
||||
// 创建CallInfo对象并设置相关信息,然后发送到服务器
|
||||
CallInfo callInfo = new CallInfo();
|
||||
callInfo.setCallTime(currentTime);
|
||||
callInfo.setPhoneNumber(incomingNumber);
|
||||
networkUtil.postRequest(callInfo);
|
||||
networkUtils.postRequest(callInfo);
|
||||
}
|
||||
|
||||
private void sendSmsInfoToServer(String sender,String messageBody){
|
||||
|
||||
/**
|
||||
* 向服务器发送短信信息。
|
||||
*
|
||||
* @param sender 发送者的手机号码。
|
||||
* @param messageBody 短信的内容。
|
||||
* 该方法首先会检查发送者和短信内容是否为空,如果为空则不进行任何操作。
|
||||
* 接着,它会格式化当前时间,并创建一个SMSInfo对象,将发送者、短信内容和接收时间设置到这个对象中。
|
||||
* 最后,使用网络工具将这个SMSInfo对象以POST请求的方式发送到服务器。
|
||||
*/
|
||||
private void sendSmsInfoToServer(String sender, String messageBody) {
|
||||
Timber.d("sendSmsInfoToServer: 处理短信信息");
|
||||
if (checkNullString(sender,messageBody)){
|
||||
if (checkNullString(sender, messageBody)) {
|
||||
return;
|
||||
}
|
||||
// 获取当前时间并格式化
|
||||
String currentTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
|
||||
// 创建SMSInfo对象并设置相关属性
|
||||
SMSInfo smsInfo = new SMSInfo();
|
||||
smsInfo.setSmsNumber(sender);
|
||||
smsInfo.setSmsContent(messageBody);
|
||||
smsInfo.setSmsAcceptanceTime(currentTime);
|
||||
networkUtil.postRequest(smsInfo);
|
||||
// 使用网络工具发送POST请求
|
||||
networkUtils.postRequest(smsInfo);
|
||||
}
|
||||
|
||||
private boolean checkNullString(String a, String b){
|
||||
if (a == null || b == null){
|
||||
/**
|
||||
* 发送微信消息。
|
||||
* 使用网络工具类向指定地址发送微信消息。
|
||||
*
|
||||
* @param weChatMsg 微信消息对象,包含消息的全部必要信息。
|
||||
*/
|
||||
public void sendWeChatMsg(WeChatMsg weChatMsg) {
|
||||
networkUtils.postRequest(weChatMsg);
|
||||
// 使用网络工具类发送POST请求,将微信消息发送出去。
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查两个字符串是否为null或"null"。
|
||||
*
|
||||
* @param a 第一个字符串
|
||||
* @param b 第二个字符串
|
||||
* @return 如果任一字符串为null或者"null",返回true;否则返回false。
|
||||
*/
|
||||
private boolean checkNullString(String a, String b) {
|
||||
// 检查任一字符串是否为null
|
||||
if (a == null || b == null) {
|
||||
return true;
|
||||
}
|
||||
if (a.equals("null") || b.equals("null")){
|
||||
// 检查任一字符串是否等于"null"
|
||||
if (a.equals("null") || b.equals("null")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动一个前台服务。该方法首先会检查系统版本,如果是在API 26及以上版本,就会创建一个通知频道。
|
||||
* 然后,构建一个通知对象,并使用该通知启动前台服务。前台服务能够在后台持续运行,即使应用被关闭或压入后台,
|
||||
* 也能保持服务的运行状态,常用于需要持续运行的服务。
|
||||
*/
|
||||
private void startForegroundService() {
|
||||
Timber.d("startForegroundService: 启动前台服务");
|
||||
// 创建通知频道(仅在API 26及以上版本中需要)
|
||||
// 检测系统版本,仅在API 26及以上版本创建通知频道
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
|
||||
NotificationManager notificationManager = getSystemService(NotificationManager.class);
|
||||
notificationManager.createNotificationChannel(channel);
|
||||
}
|
||||
|
||||
// 创建通知
|
||||
// 创建通知对象。通知内容包括标题、小图标等
|
||||
Notification notification = new NotificationCompat.Builder(this, "channel_id")
|
||||
.setContentTitle(getString(R.string.notification_title))
|
||||
.setSmallIcon(R.drawable.ic_notification) // 确保您有这个图标
|
||||
//.setContentText("sdads")
|
||||
.setSmallIcon(R.drawable.ic_notification) // 设置通知的小图标
|
||||
.build();
|
||||
|
||||
// 启动前台服务
|
||||
// 使用创建的通知启动前台服务
|
||||
startForeground(1, notification);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 当其他组件请求与服务绑定时调用此方法。该服务不提供绑定接口,因此总是返回null。
|
||||
*
|
||||
* @param intent 指示服务应该执行的操作的Intent。包含请求绑定的服务的信息。
|
||||
* @return 返回null,表示该服务不支持绑定操作。
|
||||
*/
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
@@ -140,20 +204,44 @@ public class MonitorService extends Service {
|
||||
return null; // 不提供绑定服务的接口
|
||||
}
|
||||
|
||||
public static void scheduleHeartbeat(Context context) {
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
Intent intent = new Intent(context, HeartbeatAlarmReceiver.class);
|
||||
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
|
||||
|
||||
/**
|
||||
* 调度心跳监测。此函数用于设置一个定时任务,以便周期性地触发心跳事件。
|
||||
* 心跳事件通过广播的形式由系统自动触发,用于维持应用程序在后台的活性或执行定期任务。
|
||||
*
|
||||
* @param context 应用程序的上下文环境,用于访问系统的各种服务。
|
||||
*/
|
||||
public static void scheduleHeartbeat(Context context) {
|
||||
// 获取系统的闹钟服务
|
||||
AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
||||
// 创建一个意图,指定当闹钟触发时要执行的广播接收器
|
||||
Intent intent = new Intent(context, HeartbeatAlarmReceiver.class);
|
||||
|
||||
// 根据Android版本选择合适的标志位创建一个唯一的PendingIntent
|
||||
PendingIntent pendingIntent = null;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
||||
// Android 12及以上版本需要指定FLAG_IMMUTABLE或FLAG_MUTABLE
|
||||
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE);
|
||||
} else {
|
||||
// 早期版本不需要指定这些标志
|
||||
pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
|
||||
}
|
||||
|
||||
// 计算心跳事件的触发间隔,单位为毫秒
|
||||
long intervalMillis = HEARTBEAT_INTERVAL_MINUTES * 60 * 1000; // 10分钟的毫秒数
|
||||
|
||||
// 根据Android版本选择合适的闹钟设置方法,以确保闹钟在设备休眠时也能触发
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
// 对于6.0及以上版本,使用setExactAndAllowWhileIdle方法,可以在设备闲置时精确安排闹钟
|
||||
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent);
|
||||
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
|
||||
// 对于4.4到6.0版本,使用setExact方法,可以精确安排闹钟
|
||||
alarmManager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent);
|
||||
} else {
|
||||
// 对于更早的版本,使用set方法,可以安排一个带宽醒目的闹钟
|
||||
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + intervalMillis, pendingIntent);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
package com.nbee.echolink.service;
|
||||
|
||||
import android.app.Notification;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.service.notification.NotificationListenerService;
|
||||
import android.service.notification.StatusBarNotification;
|
||||
import android.util.Log;
|
||||
|
||||
import com.nbee.echolink.model.WeChatMsg;
|
||||
import com.nbee.echolink.utils.HandleNoticeUtils;
|
||||
import com.nbee.echolink.utils.NetworkUtils;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
public class NotificationListener extends NotificationListenerService {
|
||||
private static final String TAG = "NotificationListener";
|
||||
private static final List<String> notAllowList = Arrays.asList("com.github.kr328.clash","com.google.android.dialer",
|
||||
"com.google.android.apps.messaging");
|
||||
private HandleNoticeUtils handleNoticeUtils;
|
||||
private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
|
||||
private NetworkUtils networkUtil;
|
||||
private final HashMap<String, Long> recentLogs = new HashMap<>();
|
||||
private final Handler logCleanerHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
handleNoticeUtils = new HandleNoticeUtils(this);
|
||||
networkUtil = new NetworkUtils(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNotificationPosted(StatusBarNotification sbn) {
|
||||
String packageName = sbn.getPackageName();
|
||||
if (notAllowList.contains(packageName)) {
|
||||
return;
|
||||
}
|
||||
|
||||
Bundle extras = sbn.getNotification().extras;
|
||||
String title = getNotificationText(extras, Notification.EXTRA_TITLE);
|
||||
String content = getNotificationText(extras, Notification.EXTRA_TEXT);
|
||||
String tickerText = sbn.getNotification().tickerText != null ? sbn.getNotification().tickerText.toString() : "";
|
||||
String logMessage = String.format("packageName: %s, title: %s, content: %s, tickerText: %s", packageName, title, content, tickerText);
|
||||
|
||||
if (!shouldPrintLog(logMessage)) {
|
||||
return; // 如果在30秒内已打印过,则跳过
|
||||
}
|
||||
|
||||
String appName = handleNoticeUtils.messageHandle(packageName);
|
||||
if ("微信".equals(appName)) {
|
||||
handleWeChatNotification(packageName, title, tickerText);
|
||||
}
|
||||
|
||||
Timber.d(logMessage);
|
||||
}
|
||||
|
||||
private String getNotificationText(Bundle extras, String key) {
|
||||
CharSequence charSequence = extras.getCharSequence(key);
|
||||
return charSequence != null ? charSequence.toString() : "";
|
||||
}
|
||||
|
||||
private void handleWeChatNotification(String packageName, String title, String tickerText) {
|
||||
if (tickerText.contains(":")) {
|
||||
String[] parts = tickerText.split(":", 2);
|
||||
if (parts.length < 2) return; // 安全检查,确保不会因数组越界而崩溃
|
||||
|
||||
String sender = parts[0].trim();
|
||||
String message = parts[1].trim();
|
||||
WeChatMsg weChatMsg = new WeChatMsg();
|
||||
weChatMsg.setPackageName(packageName);
|
||||
weChatMsg.setSender(sender);
|
||||
weChatMsg.setMessage(message);
|
||||
weChatMsg.setTitle(title);
|
||||
weChatMsg.setAppName("微信");
|
||||
weChatMsg.setCurrentTime(DATE_FORMAT.format(new Date()));
|
||||
|
||||
try {
|
||||
Timber.d("准备将接受的微信通知转发: %s", weChatMsg);
|
||||
networkUtil.postRequest(weChatMsg);
|
||||
} catch (Exception e) {
|
||||
Timber.e(e, "转发送微信通知失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onNotificationRemoved(StatusBarNotification sbn) {
|
||||
// 当通知被移除时调用
|
||||
Log.d(TAG, "通知被移除: " + sbn.getPackageName());
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否应打印日志
|
||||
*/
|
||||
private boolean shouldPrintLog(String logMessage) {
|
||||
Long lastPrintTime = recentLogs.get(logMessage);
|
||||
long currentTime = System.currentTimeMillis();
|
||||
|
||||
if (lastPrintTime == null || (currentTime - lastPrintTime) > 30000) {
|
||||
// 更新日志的打印时间
|
||||
recentLogs.put(logMessage, currentTime);
|
||||
|
||||
// 安排一分钟后清理这个日志条目
|
||||
logCleanerHandler.postDelayed(() -> recentLogs.remove(logMessage), 60000);
|
||||
|
||||
return true; // 如果没有打印过或距离上次打印超过30秒,则应打印
|
||||
}
|
||||
|
||||
return false; // 如果在30秒内已打印过,则不打印
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,16 @@
|
||||
package com.nbee.echolink.utils;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.provider.Settings;
|
||||
import android.telephony.TelephonyManager;
|
||||
import android.util.Log;
|
||||
|
||||
public class DeviceInfoUtils {
|
||||
/**
|
||||
* 获取设备宽度(px)
|
||||
*
|
||||
*/
|
||||
public static int getDeviceWidth(Context context) {
|
||||
return context.getResources().getDisplayMetrics().widthPixels;
|
||||
@@ -20,6 +22,7 @@ public class DeviceInfoUtils {
|
||||
public static int getDeviceHeight(Context context) {
|
||||
return context.getResources().getDisplayMetrics().heightPixels;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取厂商名
|
||||
**/
|
||||
@@ -63,8 +66,6 @@ public class DeviceInfoUtils {
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* fingerprit 信息
|
||||
**/
|
||||
public static String getDeviceFubgerprint() {
|
||||
@@ -73,7 +74,6 @@ public class DeviceInfoUtils {
|
||||
|
||||
/**
|
||||
* 硬件名
|
||||
*
|
||||
**/
|
||||
public static String getDeviceHardware() {
|
||||
return android.os.Build.HARDWARE;
|
||||
@@ -81,14 +81,12 @@ public class DeviceInfoUtils {
|
||||
|
||||
/**
|
||||
* 主机
|
||||
*
|
||||
**/
|
||||
public static String getDeviceHost() {
|
||||
return android.os.Build.HOST;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 显示ID
|
||||
**/
|
||||
public static String getDeviceDisplay() {
|
||||
@@ -97,15 +95,24 @@ public class DeviceInfoUtils {
|
||||
|
||||
/**
|
||||
* ID
|
||||
*
|
||||
**/
|
||||
public static String getDeviceId() {
|
||||
return android.os.Build.ID;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取手机用户名
|
||||
* 获取设备的Android ID。
|
||||
*
|
||||
* @param context 上下文对象,用于访问应用特定的资源和类。
|
||||
* @return 设备唯一的Android ID,是一个64位的十进制字符串。
|
||||
*/
|
||||
public static String getAndroidID(Context context) {
|
||||
// 通过Settings.Secure.getString获取设备的Android ID
|
||||
return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取手机用户名
|
||||
**/
|
||||
public static String getDeviceUser() {
|
||||
return android.os.Build.USER;
|
||||
@@ -115,9 +122,10 @@ public class DeviceInfoUtils {
|
||||
* 获取手机 硬件序列号
|
||||
**/
|
||||
public static String getDeviceSerial() {
|
||||
return android.os.Build.SERIAL;
|
||||
return "89KX0ARVK";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取手机Android 系统SDK
|
||||
*
|
||||
@@ -143,21 +151,4 @@ public class DeviceInfoUtils {
|
||||
return Locale.getDefault().getLanguage();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前系统上的语言列表(Locale列表)
|
||||
*/
|
||||
public static String getDeviceSupportLanguage() {
|
||||
Log.e("wangjie", "Local:" + Locale.GERMAN);
|
||||
Log.e("wangjie", "Local:" + Locale.ENGLISH);
|
||||
Log.e("wangjie", "Local:" + Locale.US);
|
||||
Log.e("wangjie", "Local:" + Locale.CHINESE);
|
||||
Log.e("wangjie", "Local:" + Locale.TAIWAN);
|
||||
Log.e("wangjie", "Local:" + Locale.FRANCE);
|
||||
Log.e("wangjie", "Local:" + Locale.FRENCH);
|
||||
Log.e("wangjie", "Local:" + Locale.GERMANY);
|
||||
Log.e("wangjie", "Local:" + Locale.ITALIAN);
|
||||
Log.e("wangjie", "Local:" + Locale.JAPAN);
|
||||
Log.e("wangjie", "Local:" + Locale.JAPANESE);
|
||||
return Locale.getAvailableLocales().toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.nbee.echolink.utils;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
public class HandleNoticeUtils {
|
||||
private SharedPreferencesManager spManager;
|
||||
private Context context;
|
||||
|
||||
public HandleNoticeUtils(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public String messageHandle(String packageName){
|
||||
String appName = null;
|
||||
spManager = new SharedPreferencesManager(context);
|
||||
String value = spManager.getString(packageName,"defaultValue");
|
||||
if (value.equals("defaultValue")){
|
||||
return appName;
|
||||
}
|
||||
appName = value;
|
||||
return appName;
|
||||
}
|
||||
}
|
||||
@@ -1,12 +1,12 @@
|
||||
package com.nbee.echolink.utils;
|
||||
import android.content.Context;
|
||||
import android.os.Handler;
|
||||
import android.util.Log;
|
||||
|
||||
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;
|
||||
|
||||
|
||||
@@ -18,21 +18,23 @@ import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class NetworkUtil {
|
||||
private final String TAG = "NetworkUtil";
|
||||
private String callApiUrl;
|
||||
private String smsApiUrl;
|
||||
private String accessToken;
|
||||
private Handler handler = new Handler();
|
||||
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 OkHttpClient client = new OkHttpClient();
|
||||
private static final OkHttpClient client = new OkHttpClient();
|
||||
|
||||
// API URL
|
||||
public NetworkUtil(Context context) {
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -42,11 +44,12 @@ public class NetworkUtil {
|
||||
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) {
|
||||
OkHttpClient client = new OkHttpClient();
|
||||
// ... 构建请求 ...
|
||||
Request request = buildRequest(dataObject,apiUrl);
|
||||
Timber.d("Sending request to " + apiUrl + " (Retry count: " + retryCount + ")");
|
||||
@@ -58,7 +61,6 @@ public class NetworkUtil {
|
||||
if (retryCount < MAX_RETRIES) {
|
||||
handler.postDelayed(() -> postRequestWithRetry(dataObject, retryCount + 1,apiUrl), RETRY_DELAY_MS);
|
||||
} else {
|
||||
Log.e(TAG, "onFailure: Failed after " + MAX_RETRIES + " attempts", e);
|
||||
Timber.e("onFailure: Failed after " + MAX_RETRIES + " attempts" + e);
|
||||
// 超出重试次数,处理失败情况
|
||||
}
|
||||
@@ -73,6 +75,7 @@ public class NetworkUtil {
|
||||
// ... 处理响应 ...
|
||||
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());
|
||||
}
|
||||
@@ -3,8 +3,10 @@ package com.nbee.echolink.utils;
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.provider.Settings;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.core.app.ActivityCompat;
|
||||
@@ -17,7 +19,7 @@ import timber.log.Timber;
|
||||
|
||||
public class PermissionUtils {
|
||||
private static final int PERMISSION_REQUEST_CODE = 1;
|
||||
private Activity activity;
|
||||
private final Activity activity;
|
||||
|
||||
public PermissionUtils(Activity activity) {
|
||||
this.activity = activity;
|
||||
@@ -30,11 +32,10 @@ public class PermissionUtils {
|
||||
Manifest.permission.READ_CALL_LOG,
|
||||
Manifest.permission.READ_CONTACTS,
|
||||
Manifest.permission.RECEIVE_SMS
|
||||
|
||||
};
|
||||
|
||||
if (!hasPermissions(permissions)) {
|
||||
Timber.d("请求以下权限:\n" + Arrays.toString(permissions));
|
||||
Timber.d("请求以下权限: %s", Arrays.toString(permissions));
|
||||
ActivityCompat.requestPermissions(activity, permissions, PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
}
|
||||
@@ -42,7 +43,7 @@ public class PermissionUtils {
|
||||
private boolean hasPermissions(String... permissions) {
|
||||
for (String permission : permissions) {
|
||||
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
|
||||
Timber.d("缺少权限:\n" + permission);
|
||||
Timber.d("缺少权限: %s", permission);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -62,8 +63,8 @@ public class PermissionUtils {
|
||||
|
||||
if (deniedPermissions.length() > 0) {
|
||||
// 显示对话框
|
||||
Timber.d("权限被拒绝:" + deniedPermissions.toString());
|
||||
showAlert("以下权限被拒绝:\n" + deniedPermissions.toString());
|
||||
Timber.d("权限被拒绝:%s", deniedPermissions);
|
||||
showAlert("以下权限被拒绝:\n" + deniedPermissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,15 +72,27 @@ public class PermissionUtils {
|
||||
private void showAlert(String message) {
|
||||
new AlertDialog.Builder(activity)
|
||||
.setMessage(message)
|
||||
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
})
|
||||
.setPositiveButton("确认", (dialog, which) -> dialog.dismiss())
|
||||
.create()
|
||||
.show();
|
||||
}
|
||||
private void showToast(String message) {
|
||||
Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
public boolean isNotificationServiceEnabled(Context context) {
|
||||
String packageName = context.getPackageName();
|
||||
String flat = Settings.Secure.getString(context.getContentResolver(), "enabled_notification_listeners");
|
||||
if (flat != null && !flat.isEmpty()) {
|
||||
final String[] activeListeners = flat.split(":");
|
||||
for (String activeListener : activeListeners) {
|
||||
ComponentName cn = ComponentName.unflattenFromString(activeListener);
|
||||
if (cn != null && cn.getPackageName().equals(packageName)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.nbee.echolink.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
|
||||
public class SharedPreferencesManager {
|
||||
|
||||
private static final String PREFS_NAME = "MyAppPrefs";
|
||||
private SharedPreferences sharedPreferences;
|
||||
|
||||
public SharedPreferencesManager(Context context) {
|
||||
this.sharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||
}
|
||||
|
||||
public void putString(String key, String value) {
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.putString(key, value);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
public String getString(String key,String defaultValue) {
|
||||
return sharedPreferences.getString(key,defaultValue);
|
||||
}
|
||||
|
||||
public void putBoolean(String key, boolean value) {
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.putBoolean(key, value);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
public boolean getBoolean(String key, boolean defaultValue) {
|
||||
return sharedPreferences.getBoolean(key, defaultValue);
|
||||
}
|
||||
|
||||
public void putInt(String key, int value) {
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.putInt(key, value);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
public int getInt(String key, int defaultValue) {
|
||||
return sharedPreferences.getInt(key, defaultValue);
|
||||
}
|
||||
|
||||
// 添加更多的方法来处理其他类型,如 putLong, getLong, putFloat, getFloat 等。
|
||||
|
||||
public void remove(String key) {
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.remove(key);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
public void clearAll() {
|
||||
SharedPreferences.Editor editor = sharedPreferences.edit();
|
||||
editor.clear();
|
||||
editor.apply();
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<!-- 用于显示日志标题的 TextView -->
|
||||
<TextView
|
||||
android:id="@+id/logTitleTextView"
|
||||
@@ -19,10 +20,11 @@
|
||||
|
||||
<!-- ScrollView 包含一个 TextView 用于显示日志内容 -->
|
||||
<ScrollView
|
||||
android:id="@+id/logScrollView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/logTitleTextView"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintBottom_toTopOf="@+id/clearLogsButton"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent">
|
||||
|
||||
@@ -32,5 +34,13 @@
|
||||
android:layout_height="wrap_content" />
|
||||
</ScrollView>
|
||||
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
<!-- 新增加的按钮用于清空所有日志 -->
|
||||
<Button
|
||||
android:id="@+id/clearLogsButton"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="清空所有日志"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
<string name="call_api_url">https://api.kimgo.cn/api/call</string>
|
||||
<string name="message_api_url">https://api.kimgo.cn/api/sms</string>
|
||||
<string name="heart_beat_url">https://api.kimgo.cn/heartbeat</string>
|
||||
<string name="send_wechat_msg_api_url">https://api.kimgo.cn/api/wechat</string>
|
||||
<string name="SN">XCCS3IK75OCM</string>
|
||||
<string name="access_token">gKGCDSgWV82XbU0H</string>
|
||||
<string name="notification_title">监控服务运行中</string>
|
||||
|
||||
Reference in New Issue
Block a user