Compare commits
98 Commits
70c28847cc
...
system
| Author | SHA1 | Date | |
|---|---|---|---|
| 4e8f666802 | |||
| 884aefc366 | |||
| 2c84258ae2 | |||
| 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 | |||
| edef32dc07 | |||
| a79a381b00 | |||
| cf08d5b5a5 | |||
| 8bfdaf761c | |||
| 33fa742d2e | |||
| e1241a01c3 | |||
| f46467e83d | |||
| cc0afc2338 | |||
| cbf7f3eeea | |||
| 935ded253b | |||
| f95229b421 | |||
| fef6a08ea6 | |||
| 67900cd09a | |||
| 1e71f67c59 | |||
| 003238da4c | |||
| 1938470b43 | |||
| 1220777e5a | |||
| f288d8c6f2 | |||
| e16a598617 | |||
| 38e391ff46 | |||
| 79a246cbd6 | |||
| 935bd0edc3 | |||
| 4009796ac2 | |||
| ac624c70b2 | |||
| 030e8250e6 | |||
| c0548705a2 | |||
| 6ae23c1e2f | |||
| bfc5a741a6 | |||
| cf5039ae63 | |||
| 46fe0c6d24 | |||
| e9b769be85 | |||
| cb660865a1 | |||
| 4ef4f6fe39 | |||
| 7cc38536f0 | |||
| 08403b9109 | |||
| 4cd24ebe02 | |||
| b013fa64d1 | |||
| 489431457a | |||
| d7cc7beb58 | |||
| df5a05f48c | |||
| 425876339d | |||
| cfbca44456 | |||
| 2363429491 | |||
| d3e4cc343e | |||
| a56e528421 | |||
| 2cc080b12c | |||
| 1cb45c2067 | |||
| cdecb938a1 |
100
.gitignore
vendored
100
.gitignore
vendored
@@ -1,113 +1,25 @@
|
||||
*.iml
|
||||
.gradle
|
||||
/local.properties
|
||||
/.idea/caches
|
||||
/.idea/libraries
|
||||
/.idea/modules.xml
|
||||
/.idea/workspace.xml
|
||||
/.idea/navEditor.xml
|
||||
/.idea/assetWizardSettings.xml
|
||||
.idea
|
||||
.DS_Store
|
||||
/build
|
||||
/captures
|
||||
.externalNativeBuild
|
||||
.cxx
|
||||
local.properties
|
||||
|
||||
# Built application files
|
||||
*.apk
|
||||
*.ap_
|
||||
*.aab
|
||||
|
||||
# Files for the Dalvik VM
|
||||
*.dex
|
||||
|
||||
# Java class files
|
||||
*.class
|
||||
|
||||
# Generated files
|
||||
bin/
|
||||
gen/
|
||||
out/
|
||||
release/
|
||||
|
||||
# Gradle files
|
||||
.gradle/
|
||||
build/
|
||||
|
||||
# Local configuration file (sdk path, etc)
|
||||
local.properties
|
||||
|
||||
# Proguard folder generated by Eclipse
|
||||
proguard/
|
||||
|
||||
# Log Files
|
||||
*.log
|
||||
|
||||
# Android Studio Navigation editor temp files
|
||||
.navigation/
|
||||
|
||||
# Android Studio captures folder
|
||||
captures/
|
||||
|
||||
# IntelliJ
|
||||
*.iml
|
||||
.idea/
|
||||
.idea_modules/
|
||||
|
||||
# Keystore files
|
||||
*.jks
|
||||
*.keystore
|
||||
|
||||
# External native build folder generated in Android Studio 2.2 and later
|
||||
.externalNativeBuild
|
||||
|
||||
# Google Services (e.g. APIs or Firebase)
|
||||
google-services.json
|
||||
|
||||
# Freeline
|
||||
freeline.py
|
||||
freeline/
|
||||
freeline_project_description.json
|
||||
|
||||
# fastlane
|
||||
fastlane/report.xml
|
||||
fastlane/Preview.html
|
||||
fastlane/screenshots
|
||||
fastlane/test_output
|
||||
fastlane/readme.md
|
||||
|
||||
# Version control
|
||||
vcs.xml
|
||||
|
||||
# lint
|
||||
lint/intermediates/
|
||||
lint/generated/
|
||||
lint/outputs/
|
||||
lint/tmp/
|
||||
lint/reports/
|
||||
|
||||
# Gradle and Maven with auto-import
|
||||
# When using Gradle or Maven with auto-import, you should exclude module files,
|
||||
# since they will be recreated, and may cause churn. Uncomment if using
|
||||
# auto-import.
|
||||
# .idea/modules.xml
|
||||
# .idea/*.iml
|
||||
# .idea/modules
|
||||
# *.iml
|
||||
# *.ipr
|
||||
|
||||
# CMake
|
||||
cmake-build-debug/
|
||||
|
||||
# Local config
|
||||
.idea/workspace.xml
|
||||
.idea/tasks.xml
|
||||
.idea/gradle.xml
|
||||
.idea/assetWizardSettings.xml
|
||||
.idea/dictionaries
|
||||
.idea/libraries
|
||||
.idea/caches
|
||||
|
||||
# Android Patch
|
||||
gen-external-apklibs
|
||||
app/release
|
||||
app/release/*
|
||||
app/debug
|
||||
app/debug/*
|
||||
app/debug/output-metadata.json
|
||||
|
||||
@@ -11,7 +11,7 @@ android {
|
||||
minSdk 26
|
||||
targetSdk 31
|
||||
versionCode 1
|
||||
versionName "1.0"
|
||||
versionName "1.0.1"
|
||||
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
}
|
||||
@@ -37,11 +37,12 @@ dependencies {
|
||||
implementation 'com.google.code.gson:gson:2.10'
|
||||
// https://mvnrepository.com/artifact/com.jakewharton.timber/timber
|
||||
implementation 'com.jakewharton.timber:timber:5.0.1'
|
||||
|
||||
// 添加 WorkManager 的依赖
|
||||
implementation "androidx.work:work-runtime:2.7.0"
|
||||
implementation 'androidx.appcompat:appcompat:1.2.0'
|
||||
implementation 'com.google.android.material:material:1.3.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.4'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
|
||||
}
|
||||
@@ -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",
|
||||
"outputFile": "app-debug.apk"
|
||||
}
|
||||
],
|
||||
"elementType": "File"
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
package="com.nbee.echolink"
|
||||
android:sharedUserId="android.uid.system"
|
||||
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" />
|
||||
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
|
||||
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
|
||||
@@ -20,6 +23,7 @@
|
||||
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
||||
|
||||
|
||||
|
||||
<application
|
||||
android:name=".EchoLink"
|
||||
android:allowBackup="true"
|
||||
@@ -49,6 +53,10 @@
|
||||
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
|
||||
</intent-filter>
|
||||
</receiver>
|
||||
<receiver android:name=".broadcast.HeartbeatAlarmReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="true">
|
||||
</receiver>
|
||||
<receiver android:name=".broadcast.PhoneCallReceiver"
|
||||
android:enabled="true"
|
||||
android:exported="true">
|
||||
@@ -63,6 +71,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,11 +0,0 @@
|
||||
package com.nbee.echolink;
|
||||
|
||||
import android.os.Build;
|
||||
|
||||
public class DeviceInfo {
|
||||
|
||||
public static int getApiVersion() {
|
||||
return Build.VERSION.SDK_INT;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,15 +3,17 @@ package com.nbee.echolink;
|
||||
import android.app.Application;
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
public class EchoLink extends Application {
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
|
||||
// 植入 Timber
|
||||
Timber.plant(new FileLoggingTree(this));
|
||||
|
||||
// 打印日志文件的路径
|
||||
Timber.d("Log file path: " + this.getFilesDir() + "/logs/echoLink.log");
|
||||
// 安排定时任务
|
||||
// doScheduledTask(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,52 +1,254 @@
|
||||
package com.nbee.echolink;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import androidx.core.content.ContextCompat;
|
||||
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
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();
|
||||
|
||||
|
||||
Button myButton = findViewById(R.id.myButton);
|
||||
myButton.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View view) {
|
||||
// 记录日志
|
||||
Timber.d("Button was clicked");
|
||||
}
|
||||
});
|
||||
// 检查通知权限是否开启
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
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); // 更新日志文本视图的内容为新的日志字符串
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 清除日志文件的内容。
|
||||
* 该方法首先检查是否存在名为"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");
|
||||
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();
|
||||
return "读取日志文件时出错";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.nbee.echolink.async;
|
||||
|
||||
import static android.content.Context.POWER_SERVICE;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.PowerManager;
|
||||
|
||||
import com.nbee.echolink.utils.BatteryOptimizationUtil;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
public class BatteryOptimizationTask extends AsyncTask<Void, Void, Void> {
|
||||
private Context context;
|
||||
|
||||
public BatteryOptimizationTask(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Void doInBackground(Void... voids) {
|
||||
PowerManager powerManager = (PowerManager) context.getSystemService(POWER_SERVICE);
|
||||
boolean hasIgnored = powerManager.isIgnoringBatteryOptimizations(context.getPackageName());
|
||||
if (!hasIgnored) {
|
||||
BatteryOptimizationUtil.requestIgnoreBatteryOptimization(context);
|
||||
Timber.d("请求忽略电池优化");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
package com.nbee.echolink.broadcast;
|
||||
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonSyntaxException;
|
||||
import com.nbee.echolink.R;
|
||||
import com.nbee.echolink.model.DeviceInfo;
|
||||
import com.nbee.echolink.response.ApiResponse;
|
||||
import com.nbee.echolink.service.MonitorService;
|
||||
import com.nbee.echolink.utils.DeviceInfoUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class HeartbeatAlarmReceiver extends BroadcastReceiver {
|
||||
private Context thisContext;
|
||||
private String accessToken;
|
||||
private String SN;
|
||||
|
||||
/**
|
||||
* 当接收到特定广播时,执行心跳发送逻辑的函数。
|
||||
*
|
||||
* @param context 上下文对象,用于访问应用全局功能。
|
||||
* @param intent 携带了广播的内容。
|
||||
*/
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
// 初始化上下文对象,为后续操作提供context
|
||||
thisContext = context;
|
||||
// 执行发送心跳信号的逻辑
|
||||
sendHeartbeatSignal();
|
||||
// 在发送心跳后,重新设置下一次心跳发送的时间
|
||||
MonitorService.scheduleHeartbeat(thisContext);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 发送心跳信号的函数。
|
||||
* 该函数不会返回任何值,用于向服务器发送一个心跳请求,以保持客户端的活动状态。
|
||||
* 该请求为异步请求,请求失败或成功都会在回调函数中进行处理。
|
||||
*/
|
||||
private void sendHeartbeatSignal() {
|
||||
// 获取心跳请求的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 + "失败");
|
||||
}
|
||||
|
||||
@Override
|
||||
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(); // 获取响应体内容
|
||||
Gson gson = new Gson();
|
||||
ApiResponse apiResponse = gson.fromJson(responseBody, ApiResponse.class);
|
||||
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();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 构建一个网络请求。
|
||||
*
|
||||
* @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();
|
||||
String json = gson.toJson(deviceInfo);
|
||||
// 创建请求体,使用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)
|
||||
.addHeader("Content-Type", "application/json")
|
||||
.post(body)
|
||||
.build();
|
||||
return request;
|
||||
}
|
||||
}
|
||||
60
app/src/main/java/com/nbee/echolink/model/DeviceInfo.java
Normal file
60
app/src/main/java/com/nbee/echolink/model/DeviceInfo.java
Normal file
@@ -0,0 +1,60 @@
|
||||
package com.nbee.echolink.model;
|
||||
|
||||
public class DeviceInfo {
|
||||
private String deviceBrand;
|
||||
private String deviceModel;
|
||||
private String androidVersion;
|
||||
private String sn;
|
||||
|
||||
public DeviceInfo() {
|
||||
}
|
||||
|
||||
public DeviceInfo(String deviceBrand, String deviceModel, String androidVersion,String sn) {
|
||||
this.deviceBrand = deviceBrand;
|
||||
this.deviceModel = deviceModel;
|
||||
this.androidVersion = androidVersion;
|
||||
this.sn = sn;
|
||||
}
|
||||
|
||||
public String getDeviceBrand() {
|
||||
return deviceBrand;
|
||||
}
|
||||
|
||||
public void setDeviceBrand(String deviceBrand) {
|
||||
this.deviceBrand = deviceBrand;
|
||||
}
|
||||
|
||||
public String getDeviceModel() {
|
||||
return deviceModel;
|
||||
}
|
||||
|
||||
public void setDeviceModel(String deviceModel) {
|
||||
this.deviceModel = deviceModel;
|
||||
}
|
||||
|
||||
public String getAndroidVersion() {
|
||||
return androidVersion;
|
||||
}
|
||||
|
||||
public void setAndroidVersion(String androidVersion) {
|
||||
this.androidVersion = androidVersion;
|
||||
}
|
||||
|
||||
public String getSn() {
|
||||
return sn;
|
||||
}
|
||||
|
||||
public void setSn(String sn) {
|
||||
this.sn = sn;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "DeviceInfo{" +
|
||||
"deviceBrand='" + deviceBrand + '\'' +
|
||||
", deviceModel='" + deviceModel + '\'' +
|
||||
", androidVersion='" + androidVersion + '\'' +
|
||||
", SN='" + sn + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
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 + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -1,21 +1,25 @@
|
||||
package com.nbee.echolink.service;
|
||||
|
||||
import android.app.AlarmManager;
|
||||
import android.app.Notification;
|
||||
import android.app.NotificationChannel;
|
||||
import android.app.NotificationManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.app.Service;
|
||||
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.nbee.echolink.R;
|
||||
import com.nbee.echolink.broadcast.HeartbeatAlarmReceiver;
|
||||
import com.nbee.echolink.model.CallInfo;
|
||||
import com.nbee.echolink.model.SMSInfo;
|
||||
import com.nbee.echolink.utils.NetworkUtil;
|
||||
import com.nbee.echolink.model.WeChatMsg;
|
||||
import com.nbee.echolink.utils.NetworkUtils;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
@@ -23,37 +27,63 @@ import java.util.Date;
|
||||
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 int count;
|
||||
private static final long HEARTBEAT_INTERVAL_MINUTES = 10; // 10分钟
|
||||
|
||||
/**
|
||||
* 服务创建时调用的函数。
|
||||
* 该函数在服务创建时被系统自动调用,用于初始化服务的相关操作。
|
||||
* 无参数和返回值。
|
||||
*/
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
Timber.d("MonitorService onCreate");
|
||||
startForegroundService();
|
||||
networkUtil = new NetworkUtil(this);
|
||||
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) {
|
||||
Timber.d("MonitorService onStartCommand");
|
||||
if (intent != null) {
|
||||
if (intent.hasExtra("incomingNumber")) {
|
||||
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")) {
|
||||
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);
|
||||
Timber.d("获取到短信信息,号码: " + sender);
|
||||
sendSmsInfoToServer(sender, messageBody); // 将短信信息发送到服务器
|
||||
}
|
||||
}
|
||||
return START_STICKY;
|
||||
return START_STICKY; // 返回服务启动策略
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将来电信息发送到服务器。
|
||||
*
|
||||
* @param incomingNumber 接收到的电话号码。
|
||||
* 该方法首先检查传入的电话号码是否为"null"或null,
|
||||
* 如果是,则不执行任何操作。
|
||||
* 其次,如果电话号码与上一个来电号码相同,并且时间间隔小于70秒,
|
||||
* 也不会执行发送操作,以避免频繁发送相同信息。
|
||||
* 如果通过所有检查,将创建一个包含来电信息的CallInfo对象,
|
||||
* 并使用网络工具将其发送到服务器。
|
||||
*/
|
||||
private void sendCallInfoToServer(String incomingNumber) {
|
||||
Timber.d("sendCallInfoToServer: 处理来电信息");
|
||||
if (incomingNumber.equals("null") || incomingNumber == null) {
|
||||
@@ -72,58 +102,146 @@ 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) {
|
||||
Timber.d("MonitorService onBind");
|
||||
return null; // 不提供绑定服务的接口
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 调度心跳监测。此函数用于设置一个定时任务,以便周期性地触发心跳事件。
|
||||
* 心跳事件通过广播的形式由系统自动触发,用于维持应用程序在后台的活性或执行定期任务。
|
||||
*
|
||||
* @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秒内已打印过,则不打印
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package com.nbee.echolink.utils;
|
||||
|
||||
import static android.content.Context.POWER_SERVICE;
|
||||
import static androidx.core.content.ContextCompat.getSystemService;
|
||||
import static androidx.core.content.ContextCompat.startActivity;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.PowerManager;
|
||||
import android.provider.Settings;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
public class BatteryOptimizationUtil {
|
||||
|
||||
/**
|
||||
* 检查应用是否在电池优化白名单中。
|
||||
*/
|
||||
public static boolean isAppIgnoringBatteryOptimizations(Context context) {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
|
||||
if (pm != null) {
|
||||
return pm.isIgnoringBatteryOptimizations(context.getPackageName());
|
||||
}
|
||||
}
|
||||
return true; // 在低于Android 6.0的设备上,默认为true
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求将应用加入电池优化白名单。
|
||||
*/
|
||||
public static void requestIgnoreBatteryOptimization(Context context) {
|
||||
if (isAppIgnoringBatteryOptimizations(context)) {
|
||||
Timber.d("已在忽略电池优化白名单");
|
||||
return;
|
||||
}
|
||||
Timber.d("请求电池优化白名单");
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
String packageName = context.getPackageName();
|
||||
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
|
||||
if (pm != null && !pm.isIgnoringBatteryOptimizations(packageName)) {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||||
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
|
||||
intent.setData(Uri.parse("package:" + packageName));
|
||||
context.startActivity(intent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,125 +1,131 @@
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取设备高度(px)
|
||||
*/
|
||||
public static int getDeviceHeight(Context context) {
|
||||
return context.getResources().getDisplayMetrics().heightPixels;
|
||||
}
|
||||
/**
|
||||
* 获取设备的唯一标识, 需要 “android.permission.READ_Phone_STATE”权限
|
||||
* Android 10 无法获取此权限
|
||||
*/
|
||||
// public static String getIMEI(Context context) {
|
||||
// TelephonyManager tm = (TelephonyManager) context
|
||||
// .getSystemService(Context.TELEPHONY_SERVICE);
|
||||
// String deviceId = tm.getDeviceId();
|
||||
// if (deviceId == null) {
|
||||
// return "UnKnown";
|
||||
// } else {
|
||||
// return deviceId;
|
||||
// }
|
||||
// }
|
||||
|
||||
/**
|
||||
* 获取厂商名
|
||||
* **/
|
||||
**/
|
||||
public static String getDeviceManufacturer() {
|
||||
return android.os.Build.MANUFACTURER;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取产品名
|
||||
* **/
|
||||
**/
|
||||
public static String getDeviceProduct() {
|
||||
return android.os.Build.PRODUCT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取手机品牌
|
||||
*/
|
||||
public static String getDeviceBrand() {
|
||||
return android.os.Build.BRAND;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取手机型号
|
||||
*/
|
||||
public static String getDeviceModel() {
|
||||
return android.os.Build.MODEL;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取手机主板名
|
||||
*/
|
||||
public static String getDeviceBoard() {
|
||||
return android.os.Build.BOARD;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备名
|
||||
* **/
|
||||
**/
|
||||
public static String getDeviceDevice() {
|
||||
return android.os.Build.DEVICE;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* fingerprit 信息
|
||||
* **/
|
||||
**/
|
||||
public static String getDeviceFubgerprint() {
|
||||
return android.os.Build.FINGERPRINT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 硬件名
|
||||
*
|
||||
* **/
|
||||
**/
|
||||
public static String getDeviceHardware() {
|
||||
return android.os.Build.HARDWARE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 主机
|
||||
*
|
||||
* **/
|
||||
**/
|
||||
public static String getDeviceHost() {
|
||||
return android.os.Build.HOST;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* 显示ID
|
||||
* **/
|
||||
**/
|
||||
public static String getDeviceDisplay() {
|
||||
return android.os.Build.DISPLAY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取手机 硬件序列号
|
||||
* **/
|
||||
**/
|
||||
public static String getDeviceSerial() {
|
||||
return android.os.Build.SERIAL;
|
||||
return "89KX0ARVK";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取手机Android 系统SDK
|
||||
*
|
||||
@@ -128,6 +134,7 @@ public class DeviceInfoUtils {
|
||||
public static int getDeviceSDK() {
|
||||
return android.os.Build.VERSION.SDK_INT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取手机Android 版本
|
||||
*
|
||||
@@ -136,27 +143,12 @@ public class DeviceInfoUtils {
|
||||
public static String getDeviceAndroidVersion() {
|
||||
return android.os.Build.VERSION.RELEASE;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前手机系统语言。
|
||||
*/
|
||||
public static String getDeviceDefaultLanguage() {
|
||||
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,32 +18,38 @@ 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 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);
|
||||
}
|
||||
|
||||
public <T> void postRequest(T dataObject) {
|
||||
// 使用单例的OkHttpClient
|
||||
if (dataObject instanceof CallInfo) {
|
||||
postRequestWithRetry(dataObject, 0,callApiUrl);
|
||||
postRequestWithRetry(dataObject, 0, callApiUrl);
|
||||
} else if (dataObject instanceof SMSInfo) {
|
||||
postRequestWithRetry(dataObject, 0,smsApiUrl);
|
||||
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 + ")");
|
||||
@@ -55,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);
|
||||
// 超出重试次数,处理失败情况
|
||||
}
|
||||
@@ -70,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());
|
||||
}
|
||||
@@ -2,12 +2,15 @@ package com.nbee.echolink.utils;
|
||||
|
||||
import android.Manifest;
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
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;
|
||||
import androidx.core.content.ContextCompat;
|
||||
import android.app.AlertDialog;
|
||||
import android.content.DialogInterface;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
@@ -16,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;
|
||||
@@ -29,11 +32,10 @@ public class PermissionUtils {
|
||||
Manifest.permission.READ_CALL_LOG,
|
||||
Manifest.permission.READ_CONTACTS,
|
||||
Manifest.permission.RECEIVE_SMS
|
||||
|
||||
};
|
||||
|
||||
if (!hasPermissions(permissions)) {
|
||||
Timber.d("请求以下权限:" + Arrays.toString(permissions));
|
||||
Timber.d("请求以下权限: %s", Arrays.toString(permissions));
|
||||
ActivityCompat.requestPermissions(activity, permissions, PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
}
|
||||
@@ -41,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("缺少权限:" + permission);
|
||||
Timber.d("缺少权限: %s", permission);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -61,13 +63,8 @@ public class PermissionUtils {
|
||||
|
||||
if (deniedPermissions.length() > 0) {
|
||||
// 显示对话框
|
||||
Timber.d("权限被拒绝:" + deniedPermissions.toString());
|
||||
showAlert("以下权限被拒绝:\n" + deniedPermissions.toString());
|
||||
} else {
|
||||
// 所有请求的权限都被授予
|
||||
// 在这里处理所有权限被授予的情况
|
||||
Timber.d("已获取所有权限:" + Arrays.toString(permissions));
|
||||
showToast("已授予运行所需的权限");
|
||||
Timber.d("权限被拒绝:%s", deniedPermissions);
|
||||
showAlert("以下权限被拒绝:\n" + deniedPermissions);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -75,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();
|
||||
}
|
||||
}
|
||||
193
app/src/main/java/com/nbee/echolink/utils/ShellUtils.java
Normal file
193
app/src/main/java/com/nbee/echolink/utils/ShellUtils.java
Normal file
@@ -0,0 +1,193 @@
|
||||
package com.nbee.echolink.utils;
|
||||
|
||||
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Shell工具类
|
||||
* Created by HDL on 2018/8/6.
|
||||
*/
|
||||
|
||||
public class ShellUtils {
|
||||
public static final String COMMAND_SU = "su";
|
||||
public static final String COMMAND_SH = "sh";
|
||||
public static final String COMMAND_EXIT = "exit\n";
|
||||
public static final String COMMAND_LINE_END = "\n";
|
||||
|
||||
private ShellUtils() {
|
||||
throw new AssertionError();
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看是否有了root权限
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static boolean checkRootPermission() {
|
||||
return execCommand("echo root", true, false).result == 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 执行shell命令,默认返回结果
|
||||
*
|
||||
* @param command command
|
||||
* @return
|
||||
* @see ShellUtils#execCommand(String[], boolean, boolean)
|
||||
*/
|
||||
public static CommandResult execCommand(String command, boolean isRoot) {
|
||||
return execCommand(new String[]{command}, isRoot, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 执行shell命令,默认返回结果
|
||||
*
|
||||
* @param commands command list
|
||||
* @return
|
||||
* @see ShellUtils#execCommand(String[], boolean, boolean)
|
||||
*/
|
||||
|
||||
public static CommandResult execCommand(List<String> commands, boolean isRoot) {
|
||||
return execCommand(commands == null ? null : commands.toArray(new String[]{}), isRoot, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 执行shell命令,默认返回结果
|
||||
*
|
||||
* @param commands command array
|
||||
* @return
|
||||
* @see ShellUtils#execCommand(String[], boolean, boolean)
|
||||
*/
|
||||
|
||||
public static CommandResult execCommand(String[] commands, boolean isRoot) {
|
||||
return execCommand(commands, isRoot, true);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* execute shell command
|
||||
*
|
||||
* @param command command
|
||||
* @param isNeedResultMsg whether need result msg
|
||||
* @return
|
||||
* @see ShellUtils#execCommand(String[], boolean, boolean)
|
||||
*/
|
||||
public static CommandResult execCommand(String command, boolean isRoot, boolean isNeedResultMsg) {
|
||||
return execCommand(new String[]{command}, isRoot, isNeedResultMsg);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* execute shell commands
|
||||
*
|
||||
* @param commands command list
|
||||
* @return
|
||||
* @see ShellUtils#execCommand(String[], boolean, boolean)
|
||||
*/
|
||||
public static CommandResult execCommand(List<String> commands, boolean isRoot, boolean isNeedResultMsg) {
|
||||
|
||||
return execCommand(commands == null ? null : commands.toArray(new String[]{}), isRoot, isNeedResultMsg);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* execute shell commands
|
||||
*/
|
||||
public static CommandResult execCommand(String[] commands, boolean isRoot, boolean isNeedResultMsg) {
|
||||
int result = -1;
|
||||
if (commands == null || commands.length == 0) {
|
||||
return new CommandResult(result, null, null);
|
||||
}
|
||||
Process process = null;
|
||||
BufferedReader successResult = null;
|
||||
BufferedReader errorResult = null;
|
||||
StringBuilder successMsg = null;
|
||||
StringBuilder errorMsg = null;
|
||||
DataOutputStream os = null;
|
||||
try {
|
||||
process = Runtime.getRuntime().exec(isRoot ? COMMAND_SU : COMMAND_SH);
|
||||
os = new DataOutputStream(process.getOutputStream());
|
||||
for (String command : commands) {
|
||||
if (command == null) {
|
||||
continue;
|
||||
}
|
||||
// donnot use os.writeBytes(commmand), avoid chinese charset
|
||||
// error
|
||||
os.write(command.getBytes());
|
||||
os.writeBytes(COMMAND_LINE_END);
|
||||
os.flush();
|
||||
}
|
||||
os.writeBytes(COMMAND_EXIT);
|
||||
os.flush();
|
||||
result = process.waitFor();
|
||||
// get command result
|
||||
if (isNeedResultMsg) {
|
||||
successMsg = new StringBuilder();
|
||||
errorMsg = new StringBuilder();
|
||||
successResult = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
errorResult = new BufferedReader(new InputStreamReader(process.getErrorStream()));
|
||||
String s;
|
||||
while ((s = successResult.readLine()) != null) {
|
||||
successMsg.append(s);
|
||||
}
|
||||
while ((s = errorResult.readLine()) != null) {
|
||||
errorMsg.append(s);
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (os != null) {
|
||||
os.close();
|
||||
}
|
||||
if (successResult != null) {
|
||||
successResult.close();
|
||||
}
|
||||
if (errorResult != null) {
|
||||
errorResult.close();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (process != null) {
|
||||
process.destroy();
|
||||
}
|
||||
}
|
||||
return new CommandResult(result, successMsg == null ? null : successMsg.toString(), errorMsg == null ? null : errorMsg.toString());
|
||||
}
|
||||
|
||||
public static class CommandResult {
|
||||
/**
|
||||
* 运行结果
|
||||
**/
|
||||
public int result;
|
||||
/**
|
||||
* 运行成功结果
|
||||
**/
|
||||
public String successMsg;
|
||||
/**
|
||||
* 运行失败结果
|
||||
**/
|
||||
public String errorMsg;
|
||||
|
||||
public CommandResult(int result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public CommandResult(int result, String successMsg, String errorMsg) {
|
||||
this.result = result;
|
||||
this.successMsg = successMsg;
|
||||
this.errorMsg = errorMsg;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,13 +6,41 @@
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<!-- 用于显示日志标题的 TextView -->
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:id="@+id/logTitleTextView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
android:text="日志"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
app:layout_constraintTop_toTopOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
<!-- ScrollView 包含一个 TextView 用于显示日志内容 -->
|
||||
<ScrollView
|
||||
android:id="@+id/logScrollView"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintTop_toBottomOf="@id/logTitleTextView"
|
||||
app:layout_constraintBottom_toTopOf="@+id/clearLogsButton"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/logTextView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content" />
|
||||
</ScrollView>
|
||||
|
||||
<!-- 新增加的按钮用于清空所有日志 -->
|
||||
<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>
|
||||
|
||||
@@ -10,7 +10,7 @@
|
||||
<item name="colorSecondaryVariant">@color/teal_200</item>
|
||||
<item name="colorOnSecondary">@color/black</item>
|
||||
<!-- Status bar color. -->
|
||||
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
|
||||
<item name="android:statusBarColor">@color/grey</item>
|
||||
<!-- Customize your theme here. -->
|
||||
</style>
|
||||
</resources>
|
||||
@@ -7,4 +7,5 @@
|
||||
<color name="teal_700">#FF018786</color>
|
||||
<color name="black">#FF000000</color>
|
||||
<color name="white">#FFFFFFFF</color>
|
||||
<color name="grey">#808080</color>
|
||||
</resources>
|
||||
@@ -2,5 +2,11 @@
|
||||
<string name="app_name">EchoLink</string>
|
||||
<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>
|
||||
<string name="notification_text">EchoLink正在运行</string>
|
||||
|
||||
</resources>
|
||||
@@ -2,7 +2,7 @@
|
||||
<!-- Base application theme. -->
|
||||
<style name="Theme.EchoLink" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
|
||||
<!-- Primary brand color. -->
|
||||
<item name="colorPrimary">@color/purple_500</item>
|
||||
<item name="colorPrimary">@color/teal_700</item>
|
||||
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||
<item name="colorOnPrimary">@color/white</item>
|
||||
<!-- Secondary brand color. -->
|
||||
@@ -10,7 +10,7 @@
|
||||
<item name="colorSecondaryVariant">@color/teal_700</item>
|
||||
<item name="colorOnSecondary">@color/black</item>
|
||||
<!-- Status bar color. -->
|
||||
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
|
||||
<item name="android:statusBarColor">@color/grey</item>
|
||||
<!-- Customize your theme here. -->
|
||||
</style>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user