feat: 完善壁纸应用功能并优化性能
## 主要功能改进 ### 1. 修复壁纸引擎架构 - 将 WallpaperEngine 合并到 AutoWallpaperService 作为内部类 - 修复 WallpaperService.Engine 继承问题 - 修正 Paint 类导入,使用 android.graphics.Paint ### 2. 支持现代 Android 存储框架 - ImageLoader 支持 content:// URI 格式加载图片 - ImageScanner 支持 DocumentFile API 访问 SAF 文件夹 - 保留传统 File API 兼容性 - 持久化 URI 权限 ### 3. 添加文件夹管理功能 - 新增文件夹列表展示,支持查看图片数量 - 实现文件夹移除功能,带确认对话框 - 使用 ViewHolder 模式优化列表性能 - 缓存图片数量,避免重复扫描 ### 4. 优化用户体验 - 每次启用都弹出壁纸设置引导 - 添加进度条显示加载状态 - 首次启动显示使用指南 - 异步扫描文件夹,避免阻塞主线程 ### 5. 资源文件完善 - 添加应用图标(自适应图标和传统图标) - 创建应用主题 themes.xml - 添加文件夹列表项布局 item_folder.xml - 完善颜色资源定义 ### 6. 性能优化 - 异步扫描大文件夹,避免卡顿 - 优先使用缓存数据,加快启动速度 - 异步加载图片数量统计 - ViewHolder 复用减少 findViewById 调用 ### 7. 修复编译问题 - 解决 Kotlin stdlib 依赖冲突 - 修复 TextView 导入错误 - 移除有问题的 WallpaperEngine 独立类 ## 技术改进 - 使用 DocumentFile.fromTreeUri() 处理 SAF 文件夹 - ContentResolver.openInputStream() 加载 URI 图片 - 异步任务优化 UI 响应 - 智能缓存策略减少 I/O 操作 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
390d417f5b
commit
d304297f2c
16 files changed
+759
-226
No files matched your search
+16
-1
@@ -4,7 +4,7 @@ plugins {
|
|||||||
|
|
||||||
android {
|
android {
|
||||||
namespace 'cn.hugh.bz.gk2'
|
namespace 'cn.hugh.bz.gk2'
|
||||||
compileSdk 33
|
compileSdk 34
|
||||||
|
|
||||||
defaultConfig {
|
defaultConfig {
|
||||||
applicationId "cn.hugh.bz.gk2"
|
applicationId "cn.hugh.bz.gk2"
|
||||||
@@ -27,6 +27,21 @@ android {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
configurations.all {
|
||||||
|
resolutionStrategy {
|
||||||
|
// 强制使用统一的 Kotlin 版本,避免依赖冲突
|
||||||
|
force 'org.jetbrains.kotlin:kotlin-stdlib:1.8.22'
|
||||||
|
eachDependency { DependencyResolveDetails details ->
|
||||||
|
if (details.requested.group == 'org.jetbrains.kotlin') {
|
||||||
|
details.useVersion '1.8.22'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 排除已废弃的 kotlin-stdlib-jdk7 和 kotlin-stdlib-jdk8
|
||||||
|
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk7'
|
||||||
|
exclude group: 'org.jetbrains.kotlin', module: 'kotlin-stdlib-jdk8'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation 'androidx.appcompat:appcompat:1.6.1'
|
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||||
implementation 'com.google.android.material:material:1.9.0'
|
implementation 'com.google.android.material:material:1.9.0'
|
||||||
|
|||||||
@@ -1,17 +1,27 @@
|
|||||||
package cn.hugh.bz.gk2.ui;
|
package cn.hugh.bz.gk2.ui;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
|
import android.app.WallpaperManager;
|
||||||
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.view.LayoutInflater;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewGroup;
|
||||||
|
import android.widget.ArrayAdapter;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
import android.widget.ListView;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.activity.result.ActivityResultLauncher;
|
import androidx.activity.result.ActivityResultLauncher;
|
||||||
import androidx.activity.result.contract.ActivityResultContracts;
|
import androidx.activity.result.contract.ActivityResultContracts;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
import androidx.annotation.Nullable;
|
||||||
|
import androidx.appcompat.app.AlertDialog;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
import androidx.documentfile.provider.DocumentFile;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -31,10 +41,13 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
private Button btnChangeNow;
|
private Button btnChangeNow;
|
||||||
private Button btnSelectFolders;
|
private Button btnSelectFolders;
|
||||||
private Button btnSettings;
|
private Button btnSettings;
|
||||||
|
private ListView lvFolders;
|
||||||
|
private android.widget.ProgressBar progressBar;
|
||||||
|
|
||||||
private WallpaperPreferences prefs;
|
private WallpaperPreferences prefs;
|
||||||
private AppConfig config;
|
private AppConfig config;
|
||||||
private ImageScanner scanner;
|
private ImageScanner scanner;
|
||||||
|
private FolderAdapter folderAdapter;
|
||||||
|
|
||||||
// Folder picker launcher
|
// Folder picker launcher
|
||||||
private final ActivityResultLauncher<Intent> folderPickerLauncher =
|
private final ActivityResultLauncher<Intent> folderPickerLauncher =
|
||||||
@@ -42,8 +55,14 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
|
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
|
||||||
Uri uri = result.getData().getData();
|
Uri uri = result.getData().getData();
|
||||||
if (uri != null) {
|
if (uri != null) {
|
||||||
String folderPath = uri.getPath();
|
// 持久化 URI 权限
|
||||||
addFolder(folderPath);
|
final int takeFlags = Intent.FLAG_GRANT_READ_URI_PERMISSION |
|
||||||
|
Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
|
||||||
|
getContentResolver().takePersistableUriPermission(uri, takeFlags);
|
||||||
|
|
||||||
|
// 保存完整的 URI 字符串
|
||||||
|
String folderUri = uri.toString();
|
||||||
|
addFolder(folderUri);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -59,6 +78,33 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
initViews();
|
initViews();
|
||||||
updateUI();
|
updateUI();
|
||||||
|
|
||||||
|
// 首次启动显示引导
|
||||||
|
showFirstRunGuide();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 首次启动显示使用指南
|
||||||
|
*/
|
||||||
|
private void showFirstRunGuide() {
|
||||||
|
boolean hasShownGuide = prefs.getBoolean("first_run_guide_shown", false);
|
||||||
|
|
||||||
|
if (!hasShownGuide) {
|
||||||
|
new AlertDialog.Builder(this)
|
||||||
|
.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() {
|
private void initViews() {
|
||||||
@@ -68,6 +114,12 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
btnChangeNow = findViewById(R.id.btnChangeNow);
|
btnChangeNow = findViewById(R.id.btnChangeNow);
|
||||||
btnSelectFolders = findViewById(R.id.btnSelectFolders);
|
btnSelectFolders = findViewById(R.id.btnSelectFolders);
|
||||||
btnSettings = findViewById(R.id.btnSettings);
|
btnSettings = findViewById(R.id.btnSettings);
|
||||||
|
lvFolders = findViewById(R.id.lvFolders);
|
||||||
|
progressBar = findViewById(R.id.progressBar);
|
||||||
|
|
||||||
|
// 设置文件夹列表适配器
|
||||||
|
folderAdapter = new FolderAdapter(this, config.getSelectedFolders());
|
||||||
|
lvFolders.setAdapter(folderAdapter);
|
||||||
|
|
||||||
btnToggle.setOnClickListener(v -> toggleService());
|
btnToggle.setOnClickListener(v -> toggleService());
|
||||||
btnChangeNow.setOnClickListener(v -> changeWallpaperNow());
|
btnChangeNow.setOnClickListener(v -> changeWallpaperNow());
|
||||||
@@ -82,14 +134,15 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
boolean newState = !config.isEnabled();
|
boolean newState = !config.isEnabled();
|
||||||
config.setEnabled(newState);
|
|
||||||
prefs.saveConfig(config);
|
|
||||||
|
|
||||||
if (newState) {
|
if (newState) {
|
||||||
AlarmScheduler scheduler = new AlarmScheduler(this);
|
// 每次启用都弹出壁纸设置引导
|
||||||
scheduler.scheduleWallpaperChange(config.getIntervalMinutes());
|
showSetWallpaperDialog();
|
||||||
Toast.makeText(this, "已启用", Toast.LENGTH_SHORT).show();
|
// 注意:不在这里设置 enabled,而是在用户设置壁纸后再设置
|
||||||
} else {
|
} else {
|
||||||
|
config.setEnabled(newState);
|
||||||
|
prefs.saveConfig(config);
|
||||||
|
|
||||||
AlarmScheduler scheduler = new AlarmScheduler(this);
|
AlarmScheduler scheduler = new AlarmScheduler(this);
|
||||||
scheduler.cancelSchedule();
|
scheduler.cancelSchedule();
|
||||||
Toast.makeText(this, "已禁用", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "已禁用", Toast.LENGTH_SHORT).show();
|
||||||
@@ -98,6 +151,67 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
updateUI();
|
updateUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查当前壁纸是否是本应用的壁纸
|
||||||
|
*/
|
||||||
|
private boolean isWallpaperActive() {
|
||||||
|
// 简化检查:检查是否有壁纸设置记录
|
||||||
|
return prefs.getBoolean("wallpaper_set", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 显示引导设置壁纸的对话框
|
||||||
|
*/
|
||||||
|
private void showSetWallpaperDialog() {
|
||||||
|
new AlertDialog.Builder(this)
|
||||||
|
.setTitle("设置动态壁纸")
|
||||||
|
.setMessage("点击\"确定\"后将打开壁纸选择界面,请选择\"BzForGk2\"作为动态壁纸。\n\n设置完成后返回应用即可自动启用。")
|
||||||
|
.setPositiveButton("确定", (dialog, which) -> {
|
||||||
|
openWallpaperSelector();
|
||||||
|
// 启用服务
|
||||||
|
config.setEnabled(true);
|
||||||
|
prefs.saveConfig(config);
|
||||||
|
|
||||||
|
AlarmScheduler scheduler = new AlarmScheduler(this);
|
||||||
|
scheduler.scheduleWallpaperChange(config.getIntervalMinutes());
|
||||||
|
|
||||||
|
Toast.makeText(this, "已启用,请选择壁纸", Toast.LENGTH_LONG).show();
|
||||||
|
updateUI();
|
||||||
|
})
|
||||||
|
.setNegativeButton("取消", null)
|
||||||
|
.show();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 打开系统壁纸选择界面
|
||||||
|
*/
|
||||||
|
private void openWallpaperSelector() {
|
||||||
|
try {
|
||||||
|
Intent intent = new Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER);
|
||||||
|
intent.putExtra(
|
||||||
|
WallpaperManager.EXTRA_LIVE_WALLPAPER_COMPONENT,
|
||||||
|
new android.content.ComponentName(this, cn.hugh.bz.gk2.wallpaper.AutoWallpaperService.class)
|
||||||
|
);
|
||||||
|
startActivity(intent);
|
||||||
|
} catch (Exception e) {
|
||||||
|
// 如果上述方法失败,尝试打开壁纸选择器
|
||||||
|
try {
|
||||||
|
Intent intent = new Intent(WallpaperManager.ACTION_LIVE_WALLPAPER_CHOOSER);
|
||||||
|
startActivity(intent);
|
||||||
|
Toast.makeText(this, "请选择 BzForGk2 动态壁纸", Toast.LENGTH_LONG).show();
|
||||||
|
} catch (Exception ex) {
|
||||||
|
// 最后的备用方案:打开壁纸设置页面
|
||||||
|
try {
|
||||||
|
Intent intent = new Intent("android.settings.WALLPAPER_SETTINGS");
|
||||||
|
startActivity(intent);
|
||||||
|
Toast.makeText(this, "请选择 动态壁纸 → BzForGk2", Toast.LENGTH_LONG).show();
|
||||||
|
} catch (Exception exc) {
|
||||||
|
Toast.makeText(this, "无法打开壁纸设置,请手动去系统设置中选择", Toast.LENGTH_LONG).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void changeWallpaperNow() {
|
private void changeWallpaperNow() {
|
||||||
if (config.getSelectedFolders().isEmpty()) {
|
if (config.getSelectedFolders().isEmpty()) {
|
||||||
Toast.makeText(this, "请先选择文件夹", Toast.LENGTH_SHORT).show();
|
Toast.makeText(this, "请先选择文件夹", Toast.LENGTH_SHORT).show();
|
||||||
@@ -129,11 +243,26 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
config.getSelectedFolders().add(folderPath);
|
config.getSelectedFolders().add(folderPath);
|
||||||
prefs.saveConfig(config);
|
prefs.saveConfig(config);
|
||||||
|
|
||||||
// Scan folder
|
// 异步扫描文件夹
|
||||||
scanner.scanFolder(folderPath);
|
Toast.makeText(this, "正在扫描文件夹...", Toast.LENGTH_SHORT).show();
|
||||||
|
new Thread(() -> {
|
||||||
|
scanner.scanFolder(folderPath);
|
||||||
|
runOnUiThread(() -> {
|
||||||
|
updateUI();
|
||||||
|
Toast.makeText(this, "已添加文件夹", Toast.LENGTH_SHORT).show();
|
||||||
|
});
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
updateUI();
|
private void removeFolder(int position) {
|
||||||
Toast.makeText(this, "已添加文件夹", Toast.LENGTH_SHORT).show();
|
if (position >= 0 && position < config.getSelectedFolders().size()) {
|
||||||
|
String folderPath = config.getSelectedFolders().get(position);
|
||||||
|
config.getSelectedFolders().remove(position);
|
||||||
|
prefs.saveConfig(config);
|
||||||
|
|
||||||
|
updateUI();
|
||||||
|
Toast.makeText(this, "已移除文件夹", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateUI() {
|
private void updateUI() {
|
||||||
@@ -148,13 +277,32 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
btnChangeNow.setEnabled(false);
|
btnChangeNow.setEnabled(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update folder info
|
// Update folder info - 使用缓存的数量,不重新扫描
|
||||||
int imageCount = scanner.getTotalImageCount(config.getSelectedFolders());
|
|
||||||
if (config.getSelectedFolders().isEmpty()) {
|
if (config.getSelectedFolders().isEmpty()) {
|
||||||
tvFolderInfo.setText(R.string.no_folders);
|
tvFolderInfo.setText(R.string.no_folders);
|
||||||
|
progressBar.setVisibility(android.view.View.GONE);
|
||||||
} else {
|
} else {
|
||||||
tvFolderInfo.setText(String.format("已选择 %d 个文件夹,共 %d 张图片",
|
// 显示进度条
|
||||||
config.getSelectedFolders().size(), imageCount));
|
progressBar.setVisibility(android.view.View.VISIBLE);
|
||||||
|
progressBar.setIndeterminate(true);
|
||||||
|
|
||||||
|
// 异步获取图片数量
|
||||||
|
tvFolderInfo.setText(String.format("已选择 %d 个文件夹,正在统计图片...",
|
||||||
|
config.getSelectedFolders().size()));
|
||||||
|
|
||||||
|
new Thread(() -> {
|
||||||
|
int imageCount = scanner.getTotalImageCount(config.getSelectedFolders());
|
||||||
|
runOnUiThread(() -> {
|
||||||
|
tvFolderInfo.setText(String.format("已选择 %d 个文件夹,共 %d 张图片",
|
||||||
|
config.getSelectedFolders().size(), imageCount));
|
||||||
|
progressBar.setVisibility(android.view.View.GONE);
|
||||||
|
});
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update folder list adapter
|
||||||
|
if (folderAdapter != null) {
|
||||||
|
folderAdapter.updateFolders(config.getSelectedFolders());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,4 +312,113 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
config = prefs.loadConfig();
|
config = prefs.loadConfig();
|
||||||
updateUI();
|
updateUI();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 文件夹列表适配器
|
||||||
|
*/
|
||||||
|
private class FolderAdapter extends ArrayAdapter<String> {
|
||||||
|
private final Context context;
|
||||||
|
private List<String> folders;
|
||||||
|
private final java.util.Map<String, Integer> imageCounts = new java.util.HashMap<>();
|
||||||
|
|
||||||
|
public FolderAdapter(Context context, List<String> folders) {
|
||||||
|
super(context, R.layout.item_folder, folders);
|
||||||
|
this.context = context;
|
||||||
|
this.folders = new ArrayList<>(folders);
|
||||||
|
// 异步加载图片数量
|
||||||
|
loadImageCounts();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void updateFolders(List<String> newFolders) {
|
||||||
|
this.folders = new ArrayList<>(newFolders);
|
||||||
|
clear();
|
||||||
|
addAll(newFolders);
|
||||||
|
// 异步加载图片数量
|
||||||
|
loadImageCounts();
|
||||||
|
notifyDataSetChanged();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadImageCounts() {
|
||||||
|
new Thread(() -> {
|
||||||
|
for (String folderUri : folders) {
|
||||||
|
if (!imageCounts.containsKey(folderUri)) {
|
||||||
|
int count = scanner.scanFolder(folderUri).size();
|
||||||
|
imageCounts.put(folderUri, count);
|
||||||
|
runOnUiThread(() -> notifyDataSetChanged());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
@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, 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("正在扫描...");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 移除按钮
|
||||||
|
holder.btnRemove.setOnClickListener(v -> {
|
||||||
|
new AlertDialog.Builder(context)
|
||||||
|
.setTitle("移除文件夹")
|
||||||
|
.setMessage("确定要移除这个文件夹吗?")
|
||||||
|
.setPositiveButton("确定", (dialog, which) -> {
|
||||||
|
removeFolder(position);
|
||||||
|
})
|
||||||
|
.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 class ViewHolder {
|
||||||
|
TextView tvFolderName;
|
||||||
|
TextView tvFolderPath;
|
||||||
|
TextView tvImageCount;
|
||||||
|
Button btnRemove;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,11 @@
|
|||||||
package cn.hugh.bz.gk2.utils;
|
package cn.hugh.bz.gk2.utils;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.net.Uri;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import androidx.documentfile.provider.DocumentFile;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@@ -24,8 +27,16 @@ public class ImageScanner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public List<WallpaperImage> scanFolder(String folderPath) {
|
public List<WallpaperImage> scanFolder(String folderPath) {
|
||||||
|
// 先尝试从缓存加载
|
||||||
|
List<WallpaperImage> cached = jsonHelper.loadImageIndex(folderPath);
|
||||||
|
if (cached != null && !cached.isEmpty()) {
|
||||||
|
Log.i(TAG, "Using cached index for: " + folderPath + ", count: " + cached.size());
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 检查是否需要重新扫描
|
||||||
if (!jsonHelper.needsRescan(folderPath)) {
|
if (!jsonHelper.needsRescan(folderPath)) {
|
||||||
List<WallpaperImage> cached = jsonHelper.loadImageIndex(folderPath);
|
cached = jsonHelper.loadImageIndex(folderPath);
|
||||||
if (cached != null) {
|
if (cached != null) {
|
||||||
Log.i(TAG, "Using cached index for: " + folderPath);
|
Log.i(TAG, "Using cached index for: " + folderPath);
|
||||||
return cached;
|
return cached;
|
||||||
@@ -34,29 +45,60 @@ public class ImageScanner {
|
|||||||
|
|
||||||
Log.i(TAG, "Scanning folder: " + folderPath);
|
Log.i(TAG, "Scanning folder: " + folderPath);
|
||||||
|
|
||||||
File folder = new File(folderPath);
|
|
||||||
if (!folder.exists() || !folder.isDirectory()) {
|
|
||||||
Log.e(TAG, "Folder not found or not a directory: " + folderPath);
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
List<WallpaperImage> images = new ArrayList<>();
|
List<WallpaperImage> images = new ArrayList<>();
|
||||||
File[] files = folder.listFiles();
|
|
||||||
|
|
||||||
if (files == null) {
|
// 检查是否是 URI 格式
|
||||||
return images;
|
if (folderPath.startsWith("content://")) {
|
||||||
}
|
// 使用 DocumentFile API
|
||||||
|
Uri folderUri = Uri.parse(folderPath);
|
||||||
|
DocumentFile folder = DocumentFile.fromTreeUri(context, folderUri);
|
||||||
|
|
||||||
for (File file : files) {
|
if (folder == null || !folder.exists()) {
|
||||||
if (file.isFile() && isImageFile(file)) {
|
Log.e(TAG, "Folder not found: " + folderPath);
|
||||||
WallpaperImage image = new WallpaperImage(
|
return images;
|
||||||
file.getAbsolutePath(),
|
}
|
||||||
folderPath,
|
|
||||||
file.getName(),
|
DocumentFile[] files = folder.listFiles();
|
||||||
file.lastModified(),
|
if (files == null) {
|
||||||
file.length()
|
return images;
|
||||||
);
|
}
|
||||||
images.add(image);
|
|
||||||
|
for (DocumentFile file : files) {
|
||||||
|
if (file.isFile() && isImageFile(file)) {
|
||||||
|
WallpaperImage image = new WallpaperImage(
|
||||||
|
file.getUri().toString(),
|
||||||
|
folderPath,
|
||||||
|
file.getName(),
|
||||||
|
file.lastModified(),
|
||||||
|
file.length()
|
||||||
|
);
|
||||||
|
images.add(image);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 使用传统 File API
|
||||||
|
File folder = new File(folderPath);
|
||||||
|
if (!folder.exists() || !folder.isDirectory()) {
|
||||||
|
Log.e(TAG, "Folder not found or not a directory: " + folderPath);
|
||||||
|
return new ArrayList<>();
|
||||||
|
}
|
||||||
|
|
||||||
|
File[] files = folder.listFiles();
|
||||||
|
if (files == null) {
|
||||||
|
return images;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (File file : files) {
|
||||||
|
if (file.isFile() && isImageFile(file)) {
|
||||||
|
WallpaperImage image = new WallpaperImage(
|
||||||
|
file.getAbsolutePath(),
|
||||||
|
folderPath,
|
||||||
|
file.getName(),
|
||||||
|
file.lastModified(),
|
||||||
|
file.length()
|
||||||
|
);
|
||||||
|
images.add(image);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,6 +119,16 @@ public class ImageScanner {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isImageFile(DocumentFile file) {
|
||||||
|
String name = file.getName().toLowerCase();
|
||||||
|
for (String ext : IMAGE_EXTENSIONS) {
|
||||||
|
if (name.endsWith("." + ext)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public WallpaperImage getImageAtIndex(List<String> folders, int index) {
|
public WallpaperImage getImageAtIndex(List<String> folders, int index) {
|
||||||
List<WallpaperImage> allImages = loadAllImages(folders);
|
List<WallpaperImage> allImages = loadAllImages(folders);
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,22 @@
|
|||||||
package cn.hugh.bz.gk2.wallpaper;
|
package cn.hugh.bz.gk2.wallpaper;
|
||||||
|
|
||||||
import android.app.WallpaperManager;
|
import android.graphics.Canvas;
|
||||||
import android.content.BroadcastReceiver;
|
import android.graphics.Color;
|
||||||
import android.content.Context;
|
import android.graphics.Paint;
|
||||||
import android.content.Intent;
|
import android.graphics.RectF;
|
||||||
import android.content.IntentFilter;
|
import android.os.Handler;
|
||||||
|
import android.os.Looper;
|
||||||
import android.service.wallpaper.WallpaperService;
|
import android.service.wallpaper.WallpaperService;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.SurfaceHolder;
|
||||||
|
|
||||||
import cn.hugh.bz.gk2.R;
|
import cn.hugh.bz.gk2.model.AppConfig;
|
||||||
|
import cn.hugh.bz.gk2.model.PlayMode;
|
||||||
|
import cn.hugh.bz.gk2.model.WallpaperImage;
|
||||||
|
import cn.hugh.bz.gk2.utils.ImageScanner;
|
||||||
|
|
||||||
public class AutoWallpaperService extends WallpaperService {
|
public class AutoWallpaperService extends WallpaperService {
|
||||||
|
private static final String TAG = "AutoWallpaperService";
|
||||||
|
|
||||||
private WallpaperEngine engine;
|
private WallpaperEngine engine;
|
||||||
|
|
||||||
@@ -18,7 +25,7 @@ public class AutoWallpaperService extends WallpaperService {
|
|||||||
super.onCreate();
|
super.onCreate();
|
||||||
|
|
||||||
// Register refresh receiver
|
// Register refresh receiver
|
||||||
IntentFilter filter = new IntentFilter("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER");
|
android.content.IntentFilter filter = new android.content.IntentFilter("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER");
|
||||||
registerReceiver(refreshReceiver, filter);
|
registerReceiver(refreshReceiver, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -30,13 +37,13 @@ public class AutoWallpaperService extends WallpaperService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Engine onCreateEngine() {
|
public Engine onCreateEngine() {
|
||||||
engine = new WallpaperEngine(this);
|
engine = new WallpaperEngine();
|
||||||
return engine;
|
return engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final BroadcastReceiver refreshReceiver = new BroadcastReceiver() {
|
private final android.content.BroadcastReceiver refreshReceiver = new android.content.BroadcastReceiver() {
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(android.content.Context context, android.content.Intent intent) {
|
||||||
if ("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER".equals(intent.getAction())) {
|
if ("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER".equals(intent.getAction())) {
|
||||||
if (engine != null) {
|
if (engine != null) {
|
||||||
engine.refreshWallpaper();
|
engine.refreshWallpaper();
|
||||||
@@ -44,4 +51,166 @@ public class AutoWallpaperService extends WallpaperService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class WallpaperEngine extends Engine {
|
||||||
|
private final ImageLoader imageLoader;
|
||||||
|
private final WallpaperPreferences prefs;
|
||||||
|
private final Handler handler;
|
||||||
|
|
||||||
|
private int width;
|
||||||
|
private int height;
|
||||||
|
private boolean visible = false;
|
||||||
|
|
||||||
|
public WallpaperEngine() {
|
||||||
|
this.imageLoader = new ImageLoader(AutoWallpaperService.this);
|
||||||
|
this.prefs = new WallpaperPreferences(AutoWallpaperService.this);
|
||||||
|
this.handler = new Handler(Looper.getMainLooper());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCreate(android.view.SurfaceHolder surfaceHolder) {
|
||||||
|
super.onCreate(surfaceHolder);
|
||||||
|
// 标记壁纸已设置
|
||||||
|
prefs.putBoolean("wallpaper_set", true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurfaceChanged(android.view.SurfaceHolder holder, int format,
|
||||||
|
int width, int height) {
|
||||||
|
super.onSurfaceChanged(holder, format, width, height);
|
||||||
|
this.width = width;
|
||||||
|
this.height = height;
|
||||||
|
|
||||||
|
if (visible) {
|
||||||
|
drawWallpaper();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onVisibilityChanged(boolean visible) {
|
||||||
|
super.onVisibilityChanged(visible);
|
||||||
|
this.visible = visible;
|
||||||
|
|
||||||
|
if (visible) {
|
||||||
|
drawWallpaper();
|
||||||
|
} else {
|
||||||
|
// Release resources when not visible
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSurfaceDestroyed(android.view.SurfaceHolder holder) {
|
||||||
|
super.onSurfaceDestroyed(holder);
|
||||||
|
this.visible = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void refreshWallpaper() {
|
||||||
|
if (visible) {
|
||||||
|
handler.post(this::drawWallpaper);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawWallpaper() {
|
||||||
|
android.view.SurfaceHolder holder = getSurfaceHolder();
|
||||||
|
if (holder == null || !holder.getSurface().isValid()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Canvas canvas = null;
|
||||||
|
try {
|
||||||
|
canvas = holder.lockCanvas();
|
||||||
|
if (canvas != null) {
|
||||||
|
// Clear canvas
|
||||||
|
canvas.drawColor(Color.BLACK);
|
||||||
|
|
||||||
|
// Load current wallpaper
|
||||||
|
AppConfig config = prefs.loadConfig();
|
||||||
|
if (!config.isEnabled() || config.getSelectedFolders().isEmpty()) {
|
||||||
|
drawPlaceholder(canvas);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
WallpaperImage image = getNextWallpaper(config);
|
||||||
|
if (image != null) {
|
||||||
|
drawImage(canvas, image.getUri());
|
||||||
|
|
||||||
|
// Save config
|
||||||
|
prefs.saveConfig(config);
|
||||||
|
prefs.saveLastUri(image.getUri());
|
||||||
|
} else {
|
||||||
|
drawPlaceholder(canvas);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Error drawing wallpaper", e);
|
||||||
|
} finally {
|
||||||
|
if (canvas != null) {
|
||||||
|
holder.unlockCanvasAndPost(canvas);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private WallpaperImage getNextWallpaper(AppConfig config) {
|
||||||
|
ImageScanner scanner = new ImageScanner(AutoWallpaperService.this);
|
||||||
|
String lastUri = prefs.getLastUri();
|
||||||
|
|
||||||
|
if (config.getPlayMode() == PlayMode.SEQUENTIAL) {
|
||||||
|
// Sequential mode
|
||||||
|
int index = config.getCurrentIndex();
|
||||||
|
WallpaperImage image = scanner.getImageAtIndex(config.getSelectedFolders(), index);
|
||||||
|
if (image != null) {
|
||||||
|
config.setCurrentIndex(index + 1);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Random mode
|
||||||
|
WallpaperImage image = scanner.getRandomImage(config.getSelectedFolders(), lastUri);
|
||||||
|
return image;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawImage(Canvas canvas, String uri) {
|
||||||
|
Log.d(TAG, "Drawing image: " + uri + ", canvas size: " + width + "x" + height);
|
||||||
|
android.graphics.Bitmap bitmap = imageLoader.loadImage(uri, width, height);
|
||||||
|
if (bitmap != null) {
|
||||||
|
Log.d(TAG, "Bitmap loaded: " + bitmap.getWidth() + "x" + bitmap.getHeight());
|
||||||
|
|
||||||
|
// Calculate scale to cover the entire screen while maintaining aspect ratio
|
||||||
|
float scaleX = (float) width / bitmap.getWidth();
|
||||||
|
float scaleY = (float) height / bitmap.getHeight();
|
||||||
|
float scale = Math.max(scaleX, scaleY);
|
||||||
|
|
||||||
|
float scaledWidth = bitmap.getWidth() * scale;
|
||||||
|
float scaledHeight = bitmap.getHeight() * scale;
|
||||||
|
|
||||||
|
// Center the image
|
||||||
|
float left = (width - scaledWidth) / 2f;
|
||||||
|
float top = (height - scaledHeight) / 2f;
|
||||||
|
|
||||||
|
Log.d(TAG, "Drawing at: left=" + left + ", top=" + top +
|
||||||
|
", size=" + scaledWidth + "x" + scaledHeight);
|
||||||
|
|
||||||
|
canvas.drawBitmap(bitmap, null,
|
||||||
|
new RectF(left, top, left + scaledWidth, top + scaledHeight),
|
||||||
|
null);
|
||||||
|
} else {
|
||||||
|
Log.e(TAG, "Failed to load bitmap: " + uri);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void drawPlaceholder(Canvas canvas) {
|
||||||
|
// Draw placeholder text
|
||||||
|
Paint paint = new Paint();
|
||||||
|
paint.setColor(Color.WHITE);
|
||||||
|
paint.setTextAlign(Paint.Align.CENTER);
|
||||||
|
paint.setTextSize(48);
|
||||||
|
|
||||||
|
float centerX = width / 2f;
|
||||||
|
float centerY = height / 2f;
|
||||||
|
|
||||||
|
canvas.drawText("请选择文件夹", centerX, centerY, paint);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -3,9 +3,12 @@ package cn.hugh.bz.gk2.wallpaper;
|
|||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
import android.graphics.BitmapFactory;
|
import android.graphics.BitmapFactory;
|
||||||
|
import android.net.Uri;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
public class ImageLoader {
|
public class ImageLoader {
|
||||||
private static final String TAG = "ImageLoader";
|
private static final String TAG = "ImageLoader";
|
||||||
@@ -15,11 +18,53 @@ public class ImageLoader {
|
|||||||
this.context = context;
|
this.context = context;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Bitmap loadImage(String uri, int maxWidth, int maxHeight) {
|
public Bitmap loadImage(String uriString, int maxWidth, int maxHeight) {
|
||||||
try {
|
try {
|
||||||
File file = new File(uri);
|
// 检查是否是 content:// URI
|
||||||
|
if (uriString.startsWith("content://")) {
|
||||||
|
return loadFromUri(Uri.parse(uriString), maxWidth, maxHeight);
|
||||||
|
} else {
|
||||||
|
return loadFromFile(uriString, maxWidth, maxHeight);
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Failed to load image: " + uriString, e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bitmap loadFromUri(Uri uri, int maxWidth, int maxHeight) {
|
||||||
|
try (InputStream inputStream = context.getContentResolver().openInputStream(uri)) {
|
||||||
|
// First decode with inJustDecodeBounds=true to check dimensions
|
||||||
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
||||||
|
options.inJustDecodeBounds = true;
|
||||||
|
BitmapFactory.decodeStream(inputStream, null, options);
|
||||||
|
|
||||||
|
// Calculate inSampleSize
|
||||||
|
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
|
||||||
|
|
||||||
|
// Decode bitmap with inSampleSize
|
||||||
|
options.inJustDecodeBounds = false;
|
||||||
|
options.inPreferredConfig = Bitmap.Config.RGB_565;
|
||||||
|
|
||||||
|
// Reopen stream for actual decode
|
||||||
|
try (InputStream inputStream2 = context.getContentResolver().openInputStream(uri)) {
|
||||||
|
Bitmap bitmap = BitmapFactory.decodeStream(inputStream2, null, options);
|
||||||
|
|
||||||
|
Log.i(TAG, "Loaded image from URI: " + uri + ", size: " +
|
||||||
|
(bitmap != null ? bitmap.getWidth() + "x" + bitmap.getHeight() : "null"));
|
||||||
|
return bitmap;
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
Log.e(TAG, "Failed to load from URI: " + uri, e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Bitmap loadFromFile(String filePath, int maxWidth, int maxHeight) {
|
||||||
|
try {
|
||||||
|
File file = new File(filePath);
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
Log.e(TAG, "File not found: " + uri);
|
Log.e(TAG, "File not found: " + filePath);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -37,14 +82,9 @@ public class ImageLoader {
|
|||||||
|
|
||||||
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
|
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
|
||||||
|
|
||||||
if (bitmap != null) {
|
|
||||||
// Scale to exact size if needed
|
|
||||||
bitmap = Bitmap.createScaledBitmap(bitmap, maxWidth, maxHeight, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
return bitmap;
|
return bitmap;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Log.e(TAG, "Failed to load image: " + uri, e);
|
Log.e(TAG, "Failed to load from file: " + filePath, e);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,170 +0,0 @@
|
|||||||
package cn.hugh.bz.gk2.wallpaper;
|
|
||||||
|
|
||||||
import android.graphics.Canvas;
|
|
||||||
import android.os.Handler;
|
|
||||||
import android.os.Looper;
|
|
||||||
import android.service.wallpaper.WallpaperService;
|
|
||||||
import android.util.Log;
|
|
||||||
import android.view.SurfaceHolder;
|
|
||||||
|
|
||||||
import cn.hugh.bz.gk2.model.AppConfig;
|
|
||||||
import cn.hugh.bz.gk2.model.PlayMode;
|
|
||||||
import cn.hugh.bz.gk2.model.WallpaperImage;
|
|
||||||
import cn.hugh.bz.gk2.utils.ImageScanner;
|
|
||||||
import cn.hugh.bz.gk2.utils.JsonHelper;
|
|
||||||
|
|
||||||
public class WallpaperEngine extends WallpaperService.Engine {
|
|
||||||
private static final String TAG = "WallpaperEngine";
|
|
||||||
|
|
||||||
private final AutoWallpaperService service;
|
|
||||||
private final ImageLoader imageLoader;
|
|
||||||
private final WallpaperPreferences prefs;
|
|
||||||
private final Handler handler;
|
|
||||||
|
|
||||||
private int width;
|
|
||||||
private int height;
|
|
||||||
private boolean visible = false;
|
|
||||||
|
|
||||||
public WallpaperEngine(AutoWallpaperService service) {
|
|
||||||
this.service = service;
|
|
||||||
this.imageLoader = new ImageLoader(service);
|
|
||||||
this.prefs = new WallpaperPreferences(service);
|
|
||||||
this.handler = new Handler(Looper.getMainLooper());
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onCreate(SurfaceHolder surfaceHolder) {
|
|
||||||
super.onCreate(surfaceHolder);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSurfaceChanged(SurfaceHolder holder, int format,
|
|
||||||
int width, int height) {
|
|
||||||
super.onSurfaceChanged(holder, format, width, height);
|
|
||||||
this.width = width;
|
|
||||||
this.height = height;
|
|
||||||
|
|
||||||
if (visible) {
|
|
||||||
drawWallpaper();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onVisibilityChanged(boolean visible) {
|
|
||||||
super.onVisibilityChanged(visible);
|
|
||||||
this.visible = visible;
|
|
||||||
|
|
||||||
if (visible) {
|
|
||||||
drawWallpaper();
|
|
||||||
} else {
|
|
||||||
// Release resources when not visible
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSurfaceDestroyed(SurfaceHolder holder) {
|
|
||||||
super.onSurfaceDestroyed(holder);
|
|
||||||
this.visible = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void refreshWallpaper() {
|
|
||||||
if (visible) {
|
|
||||||
handler.post(this::drawWallpaper);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void drawWallpaper() {
|
|
||||||
SurfaceHolder holder = getSurfaceHolder();
|
|
||||||
if (holder == null || !holder.getSurface().isValid()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Canvas canvas = null;
|
|
||||||
try {
|
|
||||||
canvas = holder.lockCanvas();
|
|
||||||
if (canvas != null) {
|
|
||||||
// Clear canvas
|
|
||||||
canvas.drawColor(android.graphics.Color.BLACK);
|
|
||||||
|
|
||||||
// Load current wallpaper
|
|
||||||
AppConfig config = prefs.loadConfig();
|
|
||||||
if (!config.isEnabled() || config.getSelectedFolders().isEmpty()) {
|
|
||||||
drawPlaceholder(canvas);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
WallpaperImage image = getNextWallpaper(config);
|
|
||||||
if (image != null) {
|
|
||||||
drawImage(canvas, image.getUri());
|
|
||||||
|
|
||||||
// Save config
|
|
||||||
prefs.saveConfig(config);
|
|
||||||
prefs.saveLastUri(image.getUri());
|
|
||||||
} else {
|
|
||||||
drawPlaceholder(canvas);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
Log.e(TAG, "Error drawing wallpaper", e);
|
|
||||||
} finally {
|
|
||||||
if (canvas != null) {
|
|
||||||
holder.unlockCanvasAndPost(canvas);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private WallpaperImage getNextWallpaper(AppConfig config) {
|
|
||||||
ImageScanner scanner = new ImageScanner(service);
|
|
||||||
String lastUri = prefs.getLastUri();
|
|
||||||
|
|
||||||
if (config.getPlayMode() == PlayMode.SEQUENTIAL) {
|
|
||||||
// Sequential mode
|
|
||||||
int index = config.getCurrentIndex();
|
|
||||||
WallpaperImage image = scanner.getImageAtIndex(config.getSelectedFolders(), index);
|
|
||||||
if (image != null) {
|
|
||||||
config.setCurrentIndex(index + 1);
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Random mode
|
|
||||||
WallpaperImage image = scanner.getRandomImage(config.getSelectedFolders(), lastUri);
|
|
||||||
return image;
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void drawImage(Canvas canvas, String uri) {
|
|
||||||
android.graphics.Bitmap bitmap = imageLoader.loadImage(uri, width, height);
|
|
||||||
if (bitmap != null) {
|
|
||||||
// Center and scale the image
|
|
||||||
float scale = Math.max(
|
|
||||||
(float) width / bitmap.getWidth(),
|
|
||||||
(float) height / bitmap.getHeight()
|
|
||||||
);
|
|
||||||
|
|
||||||
float scaledWidth = bitmap.getWidth() * scale;
|
|
||||||
float scaledHeight = bitmap.getHeight() * scale;
|
|
||||||
|
|
||||||
float left = (width - scaledWidth) / 2;
|
|
||||||
float top = (height - scaledHeight) / 2;
|
|
||||||
|
|
||||||
canvas.drawBitmap(bitmap, null,
|
|
||||||
new android.graphics.RectF(left, top, left + scaledWidth, top + scaledHeight),
|
|
||||||
null);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void drawPlaceholder(Canvas canvas) {
|
|
||||||
// Draw placeholder text
|
|
||||||
android.Paint paint = new android.Paint();
|
|
||||||
paint.setColor(android.graphics.Color.WHITE);
|
|
||||||
paint.setTextAlign(android.Paint.Align.CENTER);
|
|
||||||
paint.setTextSize(48);
|
|
||||||
|
|
||||||
float centerX = width / 2f;
|
|
||||||
float centerY = height / 2f;
|
|
||||||
|
|
||||||
canvas.drawText("请选择文件夹", centerX, centerY, paint);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -70,4 +70,13 @@ public class WallpaperPreferences {
|
|||||||
public String getLastUri() {
|
public String getLastUri() {
|
||||||
return prefs.getString(KEY_LAST_URI, null);
|
return prefs.getString(KEY_LAST_URI, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 通用的键值对存储方法
|
||||||
|
public boolean getBoolean(String key, boolean defaultValue) {
|
||||||
|
return prefs.getBoolean(key, defaultValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void putBoolean(String key, boolean value) {
|
||||||
|
prefs.edit().putBoolean(key, value).apply();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -84,4 +84,30 @@
|
|||||||
app:layout_constraintStart_toStartOf="parent"
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
app:layout_constraintEnd_toEndOf="parent" />
|
app:layout_constraintEnd_toEndOf="parent" />
|
||||||
|
|
||||||
|
<ProgressBar
|
||||||
|
android:id="@+id/progressBar"
|
||||||
|
style="?android:attr/progressBarStyleHorizontal"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="8dp"
|
||||||
|
android:layout_marginStart="16dp"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
android:visibility="gone"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/tvFolderInfo"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent" />
|
||||||
|
|
||||||
|
<ListView
|
||||||
|
android:id="@+id/lvFolders"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="0dp"
|
||||||
|
android:layout_marginTop="16dp"
|
||||||
|
android:layout_marginBottom="16dp"
|
||||||
|
android:layout_marginStart="16dp"
|
||||||
|
android:layout_marginEnd="16dp"
|
||||||
|
app:layout_constraintTop_toBottomOf="@id/progressBar"
|
||||||
|
app:layout_constraintBottom_toBottomOf="parent"
|
||||||
|
app:layout_constraintStart_toStartOf="parent"
|
||||||
|
app:layout_constraintEnd_toEndOf="parent" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||||
@@ -0,0 +1,56 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:padding="12dp"
|
||||||
|
android:background="?android:attr/selectableItemBackground">
|
||||||
|
|
||||||
|
<LinearLayout
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_weight="1"
|
||||||
|
android:orientation="vertical"
|
||||||
|
android:gravity="center_vertical">
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvFolderName"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:textStyle="bold"
|
||||||
|
android:textColor="@android:color/black"
|
||||||
|
android:text="文件夹名称" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvFolderPath"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="4dp"
|
||||||
|
android:textSize="12sp"
|
||||||
|
android:textColor="@android:color/darker_gray"
|
||||||
|
android:singleLine="true"
|
||||||
|
android:ellipsize="middle"
|
||||||
|
android:text="content://..." />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/tvImageCount"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_marginTop="4dp"
|
||||||
|
android:textSize="12sp"
|
||||||
|
android:textColor="@android:color/darker_gray"
|
||||||
|
android:text="0 张图片" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/btnRemove"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_gravity="center_vertical"
|
||||||
|
android:text="移除"
|
||||||
|
android:textSize="14sp"
|
||||||
|
style="?android:attr/buttonBarButtonStyle" />
|
||||||
|
|
||||||
|
</LinearLayout>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<background android:drawable="@color/ic_launcher_background"/>
|
||||||
|
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
|
||||||
|
</adaptive-icon>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="108dp"
|
||||||
|
android:height="108dp"
|
||||||
|
android:viewportWidth="108"
|
||||||
|
android:viewportHeight="108">
|
||||||
|
<path
|
||||||
|
android:fillColor="@color/ic_launcher_foreground"
|
||||||
|
android:pathData="M54,19 C35.25,19 20,34.25 20,53 C20,71.75 35.25,87 54,87 C72.75,87 88,71.75 88,53 C88,34.25 72.75,19 54,19 ZM54,79 C39.64,79 28,67.36 28,53 C28,38.64 39.64,27 54,27 C68.36,27 80,38.64 80,53 C80,67.36 68.36,79 54,79 Z"/>
|
||||||
|
<path
|
||||||
|
android:fillColor="@color/ic_launcher_foreground"
|
||||||
|
android:pathData="M54,35 C47.37,35 42,40.37 42,47 C42,53.63 47.37,59 54,59 C60.63,59 66,53.63 66,47 C66,40.37 60.63,35 54,35 ZM54,55 C49.58,55 46,51.42 46,47 C46,42.58 49.58,39 54,39 C58.42,39 62,42.58 62,47 C62,51.42 58.42,55 54,55 Z"/>
|
||||||
|
</vector>
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
|
||||||
|
<background android:drawable="@color/ic_launcher_background"/>
|
||||||
|
<foreground android:drawable="@mipmap/ic_launcher_foreground"/>
|
||||||
|
</adaptive-icon>
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="108dp"
|
||||||
|
android:height="108dp"
|
||||||
|
android:viewportWidth="108"
|
||||||
|
android:viewportHeight="108">
|
||||||
|
<path
|
||||||
|
android:fillColor="@color/ic_launcher_background"
|
||||||
|
android:pathData="M0,0 L108,0 L108,108 L0,108 Z"/>
|
||||||
|
<path
|
||||||
|
android:fillColor="@color/ic_launcher_foreground"
|
||||||
|
android:pathData="M54,19 C35.25,19 20,34.25 20,53 C20,71.75 35.25,87 54,87 C72.75,87 88,71.75 88,53 C88,34.25 72.75,19 54,19 ZM54,79 C39.64,79 28,67.36 28,53 C28,38.64 39.64,27 54,27 C68.36,27 80,38.64 80,53 C80,67.36 68.36,79 54,79 Z"/>
|
||||||
|
<path
|
||||||
|
android:fillColor="@color/ic_launcher_foreground"
|
||||||
|
android:pathData="M54,35 C47.37,35 42,40.37 42,47 C42,53.63 47.37,59 54,59 C60.63,59 66,53.63 66,47 C66,40.37 60.63,35 54,35 ZM54,55 C49.58,55 46,51.42 46,47 C46,42.58 49.58,39 54,39 C58.42,39 62,42.58 62,47 C62,51.42 58.42,55 54,55 Z"/>
|
||||||
|
</vector>
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:width="108dp"
|
||||||
|
android:height="108dp"
|
||||||
|
android:viewportWidth="108"
|
||||||
|
android:viewportHeight="108">
|
||||||
|
<path
|
||||||
|
android:fillColor="@color/ic_launcher_background"
|
||||||
|
android:pathData="M0,54 C0,24.2 24.2,0 54,0 C83.8,0 108,24.2 108,54 C108,83.8 83.8,108 54,108 C24.2,108 0,83.8 0,54 Z"/>
|
||||||
|
<path
|
||||||
|
android:fillColor="@color/ic_launcher_foreground"
|
||||||
|
android:pathData="M54,25 C38.54,25 26,37.54 26,53 C26,68.46 38.54,81 54,81 C69.46,81 82,68.46 82,53 C82,37.54 69.46,25 54,25 ZM54,75 C42.4,75 33,65.6 33,54 C33,42.4 42.4,33 54,33 C65.6,33 75,42.4 75,54 C75,65.6 65.6,75 54,75 Z"/>
|
||||||
|
<path
|
||||||
|
android:fillColor="@color/ic_launcher_foreground"
|
||||||
|
android:pathData="M54,39 C48.48,39 44,43.48 44,49 C44,54.52 48.48,59 54,59 C59.52,59 64,54.52 64,49 C64,43.48 59.52,39 54,39 ZM54,55 C50.69,55 48,52.31 48,49 C48,45.69 50.69,43 54,43 C57.31,43 60,45.69 60,49 C60,52.31 57.31,55 54,55 Z"/>
|
||||||
|
</vector>
|
||||||
@@ -7,4 +7,8 @@
|
|||||||
<color name="teal_700">#FF018786</color>
|
<color name="teal_700">#FF018786</color>
|
||||||
<color name="black">#FF000000</color>
|
<color name="black">#FF000000</color>
|
||||||
<color name="white">#FFFFFFFF</color>
|
<color name="white">#FFFFFFFF</color>
|
||||||
|
|
||||||
|
<!-- Launcher icon colors -->
|
||||||
|
<color name="ic_launcher_background">#FF6200EE</color>
|
||||||
|
<color name="ic_launcher_foreground">#FFFFFFFF</color>
|
||||||
</resources>
|
</resources>
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<resources>
|
||||||
|
<!-- Base application theme -->
|
||||||
|
<style name="Theme.BzForGk2" parent="Theme.Material3.DayNight.NoActionBar">
|
||||||
|
<!-- Primary brand color -->
|
||||||
|
<item name="colorPrimary">@color/purple_500</item>
|
||||||
|
<item name="colorPrimaryVariant">@color/purple_700</item>
|
||||||
|
<item name="colorOnPrimary">@color/white</item>
|
||||||
|
|
||||||
|
<!-- Secondary brand color -->
|
||||||
|
<item name="colorSecondary">@color/teal_200</item>
|
||||||
|
<item name="colorSecondaryVariant">@color/teal_700</item>
|
||||||
|
<item name="colorOnSecondary">@color/black</item>
|
||||||
|
|
||||||
|
<!-- Status bar color -->
|
||||||
|
<item name="android:statusBarColor">?attr/colorPrimaryVariant</item>
|
||||||
|
|
||||||
|
<!-- Customize your theme here -->
|
||||||
|
</style>
|
||||||
|
</resources>
|
||||||
Reference in new issue
Block a user