diff --git a/app/src/main/java/cn/hugh/bz/gk2/ui/SettingsFragment.java b/app/src/main/java/cn/hugh/bz/gk2/ui/SettingsFragment.java index b373de4..61a8855 100644 --- a/app/src/main/java/cn/hugh/bz/gk2/ui/SettingsFragment.java +++ b/app/src/main/java/cn/hugh/bz/gk2/ui/SettingsFragment.java @@ -4,6 +4,7 @@ import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.widget.ImageView; import android.widget.SeekBar; import android.widget.Switch; import android.widget.TextView; @@ -19,6 +20,7 @@ import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import com.bumptech.glide.Glide; import com.google.android.material.button.MaterialButton; import cn.hugh.bz.gk2.R; @@ -50,8 +52,14 @@ public class SettingsFragment extends Fragment { // 双击设置控件 private Switch switchDoubleTapEnabled; + // 固定壁纸控件 + private MaterialButton btnSelectFixedWallpaper; + private MaterialButton btnClearFixedWallpaper; + private ImageView ivFixedWallpaperPreview; + // 图片选择器 private ActivityResultLauncher pickImageLauncher; + private ActivityResultLauncher pickFixedWallpaperLauncher; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { @@ -67,11 +75,13 @@ public class SettingsFragment extends Fragment { // 初始化图片选择器 initPickImageLauncher(); + initPickFixedWallpaperLauncher(); initViews(view); loadBackgroundConfig(); loadVideoSettings(); loadDoubleTapSettings(); + loadFixedWallpaperSettings(); } private void initViews(View view) { @@ -123,8 +133,14 @@ public class SettingsFragment extends Fragment { // 双击设置控件 switchDoubleTapEnabled = view.findViewById(R.id.switchDoubleTapEnabled); + // 固定壁纸控件 + btnSelectFixedWallpaper = view.findViewById(R.id.btnSelectFixedWallpaper); + btnClearFixedWallpaper = view.findViewById(R.id.btnClearFixedWallpaper); + ivFixedWallpaperPreview = view.findViewById(R.id.ivFixedWallpaperPreview); + setupVideoSettingsListeners(); setupDoubleTapListener(); + setupFixedWallpaperListeners(); } private void setupVideoSettingsListeners() { @@ -268,11 +284,144 @@ public class SettingsFragment extends Fragment { switchDoubleTapEnabled.setChecked(config.isDoubleTapEnabled()); } + // ==================== 固定壁纸相关方法 ==================== + + /** + * 初始化固定壁纸选择器 + */ + private void initPickFixedWallpaperLauncher() { + pickFixedWallpaperLauncher = registerForActivityResult( + new ActivityResultContracts.StartActivityForResult(), + result -> { + if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) { + Uri uri = result.getData().getData(); + if (uri != null) { + // 保存持久化权限 + try { + requireContext().getContentResolver().takePersistableUriPermission( + uri, + Intent.FLAG_GRANT_READ_URI_PERMISSION + ); + } catch (Exception e) { + android.util.Log.e("SettingsFragment", "Error taking URI permission for fixed wallpaper", e); + } + + // 设置固定壁纸(只保存 URI,不启用,继续自动更新) + AppConfig config = wallpaperPrefs.loadConfig(); + config.setFixedWallpaperUri(uri.toString()); + // 注意:不设置 isFixedWallpaperEnabled = true + // 固定壁纸模式会在四指点击后启用 + wallpaperPrefs.saveConfig(config); + + // 更新预览 + updateFixedWallpaperPreview(); + + // 发送广播刷新壁纸 + refreshWallpaperService(); + + Toast.makeText(requireContext(), "固定壁纸已选择,请四指点击桌面应用", Toast.LENGTH_LONG).show(); + } + } + } + ); + } + + /** + * 设置固定壁纸监听器 + */ + private void setupFixedWallpaperListeners() { + // 选择固定壁纸 + btnSelectFixedWallpaper.setOnClickListener(v -> { + // 确认是否要设置固定壁纸 + new androidx.appcompat.app.AlertDialog.Builder(requireContext()) + .setTitle("设置固定壁纸") + .setMessage("设置固定壁纸后,请四指点击桌面将其设置为系统壁纸并禁用自动更新。") + .setPositiveButton("确定", (dialog, which) -> pickFixedWallpaper()) + .setNegativeButton("取消", null) + .show(); + }); + + // 清除固定壁纸 + btnClearFixedWallpaper.setOnClickListener(v -> { + new androidx.appcompat.app.AlertDialog.Builder(requireContext()) + .setTitle("清除固定壁纸") + .setMessage("确定要清除固定壁纸并恢复自动更新吗?") + .setPositiveButton("确定", (dialog, which) -> clearFixedWallpaper()) + .setNegativeButton("取消", null) + .show(); + }); + } + + /** + * 选择固定壁纸 + */ + private void pickFixedWallpaper() { + Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); + intent.addCategory(Intent.CATEGORY_OPENABLE); + intent.setType("image/*"); + pickFixedWallpaperLauncher.launch(intent); + } + + /** + * 清除固定壁纸 + */ + private void clearFixedWallpaper() { + AppConfig config = wallpaperPrefs.loadConfig(); + config.setFixedWallpaperEnabled(false); + config.setFixedWallpaperUri(null); + wallpaperPrefs.saveConfig(config); + + updateFixedWallpaperPreview(); + refreshWallpaperService(); + + Toast.makeText(requireContext(), "已恢复自动更新", Toast.LENGTH_SHORT).show(); + } + + /** + * 加载固定壁纸设置 + */ + private void loadFixedWallpaperSettings() { + updateFixedWallpaperPreview(); + } + + /** + * 更新固定壁纸预览 + */ + private void updateFixedWallpaperPreview() { + AppConfig config = wallpaperPrefs.loadConfig(); + String fixedUri = config.getFixedWallpaperUri(); + + if (fixedUri != null && config.isFixedWallpaperEnabled()) { + // 使用 Glide 加载缩略图 + Glide.with(this) + .load(fixedUri) + .centerCrop() + .placeholder(cn.hugh.bz.gk2.R.drawable.ic_image) + .into(ivFixedWallpaperPreview); + + btnClearFixedWallpaper.setVisibility(View.VISIBLE); + } else { + ivFixedWallpaperPreview.setImageURI(null); + btnClearFixedWallpaper.setVisibility(View.GONE); + } + } + + /** + * 刷新壁纸服务 + */ + private void refreshWallpaperService() { + Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER_SERVICE"); + intent.setPackage("cn.hugh.bz.gk2"); + intent.putExtra("force_change", true); + requireContext().sendBroadcast(intent); + } + @Override public void onResume() { super.onResume(); loadBackgroundConfig(); loadVideoSettings(); loadDoubleTapSettings(); + loadFixedWallpaperSettings(); } }