Compare commits
11 Commits
fb8f5cc077
...
d3e4cc343e
| Author | SHA1 | Date |
|---|---|---|
|
|
d3e4cc343e | |
|
|
a56e528421 | |
|
|
2cc080b12c | |
|
|
1cb45c2067 | |
|
|
cdecb938a1 | |
|
|
70c28847cc | |
|
|
b63e99b857 | |
|
|
4bd88021a2 | |
|
|
dc51082cd4 | |
|
|
f161762d92 | |
|
|
b4ce52bbdc |
|
|
@ -0,0 +1,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");
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,59 @@
|
|||
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;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.Locale;
|
||||
|
||||
public class FileLoggingTree extends Timber.DebugTree {
|
||||
private final File logFile;
|
||||
|
||||
public FileLoggingTree(Context context) {
|
||||
File logDirectory = new File(context.getFilesDir(), "logs");
|
||||
if (!logDirectory.exists()) {
|
||||
logDirectory.mkdirs();
|
||||
}
|
||||
this.logFile = new File(logDirectory, "echoLink.log");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void log(int priority, String tag, String message, Throwable t) {
|
||||
super.log(priority, tag, message, t);
|
||||
|
||||
try (FileWriter fileWriter = new FileWriter(logFile, true)) {
|
||||
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
|
||||
String timestamp = dateFormat.format(new Date());
|
||||
String logMessage = timestamp + " [" + priorityToString(priority) + "] " + tag + ": " + message + "\n";
|
||||
fileWriter.append(logMessage);
|
||||
|
||||
if (t != null) {
|
||||
fileWriter.append(Log.getStackTraceString(t)).append("\n");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
Log.e("FileLoggingTree", "Error writing log to file", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String priorityToString(int priority) {
|
||||
switch (priority) {
|
||||
case Log.ERROR:
|
||||
return "ERROR";
|
||||
case Log.WARN:
|
||||
return "WARN";
|
||||
case Log.INFO:
|
||||
return "INFO";
|
||||
case Log.DEBUG:
|
||||
return "DEBUG";
|
||||
case Log.VERBOSE:
|
||||
return "VERBOSE";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,13 +1,22 @@
|
|||
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.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import com.nbee.echolink.service.MonitorService;
|
||||
import com.nbee.echolink.utils.PermissionUtils;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
private final String TAG = "MainActivity";
|
||||
private PermissionUtils permissionUtils;
|
||||
|
|
@ -20,6 +29,16 @@ public class MainActivity extends AppCompatActivity {
|
|||
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");
|
||||
}
|
||||
});
|
||||
|
||||
// 启动MonitorService服务
|
||||
Intent serviceIntent = new Intent(this, MonitorService.class);
|
||||
startService(serviceIntent);
|
||||
|
|
|
|||
|
|
@ -20,29 +20,34 @@ import com.nbee.echolink.utils.NetworkUtil;
|
|||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
public class MonitorService extends Service {
|
||||
private NetworkUtil networkUtil;
|
||||
private static final String TAG = "MonitorService";
|
||||
private String lastPhoneNumber = null;
|
||||
private long lastCallTime = 0;
|
||||
private int count;
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
Timber.d("MonitorService onCreate");
|
||||
startForegroundService();
|
||||
networkUtil = new NetworkUtil(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
Timber.d("MonitorService onStartCommand");
|
||||
if (intent != null) {
|
||||
if (intent.hasExtra("incomingNumber")) {
|
||||
String incomingNumber = intent.getStringExtra("incomingNumber");
|
||||
Log.i(TAG, "获取到来电信息,号码: "+ incomingNumber);
|
||||
Timber.d("获取到来电信息,号码: "+ incomingNumber);
|
||||
sendCallInfoToServer(incomingNumber);
|
||||
} else if (intent.hasExtra("sender") && intent.hasExtra("messageBody")) {
|
||||
String sender = intent.getStringExtra("sender");
|
||||
String messageBody = intent.getStringExtra("messageBody");
|
||||
Log.i(TAG, "获取到短信信息,号码: "+ sender);
|
||||
Timber.d("获取到短信信息,号码: "+ sender);
|
||||
sendSmsInfoToServer(sender, messageBody);
|
||||
}
|
||||
}
|
||||
|
|
@ -50,6 +55,7 @@ public class MonitorService extends Service {
|
|||
}
|
||||
|
||||
private void sendCallInfoToServer(String incomingNumber) {
|
||||
Timber.d("sendCallInfoToServer: 处理来电信息");
|
||||
if (incomingNumber.equals("null") || incomingNumber == null) {
|
||||
return;
|
||||
}
|
||||
|
|
@ -57,7 +63,7 @@ public class MonitorService extends Service {
|
|||
long currentTimeMillis = System.currentTimeMillis();
|
||||
if (incomingNumber.equals(lastPhoneNumber) && (currentTimeMillis - lastCallTime) < 70000) {
|
||||
// 如果电话号码与上次相同,且在70秒之内,则不执行请求
|
||||
Log.i(TAG, "sendCallInfoToServer: 电话号码与上次相同,且在70秒之内,不执行请求.");
|
||||
Timber.d("sendCallInfoToServer: 电话号码与上次相同,且在70秒之内,不执行请求.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
@ -73,6 +79,7 @@ public class MonitorService extends Service {
|
|||
}
|
||||
|
||||
private void sendSmsInfoToServer(String sender,String messageBody){
|
||||
Timber.d("sendSmsInfoToServer: 处理短信信息");
|
||||
if (checkNullString(sender,messageBody)){
|
||||
return;
|
||||
}
|
||||
|
|
@ -95,6 +102,7 @@ public class MonitorService extends Service {
|
|||
}
|
||||
|
||||
private void startForegroundService() {
|
||||
Timber.d("startForegroundService: 启动前台服务");
|
||||
// 创建通知频道(仅在API 26及以上版本中需要)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
||||
NotificationChannel channel = new NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT);
|
||||
|
|
@ -104,8 +112,7 @@ public class MonitorService extends Service {
|
|||
|
||||
// 创建通知
|
||||
Notification notification = new NotificationCompat.Builder(this, "channel_id")
|
||||
.setContentTitle("服务运行中")
|
||||
.setContentText("EchoLink正在运行")
|
||||
.setContentTitle(getString(R.string.notification_title))
|
||||
.setSmallIcon(R.drawable.ic_notification) // 确保您有这个图标
|
||||
.build();
|
||||
|
||||
|
|
@ -116,6 +123,7 @@ public class MonitorService extends Service {
|
|||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
Timber.d("MonitorService onBind");
|
||||
return null; // 不提供绑定服务的接口
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ import okhttp3.MediaType;
|
|||
import okhttp3.OkHttpClient;
|
||||
import okhttp3.Request;
|
||||
import okhttp3.RequestBody;
|
||||
import timber.log.Timber;
|
||||
|
||||
public class NetworkUtil {
|
||||
private final String TAG = "NetworkUtil";
|
||||
|
|
@ -45,14 +46,17 @@ public class NetworkUtil {
|
|||
OkHttpClient client = new OkHttpClient();
|
||||
// ... 构建请求 ...
|
||||
Request request = buildRequest(dataObject,apiUrl);
|
||||
Timber.d("Sending request to " + apiUrl + " (Retry count: " + retryCount + ")");
|
||||
|
||||
client.newCall(request).enqueue(new okhttp3.Callback() {
|
||||
@Override
|
||||
public void onFailure(okhttp3.Call call, IOException e) {
|
||||
Timber.d("Request to " + apiUrl + " failed on attempt " + retryCount+ e);
|
||||
if (retryCount < MAX_RETRIES) {
|
||||
handler.postDelayed(() -> postRequestWithRetry(dataObject, retryCount + 1,apiUrl), RETRY_DELAY_MS);
|
||||
} else {
|
||||
Log.e(TAG, "onFailure: Failed after " + MAX_RETRIES + " attempts", e);
|
||||
Timber.e("onFailure: Failed after " + MAX_RETRIES + " attempts" + e);
|
||||
// 超出重试次数,处理失败情况
|
||||
}
|
||||
}
|
||||
|
|
@ -60,14 +64,14 @@ public class NetworkUtil {
|
|||
public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
|
||||
if (!response.isSuccessful()) {
|
||||
// 在这里处理响应错误
|
||||
Log.d("onResponse", response.message());
|
||||
Timber.d("Request to " + apiUrl + " returned error: " + response.code() + ", " + response.message());
|
||||
throw new IOException("Unexpected code " + response);
|
||||
}
|
||||
// ... 处理响应 ...
|
||||
Gson gson1 = new Gson();
|
||||
ApiResponse apiResponse = gson1.fromJson(response.body().string(), ApiResponse.class);
|
||||
if (apiResponse.getCode().equals(0)){
|
||||
Log.i(TAG, "onResponse ResponseCode: " + apiResponse.getCode() + "ResponseMessage" + apiResponse.getMsg());
|
||||
Timber.d("Received response from " + apiUrl + ": " + apiResponse.getCode() + " - " + apiResponse.getMsg());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -77,7 +81,7 @@ public class NetworkUtil {
|
|||
Gson gson = new Gson();
|
||||
// 将对象转换为JSON字符串
|
||||
String json = gson.toJson(dataObject);
|
||||
|
||||
Timber.d("Building request to " + apiUrl + " with data: " + json);
|
||||
// 创建请求体
|
||||
MediaType JSON = MediaType.get("application/json; charset=utf-8");
|
||||
RequestBody body = RequestBody.create(json, JSON);
|
||||
|
|
|
|||
|
|
@ -9,6 +9,10 @@ import android.app.AlertDialog;
|
|||
import android.content.DialogInterface;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import timber.log.Timber;
|
||||
|
||||
|
||||
public class PermissionUtils {
|
||||
private static final int PERMISSION_REQUEST_CODE = 1;
|
||||
|
|
@ -23,10 +27,13 @@ public class PermissionUtils {
|
|||
Manifest.permission.READ_PHONE_STATE,
|
||||
Manifest.permission.SEND_SMS,
|
||||
Manifest.permission.READ_CALL_LOG,
|
||||
Manifest.permission.READ_CONTACTS
|
||||
Manifest.permission.READ_CONTACTS,
|
||||
Manifest.permission.RECEIVE_SMS
|
||||
|
||||
};
|
||||
|
||||
if (!hasPermissions(permissions)) {
|
||||
Timber.d("请求以下权限:" + Arrays.toString(permissions));
|
||||
ActivityCompat.requestPermissions(activity, permissions, PERMISSION_REQUEST_CODE);
|
||||
}
|
||||
}
|
||||
|
|
@ -34,6 +41,7 @@ public class PermissionUtils {
|
|||
private boolean hasPermissions(String... permissions) {
|
||||
for (String permission : permissions) {
|
||||
if (ContextCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
|
||||
Timber.d("缺少权限:" + permission);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
@ -53,10 +61,12 @@ public class PermissionUtils {
|
|||
|
||||
if (deniedPermissions.length() > 0) {
|
||||
// 显示对话框
|
||||
Timber.d("权限被拒绝:" + deniedPermissions.toString());
|
||||
showAlert("以下权限被拒绝:\n" + deniedPermissions.toString());
|
||||
} else {
|
||||
// 所有请求的权限都被授予
|
||||
// 在这里处理所有权限被授予的情况
|
||||
Timber.d("已获取所有权限:" + Arrays.toString(permissions));
|
||||
showToast("已授予运行所需的权限");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@
|
|||
tools:context=".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/textView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
|
|
@ -15,4 +16,14 @@
|
|||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/myButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Click Me"
|
||||
app:layout_constraintTop_toBottomOf="@id/textView"
|
||||
app:layout_constraintStart_toStartOf="@id/textView"
|
||||
app:layout_constraintEnd_toEndOf="@id/textView" />
|
||||
|
||||
|
||||
</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>
|
||||
|
|
@ -3,4 +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="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>
|
||||
Loading…
Reference in New Issue