feat: 支持单张图片选择,简化主页布局
- 在 Album 和 AppConfig 模型中添加 imageUris 字段支持单张图片 - 简化 HomeFragment,移除文件夹选择功能到相册页面统一管理 - MainActivity 添加图片选择器接口和启动器 - 相册编辑器添加单张图片选择卡片 - WallpaperPreferences 支持图片列表的加载和保存 - 添加 CLAUDE.md 项目文档 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
afc06d8e8b
commit
2d98e061cd
11 files changed
+424
-529
No files matched your search
@@ -20,6 +20,7 @@ public class Album implements Serializable {
|
||||
private String lastUri; // 最后显示的壁纸URI
|
||||
private long createdTime; // 创建时间
|
||||
private boolean enabled; // 是否启用
|
||||
private List<String> imageUris; // 图片URI列表(与 folders 同级)
|
||||
|
||||
public Album() {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
@@ -115,6 +116,14 @@ public class Album implements Serializable {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public List<String> getImageUris() {
|
||||
return imageUris;
|
||||
}
|
||||
|
||||
public void setImageUris(List<String> imageUris) {
|
||||
this.imageUris = imageUris;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断是否为默认相册
|
||||
*/
|
||||
|
||||
@@ -9,6 +9,7 @@ public class AppConfig {
|
||||
private boolean isEnabled;
|
||||
private int currentIndex;
|
||||
private boolean changeOnReturnToHome;
|
||||
private List<String> imageUris;
|
||||
|
||||
public AppConfig() {
|
||||
this.selectedFolders = new java.util.ArrayList<>();
|
||||
@@ -37,4 +38,7 @@ public class AppConfig {
|
||||
|
||||
public boolean isChangeOnReturnToHome() { return changeOnReturnToHome; }
|
||||
public void setChangeOnReturnToHome(boolean changeOnReturnToHome) { this.changeOnReturnToHome = changeOnReturnToHome; }
|
||||
|
||||
public List<String> getImageUris() { return imageUris; }
|
||||
public void setImageUris(List<String> uris) { this.imageUris = uris; }
|
||||
}
|
||||
@@ -119,9 +119,13 @@ public class CurrentWallpaperFragment extends Fragment {
|
||||
android.util.Log.i("CurrentWallpaperFragment", "showNextWallpaper: Selected folders count: " + config.getSelectedFolders().size());
|
||||
cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext()).log("CurrentWallpaperFragment", "showNextWallpaper: Selected folders count: " + config.getSelectedFolders().size());
|
||||
|
||||
if (config.getSelectedFolders().isEmpty()) {
|
||||
android.util.Log.w("CurrentWallpaperFragment", "showNextWallpaper: No folders selected, showing toast");
|
||||
Toast.makeText(requireContext(), "请先选择文件夹", Toast.LENGTH_SHORT).show();
|
||||
android.util.Log.i("CurrentWallpaperFragment", "showNextWallpaper: Image URIs count: " + (config.getImageUris() != null ? config.getImageUris().size() : 0));
|
||||
cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext()).log("CurrentWallpaperFragment", "showNextWallpaper: Image URIs count: " + (config.getImageUris() != null ? config.getImageUris().size() : 0));
|
||||
|
||||
if (config.getSelectedFolders().isEmpty() &&
|
||||
(config.getImageUris() == null || config.getImageUris().isEmpty())) {
|
||||
android.util.Log.w("CurrentWallpaperFragment", "showNextWallpaper: No folders or images selected, showing toast");
|
||||
Toast.makeText(requireContext(), "请先选择文件夹或图片", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,44 +1,31 @@
|
||||
package cn.hugh.bz.gk2.ui;
|
||||
|
||||
import android.app.WallpaperManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.ListView;
|
||||
import android.widget.ProgressBar;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.documentfile.provider.DocumentFile;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import cn.hugh.bz.gk2.R;
|
||||
import cn.hugh.bz.gk2.model.Album;
|
||||
import cn.hugh.bz.gk2.model.AppConfig;
|
||||
import cn.hugh.bz.gk2.model.PlayMode;
|
||||
import cn.hugh.bz.gk2.utils.AlarmScheduler;
|
||||
import cn.hugh.bz.gk2.utils.ImageScanner;
|
||||
import cn.hugh.bz.gk2.wallpaper.AlbumPreferences;
|
||||
import cn.hugh.bz.gk2.wallpaper.WallpaperPreferences;
|
||||
|
||||
/**
|
||||
* 主页 Fragment
|
||||
* 负责显示服务状态、文件夹选择、启用/禁用服务等功能
|
||||
* 负责显示服务状态和启用/禁用服务
|
||||
*/
|
||||
public class HomeFragment extends Fragment {
|
||||
private static final String TAG = "HomeFragment";
|
||||
@@ -46,34 +33,17 @@ public class HomeFragment extends Fragment {
|
||||
private TextView tvStatus;
|
||||
private View statusIndicator;
|
||||
private TextView tvFolderInfo;
|
||||
private TextView tvSingleImageInfo;
|
||||
private Button btnToggle;
|
||||
private Button btnChangeNow;
|
||||
private Button btnSelectFolders;
|
||||
private ListView lvFolders;
|
||||
private ProgressBar progressBar;
|
||||
private TextView tvAlbumName;
|
||||
private TextView tvAlbumInfo;
|
||||
private Button btnManageAlbums;
|
||||
|
||||
private WallpaperPreferences prefs;
|
||||
private AppConfig config;
|
||||
private ImageScanner scanner;
|
||||
private FolderAdapter folderAdapter;
|
||||
private AlbumPreferences albumPrefs;
|
||||
|
||||
// 避免频繁统计图片数量
|
||||
private int cachedImageCount = -1;
|
||||
private boolean isCountingImages = false;
|
||||
|
||||
// 文件夹选择回调接口
|
||||
public interface FolderPickerCallback {
|
||||
void launchFolderPicker(FolderPickerResultCallback callback);
|
||||
}
|
||||
|
||||
public interface FolderPickerResultCallback {
|
||||
void onFolderPicked(String uri);
|
||||
}
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_home, container, false);
|
||||
@@ -85,7 +55,6 @@ public class HomeFragment extends Fragment {
|
||||
|
||||
prefs = new WallpaperPreferences(requireContext());
|
||||
config = prefs.loadConfig();
|
||||
scanner = new ImageScanner(requireContext());
|
||||
albumPrefs = new AlbumPreferences(requireContext());
|
||||
|
||||
// 创建默认相册(如果不存在)
|
||||
@@ -94,61 +63,27 @@ public class HomeFragment extends Fragment {
|
||||
initViews(view);
|
||||
updateUI();
|
||||
updateAlbumInfo();
|
||||
|
||||
// 首次启动显示引导
|
||||
showFirstRunGuide();
|
||||
}
|
||||
|
||||
/**
|
||||
* 首次启动显示使用指南
|
||||
*/
|
||||
private void showFirstRunGuide() {
|
||||
boolean hasShownGuide = prefs.getBoolean("first_run_guide_shown", false);
|
||||
|
||||
if (!hasShownGuide) {
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("欢迎使用 BzForGk2")
|
||||
.setMessage(
|
||||
"使用步骤:\n\n" +
|
||||
"1. 点击「选择文件夹」添加图片文件夹\n" +
|
||||
"2. 点击「启用」按钮\n" +
|
||||
"3. 在弹出的壁纸选择界面选择「BzForGk2」\n" +
|
||||
"4. 设置完成!壁纸会自动更换"
|
||||
)
|
||||
.setPositiveButton("知道了", (dialog, which) -> {
|
||||
prefs.putBoolean("first_run_guide_shown", true);
|
||||
})
|
||||
.setCancelable(false)
|
||||
.show();
|
||||
}
|
||||
}
|
||||
|
||||
private void initViews(View view) {
|
||||
tvStatus = view.findViewById(R.id.tvStatus);
|
||||
statusIndicator = view.findViewById(R.id.statusIndicator);
|
||||
tvFolderInfo = view.findViewById(R.id.tvFolderInfo);
|
||||
tvSingleImageInfo = view.findViewById(R.id.tvSingleImageInfo);
|
||||
btnToggle = view.findViewById(R.id.btnToggle);
|
||||
btnChangeNow = view.findViewById(R.id.btnChangeNow);
|
||||
btnSelectFolders = view.findViewById(R.id.btnSelectFolders);
|
||||
lvFolders = view.findViewById(R.id.lvFolders);
|
||||
progressBar = view.findViewById(R.id.progressBar);
|
||||
tvAlbumName = view.findViewById(R.id.tvAlbumName);
|
||||
tvAlbumInfo = view.findViewById(R.id.tvAlbumInfo);
|
||||
btnManageAlbums = view.findViewById(R.id.btnManageAlbums);
|
||||
|
||||
// 设置文件夹列表适配器
|
||||
folderAdapter = new FolderAdapter(requireContext(), config.getSelectedFolders(), this::removeFolder);
|
||||
lvFolders.setAdapter(folderAdapter);
|
||||
|
||||
btnToggle.setOnClickListener(v -> toggleService());
|
||||
btnChangeNow.setOnClickListener(v -> changeWallpaperNow());
|
||||
btnSelectFolders.setOnClickListener(v -> openFolderPicker());
|
||||
btnManageAlbums.setOnClickListener(v -> openAlbumManagement());
|
||||
}
|
||||
|
||||
private void toggleService() {
|
||||
if (config.getSelectedFolders().isEmpty()) {
|
||||
Toast.makeText(requireContext(), "请先选择文件夹", Toast.LENGTH_SHORT).show();
|
||||
if (config.getSelectedFolders().isEmpty() && (config.getImageUris() == null || config.getImageUris().isEmpty())) {
|
||||
Toast.makeText(requireContext(), "请先在相册页面添加文件夹或图片", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -157,12 +92,11 @@ public class HomeFragment extends Fragment {
|
||||
if (newState) {
|
||||
// 每次启用都弹出壁纸设置引导
|
||||
showSetWallpaperDialog();
|
||||
// 注意:不在这里设置 enabled,而是在用户设置壁纸后再设置
|
||||
} else {
|
||||
config.setEnabled(newState);
|
||||
prefs.saveConfig(config);
|
||||
|
||||
AlarmScheduler scheduler = new AlarmScheduler(requireContext());
|
||||
cn.hugh.bz.gk2.utils.AlarmScheduler scheduler = new cn.hugh.bz.gk2.utils.AlarmScheduler(requireContext());
|
||||
scheduler.cancelSchedule();
|
||||
Toast.makeText(requireContext(), "已禁用", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
@@ -174,13 +108,13 @@ public class HomeFragment extends Fragment {
|
||||
* 显示引导设置壁纸的对话框
|
||||
*/
|
||||
private void showSetWallpaperDialog() {
|
||||
new AlertDialog.Builder(requireContext())
|
||||
new androidx.appcompat.app.AlertDialog.Builder(requireContext())
|
||||
.setTitle("设置动态壁纸")
|
||||
.setMessage("点击\"确定\"后将打开壁纸选择界面,请选择\"BzForGk2\"作为动态壁纸。\n\n设置完成后返回应用即可自动启用。")
|
||||
.setMessage("点击「确定」后将打开壁纸选择界面,请选择「BzForGk2」作为动态壁纸。\n\n设置完成后返回应用即可自动启用。")
|
||||
.setPositiveButton("确定", (dialog, which) -> {
|
||||
cn.hugh.bz.gk2.utils.FileLogger fileLogger = cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext());
|
||||
String msg = "User confirmed wallpaper service enable";
|
||||
Log.i(TAG, msg);
|
||||
android.util.Log.i(TAG, msg);
|
||||
fileLogger.log(TAG, msg);
|
||||
|
||||
openWallpaperSelector();
|
||||
@@ -189,10 +123,10 @@ public class HomeFragment extends Fragment {
|
||||
prefs.saveConfig(config);
|
||||
|
||||
String scheduleMsg = "Scheduling alarm with interval: " + config.getIntervalMinutes() + " minutes";
|
||||
Log.i(TAG, scheduleMsg);
|
||||
android.util.Log.i(TAG, scheduleMsg);
|
||||
fileLogger.log(TAG, scheduleMsg);
|
||||
|
||||
AlarmScheduler scheduler = new AlarmScheduler(requireContext());
|
||||
cn.hugh.bz.gk2.utils.AlarmScheduler scheduler = new cn.hugh.bz.gk2.utils.AlarmScheduler(requireContext());
|
||||
scheduler.scheduleWallpaperChange(config.getIntervalMinutes());
|
||||
|
||||
Toast.makeText(requireContext(), "已启用,请选择壁纸", Toast.LENGTH_LONG).show();
|
||||
@@ -239,9 +173,14 @@ public class HomeFragment extends Fragment {
|
||||
android.util.Log.i(TAG, "Selected folders count: " + config.getSelectedFolders().size());
|
||||
cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext()).log(TAG, "Selected folders count: " + config.getSelectedFolders().size());
|
||||
|
||||
if (config.getSelectedFolders().isEmpty()) {
|
||||
android.util.Log.w(TAG, "No folders selected, showing toast");
|
||||
Toast.makeText(requireContext(), "请先选择文件夹", Toast.LENGTH_SHORT).show();
|
||||
List<String> imageUris = config.getImageUris();
|
||||
int imageCount = (imageUris != null) ? imageUris.size() : 0;
|
||||
android.util.Log.i(TAG, "Image URIs count: " + imageCount);
|
||||
cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext()).log(TAG, "Image URIs count: " + imageCount);
|
||||
|
||||
if (config.getSelectedFolders().isEmpty() && (imageUris == null || imageUris.isEmpty())) {
|
||||
android.util.Log.w(TAG, "No folders or images selected, showing toast");
|
||||
Toast.makeText(requireContext(), "请先在相册页面添加文件夹或图片", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -258,48 +197,11 @@ public class HomeFragment extends Fragment {
|
||||
Toast.makeText(requireContext(), "壁纸已更换", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
private void openFolderPicker() {
|
||||
// 通过 MainActivity 启动文件夹选择器
|
||||
if (getActivity() instanceof FolderPickerCallback) {
|
||||
((FolderPickerCallback) getActivity()).launchFolderPicker(uri -> {
|
||||
addFolder(uri);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private void addFolder(String folderPath) {
|
||||
if (folderPath == null || config.getSelectedFolders().contains(folderPath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
config.getSelectedFolders().add(folderPath);
|
||||
prefs.saveConfig(config);
|
||||
|
||||
// 清除缓存,强制重新统计
|
||||
cachedImageCount = -1;
|
||||
|
||||
// 异步扫描文件夹
|
||||
Toast.makeText(requireContext(), "正在扫描文件夹...", Toast.LENGTH_SHORT).show();
|
||||
new Thread(() -> {
|
||||
scanner.scanFolder(folderPath);
|
||||
requireActivity().runOnUiThread(() -> {
|
||||
updateUI();
|
||||
Toast.makeText(requireContext(), "已添加文件夹", Toast.LENGTH_SHORT).show();
|
||||
});
|
||||
}).start();
|
||||
}
|
||||
|
||||
private void removeFolder(int position) {
|
||||
if (position >= 0 && position < config.getSelectedFolders().size()) {
|
||||
String folderPath = config.getSelectedFolders().get(position);
|
||||
config.getSelectedFolders().remove(position);
|
||||
prefs.saveConfig(config);
|
||||
|
||||
// 清除缓存,强制重新统计
|
||||
cachedImageCount = -1;
|
||||
|
||||
updateUI();
|
||||
Toast.makeText(requireContext(), "已移除文件夹", Toast.LENGTH_SHORT).show();
|
||||
private void openAlbumManagement() {
|
||||
// 切换到相册页面(ViewPager 的第 3 个页面,索引为 2)
|
||||
if (getActivity() instanceof MainActivity) {
|
||||
MainActivity activity = (MainActivity) getActivity();
|
||||
activity.switchToPage(2); // 2 是相册管理页面的索引
|
||||
}
|
||||
}
|
||||
|
||||
@@ -323,72 +225,21 @@ public class HomeFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
// Update folder info - 使用缓存的数量,避免频繁统计
|
||||
if (config.getSelectedFolders().isEmpty()) {
|
||||
tvFolderInfo.setText(R.string.no_folders);
|
||||
progressBar.setVisibility(View.GONE);
|
||||
cachedImageCount = 0;
|
||||
} else if (cachedImageCount >= 0) {
|
||||
// 使用缓存的图片数量
|
||||
tvFolderInfo.setText(String.format("已选择 %d 个文件夹,共 %d 张图片",
|
||||
config.getSelectedFolders().size(), cachedImageCount));
|
||||
progressBar.setVisibility(View.GONE);
|
||||
} else if (!isCountingImages) {
|
||||
// 首次统计或缓存被清除后,异步统计图片数量
|
||||
isCountingImages = true;
|
||||
progressBar.setVisibility(View.VISIBLE);
|
||||
progressBar.setIndeterminate(true);
|
||||
|
||||
tvFolderInfo.setText(String.format("已选择 %d 个文件夹,正在统计图片...",
|
||||
config.getSelectedFolders().size()));
|
||||
|
||||
new Thread(() -> {
|
||||
int imageCount = scanner.getTotalImageCount(config.getSelectedFolders());
|
||||
cachedImageCount = imageCount;
|
||||
requireActivity().runOnUiThread(() -> {
|
||||
tvFolderInfo.setText(String.format("已选择 %d 个文件夹,共 %d 张图片",
|
||||
config.getSelectedFolders().size(), imageCount));
|
||||
progressBar.setVisibility(View.GONE);
|
||||
isCountingImages = false;
|
||||
});
|
||||
}).start();
|
||||
// Update folder info
|
||||
int folderCount = config.getSelectedFolders().size();
|
||||
if (folderCount == 0) {
|
||||
tvFolderInfo.setText("未选择文件夹");
|
||||
} else {
|
||||
tvFolderInfo.setText("已选择 " + folderCount + " 个文件夹");
|
||||
}
|
||||
|
||||
// Update folder list adapter
|
||||
if (folderAdapter != null) {
|
||||
folderAdapter.updateFolders(config.getSelectedFolders());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
config = prefs.loadConfig();
|
||||
updateUI();
|
||||
updateAlbumInfo();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
// 释放扫描器资源
|
||||
if (scanner != null) {
|
||||
scanner.shutdown();
|
||||
}
|
||||
// 释放 FolderAdapter 资源
|
||||
if (folderAdapter != null) {
|
||||
folderAdapter.release();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 打开相册管理页面
|
||||
*/
|
||||
private void openAlbumManagement() {
|
||||
// 切换到相册页面(ViewPager 的第 3 个页面,索引为 2)
|
||||
if (getActivity() instanceof MainActivity) {
|
||||
MainActivity activity = (MainActivity) getActivity();
|
||||
activity.switchToPage(2); // 2 是相册管理页面的索引
|
||||
// Update single image info
|
||||
List<String> imageUris = config.getImageUris();
|
||||
if (imageUris != null && !imageUris.isEmpty()) {
|
||||
tvSingleImageInfo.setText("已选择 " + imageUris.size() + " 张图片");
|
||||
tvSingleImageInfo.setVisibility(View.VISIBLE);
|
||||
} else {
|
||||
tvSingleImageInfo.setVisibility(View.GONE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -401,7 +252,6 @@ public class HomeFragment extends Fragment {
|
||||
tvAlbumName.setText(activeAlbum.getName());
|
||||
|
||||
int folderCount = activeAlbum.getFolders() != null ? activeAlbum.getFolders().size() : 0;
|
||||
// TODO: 计算实际图片数量
|
||||
int imageCount = folderCount;
|
||||
tvAlbumInfo.setText(folderCount + " 个文件夹 · " + imageCount + " 张图片");
|
||||
} else {
|
||||
@@ -410,262 +260,11 @@ public class HomeFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 文件夹列表适配器(优化版)
|
||||
* - 定时扫描:1小时刷新一次
|
||||
* - 按需扫描:仅扫描未缓存的文件夹
|
||||
* - 可取消:支持取消正在进行的扫描
|
||||
*/
|
||||
private static class FolderAdapter extends ArrayAdapter<String> {
|
||||
private static final long SCAN_INTERVAL = 60 * 60 * 1000; // 1小时
|
||||
private static final String TAG = "FolderAdapter";
|
||||
|
||||
private final Context context;
|
||||
private List<String> folders;
|
||||
private final java.util.Map<String, Integer> imageCounts = new java.util.HashMap<>();
|
||||
private ImageScanner scanner;
|
||||
private OnFolderRemoveListener removeListener;
|
||||
private boolean isScanning = false;
|
||||
private volatile boolean isCancelled = false;
|
||||
|
||||
// 定时刷新 Handler
|
||||
private final Handler refreshHandler;
|
||||
private final Runnable refreshRunnable;
|
||||
|
||||
public interface OnFolderRemoveListener {
|
||||
void onFolderRemove(int position);
|
||||
}
|
||||
|
||||
public FolderAdapter(Context context, List<String> folders, OnFolderRemoveListener listener) {
|
||||
super(context, R.layout.item_folder_card, folders);
|
||||
this.context = context;
|
||||
this.folders = new ArrayList<>(folders);
|
||||
this.scanner = new ImageScanner(context);
|
||||
this.removeListener = listener;
|
||||
this.refreshHandler = new Handler(Looper.getMainLooper());
|
||||
|
||||
// 初始加载所有文件夹的图片数量
|
||||
loadAllImageCounts();
|
||||
|
||||
// 启动定时刷新任务
|
||||
refreshRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
if (!isScanning) {
|
||||
refreshAllImageCounts();
|
||||
}
|
||||
refreshHandler.postDelayed(this, SCAN_INTERVAL);
|
||||
}
|
||||
};
|
||||
refreshHandler.postDelayed(refreshRunnable, SCAN_INTERVAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新所有文件夹的图片数量(定时刷新用)
|
||||
*/
|
||||
private void refreshAllImageCounts() {
|
||||
if (isScanning) {
|
||||
return; // 避免并发扫描
|
||||
}
|
||||
|
||||
isScanning = true;
|
||||
isCancelled = false;
|
||||
|
||||
new Thread(() -> {
|
||||
try {
|
||||
for (String folderUri : folders) {
|
||||
if (isCancelled) {
|
||||
break;
|
||||
}
|
||||
|
||||
int count = scanner.scanFolder(folderUri).size();
|
||||
imageCounts.put(folderUri, count);
|
||||
|
||||
// 在主线程更新 UI
|
||||
new Handler(Looper.getMainLooper()).post(() ->
|
||||
notifyDataSetChanged()
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
isScanning = false;
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始加载所有文件夹的图片数量
|
||||
*/
|
||||
private void loadAllImageCounts() {
|
||||
if (isScanning) {
|
||||
return;
|
||||
}
|
||||
|
||||
isScanning = true;
|
||||
isCancelled = false;
|
||||
|
||||
new Thread(() -> {
|
||||
try {
|
||||
for (String folderUri : folders) {
|
||||
if (isCancelled) {
|
||||
break;
|
||||
}
|
||||
|
||||
int count = scanner.scanFolder(folderUri).size();
|
||||
imageCounts.put(folderUri, count);
|
||||
|
||||
// 在主线程更新 UI
|
||||
new Handler(Looper.getMainLooper()).post(() ->
|
||||
notifyDataSetChanged()
|
||||
);
|
||||
}
|
||||
} finally {
|
||||
isScanning = false;
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public void updateFolders(List<String> newFolders) {
|
||||
this.folders = new ArrayList<>(newFolders);
|
||||
clear();
|
||||
addAll(newFolders);
|
||||
|
||||
// 清除不在新列表中的文件夹的缓存
|
||||
imageCounts.keySet().retainAll(newFolders);
|
||||
|
||||
// 仅扫描新添加的文件夹(没有缓存的)
|
||||
loadMissingImageCounts();
|
||||
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
/**
|
||||
* 仅加载缺失的图片数量(避免重复扫描)
|
||||
*/
|
||||
private void loadMissingImageCounts() {
|
||||
new Thread(() -> {
|
||||
for (String folderUri : folders) {
|
||||
if (isCancelled) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!imageCounts.containsKey(folderUri)) {
|
||||
int count = scanner.scanFolder(folderUri).size();
|
||||
imageCounts.put(folderUri, count);
|
||||
|
||||
// 在主线程更新 UI
|
||||
new Handler(Looper.getMainLooper()).post(() ->
|
||||
notifyDataSetChanged()
|
||||
);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 扫描指定文件夹(用于新添加文件夹时)
|
||||
*/
|
||||
public void scanFolder(String folderUri) {
|
||||
new Thread(() -> {
|
||||
int count = scanner.scanFolder(folderUri).size();
|
||||
imageCounts.put(folderUri, count);
|
||||
new Handler(Looper.getMainLooper()).post(() ->
|
||||
notifyDataSetChanged()
|
||||
);
|
||||
}).start();
|
||||
}
|
||||
|
||||
/**
|
||||
* 取消正在进行的扫描
|
||||
*/
|
||||
public void cancelScanning() {
|
||||
isCancelled = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 释放资源
|
||||
*/
|
||||
public void release() {
|
||||
isCancelled = true;
|
||||
if (refreshHandler != null && refreshRunnable != null) {
|
||||
refreshHandler.removeCallbacks(refreshRunnable);
|
||||
}
|
||||
if (scanner != null) {
|
||||
scanner.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
|
||||
ViewHolder holder;
|
||||
if (convertView == null) {
|
||||
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
||||
convertView = inflater.inflate(R.layout.item_folder_card, parent, false);
|
||||
holder = new ViewHolder();
|
||||
holder.tvFolderName = convertView.findViewById(R.id.tvFolderName);
|
||||
holder.tvFolderPath = convertView.findViewById(R.id.tvFolderPath);
|
||||
holder.tvImageCount = convertView.findViewById(R.id.tvImageCount);
|
||||
holder.btnRemove = convertView.findViewById(R.id.btnRemove);
|
||||
convertView.setTag(holder);
|
||||
} else {
|
||||
holder = (ViewHolder) convertView.getTag();
|
||||
}
|
||||
|
||||
String folderUri = folders.get(position);
|
||||
|
||||
// 解析 URI 获取文件夹名称
|
||||
Uri uri = Uri.parse(folderUri);
|
||||
String folderName = getLastPathSegment(uri);
|
||||
holder.tvFolderName.setText(folderName);
|
||||
holder.tvFolderPath.setText(folderUri);
|
||||
|
||||
// 使用缓存的图片数量
|
||||
Integer count = imageCounts.get(folderUri);
|
||||
if (count != null) {
|
||||
holder.tvImageCount.setText(count + " 张图片");
|
||||
} else {
|
||||
holder.tvImageCount.setText("正在扫描...");
|
||||
}
|
||||
|
||||
// 移除按钮
|
||||
final int positionFinal = position;
|
||||
holder.btnRemove.setOnClickListener(v -> {
|
||||
new AlertDialog.Builder(context)
|
||||
.setTitle("移除文件夹")
|
||||
.setMessage("确定要移除这个文件夹吗?")
|
||||
.setPositiveButton("确定", (dialog, which) -> {
|
||||
if (removeListener != null) {
|
||||
removeListener.onFolderRemove(positionFinal);
|
||||
}
|
||||
})
|
||||
.setNegativeButton("取消", null)
|
||||
.show();
|
||||
});
|
||||
|
||||
return convertView;
|
||||
}
|
||||
|
||||
private String getLastPathSegment(Uri uri) {
|
||||
String path = uri.getLastPathSegment();
|
||||
if (path != null) {
|
||||
// 处理 content://com.android.externalstorage.documents/tree/primary%3ADCIM 这种格式
|
||||
if (path.contains(":")) {
|
||||
String[] parts = path.split(":");
|
||||
if (parts.length > 1) {
|
||||
return parts[1];
|
||||
}
|
||||
}
|
||||
return path;
|
||||
}
|
||||
return "未知文件夹";
|
||||
}
|
||||
|
||||
// ViewHolder 模式优化
|
||||
private static class ViewHolder {
|
||||
TextView tvFolderName;
|
||||
TextView tvFolderPath;
|
||||
TextView tvImageCount;
|
||||
Button btnRemove;
|
||||
}
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
config = prefs.loadConfig();
|
||||
updateUI();
|
||||
updateAlbumInfo();
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,25 @@ import cn.hugh.bz.gk2.R;
|
||||
* 主 Activity
|
||||
* 作为 Fragment 容器,托管 ViewPager2 和 BottomNavigationView
|
||||
*/
|
||||
public class MainActivity extends AppCompatActivity implements HomeFragment.FolderPickerCallback {
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
// 文件夹选择回调接口
|
||||
public interface FolderPickerCallback {
|
||||
void launchFolderPicker(FolderPickerResultCallback callback);
|
||||
}
|
||||
|
||||
public interface FolderPickerResultCallback {
|
||||
void onFolderPicked(String uri);
|
||||
}
|
||||
|
||||
// 图片选择回调接口
|
||||
public interface ImagePickerCallback {
|
||||
void launchImagePicker(ImagePickerResultCallback callback);
|
||||
}
|
||||
|
||||
public interface ImagePickerResultCallback {
|
||||
void onImagePicked(String uri);
|
||||
}
|
||||
|
||||
private ViewPager2 viewPager;
|
||||
private BottomNavigationView bottomNav;
|
||||
@@ -28,7 +46,11 @@ public class MainActivity extends AppCompatActivity implements HomeFragment.Fold
|
||||
|
||||
// Folder picker launcher
|
||||
private ActivityResultLauncher<Intent> folderPickerLauncher;
|
||||
private HomeFragment.FolderPickerResultCallback currentCallback;
|
||||
private FolderPickerResultCallback currentCallback;
|
||||
|
||||
// Image picker launcher
|
||||
private ActivityResultLauncher<Intent> imagePickerLauncher;
|
||||
private ImagePickerResultCallback imagePickerCallback;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@@ -36,6 +58,7 @@ public class MainActivity extends AppCompatActivity implements HomeFragment.Fold
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
initFolderPickerLauncher();
|
||||
initImagePickerLauncher();
|
||||
initViews();
|
||||
setupViewPager();
|
||||
setupBottomNav();
|
||||
@@ -70,6 +93,31 @@ public class MainActivity extends AppCompatActivity implements HomeFragment.Fold
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化图片选择器
|
||||
*/
|
||||
private void initImagePickerLauncher() {
|
||||
imagePickerLauncher = registerForActivityResult(
|
||||
new ActivityResultContracts.StartActivityForResult(),
|
||||
result -> {
|
||||
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
|
||||
Uri uri = result.getData().getData();
|
||||
if (uri != null) {
|
||||
// 持久化 URI 权限
|
||||
final int takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION;
|
||||
getContentResolver().takePersistableUriPermission(uri, takeFlags);
|
||||
|
||||
String imageUri = uri.toString();
|
||||
if (imagePickerCallback != null) {
|
||||
imagePickerCallback.onImagePicked(imageUri);
|
||||
imagePickerCallback = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private void initViews() {
|
||||
viewPager = findViewById(R.id.viewPager);
|
||||
bottomNav = findViewById(R.id.bottomNav);
|
||||
@@ -90,16 +138,25 @@ public class MainActivity extends AppCompatActivity implements HomeFragment.Fold
|
||||
}
|
||||
|
||||
/**
|
||||
* 实现 HomeFragment.FolderPickerCallback 接口
|
||||
* 启动文件夹选择器
|
||||
*/
|
||||
@Override
|
||||
public void launchFolderPicker(HomeFragment.FolderPickerResultCallback callback) {
|
||||
public void launchFolderPicker(FolderPickerResultCallback callback) {
|
||||
this.currentCallback = callback;
|
||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
|
||||
folderPickerLauncher.launch(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 启动图片选择器
|
||||
*/
|
||||
public void launchImagePicker(ImagePickerResultCallback callback) {
|
||||
this.imagePickerCallback = callback;
|
||||
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
||||
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
||||
intent.setType("image/*");
|
||||
imagePickerLauncher.launch(intent);
|
||||
}
|
||||
|
||||
/**
|
||||
* 切换到指定的页面
|
||||
* @param position 页面索引(0=主页,1=壁纸,2=相册,3=历史)
|
||||
|
||||
@@ -105,6 +105,15 @@ public class WallpaperPreferences {
|
||||
config.setEnabled(album.isEnabled());
|
||||
config.setCurrentIndex(album.getCurrentIndex());
|
||||
config.setChangeOnReturnToHome(album.isChangeOnReturnToHome());
|
||||
|
||||
// 图片列表
|
||||
List<String> imageUris = album.getImageUris();
|
||||
config.setImageUris(imageUris != null ? new ArrayList<>(imageUris) : new ArrayList<>());
|
||||
|
||||
// 添加日志:记录从相册加载的图片数量
|
||||
android.util.Log.i("WallpaperPreferences", "loadConfig: album=" + album.getName() +
|
||||
", folders=" + album.getFolders().size() +
|
||||
", images=" + (imageUris != null ? imageUris.size() : 0));
|
||||
} else {
|
||||
// 默认配置
|
||||
config.setSelectedFolders(new ArrayList<>());
|
||||
@@ -113,6 +122,9 @@ public class WallpaperPreferences {
|
||||
config.setEnabled(false);
|
||||
config.setCurrentIndex(0);
|
||||
config.setChangeOnReturnToHome(true);
|
||||
config.setImageUris(new ArrayList<>());
|
||||
|
||||
android.util.Log.w("WallpaperPreferences", "loadConfig: No active album found");
|
||||
}
|
||||
|
||||
return config;
|
||||
@@ -137,6 +149,7 @@ public class WallpaperPreferences {
|
||||
album.setEnabled(config.isEnabled());
|
||||
album.setCurrentIndex(config.getCurrentIndex());
|
||||
album.setChangeOnReturnToHome(config.isChangeOnReturnToHome());
|
||||
album.setImageUris(new ArrayList<>(config.getImageUris()));
|
||||
|
||||
// 保存相册
|
||||
albumPrefs.updateAlbum(album);
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24"
|
||||
android:viewportHeight="24"
|
||||
android:tint="?attr/colorControlNormal">
|
||||
<path
|
||||
android:fillColor="@android:color/white"
|
||||
android:pathData="M19,6.41L17.59,5 12,10.59 6.41,5 5,6.41 10.59,12 5,17.59 6.41,19 12,13.41 17.59,19 19,17.59 13.41,12z"/>
|
||||
</vector>
|
||||
@@ -145,6 +145,61 @@
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
<!-- 单张图片选择 -->
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginBottom="16dp"
|
||||
app:cardElevation="2dp"
|
||||
app:cardCornerRadius="12dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="16dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="单张图片"
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/text_primary" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/btnAddImage"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="36dp"
|
||||
android:text="+ 添加"
|
||||
android:textSize="12sp"
|
||||
app:icon="@drawable/ic_add"
|
||||
app:iconGravity="textStart"
|
||||
app:iconPadding="4dp"
|
||||
app:cornerRadius="8dp"
|
||||
style="@style/Widget.Material3.Button.OutlinedButton" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/rvImages"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:nestedScrollingEnabled="false"
|
||||
tools:listitem="@layout/item_folder_card" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
<!-- 播放模式 -->
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:layout_width="match_parent"
|
||||
|
||||
@@ -165,7 +165,7 @@
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
<!-- 文件夹管理卡片 -->
|
||||
<!-- 相册详细信息卡片 -->
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
@@ -173,76 +173,6 @@
|
||||
app:cardElevation="4dp"
|
||||
app:cardCornerRadius="16dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="20dp">
|
||||
|
||||
<!-- 标题和按钮 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:gravity="center_vertical"
|
||||
android:orientation="horizontal">
|
||||
|
||||
<TextView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="图片文件夹"
|
||||
android:textSize="18sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/text_primary"
|
||||
android:drawableStart="@drawable/ic_folder"
|
||||
android:drawablePadding="8dp" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/btnSelectFolders"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="48dp"
|
||||
android:text="@string/select_folders"
|
||||
android:textSize="14sp"
|
||||
app:icon="@drawable/ic_add"
|
||||
app:iconGravity="textStart"
|
||||
app:cornerRadius="12dp"
|
||||
style="@style/Widget.Material3.Button.TonalButton" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 文件夹信息 -->
|
||||
<TextView
|
||||
android:id="@+id/tvFolderInfo"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/no_folders"
|
||||
android:textSize="14sp"
|
||||
android:layout_marginTop="12dp"
|
||||
android:textColor="@color/text_secondary"
|
||||
android:gravity="center" />
|
||||
|
||||
<!-- 进度条 -->
|
||||
<ProgressBar
|
||||
android:id="@+id/progressBar"
|
||||
style="?android:attr/progressBarStyleHorizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginTop="8dp"
|
||||
android:indeterminate="true"
|
||||
android:visibility="gone"
|
||||
tools:visibility="visible" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
|
||||
<!-- 文件夹列表卡片 -->
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:cardElevation="4dp"
|
||||
app:cardCornerRadius="16dp">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
@@ -252,19 +182,28 @@
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="已选择的文件夹"
|
||||
android:text="相册详情"
|
||||
android:textSize="16sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/text_primary"
|
||||
android:layout_marginBottom="12dp" />
|
||||
|
||||
<ListView
|
||||
android:id="@+id/lvFolders"
|
||||
<TextView
|
||||
android:id="@+id/tvFolderInfo"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:divider="@null"
|
||||
android:dividerHeight="0dp"
|
||||
tools:listitem="@layout/item_folder_card" />
|
||||
android:text="未选择文件夹或图片"
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/text_secondary" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/tvSingleImageInfo"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text=""
|
||||
android:textSize="14sp"
|
||||
android:textColor="@color/text_secondary"
|
||||
android:layout_marginTop="8dp" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
<string name="disable">禁用</string>
|
||||
<string name="change_now">立即更换</string>
|
||||
<string name="select_folders">选择文件夹</string>
|
||||
<string name="select_single_image">选择单张图片</string>
|
||||
<string name="status_enabled">运行中</string>
|
||||
<string name="status_disabled">已停止</string>
|
||||
<string name="next_change">下次更换:%d 分钟</string>
|
||||
|
||||
Reference in new issue
Block a user