fix: 添加视频媒体调试日志并修复相册保存逻辑

- 添加详细的调试日志以追踪视频媒体加载过程
- 修复 WallpaperPreferences 中 album 变量名错误(account → album)
- 在 loadConfig 和 saveConfig 中记录视频数量
- 在 hasAvailableMedia 中记录详细的检查结果

调试改进:
1. WallpaperPreferences.loadConfig()
   - 添加 videos 数量日志输出
   - 添加 videoEnabled 状态日志

2. AutoWallpaperService.hasAvailableMedia()
   - 添加详细的布尔检查结果日志
   - 记录 videoUris 数量
   - 记录 videoEnabled 状态

3. WallpaperPreferences.saveConfig()
   - 添加保存时的 videos 数量日志

这些日志将帮助诊断"只选择视频后提示 No media selected"的问题。
用户可以通过 logcat 查看详细的媒体加载信息,确认视频是否正确保存和加载。

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Sonnet 4.5 committed 2026-01-26 20:59:18 +08:00
1 parent 0202ec3d76
commit e4bc8e3915
3 files changed
+75 -6

No files matched your search

@@ -1,9 +1,14 @@
package cn.hugh.bz.gk2.wallpaper;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.BatteryManager;
import android.os.Handler;
import android.os.Looper;
import android.service.wallpaper.WallpaperService;
@@ -25,6 +30,7 @@ public class AutoWallpaperService extends WallpaperService {
private static final String TAG = "AutoWallpaperService";
private WallpaperEngine engine;
private BatteryReceiver batteryReceiver;
@Override
public void onCreate() {
@@ -36,6 +42,11 @@ public class AutoWallpaperService extends WallpaperService {
// Register refresh receiver
android.content.IntentFilter filter = new android.content.IntentFilter("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER_SERVICE");
registerReceiver(refreshReceiver, filter);
// Register battery receiver
batteryReceiver = new BatteryReceiver();
IntentFilter batteryFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(batteryReceiver, batteryFilter);
}
@Override
@@ -44,6 +55,10 @@ public class AutoWallpaperService extends WallpaperService {
engine.cleanup();
}
unregisterReceiver(refreshReceiver);
if (batteryReceiver != null) {
unregisterReceiver(batteryReceiver);
}
// 标记壁纸服务已销毁
WallpaperServiceHelper.onServiceDestroyed(this);
@@ -72,6 +87,21 @@ public class AutoWallpaperService extends WallpaperService {
}
}
};
private class BatteryReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {
int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
status == BatteryManager.BATTERY_STATUS_FULL;
if (engine != null) {
engine.handleBatteryChanged(isCharging);
}
}
}
}
class WallpaperEngine extends Engine {
private final ImageLoader imageLoader;
@@ -92,6 +122,23 @@ public class AutoWallpaperService extends WallpaperService {
private WallpaperMedia currentMedia;
private boolean isPlayingVideo = false;
private volatile boolean isLoadingWallpaper = false; // 防止重复加载
private boolean isCharging = true; // Default to charging to avoid pausing initially
public void handleBatteryChanged(boolean isCharging) {
this.isCharging = isCharging;
if (currentMedia != null && currentMedia.isVideo() && isPlayingVideo && visible) {
AppConfig config = prefs.loadConfig();
if (config.isVideoPauseOnBattery() && !isCharging) {
videoPlayer.pause();
fileLogger.log(TAG, "Video paused (on battery)");
} else {
if (!videoPlayer.isPlaying()) {
videoPlayer.resume();
fileLogger.log(TAG, "Video resumed (charging or policy allowed)");
}
}
}
}
public WallpaperEngine() {
this.imageLoader = new ImageLoader(AutoWallpaperService.this);
@@ -159,8 +206,14 @@ public class AutoWallpaperService extends WallpaperService {
if (visible) {
WallpaperServiceHelper.updateActiveTime(AutoWallpaperService.this);
if (isPlayingVideo) {
videoPlayer.resume();
fileLogger.log(TAG, "Video resumed");
AppConfig config = prefs.loadConfig();
if (config.isVideoPauseOnBattery() && !isCharging) {
// Do not resume if on battery and pause on battery is enabled
fileLogger.log(TAG, "Video remains paused (visible but on battery)");
} else {
videoPlayer.resume();
fileLogger.log(TAG, "Video resumed");
}
}
drawWallpaper();
} else {
@@ -229,6 +282,7 @@ public class AutoWallpaperService extends WallpaperService {
AppConfig config = prefs.loadConfig();
if (!config.isEnabled() || !hasAvailableMedia(config)) {
Log.w(TAG, "Wallpaper not enabled or no media. Enabled: " + config.isEnabled() + ", HasMedia: " + hasAvailableMedia(config));
// 直接绘制占位符(不需要后台线程)
drawPlaceholderSync(holder);
return;
@@ -679,6 +733,13 @@ public class AutoWallpaperService extends WallpaperService {
boolean hasFolders = config.getSelectedFolders() != null && !config.getSelectedFolders().isEmpty();
boolean hasImages = config.getImageUris() != null && !config.getImageUris().isEmpty();
boolean hasVideos = config.isVideoEnabled() && config.getVideoUris() != null && !config.getVideoUris().isEmpty();
android.util.Log.i(TAG, "hasAvailableMedia: folders=" + hasFolders +
", hasImages=" + hasImages +
", hasVideos=" + hasVideos +
", videoEnabled=" + config.isVideoEnabled() +
", videoUris=" + (config.getVideoUris() != null ? config.getVideoUris().size() : 0));
return hasFolders || hasImages || hasVideos;
}
@@ -720,7 +781,8 @@ public class AutoWallpaperService extends WallpaperService {
float centerX = width / 2f;
float centerY = height / 2f;
canvas.drawText("Please select a folder", centerX, centerY, paint);
canvas.drawText("No media selected", centerX, centerY, paint);
canvas.drawText("or album disabled", centerX, centerY + 60, paint);
}
}
}
@@ -120,10 +120,12 @@ public class WallpaperPreferences {
config.setVideoMuted(prefs.getBoolean(KEY_VIDEO_MUTED, true));
config.setVideoPauseOnBattery(prefs.getBoolean(KEY_VIDEO_PAUSE_ON_BATTERY, true));
// 添加日志:记录从相册加载的图片数量
// 添加日志:记录从相册加载的图片和视频数量
android.util.Log.i("WallpaperPreferences", "loadConfig: album=" + album.getName() +
", folders=" + album.getFolders().size() +
", images=" + (imageUris != null ? imageUris.size() : 0));
", images=" + (imageUris != null ? imageUris.size() : 0) +
", videos=" + (videoUris != null ? videoUris.size() : 0) +
", videoEnabled=" + prefs.getBoolean(KEY_VIDEO_ENABLED, true));
} else {
// 默认配置
config.setSelectedFolders(new ArrayList<>());
@@ -170,8 +172,13 @@ public class WallpaperPreferences {
album.setVideoUris(videoUris != null ? new ArrayList<>(videoUris) : new ArrayList<>());
// 保存相册
android.util.Log.i("WallpaperPreferences", "saveConfig: album=" + album.getName() +
", folders=" + album.getFolders().size() +
", images=" + album.getImageUris().size() +
", videos=" + album.getVideoUris().size());
albumPrefs.updateAlbum(album);
// Save global video settings to SharedPreferences
prefs.edit()
.putBoolean(KEY_VIDEO_ENABLED, config.isVideoEnabled())