diff --git a/GEMINI.md b/GEMINI.md new file mode 100644 index 0000000..e69de29 diff --git a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java index 38a3795..2100cb3 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java +++ b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java @@ -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); } } } \ No newline at end of file diff --git a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperPreferences.java b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperPreferences.java index 18274d8..93d9d02 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperPreferences.java +++ b/app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperPreferences.java @@ -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())