feat: 添加壁纸历史记录功能
新增全局壁纸历史记录功能,记录所有使用过的壁纸,支持缩略图展示和系统相册打开。 核心功能: - 自动记录壁纸使用历史(最多保存50条) - 缩略图缓存(保持原图宽高比) - 2列网格布局展示 - 点击历史记录使用系统相册打开 - 支持删除单条记录和清空全部 新增文件: - WallpaperHistory.java - 历史记录数据模型 - WallpaperHistoryManager.java - 历史记录管理器 - WallpaperHistoryFragment.java - 历史页面 - fragment_wallpaper_history.xml - 页面布局 - item_history_card.xml - 卡片项布局 - ic_history.xml - 历史图标 修改文件: - AutoWallpaperService.java - 集成历史记录保存 - ViewPagerAdapter.java - 扩展为5个页面 - MainActivity.java - 更新页面切换范围 - bottom_nav_menu.xml - 添加历史标签 - strings.xml - 添加历史标签文本 - colors.xml - 添加图片占位符颜色 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
20b8f5fc93
commit
62215a9279
13 files changed
+830
-5
No files matched your search
@@ -0,0 +1,79 @@
|
||||
package cn.hugh.bz.gk2.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 壁纸历史记录数据模型
|
||||
* 记录所有使用过的壁纸信息
|
||||
*/
|
||||
public class WallpaperHistory implements Serializable {
|
||||
private String id; // 历史记录唯一ID
|
||||
private String uri; // 壁纸URI
|
||||
private String albumId; // 所属相册ID
|
||||
private String albumName; // 所属相册名称
|
||||
private long timestamp; // 使用时间戳
|
||||
private String thumbnailPath; // 缩略图缓存路径
|
||||
|
||||
public WallpaperHistory() {
|
||||
this.id = UUID.randomUUID().toString();
|
||||
this.timestamp = System.currentTimeMillis();
|
||||
}
|
||||
|
||||
public WallpaperHistory(String uri, String albumId, String albumName, String thumbnailPath) {
|
||||
this();
|
||||
this.uri = uri;
|
||||
this.albumId = albumId;
|
||||
this.albumName = albumName;
|
||||
this.thumbnailPath = thumbnailPath;
|
||||
}
|
||||
|
||||
// Getters and Setters
|
||||
public String getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(String id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getUri() {
|
||||
return uri;
|
||||
}
|
||||
|
||||
public void setUri(String uri) {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public String getAlbumId() {
|
||||
return albumId;
|
||||
}
|
||||
|
||||
public void setAlbumId(String albumId) {
|
||||
this.albumId = albumId;
|
||||
}
|
||||
|
||||
public String getAlbumName() {
|
||||
return albumName;
|
||||
}
|
||||
|
||||
public void setAlbumName(String albumName) {
|
||||
this.albumName = albumName;
|
||||
}
|
||||
|
||||
public long getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(long timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
|
||||
public String getThumbnailPath() {
|
||||
return thumbnailPath;
|
||||
}
|
||||
|
||||
public void setThumbnailPath(String thumbnailPath) {
|
||||
this.thumbnailPath = thumbnailPath;
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,9 @@ public class AlbumManagementFragment extends Fragment {
|
||||
private AlbumAdapter adapter;
|
||||
private List<Album> albums;
|
||||
|
||||
// 目标相册ID(用于从历史页面跳转)
|
||||
public static String targetAlbumId = null;
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
@@ -157,6 +160,42 @@ public class AlbumManagementFragment extends Fragment {
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
loadAlbums(); // 从编辑页面返回时刷新列表
|
||||
|
||||
// 处理从历史页面跳转过来的逻辑
|
||||
if (targetAlbumId != null) {
|
||||
handleTargetAlbum();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理目标相册(从历史页面跳转)
|
||||
*/
|
||||
private void handleTargetAlbum() {
|
||||
if (albums == null || targetAlbumId == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 查找目标相册
|
||||
Album targetAlbum = null;
|
||||
for (Album album : albums) {
|
||||
if (album.getId().equals(targetAlbumId)) {
|
||||
targetAlbum = album;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (targetAlbum != null) {
|
||||
// 高亮显示该相册(可以添加视觉反馈,比如滚动到该位置并闪烁)
|
||||
// 这里简单切换到该相册
|
||||
albumPrefs.setActiveAlbumId(targetAlbumId);
|
||||
Toast.makeText(requireContext(), "已跳转到相册:" + targetAlbum.getName(), Toast.LENGTH_SHORT).show();
|
||||
loadAlbums(); // 刷新列表以更新"当前"徽章
|
||||
} else {
|
||||
Toast.makeText(requireContext(), "未找到相册", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
|
||||
// 重置目标相册ID
|
||||
targetAlbumId = null;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -102,10 +102,10 @@ public class MainActivity extends AppCompatActivity implements HomeFragment.Fold
|
||||
|
||||
/**
|
||||
* 切换到指定的页面
|
||||
* @param position 页面索引(0=主页,1=壁纸,2=相册,3=设置)
|
||||
* @param position 页面索引(0=主页,1=壁纸,2=相册,3=历史,4=设置)
|
||||
*/
|
||||
public void switchToPage(int position) {
|
||||
if (viewPager != null && position >= 0 && position < 4) {
|
||||
if (viewPager != null && position >= 0 && position < 5) {
|
||||
viewPager.setCurrentItem(position, false);
|
||||
// 更新底部导航选中状态
|
||||
bottomNav.getMenu().getItem(position).setChecked(true);
|
||||
|
||||
@@ -6,10 +6,10 @@ import androidx.fragment.app.FragmentActivity;
|
||||
import androidx.viewpager2.adapter.FragmentStateAdapter;
|
||||
|
||||
/**
|
||||
* ViewPager2 适配器,用于管理四个 Fragment 页面
|
||||
* ViewPager2 适配器,用于管理五个 Fragment 页面
|
||||
*/
|
||||
public class ViewPagerAdapter extends FragmentStateAdapter {
|
||||
private static final int NUM_PAGES = 4;
|
||||
private static final int NUM_PAGES = 5;
|
||||
|
||||
public ViewPagerAdapter(FragmentActivity fa) {
|
||||
super(fa);
|
||||
@@ -26,6 +26,8 @@ public class ViewPagerAdapter extends FragmentStateAdapter {
|
||||
case 2:
|
||||
return new AlbumManagementFragment();
|
||||
case 3:
|
||||
return new WallpaperHistoryFragment();
|
||||
case 4:
|
||||
return new SettingsFragment();
|
||||
default:
|
||||
return new HomeFragment();
|
||||
|
||||
@@ -0,0 +1,256 @@
|
||||
package cn.hugh.bz.gk2.ui;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.provider.DocumentsContract;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.TextView;
|
||||
import android.widget.Toast;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import androidx.fragment.app.Fragment;
|
||||
import androidx.recyclerview.widget.GridLayoutManager;
|
||||
import androidx.recyclerview.widget.RecyclerView;
|
||||
|
||||
import com.google.android.material.button.MaterialButton;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import cn.hugh.bz.gk2.R;
|
||||
import cn.hugh.bz.gk2.model.WallpaperHistory;
|
||||
import cn.hugh.bz.gk2.wallpaper.WallpaperHistoryManager;
|
||||
|
||||
/**
|
||||
* 壁纸历史页面
|
||||
* 显示所有使用过的壁纸历史记录,支持缩略图展示和跳转到相册
|
||||
*/
|
||||
public class WallpaperHistoryFragment extends Fragment {
|
||||
|
||||
private RecyclerView rvHistory;
|
||||
private MaterialButton btnClearHistory;
|
||||
private TextView tvEmptyHint;
|
||||
|
||||
private WallpaperHistoryManager historyManager;
|
||||
private HistoryAdapter adapter;
|
||||
private List<WallpaperHistory> historyList;
|
||||
|
||||
@Override
|
||||
public void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
historyManager = new WallpaperHistoryManager(requireContext());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
return inflater.inflate(R.layout.fragment_wallpaper_history, container, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
||||
super.onViewCreated(view, savedInstanceState);
|
||||
|
||||
initViews(view);
|
||||
loadHistory();
|
||||
}
|
||||
|
||||
private void initViews(View view) {
|
||||
rvHistory = view.findViewById(R.id.rvHistory);
|
||||
btnClearHistory = view.findViewById(R.id.btnClearHistory);
|
||||
tvEmptyHint = view.findViewById(R.id.tvEmptyHint);
|
||||
|
||||
// 设置 RecyclerView 为2列网格布局
|
||||
rvHistory.setLayoutManager(new GridLayoutManager(requireContext(), 2));
|
||||
adapter = new HistoryAdapter(requireContext(), historyList, new HistoryAdapter.HistoryCallback() {
|
||||
@Override
|
||||
public void onItemClick(WallpaperHistory history) {
|
||||
openAlbumForHistory(history);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDelete(WallpaperHistory history) {
|
||||
deleteHistory(history);
|
||||
}
|
||||
});
|
||||
rvHistory.setAdapter(adapter);
|
||||
|
||||
// 清空历史按钮
|
||||
btnClearHistory.setOnClickListener(v -> confirmClearHistory());
|
||||
}
|
||||
|
||||
private void loadHistory() {
|
||||
historyList = historyManager.loadHistory();
|
||||
adapter.updateHistory(historyList);
|
||||
updateEmptyHint();
|
||||
}
|
||||
|
||||
private void updateEmptyHint() {
|
||||
if (historyList == null || historyList.isEmpty()) {
|
||||
tvEmptyHint.setVisibility(View.VISIBLE);
|
||||
rvHistory.setVisibility(View.GONE);
|
||||
btnClearHistory.setEnabled(false);
|
||||
} else {
|
||||
tvEmptyHint.setVisibility(View.GONE);
|
||||
rvHistory.setVisibility(View.VISIBLE);
|
||||
btnClearHistory.setEnabled(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 点击历史记录,使用系统相册/图片查看器打开
|
||||
*/
|
||||
private void openAlbumForHistory(WallpaperHistory history) {
|
||||
try {
|
||||
Uri uri = Uri.parse(history.getUri());
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setDataAndType(uri, "image/*");
|
||||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||||
startActivity(intent);
|
||||
} catch (Exception e) {
|
||||
Toast.makeText(requireContext(), "无法打开图片", Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除单条历史记录
|
||||
*/
|
||||
private void deleteHistory(WallpaperHistory history) {
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("删除记录")
|
||||
.setMessage("确定要删除这条历史记录吗?")
|
||||
.setPositiveButton("删除", (dialog, which) -> {
|
||||
historyManager.deleteHistory(history.getId());
|
||||
Toast.makeText(requireContext(), "已删除", Toast.LENGTH_SHORT).show();
|
||||
loadHistory();
|
||||
})
|
||||
.setNegativeButton("取消", null)
|
||||
.show();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有历史记录
|
||||
*/
|
||||
private void confirmClearHistory() {
|
||||
new AlertDialog.Builder(requireContext())
|
||||
.setTitle("清空历史")
|
||||
.setMessage("确定要清空所有历史记录吗?此操作不可恢复。")
|
||||
.setPositiveButton("清空", (dialog, which) -> {
|
||||
historyManager.clearHistory();
|
||||
Toast.makeText(requireContext(), "已清空所有历史记录", Toast.LENGTH_SHORT).show();
|
||||
loadHistory();
|
||||
})
|
||||
.setNegativeButton("取消", null)
|
||||
.show();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResume() {
|
||||
super.onResume();
|
||||
loadHistory(); // 从其他页面返回时刷新列表
|
||||
}
|
||||
|
||||
/**
|
||||
* 历史记录适配器
|
||||
*/
|
||||
private static class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.HistoryViewHolder> {
|
||||
|
||||
private final Context context;
|
||||
private List<WallpaperHistory> historyList;
|
||||
private final HistoryCallback callback;
|
||||
private final WallpaperHistoryManager historyManager;
|
||||
|
||||
public HistoryAdapter(Context context, List<WallpaperHistory> historyList, HistoryCallback callback) {
|
||||
this.context = context;
|
||||
this.historyList = historyList != null ? historyList : new ArrayList<>();
|
||||
this.callback = callback;
|
||||
this.historyManager = new WallpaperHistoryManager(context);
|
||||
}
|
||||
|
||||
public void updateHistory(List<WallpaperHistory> historyList) {
|
||||
this.historyList = historyList != null ? historyList : new ArrayList<>();
|
||||
notifyDataSetChanged();
|
||||
}
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public HistoryViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
|
||||
View view = LayoutInflater.from(context).inflate(R.layout.item_history_card, parent, false);
|
||||
return new HistoryViewHolder(view);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onBindViewHolder(@NonNull HistoryViewHolder holder, int position) {
|
||||
WallpaperHistory history = historyList.get(position);
|
||||
holder.bind(history);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getItemCount() {
|
||||
return historyList.size();
|
||||
}
|
||||
|
||||
class HistoryViewHolder extends RecyclerView.ViewHolder {
|
||||
private final android.widget.ImageView ivThumbnail;
|
||||
private final TextView tvAlbumName;
|
||||
private final TextView tvTimestamp;
|
||||
private final MaterialButton btnDelete;
|
||||
|
||||
public HistoryViewHolder(@NonNull View itemView) {
|
||||
super(itemView);
|
||||
ivThumbnail = itemView.findViewById(R.id.ivThumbnail);
|
||||
tvAlbumName = itemView.findViewById(R.id.tvAlbumName);
|
||||
tvTimestamp = itemView.findViewById(R.id.tvTimestamp);
|
||||
btnDelete = itemView.findViewById(R.id.btnDelete);
|
||||
}
|
||||
|
||||
public void bind(WallpaperHistory history) {
|
||||
// 加载缩略图
|
||||
Bitmap thumbnail = historyManager.loadThumbnail(history.getThumbnailPath());
|
||||
if (thumbnail != null) {
|
||||
ivThumbnail.setImageBitmap(thumbnail);
|
||||
} else {
|
||||
// 显示占位符
|
||||
ivThumbnail.setImageDrawable(null);
|
||||
}
|
||||
|
||||
// 相册名称
|
||||
tvAlbumName.setText(history.getAlbumName());
|
||||
|
||||
// 时间戳格式化
|
||||
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm", Locale.getDefault());
|
||||
String timestamp = sdf.format(new Date(history.getTimestamp()));
|
||||
tvTimestamp.setText(timestamp);
|
||||
|
||||
// 卡片点击事件
|
||||
itemView.setOnClickListener(v -> {
|
||||
if (callback != null) {
|
||||
callback.onItemClick(history);
|
||||
}
|
||||
});
|
||||
|
||||
// 删除按钮
|
||||
btnDelete.setOnClickListener(v -> {
|
||||
if (callback != null) {
|
||||
callback.onDelete(history);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
interface HistoryCallback {
|
||||
void onItemClick(WallpaperHistory history);
|
||||
void onDelete(WallpaperHistory history);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import android.service.wallpaper.WallpaperService;
|
||||
import android.util.Log;
|
||||
import android.view.SurfaceHolder;
|
||||
|
||||
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.model.WallpaperImage;
|
||||
@@ -56,6 +57,8 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
class WallpaperEngine extends Engine {
|
||||
private final ImageLoader imageLoader;
|
||||
private final WallpaperPreferences prefs;
|
||||
private final WallpaperHistoryManager historyManager;
|
||||
private final AlbumPreferences albumPrefs;
|
||||
private final Handler handler;
|
||||
|
||||
private int width;
|
||||
@@ -66,6 +69,8 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
public WallpaperEngine() {
|
||||
this.imageLoader = new ImageLoader(AutoWallpaperService.this);
|
||||
this.prefs = new WallpaperPreferences(AutoWallpaperService.this);
|
||||
this.historyManager = new WallpaperHistoryManager(AutoWallpaperService.this);
|
||||
this.albumPrefs = new AlbumPreferences(AutoWallpaperService.this);
|
||||
this.handler = new Handler(Looper.getMainLooper());
|
||||
}
|
||||
|
||||
@@ -227,6 +232,17 @@ public class AutoWallpaperService extends WallpaperService {
|
||||
canvas.drawBitmap(bitmap, null,
|
||||
new RectF(left, top, left + scaledWidth, top + scaledHeight),
|
||||
null);
|
||||
|
||||
// 保存到历史记录
|
||||
Album activeAlbum = albumPrefs.getActiveAlbum();
|
||||
if (activeAlbum != null) {
|
||||
historyManager.addHistory(
|
||||
uri,
|
||||
activeAlbum.getId(),
|
||||
activeAlbum.getName(),
|
||||
bitmap
|
||||
);
|
||||
}
|
||||
} else {
|
||||
Log.e(TAG, "Failed to load bitmap: " + uri);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,267 @@
|
||||
package cn.hugh.bz.gk2.wallpaper;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import cn.hugh.bz.gk2.model.WallpaperHistory;
|
||||
|
||||
/**
|
||||
* 壁纸历史管理器
|
||||
* 负责管理壁纸历史记录的存储、加载和缩略图缓存
|
||||
*/
|
||||
public class WallpaperHistoryManager {
|
||||
private static final String TAG = "WallpaperHistoryManager";
|
||||
private static final String PREFS_NAME = "wallpaper_history_prefs";
|
||||
private static final String KEY_HISTORY = "history";
|
||||
private static final int MAX_HISTORY_COUNT = 50;
|
||||
private static final int THUMBNAIL_SIZE = 200;
|
||||
private static final int THUMBNAIL_QUALITY = 85;
|
||||
|
||||
private final SharedPreferences prefs;
|
||||
private final Gson gson;
|
||||
private final File thumbnailDir;
|
||||
|
||||
public WallpaperHistoryManager(Context context) {
|
||||
this.prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
||||
this.gson = new Gson();
|
||||
|
||||
// 初始化缩略图缓存目录
|
||||
this.thumbnailDir = new File(context.getCacheDir(), "wallpaper_thumbnails");
|
||||
if (!thumbnailDir.exists()) {
|
||||
thumbnailDir.mkdirs();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加历史记录
|
||||
* @param uri 壁纸URI
|
||||
* @param albumId 所属相册ID
|
||||
* @param albumName 所属相册名称
|
||||
* @param bitmap 壁纸位图(用于生成缩略图)
|
||||
*/
|
||||
public void addHistory(String uri, String albumId, String albumName, Bitmap bitmap) {
|
||||
if (uri == null || albumId == null || albumName == null) {
|
||||
Log.w(TAG, "Invalid parameters: uri, albumId, or albumName is null");
|
||||
return;
|
||||
}
|
||||
|
||||
List<WallpaperHistory> historyList = loadHistory();
|
||||
|
||||
// URI去重:删除相同URI的旧记录
|
||||
Iterator<WallpaperHistory> iterator = historyList.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
WallpaperHistory history = iterator.next();
|
||||
if (uri.equals(history.getUri())) {
|
||||
// 删除旧的缩略图文件
|
||||
deleteThumbnailFile(history.getThumbnailPath());
|
||||
iterator.remove();
|
||||
Log.d(TAG, "Removed duplicate history record for URI: " + uri);
|
||||
}
|
||||
}
|
||||
|
||||
// 生成缩略图
|
||||
String thumbnailPath = saveThumbnail(bitmap);
|
||||
|
||||
// 创建新的历史记录
|
||||
WallpaperHistory history = new WallpaperHistory(uri, albumId, albumName, thumbnailPath);
|
||||
historyList.add(0, history); // 添加到列表开头
|
||||
|
||||
// FIFO队列:超过50条时删除最旧的记录
|
||||
if (historyList.size() > MAX_HISTORY_COUNT) {
|
||||
WallpaperHistory oldest = historyList.remove(historyList.size() - 1);
|
||||
deleteThumbnailFile(oldest.getThumbnailPath());
|
||||
Log.d(TAG, "Removed oldest history record, size now: " + historyList.size());
|
||||
}
|
||||
|
||||
// 保存到 SharedPreferences
|
||||
saveHistoryList(historyList);
|
||||
Log.d(TAG, "Added new history record, total count: " + historyList.size());
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载所有历史记录(按时间倒序)
|
||||
*/
|
||||
public List<WallpaperHistory> loadHistory() {
|
||||
String historyJson = prefs.getString(KEY_HISTORY, null);
|
||||
if (historyJson == null) {
|
||||
return new ArrayList<>();
|
||||
}
|
||||
|
||||
try {
|
||||
Type listType = new TypeToken<ArrayList<WallpaperHistory>>(){}.getType();
|
||||
List<WallpaperHistory> historyList = gson.fromJson(historyJson, listType);
|
||||
|
||||
// 按时间倒序排序
|
||||
Collections.sort(historyList, new Comparator<WallpaperHistory>() {
|
||||
@Override
|
||||
public int compare(WallpaperHistory h1, WallpaperHistory h2) {
|
||||
return Long.compare(h2.getTimestamp(), h1.getTimestamp());
|
||||
}
|
||||
});
|
||||
|
||||
return historyList;
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Failed to load history", e);
|
||||
return new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除单条历史记录
|
||||
*/
|
||||
public void deleteHistory(String historyId) {
|
||||
List<WallpaperHistory> historyList = loadHistory();
|
||||
|
||||
Iterator<WallpaperHistory> iterator = historyList.iterator();
|
||||
while (iterator.hasNext()) {
|
||||
WallpaperHistory history = iterator.next();
|
||||
if (historyId.equals(history.getId())) {
|
||||
// 删除缩略图文件
|
||||
deleteThumbnailFile(history.getThumbnailPath());
|
||||
iterator.remove();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
saveHistoryList(historyList);
|
||||
Log.d(TAG, "Deleted history record: " + historyId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有历史记录
|
||||
*/
|
||||
public void clearHistory() {
|
||||
List<WallpaperHistory> historyList = loadHistory();
|
||||
|
||||
// 删除所有缩略图文件
|
||||
for (WallpaperHistory history : historyList) {
|
||||
deleteThumbnailFile(history.getThumbnailPath());
|
||||
}
|
||||
|
||||
// 清空列表
|
||||
saveHistoryList(new ArrayList<WallpaperHistory>());
|
||||
Log.d(TAG, "Cleared all history records");
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成并保存缩略图(保持宽高比)
|
||||
* @param bitmap 原始位图
|
||||
* @return 缩略图文件路径
|
||||
*/
|
||||
private String saveThumbnail(Bitmap bitmap) {
|
||||
if (bitmap == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// 生成唯一文件名
|
||||
String fileName = System.currentTimeMillis() + "_" + (int)(Math.random() * 1000) + ".jpg";
|
||||
File thumbnailFile = new File(thumbnailDir, fileName);
|
||||
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
// 计算缩放比例,保持宽高比
|
||||
int originalWidth = bitmap.getWidth();
|
||||
int originalHeight = bitmap.getHeight();
|
||||
float scale = Math.min(
|
||||
(float) THUMBNAIL_SIZE / originalWidth,
|
||||
(float) THUMBNAIL_SIZE / originalHeight
|
||||
);
|
||||
|
||||
int scaledWidth = Math.round(originalWidth * scale);
|
||||
int scaledHeight = Math.round(originalHeight * scale);
|
||||
|
||||
// 缩放位图,保持宽高比
|
||||
Bitmap thumbnail = Bitmap.createScaledBitmap(bitmap, scaledWidth, scaledHeight, true);
|
||||
|
||||
// 保存为 JPEG
|
||||
fos = new FileOutputStream(thumbnailFile);
|
||||
thumbnail.compress(Bitmap.CompressFormat.JPEG, THUMBNAIL_QUALITY, fos);
|
||||
|
||||
Log.d(TAG, "Saved thumbnail: " + thumbnailFile.getAbsolutePath() +
|
||||
" (original: " + originalWidth + "x" + originalHeight +
|
||||
", scaled: " + scaledWidth + "x" + scaledHeight + ")");
|
||||
return thumbnailFile.getAbsolutePath();
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Failed to save thumbnail", e);
|
||||
return null;
|
||||
} finally {
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "Failed to close FileOutputStream", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加载缩略图
|
||||
* @param path 缩略图文件路径
|
||||
* @return 缩略图位图,失败返回 null
|
||||
*/
|
||||
public Bitmap loadThumbnail(String path) {
|
||||
if (path == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
File thumbnailFile = new File(path);
|
||||
if (!thumbnailFile.exists()) {
|
||||
Log.w(TAG, "Thumbnail file not found: " + path);
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
return BitmapFactory.decodeFile(path);
|
||||
} catch (Exception e) {
|
||||
Log.e(TAG, "Failed to load thumbnail: " + path, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除缩略图文件
|
||||
*/
|
||||
private void deleteThumbnailFile(String path) {
|
||||
if (path == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
File file = new File(path);
|
||||
if (file.exists()) {
|
||||
boolean deleted = file.delete();
|
||||
Log.d(TAG, "Deleted thumbnail file: " + path + ", success: " + deleted);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存历史记录列表到 SharedPreferences
|
||||
*/
|
||||
private void saveHistoryList(List<WallpaperHistory> historyList) {
|
||||
String historyJson = gson.toJson(historyList);
|
||||
prefs.edit().putString(KEY_HISTORY, historyJson).apply();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取历史记录数量
|
||||
*/
|
||||
public int getHistoryCount() {
|
||||
return loadHistory().size();
|
||||
}
|
||||
}
|
||||
@@ -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="M13,3c-4.97,0 -9,4.03 -9,9H1l3.89,3.89 0.07,0.14L9,12H6c0,-3.87 3.13,-7 7,-7s7,3.13 7,7 -3.13,7 -7,7c-1.93,0 -3.68,-0.79 -4.94,-2.06l-1.42,1.42C8.27,19.99 10.51,21 13,21c4.97,0 9,-4.03 9,-9s-4.03,-9 -9,-9zM12,8v5l4.28,2.54 0.72,-1.21 -3.5,-2.08V8H12z"/>
|
||||
</vector>
|
||||
@@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
android:background="@color/background">
|
||||
|
||||
<!-- 标题栏 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="horizontal"
|
||||
android:gravity="center_vertical"
|
||||
android:paddingStart="16dp"
|
||||
android:paddingEnd="16dp"
|
||||
android:paddingTop="16dp"
|
||||
android:paddingBottom="12dp"
|
||||
android:background="@color/surface">
|
||||
|
||||
<TextView
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="壁纸历史"
|
||||
android:textSize="20sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/text_primary" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/btnClearHistory"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="36dp"
|
||||
android:text="清空"
|
||||
android:textSize="14sp"
|
||||
app:icon="@drawable/ic_delete"
|
||||
app:iconGravity="textStart"
|
||||
app:iconPadding="4dp"
|
||||
app:cornerRadius="8dp"
|
||||
style="@style/Widget.Material3.Button.OutlinedButton"
|
||||
android:textColor="@color/error"
|
||||
app:iconTint="@color/error"
|
||||
app:strokeColor="@color/error" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
<!-- 历史记录列表 -->
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/rvHistory"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:padding="8dp"
|
||||
android:clipToPadding="false" />
|
||||
|
||||
<!-- 空状态提示 -->
|
||||
<TextView
|
||||
android:id="@+id/tvEmptyHint"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
android:layout_weight="1"
|
||||
android:text="暂无历史记录\n壁纸使用后会自动记录在这里"
|
||||
android:textSize="16sp"
|
||||
android:textColor="@color/text_hint"
|
||||
android:gravity="center"
|
||||
android:visibility="gone" />
|
||||
|
||||
</LinearLayout>
|
||||
@@ -0,0 +1,80 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_margin="6dp"
|
||||
app:cardElevation="2dp"
|
||||
app:cardCornerRadius="12dp"
|
||||
android:clickable="true"
|
||||
android:focusable="true">
|
||||
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical">
|
||||
|
||||
<!-- 缩略图 -->
|
||||
<ImageView
|
||||
android:id="@+id/ivThumbnail"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="150dp"
|
||||
android:scaleType="centerCrop"
|
||||
android:background="@color/image_placeholder" />
|
||||
|
||||
<!-- 信息区域 -->
|
||||
<LinearLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:orientation="vertical"
|
||||
android:padding="12dp">
|
||||
|
||||
<!-- 相册名称 -->
|
||||
<TextView
|
||||
android:id="@+id/tvAlbumName"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="相册名称"
|
||||
android:textSize="14sp"
|
||||
android:textStyle="bold"
|
||||
android:textColor="@color/text_primary"
|
||||
android:maxLines="1"
|
||||
android:ellipsize="end"
|
||||
android:drawableStart="@drawable/ic_album"
|
||||
android:drawablePadding="4dp" />
|
||||
|
||||
<!-- 时间戳 -->
|
||||
<TextView
|
||||
android:id="@+id/tvTimestamp"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="01-15 14:30"
|
||||
android:textSize="12sp"
|
||||
android:textColor="@color/text_hint"
|
||||
android:layout_marginTop="4dp"
|
||||
android:drawableStart="@drawable/ic_schedule"
|
||||
android:drawablePadding="4dp" />
|
||||
|
||||
<!-- 删除按钮 -->
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/btnDelete"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="36dp"
|
||||
android:text="删除"
|
||||
android:textSize="12sp"
|
||||
android:layout_marginTop="8dp"
|
||||
app:icon="@drawable/ic_delete"
|
||||
app:iconGravity="textStart"
|
||||
app:iconPadding="4dp"
|
||||
app:cornerRadius="6dp"
|
||||
style="@style/Widget.Material3.Button.OutlinedButton"
|
||||
android:textColor="@color/error"
|
||||
app:iconTint="@color/error"
|
||||
app:strokeColor="@color/error" />
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
||||
@@ -18,9 +18,15 @@
|
||||
android:title="@string/nav_albums"
|
||||
android:orderInCategory="2" />
|
||||
|
||||
<item
|
||||
android:id="@+id/nav_history"
|
||||
android:icon="@drawable/ic_history"
|
||||
android:title="@string/nav_history"
|
||||
android:orderInCategory="3" />
|
||||
|
||||
<item
|
||||
android:id="@+id/nav_settings"
|
||||
android:icon="@drawable/ic_settings"
|
||||
android:title="@string/nav_settings"
|
||||
android:orderInCategory="3" />
|
||||
android:orderInCategory="4" />
|
||||
</menu>
|
||||
@@ -31,6 +31,7 @@
|
||||
<!-- 卡片颜色 -->
|
||||
<color name="card_background">#FFFFFF</color>
|
||||
<color name="card_stroke">#E0E0E0</color>
|
||||
<color name="image_placeholder">#E0E0E0</color>
|
||||
|
||||
<!-- Launcher icon colors -->
|
||||
<color name="ic_launcher_background">#FF6200EE</color>
|
||||
|
||||
@@ -20,5 +20,6 @@
|
||||
<string name="nav_home">主页</string>
|
||||
<string name="nav_wallpaper">壁纸</string>
|
||||
<string name="nav_albums">相册</string>
|
||||
<string name="nav_history">历史</string>
|
||||
<string name="nav_settings">设置</string>
|
||||
</resources>
|
||||
Reference in new issue
Block a user