优化当前壁纸页信息与交互
This commit is contained in:
1 parent
4347fa92b2
commit
19e433c402
2 files changed
+254
-134
No files matched your search
@@ -6,11 +6,11 @@ import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.OpenableColumns;
|
||||
import android.database.Cursor;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
@@ -18,6 +18,9 @@ import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.google.android.material.button.MaterialButton;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import cn.hugh.bz.gk2.R;
|
||||
import cn.hugh.bz.gk2.utils.BackgroundHelper;
|
||||
import cn.hugh.bz.gk2.utils.ImageLoader;
|
||||
@@ -30,13 +33,16 @@ import cn.hugh.bz.gk2.wallpaper.WallpaperPreferences;
|
||||
public class CurrentWallpaperFragment extends Fragment {
|
||||
|
||||
private ImageView ivWallpaper;
|
||||
private TextView tvWallpaperPath;
|
||||
private TextView tvWallpaperSize;
|
||||
private Button btnNext;
|
||||
private View touchOverlay;
|
||||
private TextView tvWallpaperName;
|
||||
private TextView tvResolution;
|
||||
private TextView tvFileSize;
|
||||
private TextView tvPath;
|
||||
private MaterialButton btnNext;
|
||||
private MaterialButton btnOpenGallery;
|
||||
|
||||
private WallpaperPreferences prefs;
|
||||
private ImageLoader imageLoader;
|
||||
private cn.hugh.bz.gk2.utils.ImageScanner scanner;
|
||||
|
||||
@Override
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
@@ -49,7 +55,6 @@ public class CurrentWallpaperFragment extends Fragment {
|
||||
|
||||
prefs = new WallpaperPreferences(requireContext());
|
||||
imageLoader = new ImageLoader(requireContext());
|
||||
scanner = new cn.hugh.bz.gk2.utils.ImageScanner(requireContext());
|
||||
|
||||
initViews(view);
|
||||
loadCurrentWallpaper();
|
||||
@@ -60,12 +65,17 @@ public class CurrentWallpaperFragment extends Fragment {
|
||||
|
||||
private void initViews(View view) {
|
||||
ivWallpaper = view.findViewById(R.id.ivWallpaper);
|
||||
tvWallpaperPath = view.findViewById(R.id.tvWallpaperPath);
|
||||
tvWallpaperSize = view.findViewById(R.id.tvWallpaperSize);
|
||||
touchOverlay = view.findViewById(R.id.touchOverlay);
|
||||
tvWallpaperName = view.findViewById(R.id.tvWallpaperName);
|
||||
tvResolution = view.findViewById(R.id.tvResolution);
|
||||
tvFileSize = view.findViewById(R.id.tvFileSize);
|
||||
tvPath = view.findViewById(R.id.tvPath);
|
||||
btnNext = view.findViewById(R.id.btnNext);
|
||||
btnOpenGallery = view.findViewById(R.id.btnOpenGallery);
|
||||
|
||||
btnNext.setOnClickListener(v -> showNextWallpaper());
|
||||
ivWallpaper.setOnClickListener(v -> openInGallery());
|
||||
btnOpenGallery.setOnClickListener(v -> openInGallery());
|
||||
touchOverlay.setOnClickListener(v -> openInGallery());
|
||||
}
|
||||
|
||||
private void loadCurrentWallpaper() {
|
||||
@@ -77,17 +87,30 @@ public class CurrentWallpaperFragment extends Fragment {
|
||||
}
|
||||
|
||||
// 显示加载状态
|
||||
tvWallpaperPath.setText("正在加载壁纸...");
|
||||
tvWallpaperName.setText("加载中...");
|
||||
tvResolution.setText("-- x --");
|
||||
tvFileSize.setText("-- MB");
|
||||
tvPath.setText(currentUri);
|
||||
btnNext.setEnabled(false);
|
||||
|
||||
// 在后台线程加载图片
|
||||
// 获取文件大小和名称
|
||||
new Thread(() -> {
|
||||
String fileSizeStr = getFileSize(Uri.parse(currentUri));
|
||||
String displayName = getDisplayName(currentUri);
|
||||
|
||||
requireActivity().runOnUiThread(() -> {
|
||||
tvFileSize.setText(fileSizeStr);
|
||||
tvWallpaperName.setText(displayName);
|
||||
});
|
||||
|
||||
// 加载图片
|
||||
Bitmap bitmap = imageLoader.loadImage(currentUri, 1080, 1920);
|
||||
|
||||
requireActivity().runOnUiThread(() -> {
|
||||
if (bitmap != null) {
|
||||
ivWallpaper.setImageBitmap(bitmap);
|
||||
tvWallpaperPath.setText(getDisplayName(currentUri));
|
||||
tvWallpaperSize.setText(bitmap.getWidth() + " x " + bitmap.getHeight());
|
||||
tvResolution.setText(bitmap.getWidth() + " x " + bitmap.getHeight());
|
||||
btnNext.setEnabled(true);
|
||||
} else {
|
||||
showError();
|
||||
}
|
||||
@@ -95,55 +118,74 @@ public class CurrentWallpaperFragment extends Fragment {
|
||||
}).start();
|
||||
}
|
||||
|
||||
private String getFileSize(Uri uri) {
|
||||
try (Cursor cursor = requireContext().getContentResolver().query(uri, null, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);
|
||||
if (sizeIndex != -1) {
|
||||
long size = cursor.getLong(sizeIndex);
|
||||
return formatFileSize(size);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return "未知大小";
|
||||
}
|
||||
|
||||
private String formatFileSize(long size) {
|
||||
if (size <= 0) return "0 B";
|
||||
final String[] units = new String[]{"B", "KB", "MB", "GB", "TB"};
|
||||
int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
|
||||
return String.format("%.1f %s", size / Math.pow(1024, digitGroups), units[digitGroups]);
|
||||
}
|
||||
|
||||
private void showNoWallpaper() {
|
||||
ivWallpaper.setImageDrawable(null);
|
||||
tvWallpaperPath.setText("尚未设置壁纸");
|
||||
tvWallpaperSize.setText("");
|
||||
tvWallpaperName.setText("未设置壁纸");
|
||||
tvResolution.setText("--");
|
||||
tvFileSize.setText("--");
|
||||
tvPath.setText("");
|
||||
btnNext.setEnabled(false);
|
||||
btnOpenGallery.setEnabled(false);
|
||||
}
|
||||
|
||||
private void showError() {
|
||||
ivWallpaper.setImageDrawable(null);
|
||||
tvWallpaperPath.setText("加载壁纸失败");
|
||||
tvWallpaperSize.setText("");
|
||||
btnNext.setEnabled(false);
|
||||
tvWallpaperName.setText("加载失败");
|
||||
btnNext.setEnabled(true); // 允许尝试下一张
|
||||
}
|
||||
|
||||
private void showNextWallpaper() {
|
||||
android.util.Log.i("CurrentWallpaperFragment", "showNextWallpaper() called");
|
||||
cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext()).log("CurrentWallpaperFragment", "showNextWallpaper() called");
|
||||
|
||||
|
||||
cn.hugh.bz.gk2.model.AppConfig config = prefs.loadConfig();
|
||||
|
||||
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());
|
||||
|
||||
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();
|
||||
Toast.makeText(requireContext(), "请先在相册页面添加图片", Toast.LENGTH_SHORT).show();
|
||||
return;
|
||||
}
|
||||
|
||||
android.util.Log.i("CurrentWallpaperFragment", "showNextWallpaper: sending broadcast");
|
||||
cn.hugh.bz.gk2.utils.FileLogger.getInstance(requireContext()).log("CurrentWallpaperFragment", "showNextWallpaper: sending broadcast");
|
||||
// 禁用按钮防止重复点击
|
||||
btnNext.setEnabled(false);
|
||||
btnNext.setText("切换中...");
|
||||
|
||||
// 直接实例化 BroadcastReceiver 并调用 onReceive(避免隐式广播限制)
|
||||
// 发送广播
|
||||
cn.hugh.bz.gk2.receiver.WallpaperChangeReceiver receiver = new cn.hugh.bz.gk2.receiver.WallpaperChangeReceiver();
|
||||
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER");
|
||||
intent.putExtra("force_change", true); // 用户主动触发
|
||||
intent.putExtra("scheduled", false); // 非定时触发
|
||||
intent.putExtra("force_change", true);
|
||||
intent.putExtra("scheduled", false);
|
||||
receiver.onReceive(requireContext(), intent);
|
||||
|
||||
Toast.makeText(requireContext(), "已切换到下一张", Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(requireContext(), "已切换", Toast.LENGTH_SHORT).show();
|
||||
|
||||
// 延迟刷新预览
|
||||
// 延迟刷新
|
||||
new Handler(Looper.getMainLooper()).postDelayed(() -> {
|
||||
loadCurrentWallpaper();
|
||||
}, 500);
|
||||
btnNext.setText("切换下一张");
|
||||
btnNext.setEnabled(true);
|
||||
}, 800);
|
||||
}
|
||||
|
||||
private void openInGallery() {
|
||||
@@ -155,28 +197,38 @@ public class CurrentWallpaperFragment extends Fragment {
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(requireContext(), "无法打开相册", Toast.LENGTH_SHORT).show();
|
||||
Toast.makeText(requireContext(), "无法打开图片", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String getDisplayName(String uriString) {
|
||||
if (uriString == null || uriString.isEmpty()) {
|
||||
return "未知壁纸";
|
||||
return "未知图片";
|
||||
}
|
||||
|
||||
try {
|
||||
Uri uri = Uri.parse(uriString);
|
||||
|
||||
// 尝试从 ContentResolver 获取文件名
|
||||
try (Cursor cursor = requireContext().getContentResolver().query(uri, null, null, null, null)) {
|
||||
if (cursor != null && cursor.moveToFirst()) {
|
||||
int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
|
||||
if (nameIndex != -1) {
|
||||
return cursor.getString(nameIndex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 如果失败,回退到路径解析
|
||||
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) {
|
||||
path = parts[1];
|
||||
}
|
||||
}
|
||||
// 移除文件扩展名
|
||||
if (path.contains("/")) {
|
||||
String[] segments = path.split("/");
|
||||
path = segments[segments.length - 1];
|
||||
@@ -187,7 +239,7 @@ public class CurrentWallpaperFragment extends Fragment {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
return "当前壁纸";
|
||||
return "未命名图片";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -195,19 +247,9 @@ public class CurrentWallpaperFragment extends Fragment {
|
||||
super.onResume();
|
||||
loadCurrentWallpaper();
|
||||
|
||||
// 重新应用背景图片
|
||||
View rootView = getView();
|
||||
if (rootView != null) {
|
||||
BackgroundHelper.applyBackground(requireContext(), rootView);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroyView() {
|
||||
super.onDestroyView();
|
||||
// 释放图片资源
|
||||
if (ivWallpaper != null) {
|
||||
ivWallpaper.setImageDrawable(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user