feat: 添加返回桌面更换壁纸配置开关

允许用户控制是否在返回桌面时自动更换壁纸,默认开启以提供更好的用户体验。

新增功能:
- 添加 changeOnReturnToHome 配置项(默认开启)
- 设置界面新增开关控件,支持用户自定义
- 优化壁纸服务逻辑,根据配置决定是否更换壁纸
- 关闭状态下复用当前壁纸,减少性能消耗

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
hughhuangandClaude Sonnet 4.5 committed 2026-01-22 20:14:06 +08:00
1 parent d304297f2c
commit a5797b08a8
6 files changed
+87 -8

No files matched your search

@@ -8,6 +8,7 @@ public class AppConfig {
private int intervalMinutes;
private boolean isEnabled;
private int currentIndex;
private boolean changeOnReturnToHome;
public AppConfig() {
this.selectedFolders = new java.util.ArrayList<>();
@@ -15,6 +16,7 @@ public class AppConfig {
this.intervalMinutes = 30;
this.isEnabled = false;
this.currentIndex = 0;
this.changeOnReturnToHome = true;
}
// Getters and Setters
@@ -32,4 +34,7 @@ public class AppConfig {
public int getCurrentIndex() { return currentIndex; }
public void setCurrentIndex(int index) { this.currentIndex = index; }
public boolean isChangeOnReturnToHome() { return changeOnReturnToHome; }
public void setChangeOnReturnToHome(boolean changeOnReturnToHome) { this.changeOnReturnToHome = changeOnReturnToHome; }
}
@@ -5,6 +5,7 @@ import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;
@@ -22,6 +23,7 @@ public class SettingsActivity extends AppCompatActivity {
private RadioButton rbRandom;
private SeekBar seekBarInterval;
private TextView tvIntervalValue;
private Switch switchChangeOnReturn;
private Button btnSave;
private WallpaperPreferences prefs;
@@ -44,6 +46,7 @@ public class SettingsActivity extends AppCompatActivity {
rbRandom = findViewById(R.id.rbRandom);
seekBarInterval = findViewById(R.id.seekBarInterval);
tvIntervalValue = findViewById(R.id.tvIntervalValue);
switchChangeOnReturn = findViewById(R.id.switchChangeOnReturn);
btnSave = findViewById(R.id.btnSave);
seekBarInterval.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@@ -67,6 +70,7 @@ public class SettingsActivity extends AppCompatActivity {
rbRandom.setChecked(config.getPlayMode() == PlayMode.RANDOM);
seekBarInterval.setProgress(config.getIntervalMinutes());
tvIntervalValue.setText(config.getIntervalMinutes() + " 分钟");
switchChangeOnReturn.setChecked(config.isChangeOnReturnToHome());
}
private void saveConfig() {
@@ -78,6 +82,9 @@ public class SettingsActivity extends AppCompatActivity {
int interval = seekBarInterval.getProgress();
config.setIntervalMinutes(interval);
// Update change on return to home
config.setChangeOnReturnToHome(switchChangeOnReturn.isChecked());
// Save
prefs.saveConfig(config);
@@ -130,15 +130,37 @@ public class AutoWallpaperService extends WallpaperService {
return;
}
WallpaperImage image = getNextWallpaper(config);
if (image != null) {
drawImage(canvas, image.getUri());
WallpaperImage image;
// Save config
prefs.saveConfig(config);
prefs.saveLastUri(image.getUri());
// 核心逻辑:根据配置决定是否更换壁纸
if (config.isChangeOnReturnToHome()) {
// 开启状态:每次返回桌面都更换到下一张
image = getNextWallpaper(config);
if (image != null) {
drawImage(canvas, image.getUri());
prefs.saveConfig(config);
prefs.saveLastUri(image.getUri());
} else {
drawPlaceholder(canvas);
}
} else {
drawPlaceholder(canvas);
// 关闭状态:使用当前壁纸(不更换)
String lastUri = prefs.getLastUri();
if (lastUri != null) {
// 复用当前壁纸
drawImage(canvas, lastUri);
} else {
// 没有当前壁纸,使用下一张
image = getNextWallpaper(config);
if (image != null) {
drawImage(canvas, image.getUri());
prefs.saveConfig(config);
prefs.saveLastUri(image.getUri());
} else {
drawPlaceholder(canvas);
}
}
}
}
} catch (Exception e) {
@@ -21,6 +21,7 @@ public class WallpaperPreferences {
private static final String KEY_ENABLED = "enabled";
private static final String KEY_CURRENT_INDEX = "current_index";
private static final String KEY_LAST_URI = "last_uri";
private static final String KEY_CHANGE_ON_RETURN = "change_on_return_to_home";
private final SharedPreferences prefs;
private final Gson gson;
@@ -46,6 +47,7 @@ public class WallpaperPreferences {
config.setIntervalMinutes(prefs.getInt(KEY_INTERVAL, 30));
config.setEnabled(prefs.getBoolean(KEY_ENABLED, false));
config.setCurrentIndex(prefs.getInt(KEY_CURRENT_INDEX, 0));
config.setChangeOnReturnToHome(prefs.getBoolean(KEY_CHANGE_ON_RETURN, true));
return config;
}
@@ -59,6 +61,7 @@ public class WallpaperPreferences {
editor.putInt(KEY_INTERVAL, config.getIntervalMinutes());
editor.putBoolean(KEY_ENABLED, config.isEnabled());
editor.putInt(KEY_CURRENT_INDEX, config.getCurrentIndex());
editor.putBoolean(KEY_CHANGE_ON_RETURN, config.isChangeOnReturnToHome());
editor.apply();
}