diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9fd8c96..a62fed5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,6 +15,8 @@ android:maxSdkVersion="28" /> + writeTestLogs()); + layout.addView(writeLogButton); + + // 刷新信息按钮 + refreshInfoButton = new Button(this); + refreshInfoButton.setText("刷新路径信息"); + refreshInfoButton.setOnClickListener(v -> refreshPathInfo()); + layout.addView(refreshInfoButton); + + scrollView.addView(layout); + setContentView(scrollView); + + // 显示初始路径信息 + refreshPathInfo(); + } + + private void writeTestLogs() { + LogManager.logInfo(TAG, "这是一条测试信息日志"); + LogManager.logWarning(TAG, "这是一条测试警告日志"); + LogManager.logError(TAG, "这是一条测试错误日志"); + LogManager.logOperation("测试操作", "写入测试日志成功"); + LogManager.logPerformance("测试性能", 100); + + Toast.makeText(this, "测试日志已写入", Toast.LENGTH_SHORT).show(); + + // 延迟刷新路径信息 + pathInfoText.postDelayed(this::refreshPathInfo, 1000); + } + + private void refreshPathInfo() { + String pathInfo = LogManager.getLogPathInfo(this); + pathInfoText.setText("日志路径信息:\n\n" + pathInfo); + Log.i(TAG, "日志路径信息:\n" + pathInfo); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/ouxuan/oxface/MainActivity.java b/app/src/main/java/com/ouxuan/oxface/MainActivity.java index ab79008..9b6269f 100644 --- a/app/src/main/java/com/ouxuan/oxface/MainActivity.java +++ b/app/src/main/java/com/ouxuan/oxface/MainActivity.java @@ -81,6 +81,9 @@ public class MainActivity extends AppCompatActivity { // 设置密码显示/隐藏切换 setupPasswordToggle(); + + // 设置长按登录按钮显示日志路径信息(调试功能) + setupLogPathDebug(); } /** @@ -679,4 +682,63 @@ public class MainActivity extends AppCompatActivity { LogManager.logOperation("MainActivity", "应用退出,停止保持活跃功能"); } } + + /** + * 设置日志路径调试功能(长按登录按钮) + */ + private void setupLogPathDebug() { + buttonLogin.setOnLongClickListener(new View.OnLongClickListener() { + @Override + public boolean onLongClick(View v) { + showLogPathInfo(); + return true; + } + }); + } + + /** + * 显示日志路径信息 + */ + private void showLogPathInfo() { + String pathInfo = LogManager.getLogPathInfo(this); + + // 在日志中记录 + android.util.Log.i("MainActivity", "日志路径信息:\n" + pathInfo); + + // 写入一条测试日志 + LogManager.logOperation("调试", "用户查看日志路径信息"); + + // 创建对话框显示路径信息 + android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(this); + builder.setTitle("日志路径信息(调试)"); + builder.setMessage(pathInfo); + builder.setPositiveButton("复制路径", new android.content.DialogInterface.OnClickListener() { + @Override + public void onClick(android.content.DialogInterface dialog, int which) { + // 复制日志目录路径到剪贴板 + android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(android.content.Context.CLIPBOARD_SERVICE); + if (clipboard != null) { + LogManager logManager = LogManager.getInstance(MainActivity.this); + String logDirPath = logManager.getLogDirectory().getAbsolutePath(); + android.content.ClipData clip = android.content.ClipData.newPlainText("日志目录路径", logDirPath); + clipboard.setPrimaryClip(clip); + showToast("日志目录路径已复制: " + logDirPath); + } + } + }); + builder.setNegativeButton("关闭", null); + builder.setNeutralButton("写入测试日志", new android.content.DialogInterface.OnClickListener() { + @Override + public void onClick(android.content.DialogInterface dialog, int which) { + // 写入一些测试日志 + LogManager.logInfo("DEBUG", "这是一条测试信息日志 - " + new java.util.Date()); + LogManager.logWarning("DEBUG", "这是一条测试警告日志 - " + new java.util.Date()); + LogManager.logError("DEBUG", "这是一条测试错误日志 - " + new java.util.Date()); + LogManager.logOperation("测试操作", "用户触发测试日志写入"); + LogManager.logPerformance("测试性能", 88); + showToast("测试日志已写入,请稍后再查看路径信息"); + } + }); + builder.show(); + } } \ No newline at end of file diff --git a/app/src/main/java/com/ouxuan/oxface/utils/LogManager.java b/app/src/main/java/com/ouxuan/oxface/utils/LogManager.java index 486270b..85ed0f9 100644 --- a/app/src/main/java/com/ouxuan/oxface/utils/LogManager.java +++ b/app/src/main/java/com/ouxuan/oxface/utils/LogManager.java @@ -1,6 +1,8 @@ package com.ouxuan.oxface.utils; import android.content.Context; +import android.os.Build; +import android.os.Environment; import android.util.Log; import java.io.BufferedWriter; @@ -56,14 +58,18 @@ public class LogManager { this.dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.getDefault()); this.logQueue = new LinkedBlockingQueue<>(); - // 创建日志目录 - this.logDir = new File(context.getFilesDir(), LOG_DIR); + // 优先使用外部存储目录,便于用户访问日志 + this.logDir = getLogStorageDirectory(context); if (!logDir.exists()) { - logDir.mkdirs(); + boolean created = logDir.mkdirs(); + Log.i(TAG, "创建日志目录: " + logDir.getAbsolutePath() + ", 结果: " + created); } // 设置当前日志文件 this.currentLogFile = new File(logDir, LOG_FILE_NAME); + + // 输出日志路径信息 + Log.i(TAG, "日志保存路径: " + logDir.getAbsolutePath()); } public static synchronized LogManager getInstance(Context context) { @@ -372,4 +378,68 @@ public class LogManager { public long getLogFileSize() { return currentLogFile.exists() ? currentLogFile.length() : 0; } + + /** + * 获取日志存储目录 + * 优先使用外部存储,如果不可用则使用内部存储 + */ + private File getLogStorageDirectory(Context context) { + File logDir = null; + + // Android 10(API 29)+优先使用应用专用外部目录 + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { + File externalFilesDir = context.getExternalFilesDir(null); + if (externalFilesDir != null) { + logDir = new File(externalFilesDir, LOG_DIR); + Log.i(TAG, "使用应用专用外部目录: " + logDir.getAbsolutePath()); + } + } + + // 如果外部存储不可用,使用外部存储根目录(需要权限) + if (logDir == null && Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { + File externalDir = new File(Environment.getExternalStorageDirectory(), + "Android/data/" + context.getPackageName() + "/files/" + LOG_DIR); + if (externalDir.getParentFile() != null) { + logDir = externalDir; + Log.i(TAG, "使用外部存储根目录: " + logDir.getAbsolutePath()); + } + } + + // 如果都不可用,回退到内部存储 + if (logDir == null) { + logDir = new File(context.getFilesDir(), LOG_DIR); + Log.w(TAG, "回退到内部存储目录: " + logDir.getAbsolutePath()); + } + + return logDir; + } + + /** + * 获取日志路径信息,用于调试和用户查找日志 + */ + public static String getLogPathInfo(Context context) { + if (instance == null) { + return "日志管理器未初始化"; + } + + StringBuilder info = new StringBuilder(); + info.append("日志目录: ").append(instance.logDir.getAbsolutePath()).append("\n"); + info.append("当前日志文件: ").append(instance.currentLogFile.getAbsolutePath()).append("\n"); + info.append("目录是否存在: ").append(instance.logDir.exists()).append("\n"); + info.append("日志文件大小: ").append(instance.getLogFileSize()).append(" bytes\n"); + + // 列出目录中的所有日志文件 + File[] logFiles = instance.logDir.listFiles(); + if (logFiles != null && logFiles.length > 0) { + info.append("日志文件列表:\n"); + for (File file : logFiles) { + info.append(" - ").append(file.getName()) + .append(" (").append(file.length()).append(" bytes)\n"); + } + } else { + info.append("目录中暂无日志文件\n"); + } + + return info.toString(); + } } \ No newline at end of file