2069 lines
60 KiB
Markdown
2069 lines
60 KiB
Markdown
# 自动壁纸更换应用 MVP 实施计划
|
||||
|
|
|
|||
|
|
> **For Claude:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.
|
|||
|
|
|
|||
|
|
**目标:** 构建一个自动壁纸更换 Android 应用,支持选择文件夹、顺序/随机模式、可配置更换间隔(最小1分钟),兼容鸿蒙 4.2 系统。
|
|||
|
|
|
|||
|
|
**架构:** 基于 `WallpaperService` 的 Live Wallpaper 实现,使用 `AlarmManager` 定时触发 Canvas 重绘,`SharedPreferences` + JSON 存储配置和图片索引。
|
|||
|
|
|
|||
|
|
**技术栈:**
|
|||
|
|
- Java (不是 Kotlin)
|
|||
|
|
- Android SDK (target API 31, 最低 API 24)
|
|||
|
|
- Gradle 构建系统
|
|||
|
|
- IntelliJ IDEA
|
|||
|
|
- Glide 图片加载库(或 BitmapFactory)
|
|||
|
|
- SharedPreferences + JSON 文件存储
|
|||
|
|
|
|||
|
|
**应用信息:**
|
|||
|
|
- 应用名:bzForGk2
|
|||
|
|
- 包名:cn.hugh.bz.gk2
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## MVP 功能范围
|
|||
|
|
|
|||
|
|
### 第一阶段 MVP(本计划):
|
|||
|
|
1. ✅ WallpaperService 核心框架
|
|||
|
|
2. ✅ 文件夹选择和图片扫描
|
|||
|
|
3. ✅ 顺序播放模式
|
|||
|
|
4. ✅ 随机播放模式
|
|||
|
|
5. ✅ AlarmManager 定时任务(1分钟间隔)
|
|||
|
|
6. ✅ SharedPreferences 配置存储
|
|||
|
|
7. ✅ 设置页面 UI
|
|||
|
|
8. ✅ 间隔时间选择(1-60分钟)
|
|||
|
|
9. ✅ 播放模式切换
|
|||
|
|
|
|||
|
|
### 第二阶段(未来):
|
|||
|
|
- 增量扫描优化
|
|||
|
|
- 图片元数据缓存
|
|||
|
|
- 播放历史记录
|
|||
|
|
- 主题和深色模式
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 项目结构
|
|||
|
|
|
|||
|
|
```
|
|||
|
|
bzForGk2/
|
|||
|
|
├── app/
|
|||
|
|
│ ├── src/
|
|||
|
|
│ │ ├── main/
|
|||
|
|
│ │ │ ├── java/cn/hugh/bz/gk2/
|
|||
|
|
│ │ │ │ ├── BzForGk2Application.java
|
|||
|
|
│ │ │ │ ├── wallpaper/
|
|||
|
|
│ │ │ │ │ ├── AutoWallpaperService.java
|
|||
|
|
│ │ │ │ │ ├── WallpaperEngine.java
|
|||
|
|
│ │ │ │ │ ├── ImageLoader.java
|
|||
|
|
│ │ │ │ │ └── WallpaperPreferences.java
|
|||
|
|
│ │ │ │ ├── provider/
|
|||
|
|
│ │ │ │ │ └── FileProvider.java
|
|||
|
|
│ │ │ │ ├── ui/
|
|||
|
|
│ │ │ │ │ ├── MainActivity.java
|
|||
|
|
│ │ │ │ │ ├── SettingsActivity.java
|
|||
|
|
│ │ │ │ │ ├── FolderPickerFragment.java
|
|||
|
|
│ │ │ │ │ └── adapters/
|
|||
|
|
│ │ │ │ │ └── FolderAdapter.java
|
|||
|
|
│ │ │ │ ├── model/
|
|||
|
|
│ │ │ │ │ ├── WallpaperImage.java
|
|||
|
|
│ │ │ │ │ ├── PlayMode.java
|
|||
|
|
│ │ │ │ │ └── AppConfig.java
|
|||
|
|
│ │ │ │ ├── receiver/
|
|||
|
|
│ │ │ │ │ └── WallpaperChangeReceiver.java
|
|||
|
|
│ │ │ │ └── utils/
|
|||
|
|
│ │ │ │ ├── ImageScanner.java
|
|||
|
|
│ │ │ │ ├── AlarmScheduler.java
|
|||
|
|
│ │ │ │ └── JsonHelper.java
|
|||
|
|
│ │ │ ├── res/
|
|||
|
|
│ │ │ │ ├── layout/
|
|||
|
|
│ │ │ │ │ ├── activity_main.xml
|
|||
|
|
│ │ │ │ │ ├── activity_settings.xml
|
|||
|
|
│ │ │ │ │ ├── fragment_folder_picker.xml
|
|||
|
|
│ │ │ │ │ ├── item_folder.xml
|
|||
|
|
│ │ │ │ │ └── wallpaper_preview.xml
|
|||
|
|
│ │ │ │ ├── values/
|
|||
|
|
│ │ │ │ │ ├── strings.xml
|
|||
|
|
│ │ │ │ │ ├── colors.xml
|
|||
|
|
│ │ │ │ │ └── dimens.xml
|
|||
|
|
│ │ │ │ └── xml/
|
|||
|
|
│ │ │ │ └── wallpaper_info.xml
|
|||
|
|
│ │ │ └── AndroidManifest.xml
|
|||
|
|
│ │ └── test/java/cn/hugh/bz/gk2/
|
|||
|
|
│ ├── build.gradle
|
|||
|
|
│ └── proguard-rules.pro
|
|||
|
|
├── gradle/
|
|||
|
|
│ └── wrapper/
|
|||
|
|
│ └── gradle-wrapper.properties
|
|||
|
|
├── build.gradle
|
|||
|
|
├── settings.gradle
|
|||
|
|
└── gradlew
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 1: 初始化 Android 项目结构
|
|||
|
|
|
|||
|
|
**目标:** 创建基础的 Android 项目骨架,配置 Gradle 构建文件。
|
|||
|
|
|
|||
|
|
**Files:**
|
|||
|
|
- Create: `settings.gradle`
|
|||
|
|
- Create: `build.gradle`
|
|||
|
|
- Create: `app/build.gradle`
|
|||
|
|
- Create: `gradle/wrapper/gradle-wrapper.properties`
|
|||
|
|
- Create: `app/src/main/AndroidManifest.xml`
|
|||
|
|
|
|||
|
|
### Step 1: 创建根级 settings.gradle
|
|||
|
|
|
|||
|
|
```gradle
|
|||
|
|
rootProject.name = "bzForGk2"
|
|||
|
|
include ':app'
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add settings.gradle
|
|||
|
|
git commit -m "chore: initialize project settings"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 2: 创建根级 build.gradle
|
|||
|
|
|
|||
|
|
```gradle
|
|||
|
|
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
|||
|
|
buildscript {
|
|||
|
|
repositories {
|
|||
|
|
google()
|
|||
|
|
mavenCentral()
|
|||
|
|
}
|
|||
|
|
dependencies {
|
|||
|
|
classpath 'com.android.tools.build:gradle:8.1.0'
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
allprojects {
|
|||
|
|
repositories {
|
|||
|
|
google()
|
|||
|
|
mavenCentral()
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
task clean(type: Delete) {
|
|||
|
|
delete rootProject.buildDir
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add build.gradle
|
|||
|
|
git commit -m "chore: add root build configuration"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 3: 创建 app/build.gradle
|
|||
|
|
|
|||
|
|
```gradle
|
|||
|
|
plugins {
|
|||
|
|
id 'com.android.application'
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
android {
|
|||
|
|
namespace 'cn.hugh.bz.gk2'
|
|||
|
|
compileSdk 33
|
|||
|
|
|
|||
|
|
defaultConfig {
|
|||
|
|
applicationId "cn.hugh.bz.gk2"
|
|||
|
|
minSdk 24
|
|||
|
|
targetSdk 31
|
|||
|
|
versionCode 1
|
|||
|
|
versionName "1.0"
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
buildTypes {
|
|||
|
|
release {
|
|||
|
|
minifyEnabled false
|
|||
|
|
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
compileOptions {
|
|||
|
|
sourceCompatibility JavaVersion.VERSION_1_8
|
|||
|
|
targetCompatibility JavaVersion.VERSION_1_8
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
dependencies {
|
|||
|
|
implementation 'androidx.appcompat:appcompat:1.6.1'
|
|||
|
|
implementation 'com.google.android.material:material:1.9.0'
|
|||
|
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
|||
|
|
|
|||
|
|
// Glide for image loading
|
|||
|
|
implementation 'com.github.bumptech.glide:glide:4.16.0'
|
|||
|
|
annotationProcessor 'com.github.bumptech.glide:compiler:4.16.0'
|
|||
|
|
|
|||
|
|
// JSON parsing
|
|||
|
|
implementation 'com.google.code.gson:gson:2.10.1'
|
|||
|
|
|
|||
|
|
// Permissions
|
|||
|
|
implementation 'androidx.core:core:1.12.0'
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/build.gradle
|
|||
|
|
git commit -m "chore: configure app module with dependencies"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 4: 创建 AndroidManifest.xml
|
|||
|
|
|
|||
|
|
```xml
|
|||
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|||
|
|
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
|||
|
|
|
|||
|
|
<!-- 权限声明 -->
|
|||
|
|
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
|
|||
|
|
android:maxSdkVersion="32" />
|
|||
|
|
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
|
|||
|
|
<uses-permission android:name="android.permission.SET_WALLPAPER" />
|
|||
|
|
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" />
|
|||
|
|
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
|
|||
|
|
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
|
|||
|
|
|
|||
|
|
<application
|
|||
|
|
android:allowBackup="true"
|
|||
|
|
android:icon="@mipmap/ic_launcher"
|
|||
|
|
android:label="@string/app_name"
|
|||
|
|
android:roundIcon="@mipmap/ic_launcher_round"
|
|||
|
|
android:supportsRtl="true"
|
|||
|
|
android:theme="@style/Theme.BzForGk2">
|
|||
|
|
|
|||
|
|
<!-- Main Activity -->
|
|||
|
|
<activity
|
|||
|
|
android:name=".ui.MainActivity"
|
|||
|
|
android:exported="true"
|
|||
|
|
android:theme="@style/Theme.BzForGk2">
|
|||
|
|
<intent-filter>
|
|||
|
|
<action android:name="android.intent.action.MAIN" />
|
|||
|
|
<category android:name="android.intent.category.LAUNCHER" />
|
|||
|
|
</intent-filter>
|
|||
|
|
</activity>
|
|||
|
|
|
|||
|
|
<!-- Settings Activity -->
|
|||
|
|
<activity
|
|||
|
|
android:name=".ui.SettingsActivity"
|
|||
|
|
android:exported="false"
|
|||
|
|
android:label="@string/settings"
|
|||
|
|
android:parentActivityName=".ui.MainActivity">
|
|||
|
|
<meta-data
|
|||
|
|
android:name="android.support.PARENT_ACTIVITY"
|
|||
|
|
android:value=".ui.MainActivity" />
|
|||
|
|
</activity>
|
|||
|
|
|
|||
|
|
<!-- Wallpaper Service -->
|
|||
|
|
<service
|
|||
|
|
android:name=".wallpaper.AutoWallpaperService"
|
|||
|
|
android:description="@string/wallpaper_description"
|
|||
|
|
android:enabled="true"
|
|||
|
|
android:icon="@mipmap/ic_launcher"
|
|||
|
|
android:label="@string/app_name"
|
|||
|
|
android:permission="android.permission.BIND_WALLPAPER"
|
|||
|
|
android:exported="true">
|
|||
|
|
<intent-filter>
|
|||
|
|
<action android:name="android.service.wallpaper.WallpaperService" />
|
|||
|
|
</intent-filter>
|
|||
|
|
|
|||
|
|
<meta-data
|
|||
|
|
android:name="android.service.wallpaper"
|
|||
|
|
android:resource="@xml/wallpaper_info" />
|
|||
|
|
</service>
|
|||
|
|
|
|||
|
|
<!-- Broadcast Receiver for wallpaper changes -->
|
|||
|
|
<receiver
|
|||
|
|
android:name=".receiver.WallpaperChangeReceiver"
|
|||
|
|
android:enabled="true"
|
|||
|
|
android:exported="false">
|
|||
|
|
<intent-filter>
|
|||
|
|
<action android:name="cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER" />
|
|||
|
|
</intent-filter>
|
|||
|
|
</receiver>
|
|||
|
|
</application>
|
|||
|
|
</manifest>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/AndroidManifest.xml
|
|||
|
|
git commit -m "feat: add manifest with permissions and components"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 2: 创建数据模型
|
|||
|
|
|
|||
|
|
**目标:** 创建应用所需的数据模型类。
|
|||
|
|
|
|||
|
|
**Files:**
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/model/PlayMode.java`
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/model/WallpaperImage.java`
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/model/AppConfig.java`
|
|||
|
|
|
|||
|
|
### Step 1: 创建 PlayMode 枚举
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.model;
|
|||
|
|
|
|||
|
|
public enum PlayMode {
|
|||
|
|
SEQUENTIAL("顺序播放"),
|
|||
|
|
RANDOM("随机播放");
|
|||
|
|
|
|||
|
|
private final String displayName;
|
|||
|
|
|
|||
|
|
PlayMode(String displayName) {
|
|||
|
|
this.displayName = displayName;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public String getDisplayName() {
|
|||
|
|
return displayName;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/model/PlayMode.java
|
|||
|
|
git commit -m "feat: add PlayMode enum"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 2: 创建 WallpaperImage 数据类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.model;
|
|||
|
|
|
|||
|
|
public class WallpaperImage {
|
|||
|
|
private String uri;
|
|||
|
|
private String folderPath;
|
|||
|
|
private String name;
|
|||
|
|
private long lastModified;
|
|||
|
|
private long fileSize;
|
|||
|
|
|
|||
|
|
public WallpaperImage(String uri, String folderPath, String name,
|
|||
|
|
long lastModified, long fileSize) {
|
|||
|
|
this.uri = uri;
|
|||
|
|
this.folderPath = folderPath;
|
|||
|
|
this.name = name;
|
|||
|
|
this.lastModified = lastModified;
|
|||
|
|
this.fileSize = fileSize;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Getters
|
|||
|
|
public String getUri() { return uri; }
|
|||
|
|
public String getFolderPath() { return folderPath; }
|
|||
|
|
public String getName() { return name; }
|
|||
|
|
public long getLastModified() { return lastModified; }
|
|||
|
|
public long getFileSize() { return fileSize; }
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/model/WallpaperImage.java
|
|||
|
|
git commit -m "feat: add WallpaperImage model"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 3: 创建 AppConfig 配置类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.model;
|
|||
|
|
|
|||
|
|
import java.util.List;
|
|||
|
|
|
|||
|
|
public class AppConfig {
|
|||
|
|
private List<String> selectedFolders;
|
|||
|
|
private PlayMode playMode;
|
|||
|
|
private int intervalMinutes;
|
|||
|
|
private boolean isEnabled;
|
|||
|
|
private int currentIndex;
|
|||
|
|
|
|||
|
|
public AppConfig() {
|
|||
|
|
this.selectedFolders = new java.util.ArrayList<>();
|
|||
|
|
this.playMode = PlayMode.SEQUENTIAL;
|
|||
|
|
this.intervalMinutes = 30;
|
|||
|
|
this.isEnabled = false;
|
|||
|
|
this.currentIndex = 0;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Getters and Setters
|
|||
|
|
public List<String> getSelectedFolders() { return selectedFolders; }
|
|||
|
|
public void setSelectedFolders(List<String> folders) { this.selectedFolders = folders; }
|
|||
|
|
|
|||
|
|
public PlayMode getPlayMode() { return playMode; }
|
|||
|
|
public void setPlayMode(PlayMode mode) { this.playMode = mode; }
|
|||
|
|
|
|||
|
|
public int getIntervalMinutes() { return intervalMinutes; }
|
|||
|
|
public void setIntervalMinutes(int minutes) { this.intervalMinutes = minutes; }
|
|||
|
|
|
|||
|
|
public boolean isEnabled() { return isEnabled; }
|
|||
|
|
public void setEnabled(boolean enabled) { isEnabled = enabled; }
|
|||
|
|
|
|||
|
|
public int getCurrentIndex() { return currentIndex; }
|
|||
|
|
public void setCurrentIndex(int index) { this.currentIndex = index; }
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/model/AppConfig.java
|
|||
|
|
git commit -m "feat: add AppConfig model"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 3: 创建 SharedPreferences 管理类
|
|||
|
|
|
|||
|
|
**目标:** 创建用于持久化配置的工具类。
|
|||
|
|
|
|||
|
|
**Files:**
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperPreferences.java`
|
|||
|
|
|
|||
|
|
### Step 1: 创建 WallpaperPreferences 类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.wallpaper;
|
|||
|
|
|
|||
|
|
import android.content.Context;
|
|||
|
|
import android.content.SharedPreferences;
|
|||
|
|
|
|||
|
|
import com.google.gson.Gson;
|
|||
|
|
import com.google.gson.reflect.TypeToken;
|
|||
|
|
|
|||
|
|
import java.lang.reflect.Type;
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.List;
|
|||
|
|
|
|||
|
|
import cn.hugh.bz.gk2.model.AppConfig;
|
|||
|
|
import cn.hugh.bz.gk2.model.PlayMode;
|
|||
|
|
|
|||
|
|
public class WallpaperPreferences {
|
|||
|
|
private static final String PREFS_NAME = "wallpaper_prefs";
|
|||
|
|
private static final String KEY_FOLDERS = "selected_folders";
|
|||
|
|
private static final String KEY_PLAY_MODE = "play_mode";
|
|||
|
|
private static final String KEY_INTERVAL = "interval_minutes";
|
|||
|
|
private static final String KEY_ENABLED = "enabled";
|
|||
|
|
private static final String KEY_CURRENT_INDEX = "current_index";
|
|||
|
|
private static final String KEY_LAST_URI = "last_uri";
|
|||
|
|
|
|||
|
|
private final SharedPreferences prefs;
|
|||
|
|
private final Gson gson;
|
|||
|
|
|
|||
|
|
public WallpaperPreferences(Context context) {
|
|||
|
|
this.prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
|
|||
|
|
this.gson = new Gson();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public AppConfig loadConfig() {
|
|||
|
|
AppConfig config = new AppConfig();
|
|||
|
|
|
|||
|
|
String foldersJson = prefs.getString(KEY_FOLDERS, null);
|
|||
|
|
if (foldersJson != null) {
|
|||
|
|
Type listType = new TypeToken<ArrayList<String>>(){}.getType();
|
|||
|
|
List<String> folders = gson.fromJson(foldersJson, listType);
|
|||
|
|
config.setSelectedFolders(folders);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
String modeString = prefs.getString(KEY_PLAY_MODE, PlayMode.SEQUENTIAL.name());
|
|||
|
|
config.setPlayMode(PlayMode.valueOf(modeString));
|
|||
|
|
|
|||
|
|
config.setIntervalMinutes(prefs.getInt(KEY_INTERVAL, 30));
|
|||
|
|
config.setEnabled(prefs.getBoolean(KEY_ENABLED, false));
|
|||
|
|
config.setCurrentIndex(prefs.getInt(KEY_CURRENT_INDEX, 0));
|
|||
|
|
|
|||
|
|
return config;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void saveConfig(AppConfig config) {
|
|||
|
|
SharedPreferences.Editor editor = prefs.edit();
|
|||
|
|
|
|||
|
|
String foldersJson = gson.toJson(config.getSelectedFolders());
|
|||
|
|
editor.putString(KEY_FOLDERS, foldersJson);
|
|||
|
|
editor.putString(KEY_PLAY_MODE, config.getPlayMode().name());
|
|||
|
|
editor.putInt(KEY_INTERVAL, config.getIntervalMinutes());
|
|||
|
|
editor.putBoolean(KEY_ENABLED, config.isEnabled());
|
|||
|
|
editor.putInt(KEY_CURRENT_INDEX, config.getCurrentIndex());
|
|||
|
|
|
|||
|
|
editor.apply();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void saveLastUri(String uri) {
|
|||
|
|
prefs.edit().putString(KEY_LAST_URI, uri).apply();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public String getLastUri() {
|
|||
|
|
return prefs.getString(KEY_LAST_URI, null);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperPreferences.java
|
|||
|
|
git commit -m "feat: add SharedPreferences manager"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 4: 实现 WallpaperService 核心服务
|
|||
|
|
|
|||
|
|
**目标:** 实现 Live Wallpaper 核心服务,负责在 Canvas 上绘制图片。
|
|||
|
|
|
|||
|
|
**Files:**
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java`
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperEngine.java`
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/wallpaper/ImageLoader.java`
|
|||
|
|
|
|||
|
|
### Step 1: 创建 ImageLoader 类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.wallpaper;
|
|||
|
|
|
|||
|
|
import android.content.Context;
|
|||
|
|
import android.graphics.Bitmap;
|
|||
|
|
import android.graphics.BitmapFactory;
|
|||
|
|
import android.util.Log;
|
|||
|
|
|
|||
|
|
import java.io.File;
|
|||
|
|
|
|||
|
|
public class ImageLoader {
|
|||
|
|
private static final String TAG = "ImageLoader";
|
|||
|
|
private final Context context;
|
|||
|
|
|
|||
|
|
public ImageLoader(Context context) {
|
|||
|
|
this.context = context;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public Bitmap loadImage(String uri, int maxWidth, int maxHeight) {
|
|||
|
|
try {
|
|||
|
|
File file = new File(uri);
|
|||
|
|
if (!file.exists()) {
|
|||
|
|
Log.e(TAG, "File not found: " + uri);
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// First decode with inJustDecodeBounds=true to check dimensions
|
|||
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|||
|
|
options.inJustDecodeBounds = true;
|
|||
|
|
BitmapFactory.decodeFile(file.getAbsolutePath(), options);
|
|||
|
|
|
|||
|
|
// Calculate inSampleSize
|
|||
|
|
options.inSampleSize = calculateInSampleSize(options, maxWidth, maxHeight);
|
|||
|
|
|
|||
|
|
// Decode bitmap with inSampleSize
|
|||
|
|
options.inJustDecodeBounds = false;
|
|||
|
|
options.inPreferredConfig = Bitmap.Config.RGB_565;
|
|||
|
|
|
|||
|
|
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;
|
|||
|
|
} catch (Exception e) {
|
|||
|
|
Log.e(TAG, "Failed to load image: " + uri, e);
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private int calculateInSampleSize(BitmapFactory.Options options,
|
|||
|
|
int reqWidth, int reqHeight) {
|
|||
|
|
final int height = options.outHeight;
|
|||
|
|
final int width = options.outWidth;
|
|||
|
|
int inSampleSize = 1;
|
|||
|
|
|
|||
|
|
if (height > reqHeight || width > reqWidth) {
|
|||
|
|
final int halfHeight = height / 2;
|
|||
|
|
final int halfWidth = width / 2;
|
|||
|
|
|
|||
|
|
while ((halfHeight / inSampleSize) >= reqHeight
|
|||
|
|
&& (halfWidth / inSampleSize) >= reqWidth) {
|
|||
|
|
inSampleSize *= 2;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return inSampleSize;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/wallpaper/ImageLoader.java
|
|||
|
|
git commit -m "feat: add image loader with sampling"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 2: 创建 WallpaperEngine 类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
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();
|
|||
|
|
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) {
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/wallpaper/WallpaperEngine.java
|
|||
|
|
git commit -m "feat: add wallpaper engine with drawing logic"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 3: 创建 AutoWallpaperService 类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.wallpaper;
|
|||
|
|
|
|||
|
|
import android.app.WallpaperManager;
|
|||
|
|
import android.content.Intent;
|
|||
|
|
import android.service.wallpaper.WallpaperService;
|
|||
|
|
|
|||
|
|
import cn.hugh.bz.gk2.R;
|
|||
|
|
|
|||
|
|
public class AutoWallpaperService extends WallpaperService {
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public Engine onCreateEngine() {
|
|||
|
|
return new WallpaperEngine(this);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java
|
|||
|
|
git commit -m "feat: add wallpaper service"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 5: 实现 AlarmScheduler 定时调度
|
|||
|
|
|
|||
|
|
**目标:** 实现定时任务调度,支持 1-60 分钟间隔。
|
|||
|
|
|
|||
|
|
**Files:**
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/utils/AlarmScheduler.java`
|
|||
|
|
|
|||
|
|
### Step 1: 创建 AlarmScheduler 类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.utils;
|
|||
|
|
|
|||
|
|
import android.app.AlarmManager;
|
|||
|
|
import android.app.PendingIntent;
|
|||
|
|
import android.content.Context;
|
|||
|
|
import android.content.Intent;
|
|||
|
|
import android.os.Build;
|
|||
|
|
import android.util.Log;
|
|||
|
|
|
|||
|
|
import cn.hugh.bz.gk2.receiver.WallpaperChangeReceiver;
|
|||
|
|
|
|||
|
|
public class AlarmScheduler {
|
|||
|
|
private static final String TAG = "AlarmScheduler";
|
|||
|
|
private static final int REQUEST_CODE = 1000;
|
|||
|
|
|
|||
|
|
private final Context context;
|
|||
|
|
private final AlarmManager alarmManager;
|
|||
|
|
|
|||
|
|
public AlarmScheduler(Context context) {
|
|||
|
|
this.context = context;
|
|||
|
|
this.alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void scheduleWallpaperChange(long intervalMinutes) {
|
|||
|
|
// Cancel existing alarm
|
|||
|
|
cancelSchedule();
|
|||
|
|
|
|||
|
|
// Calculate interval in milliseconds
|
|||
|
|
long intervalMillis = intervalMinutes * 60 * 1000;
|
|||
|
|
long triggerAtMillis = System.currentTimeMillis() + intervalMillis;
|
|||
|
|
|
|||
|
|
// Create pending intent
|
|||
|
|
Intent intent = new Intent(context, WallpaperChangeReceiver.class);
|
|||
|
|
intent.setAction(WallpaperChangeReceiver.ACTION_CHANGE_WALLPAPER);
|
|||
|
|
|
|||
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
|||
|
|
context,
|
|||
|
|
REQUEST_CODE,
|
|||
|
|
intent,
|
|||
|
|
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
// Schedule alarm
|
|||
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
|
|||
|
|
// Android 12+ needs exact alarm permission
|
|||
|
|
if (alarmManager.canScheduleExactAlarms()) {
|
|||
|
|
alarmManager.setExactAndAllowWhileIdle(
|
|||
|
|
AlarmManager.RTC_WAKEUP,
|
|||
|
|
triggerAtMillis,
|
|||
|
|
pendingIntent
|
|||
|
|
);
|
|||
|
|
} else {
|
|||
|
|
// Fallback to inexact alarm
|
|||
|
|
alarmManager.setAndAllowWhileIdle(
|
|||
|
|
AlarmManager.RTC_WAKEUP,
|
|||
|
|
triggerAtMillis,
|
|||
|
|
pendingIntent
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
|||
|
|
alarmManager.setExactAndAllowWhileIdle(
|
|||
|
|
AlarmManager.RTC_WAKEUP,
|
|||
|
|
triggerAtMillis,
|
|||
|
|
pendingIntent
|
|||
|
|
);
|
|||
|
|
} else {
|
|||
|
|
alarmManager.setExact(
|
|||
|
|
AlarmManager.RTC_WAKEUP,
|
|||
|
|
triggerAtMillis,
|
|||
|
|
pendingIntent
|
|||
|
|
);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Log.i(TAG, "Scheduled wallpaper change every " + intervalMinutes + " minutes");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void cancelSchedule() {
|
|||
|
|
Intent intent = new Intent(context, WallpaperChangeReceiver.class);
|
|||
|
|
intent.setAction(WallpaperChangeReceiver.ACTION_CHANGE_WALLPAPER);
|
|||
|
|
|
|||
|
|
PendingIntent pendingIntent = PendingIntent.getBroadcast(
|
|||
|
|
context,
|
|||
|
|
REQUEST_CODE,
|
|||
|
|
intent,
|
|||
|
|
PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_IMMUTABLE
|
|||
|
|
);
|
|||
|
|
|
|||
|
|
alarmManager.cancel(pendingIntent);
|
|||
|
|
Log.i(TAG, "Cancelled wallpaper change schedule");
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/utils/AlarmScheduler.java
|
|||
|
|
git commit -m "feat: add alarm scheduler with exact timing"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 6: 实现 BroadcastReceiver 接收器
|
|||
|
|
|
|||
|
|
**目标:** 实现接收定时任务的广播接收器,触发壁纸更新。
|
|||
|
|
|
|||
|
|
**Files:**
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/receiver/WallpaperChangeReceiver.java`
|
|||
|
|
|
|||
|
|
### Step 1: 创建 WallpaperChangeReceiver 类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.receiver;
|
|||
|
|
|
|||
|
|
import android.content.BroadcastReceiver;
|
|||
|
|
import android.content.Context;
|
|||
|
|
import android.content.Intent;
|
|||
|
|
import android.util.Log;
|
|||
|
|
|
|||
|
|
import cn.hugh.bz.gk2.utils.AlarmScheduler;
|
|||
|
|
|
|||
|
|
public class WallpaperChangeReceiver extends BroadcastReceiver {
|
|||
|
|
private static final String TAG = "WallpaperChangeReceiver";
|
|||
|
|
public static final String ACTION_CHANGE_WALLPAPER = "cn.hugh.bz.gk2.ACTION_CHANGE_WALLPAPER";
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void onReceive(Context context, Intent intent) {
|
|||
|
|
if (ACTION_CHANGE_WALLPAPER.equals(intent.getAction())) {
|
|||
|
|
Log.i(TAG, "Received wallpaper change request");
|
|||
|
|
|
|||
|
|
// Refresh wallpaper
|
|||
|
|
WallpaperRefreshHelper.refreshWallpaper(context);
|
|||
|
|
|
|||
|
|
// Schedule next change
|
|||
|
|
cn.hugh.bz.gk2.wallpaper.WallpaperPreferences prefs =
|
|||
|
|
new cn.hugh.bz.gk2.wallpaper.WallpaperPreferences(context);
|
|||
|
|
cn.hugh.bz.gk2.model.AppConfig config = prefs.loadConfig();
|
|||
|
|
|
|||
|
|
if (config.isEnabled()) {
|
|||
|
|
AlarmScheduler scheduler = new AlarmScheduler(context);
|
|||
|
|
scheduler.scheduleWallpaperChange(config.getIntervalMinutes());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Helper class to refresh wallpaper
|
|||
|
|
class WallpaperRefreshHelper {
|
|||
|
|
static void refreshWallpaper(Context context) {
|
|||
|
|
// Send broadcast to wallpaper service
|
|||
|
|
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER");
|
|||
|
|
context.sendBroadcast(intent);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/receiver/WallpaperChangeReceiver.java
|
|||
|
|
git commit -m "feat: add broadcast receiver for wallpaper changes"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 2: 更新 AutoWallpaperService 支持刷新广播
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.wallpaper;
|
|||
|
|
|
|||
|
|
import android.app.WallpaperManager;
|
|||
|
|
import android.content.BroadcastReceiver;
|
|||
|
|
import android.content.Context;
|
|||
|
|
import android.content.Intent;
|
|||
|
|
import android.content.IntentFilter;
|
|||
|
|
import android.service.wallpaper.WallpaperService;
|
|||
|
|
|
|||
|
|
import cn.hugh.bz.gk2.R;
|
|||
|
|
|
|||
|
|
public class AutoWallpaperService extends WallpaperService {
|
|||
|
|
|
|||
|
|
private WallpaperEngine engine;
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void onCreate() {
|
|||
|
|
super.onCreate();
|
|||
|
|
|
|||
|
|
// Register refresh receiver
|
|||
|
|
IntentFilter filter = new IntentFilter("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER");
|
|||
|
|
registerReceiver(refreshReceiver, filter);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void onDestroy() {
|
|||
|
|
unregisterReceiver(refreshReceiver);
|
|||
|
|
super.onDestroy();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public Engine onCreateEngine() {
|
|||
|
|
engine = new WallpaperEngine(this);
|
|||
|
|
return engine;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private final BroadcastReceiver refreshReceiver = new BroadcastReceiver() {
|
|||
|
|
@Override
|
|||
|
|
public void onReceive(Context context, Intent intent) {
|
|||
|
|
if ("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER".equals(intent.getAction())) {
|
|||
|
|
if (engine != null) {
|
|||
|
|
engine.refreshWallpaper();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/wallpaper/AutoWallpaperService.java
|
|||
|
|
git commit -m "feat: add refresh broadcast support to service"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 7: 实现 ImageScanner 图片扫描工具
|
|||
|
|
|
|||
|
|
**目标:** 实现文件夹扫描和图片索引功能。
|
|||
|
|
|
|||
|
|
**Files:**
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/utils/ImageScanner.java`
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/utils/JsonHelper.java`
|
|||
|
|
|
|||
|
|
### Step 1: 创建 JsonHelper 类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.utils;
|
|||
|
|
|
|||
|
|
import android.content.Context;
|
|||
|
|
import android.util.Log;
|
|||
|
|
|
|||
|
|
import com.google.gson.Gson;
|
|||
|
|
import com.google.gson.GsonBuilder;
|
|||
|
|
|
|||
|
|
import java.io.File;
|
|||
|
|
import java.io.FileReader;
|
|||
|
|
import java.io.FileWriter;
|
|||
|
|
import java.io.IOException;
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.List;
|
|||
|
|
|
|||
|
|
import cn.hugh.bz.gk2.model.WallpaperImage;
|
|||
|
|
|
|||
|
|
public class JsonHelper {
|
|||
|
|
private static final String TAG = "JsonHelper";
|
|||
|
|
private static final String INDEX_DIR = "image_indices";
|
|||
|
|
|
|||
|
|
private final Context context;
|
|||
|
|
private final Gson gson;
|
|||
|
|
private final File indicesDir;
|
|||
|
|
|
|||
|
|
public JsonHelper(Context context) {
|
|||
|
|
this.context = context;
|
|||
|
|
this.gson = new GsonBuilder().setPrettyPrinting().create();
|
|||
|
|
this.indicesDir = new File(context.getFilesDir(), INDEX_DIR);
|
|||
|
|
if (!indicesDir.exists()) {
|
|||
|
|
indicesDir.mkdirs();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public void saveIndex(String folderPath, List<WallpaperImage> images) {
|
|||
|
|
String fileName = getIndexFileName(folderPath);
|
|||
|
|
File indexFile = new File(indicesDir, fileName);
|
|||
|
|
|
|||
|
|
IndexData data = new IndexData(folderPath, new File(folderPath).getName(),
|
|||
|
|
System.currentTimeMillis(), images.size(), images);
|
|||
|
|
|
|||
|
|
try (FileWriter writer = new FileWriter(indexFile)) {
|
|||
|
|
gson.toJson(data, writer);
|
|||
|
|
Log.i(TAG, "Saved index: " + fileName + " with " + images.size() + " images");
|
|||
|
|
} catch (IOException e) {
|
|||
|
|
Log.e(TAG, "Failed to save index", e);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public List<WallpaperImage> loadImageIndex(String folderPath) {
|
|||
|
|
String fileName = getIndexFileName(folderPath);
|
|||
|
|
File indexFile = new File(indicesDir, fileName);
|
|||
|
|
|
|||
|
|
if (!indexFile.exists()) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
try (FileReader reader = new FileReader(indexFile)) {
|
|||
|
|
IndexData data = gson.fromJson(reader, IndexData.class);
|
|||
|
|
return data.images;
|
|||
|
|
} catch (IOException e) {
|
|||
|
|
Log.e(TAG, "Failed to load index", e);
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private String getIndexFileName(String folderPath) {
|
|||
|
|
int hash = folderPath.hashCode();
|
|||
|
|
return "folder_" + Math.abs(hash) + ".json";
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public boolean needsRescan(String folderPath) {
|
|||
|
|
List<WallpaperImage> cached = loadImageIndex(folderPath);
|
|||
|
|
if (cached == null) {
|
|||
|
|
return true;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
File folder = new File(folderPath);
|
|||
|
|
if (!folder.exists()) {
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
String fileName = getIndexFileName(folderPath);
|
|||
|
|
File indexFile = new File(indicesDir, fileName);
|
|||
|
|
|
|||
|
|
return folder.lastModified() > indexFile.lastModified();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private static class IndexData {
|
|||
|
|
String folderPath;
|
|||
|
|
String folderName;
|
|||
|
|
long lastScanned;
|
|||
|
|
int imageCount;
|
|||
|
|
List<WallpaperImage> images;
|
|||
|
|
|
|||
|
|
IndexData() {}
|
|||
|
|
|
|||
|
|
IndexData(String folderPath, String folderName, long lastScanned,
|
|||
|
|
int imageCount, List<WallpaperImage> images) {
|
|||
|
|
this.folderPath = folderPath;
|
|||
|
|
this.folderName = folderName;
|
|||
|
|
this.lastScanned = lastScanned;
|
|||
|
|
this.imageCount = imageCount;
|
|||
|
|
this.images = images;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/utils/JsonHelper.java
|
|||
|
|
git commit -m "feat: add JSON index storage"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 2: 创建 ImageScanner 类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.utils;
|
|||
|
|
|
|||
|
|
import android.content.Context;
|
|||
|
|
import android.util.Log;
|
|||
|
|
|
|||
|
|
import java.io.File;
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.Collections;
|
|||
|
|
import java.util.List;
|
|||
|
|
import java.util.Random;
|
|||
|
|
|
|||
|
|
import cn.hugh.bz.gk2.model.WallpaperImage;
|
|||
|
|
|
|||
|
|
public class ImageScanner {
|
|||
|
|
private static final String TAG = "ImageScanner";
|
|||
|
|
private static final List<String> IMAGE_EXTENSIONS = List.of("jpg", "jpeg", "png", "webp", "bmp");
|
|||
|
|
|
|||
|
|
private final Context context;
|
|||
|
|
private final JsonHelper jsonHelper;
|
|||
|
|
|
|||
|
|
public ImageScanner(Context context) {
|
|||
|
|
this.context = context;
|
|||
|
|
this.jsonHelper = new JsonHelper(context);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public List<WallpaperImage> scanFolder(String folderPath) {
|
|||
|
|
if (!jsonHelper.needsRescan(folderPath)) {
|
|||
|
|
List<WallpaperImage> cached = jsonHelper.loadImageIndex(folderPath);
|
|||
|
|
if (cached != null) {
|
|||
|
|
Log.i(TAG, "Using cached index for: " + folderPath);
|
|||
|
|
return cached;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
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<>();
|
|||
|
|
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);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Save to JSON
|
|||
|
|
jsonHelper.saveIndex(folderPath, images);
|
|||
|
|
|
|||
|
|
Log.i(TAG, "Found " + images.size() + " images in " + folderPath);
|
|||
|
|
return images;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private boolean isImageFile(File 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) {
|
|||
|
|
List<WallpaperImage> allImages = loadAllImages(folders);
|
|||
|
|
|
|||
|
|
if (allImages.isEmpty() || index < 0 || index >= allImages.size()) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return allImages.get(index);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public WallpaperImage getRandomImage(List<String> folders, String excludeUri) {
|
|||
|
|
List<WallpaperImage> allImages = loadAllImages(folders);
|
|||
|
|
|
|||
|
|
if (allImages.isEmpty()) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Filter out excluded image
|
|||
|
|
List<WallpaperImage> candidates = new ArrayList<>();
|
|||
|
|
for (WallpaperImage image : allImages) {
|
|||
|
|
if (!image.getUri().equals(excludeUri)) {
|
|||
|
|
candidates.add(image);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if (candidates.isEmpty()) {
|
|||
|
|
// All images are the same as excluded, return any
|
|||
|
|
return allImages.get(0);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Random random = new Random();
|
|||
|
|
return candidates.get(random.nextInt(candidates.size()));
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public int getTotalImageCount(List<String> folders) {
|
|||
|
|
return loadAllImages(folders).size();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private List<WallpaperImage> loadAllImages(List<String> folders) {
|
|||
|
|
List<WallpaperImage> allImages = new ArrayList<>();
|
|||
|
|
|
|||
|
|
for (String folder : folders) {
|
|||
|
|
List<WallpaperImage> images = jsonHelper.loadImageIndex(folder);
|
|||
|
|
if (images != null) {
|
|||
|
|
allImages.addAll(images);
|
|||
|
|
} else {
|
|||
|
|
// Scan if no cache
|
|||
|
|
allImages.addAll(scanFolder(folder));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return allImages;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/utils/ImageScanner.java
|
|||
|
|
git commit -m "feat: add image scanner with folder scanning"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 8: 创建 MainActivity 主界面
|
|||
|
|
|
|||
|
|
**目标:** 创建主界面,显示当前壁纸预览和快速操作按钮。
|
|||
|
|
|
|||
|
|
**Files:**
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/ui/MainActivity.java`
|
|||
|
|
- Create: `app/src/main/res/layout/activity_main.xml`
|
|||
|
|
- Create: `app/src/main/res/values/strings.xml`
|
|||
|
|
- Create: `app/src/main/res/values/colors.xml`
|
|||
|
|
|
|||
|
|
### Step 1: 创建 strings.xml
|
|||
|
|
|
|||
|
|
```xml
|
|||
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|||
|
|
<resources>
|
|||
|
|
<string name="app_name">自动壁纸</string>
|
|||
|
|
<string name="wallpaper_description">自动更换您的桌面壁纸</string>
|
|||
|
|
<string name="settings">设置</string>
|
|||
|
|
<string name="enable">启用</string>
|
|||
|
|
<string name="disable">禁用</string>
|
|||
|
|
<string name="change_now">立即更换</string>
|
|||
|
|
<string name="select_folders">选择文件夹</string>
|
|||
|
|
<string name="status_enabled">运行中</string>
|
|||
|
|
<string name="status_disabled">已停止</string>
|
|||
|
|
<string name="next_change">下次更换:%d 分钟</string>
|
|||
|
|
<string name="no_folders">未选择文件夹</string>
|
|||
|
|
</resources>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/res/values/strings.xml
|
|||
|
|
git commit -m "feat: add string resources"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 2: 创建 colors.xml
|
|||
|
|
|
|||
|
|
```xml
|
|||
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|||
|
|
<resources>
|
|||
|
|
<color name="purple_200">#FFBB86FC</color>
|
|||
|
|
<color name="purple_500">#FF6200EE</color>
|
|||
|
|
<color name="purple_700">#FF3700B3</color>
|
|||
|
|
<color name="teal_200">#FF03DAC5</color>
|
|||
|
|
<color name="teal_700">#FF018786</color>
|
|||
|
|
<color name="black">#FF000000</color>
|
|||
|
|
<color name="white">#FFFFFFFF</color>
|
|||
|
|
</resources>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/res/values/colors.xml
|
|||
|
|
git commit -m "feat: add color resources"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 3: 创建 activity_main.xml 布局
|
|||
|
|
|
|||
|
|
```xml
|
|||
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|||
|
|
<androidx.constraintlayout.widget.ConstraintLayout
|
|||
|
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
|||
|
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|||
|
|
xmlns:tools="http://schemas.android.com/tools"
|
|||
|
|
android:layout_width="match_parent"
|
|||
|
|
android:layout_height="match_parent"
|
|||
|
|
tools:context=".ui.MainActivity">
|
|||
|
|
|
|||
|
|
<TextView
|
|||
|
|
android:id="@+id/tvTitle"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="@string/app_name"
|
|||
|
|
android:textSize="24sp"
|
|||
|
|
android:textStyle="bold"
|
|||
|
|
android:layout_marginTop="16dp"
|
|||
|
|
app:layout_constraintTop_toTopOf="parent"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent" />
|
|||
|
|
|
|||
|
|
<TextView
|
|||
|
|
android:id="@+id/tvStatus"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="@string/status_disabled"
|
|||
|
|
android:textSize="18sp"
|
|||
|
|
android:layout_marginTop="24dp"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/tvTitle"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent" />
|
|||
|
|
|
|||
|
|
<Button
|
|||
|
|
android:id="@+id/btnToggle"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="@string/enable"
|
|||
|
|
android:layout_marginTop="16dp"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/tvStatus"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent" />
|
|||
|
|
|
|||
|
|
<Button
|
|||
|
|
android:id="@+id/btnChangeNow"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="@string/change_now"
|
|||
|
|
android:layout_marginTop="8dp"
|
|||
|
|
android:enabled="false"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/btnToggle"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent" />
|
|||
|
|
|
|||
|
|
<Button
|
|||
|
|
android:id="@+id/btnSelectFolders"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="@string/select_folders"
|
|||
|
|
android:layout_marginTop="8dp"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/btnChangeNow"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent" />
|
|||
|
|
|
|||
|
|
<Button
|
|||
|
|
android:id="@+id/btnSettings"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="@string/settings"
|
|||
|
|
android:layout_marginTop="8dp"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/btnSelectFolders"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent" />
|
|||
|
|
|
|||
|
|
<TextView
|
|||
|
|
android:id="@+id/tvFolderInfo"
|
|||
|
|
android:layout_width="0dp"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="@string/no_folders"
|
|||
|
|
android:textAlignment="center"
|
|||
|
|
android:layout_marginTop="24dp"
|
|||
|
|
android:layout_marginStart="16dp"
|
|||
|
|
android:layout_marginEnd="16dp"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/btnSettings"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent" />
|
|||
|
|
|
|||
|
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/res/layout/activity_main.xml
|
|||
|
|
git commit -m "feat: add main layout"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 4: 创建 MainActivity 类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.ui;
|
|||
|
|
|
|||
|
|
import android.app.Activity;
|
|||
|
|
import android.content.Intent;
|
|||
|
|
import android.net.Uri;
|
|||
|
|
import android.os.Bundle;
|
|||
|
|
import android.widget.Button;
|
|||
|
|
import android.widget.TextView;
|
|||
|
|
import android.widget.Toast;
|
|||
|
|
|
|||
|
|
import androidx.activity.result.ActivityResultLauncher;
|
|||
|
|
import androidx.activity.result.contract.ActivityResultContracts;
|
|||
|
|
import androidx.annotation.NonNull;
|
|||
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|||
|
|
|
|||
|
|
import java.util.ArrayList;
|
|||
|
|
import java.util.List;
|
|||
|
|
|
|||
|
|
import cn.hugh.bz.gk2.model.AppConfig;
|
|||
|
|
import cn.hugh.bz.gk2.model.PlayMode;
|
|||
|
|
import cn.hugh.bz.gk2.utils.AlarmScheduler;
|
|||
|
|
import cn.hugh.bz.gk2.utils.ImageScanner;
|
|||
|
|
import cn.hugh.bz.gk2.wallpaper.WallpaperPreferences;
|
|||
|
|
|
|||
|
|
public class MainActivity extends AppCompatActivity {
|
|||
|
|
|
|||
|
|
private TextView tvStatus;
|
|||
|
|
private TextView tvFolderInfo;
|
|||
|
|
private Button btnToggle;
|
|||
|
|
private Button btnChangeNow;
|
|||
|
|
private Button btnSelectFolders;
|
|||
|
|
private Button btnSettings;
|
|||
|
|
|
|||
|
|
private WallpaperPreferences prefs;
|
|||
|
|
private AppConfig config;
|
|||
|
|
private ImageScanner scanner;
|
|||
|
|
|
|||
|
|
// Folder picker launcher
|
|||
|
|
private final ActivityResultLauncher<Intent> folderPickerLauncher =
|
|||
|
|
registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
|
|||
|
|
if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
|
|||
|
|
Uri uri = result.getData().getData();
|
|||
|
|
if (uri != null) {
|
|||
|
|
String folderPath = uri.getPath();
|
|||
|
|
addFolder(folderPath);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|||
|
|
super.onCreate(savedInstanceState);
|
|||
|
|
setContentView(R.layout.activity_main);
|
|||
|
|
|
|||
|
|
prefs = new WallpaperPreferences(this);
|
|||
|
|
config = prefs.loadConfig();
|
|||
|
|
scanner = new ImageScanner(this);
|
|||
|
|
|
|||
|
|
initViews();
|
|||
|
|
updateUI();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void initViews() {
|
|||
|
|
tvStatus = findViewById(R.id.tvStatus);
|
|||
|
|
tvFolderInfo = findViewById(R.id.tvFolderInfo);
|
|||
|
|
btnToggle = findViewById(R.id.btnToggle);
|
|||
|
|
btnChangeNow = findViewById(R.id.btnChangeNow);
|
|||
|
|
btnSelectFolders = findViewById(R.id.btnSelectFolders);
|
|||
|
|
btnSettings = findViewById(R.id.btnSettings);
|
|||
|
|
|
|||
|
|
btnToggle.setOnClickListener(v -> toggleService());
|
|||
|
|
btnChangeNow.setOnClickListener(v -> changeWallpaperNow());
|
|||
|
|
btnSelectFolders.setOnClickListener(v -> openFolderPicker());
|
|||
|
|
btnSettings.setOnClickListener(v -> openSettings());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void toggleService() {
|
|||
|
|
if (config.getSelectedFolders().isEmpty()) {
|
|||
|
|
Toast.makeText(this, "请先选择文件夹", Toast.LENGTH_SHORT).show();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
boolean newState = !config.isEnabled();
|
|||
|
|
config.setEnabled(newState);
|
|||
|
|
prefs.saveConfig(config);
|
|||
|
|
|
|||
|
|
if (newState) {
|
|||
|
|
AlarmScheduler scheduler = new AlarmScheduler(this);
|
|||
|
|
scheduler.scheduleWallpaperChange(config.getIntervalMinutes());
|
|||
|
|
Toast.makeText(this, "已启用", Toast.LENGTH_SHORT).show();
|
|||
|
|
} else {
|
|||
|
|
AlarmScheduler scheduler = new AlarmScheduler(this);
|
|||
|
|
scheduler.cancelSchedule();
|
|||
|
|
Toast.makeText(this, "已禁用", Toast.LENGTH_SHORT).show();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
updateUI();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void changeWallpaperNow() {
|
|||
|
|
if (config.getSelectedFolders().isEmpty()) {
|
|||
|
|
Toast.makeText(this, "请先选择文件夹", Toast.LENGTH_SHORT).show();
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Trigger wallpaper refresh
|
|||
|
|
Intent intent = new Intent("cn.hugh.bz.gk2.ACTION_REFRESH_WALLPAPER");
|
|||
|
|
sendBroadcast(intent);
|
|||
|
|
|
|||
|
|
Toast.makeText(this, "壁纸已更换", Toast.LENGTH_SHORT).show();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void openFolderPicker() {
|
|||
|
|
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
|
|||
|
|
folderPickerLauncher.launch(intent);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void openSettings() {
|
|||
|
|
Intent intent = new Intent(this, SettingsActivity.class);
|
|||
|
|
startActivity(intent);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void addFolder(String folderPath) {
|
|||
|
|
if (folderPath == null || config.getSelectedFolders().contains(folderPath)) {
|
|||
|
|
return;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
config.getSelectedFolders().add(folderPath);
|
|||
|
|
prefs.saveConfig(config);
|
|||
|
|
|
|||
|
|
// Scan folder
|
|||
|
|
scanner.scanFolder(folderPath);
|
|||
|
|
|
|||
|
|
updateUI();
|
|||
|
|
Toast.makeText(this, "已添加文件夹", Toast.LENGTH_SHORT).show();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void updateUI() {
|
|||
|
|
// Update status
|
|||
|
|
if (config.isEnabled()) {
|
|||
|
|
tvStatus.setText(R.string.status_enabled);
|
|||
|
|
btnToggle.setText(R.string.disable);
|
|||
|
|
btnChangeNow.setEnabled(true);
|
|||
|
|
} else {
|
|||
|
|
tvStatus.setText(R.string.status_disabled);
|
|||
|
|
btnToggle.setText(R.string.enable);
|
|||
|
|
btnChangeNow.setEnabled(false);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Update folder info
|
|||
|
|
int imageCount = scanner.getTotalImageCount(config.getSelectedFolders());
|
|||
|
|
if (config.getSelectedFolders().isEmpty()) {
|
|||
|
|
tvFolderInfo.setText(R.string.no_folders);
|
|||
|
|
} else {
|
|||
|
|
tvFolderInfo.setText(String.format("已选择 %d 个文件夹,共 %d 张图片",
|
|||
|
|
config.getSelectedFolders().size(), imageCount));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
protected void onResume() {
|
|||
|
|
super.onResume();
|
|||
|
|
config = prefs.loadConfig();
|
|||
|
|
updateUI();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/ui/MainActivity.java
|
|||
|
|
git commit -m "feat: add main activity with UI controls"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 9: 创建 SettingsActivity 设置页面
|
|||
|
|
|
|||
|
|
**目标:** 创建设置页面,支持修改间隔时间和播放模式。
|
|||
|
|
|
|||
|
|
**Files:**
|
|||
|
|
- Create: `app/src/main/java/cn/hugh/bz/gk2/ui/SettingsActivity.java`
|
|||
|
|
- Create: `app/src/main/res/layout/activity_settings.xml`
|
|||
|
|
|
|||
|
|
### Step 1: 创建 activity_settings.xml
|
|||
|
|
|
|||
|
|
```xml
|
|||
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|||
|
|
<androidx.constraintlayout.widget.ConstraintLayout
|
|||
|
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
|||
|
|
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|||
|
|
xmlns:tools="http://schemas.android.com/tools"
|
|||
|
|
android:layout_width="match_parent"
|
|||
|
|
android:layout_height="match_parent"
|
|||
|
|
tools:context=".ui.SettingsActivity">
|
|||
|
|
|
|||
|
|
<TextView
|
|||
|
|
android:id="@+id/tvTitle"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="设置"
|
|||
|
|
android:textSize="24sp"
|
|||
|
|
android:textStyle="bold"
|
|||
|
|
android:layout_margin="16dp"
|
|||
|
|
app:layout_constraintTop_toTopOf="parent"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent" />
|
|||
|
|
|
|||
|
|
<TextView
|
|||
|
|
android:id="@+id/tvPlayModeLabel"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="播放模式"
|
|||
|
|
android:textSize="18sp"
|
|||
|
|
android:layout_marginStart="16dp"
|
|||
|
|
android:layout_marginTop="24dp"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/tvTitle"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent" />
|
|||
|
|
|
|||
|
|
<RadioGroup
|
|||
|
|
android:id="@+id/rgPlayMode"
|
|||
|
|
android:layout_width="0dp"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:layout_marginTop="8dp"
|
|||
|
|
android:layout_marginStart="16dp"
|
|||
|
|
android:layout_marginEnd="16dp"
|
|||
|
|
android:orientation="horizontal"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/tvPlayModeLabel"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent">
|
|||
|
|
|
|||
|
|
<RadioButton
|
|||
|
|
android:id="@+id/rbSequential"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="顺序播放" />
|
|||
|
|
|
|||
|
|
<RadioButton
|
|||
|
|
android:id="@+id/rbRandom"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:layout_marginStart="16dp"
|
|||
|
|
android:text="随机播放" />
|
|||
|
|
</RadioGroup>
|
|||
|
|
|
|||
|
|
<TextView
|
|||
|
|
android:id="@+id/tvIntervalLabel"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="更换间隔(分钟)"
|
|||
|
|
android:textSize="18sp"
|
|||
|
|
android:layout_marginStart="16dp"
|
|||
|
|
android:layout_marginTop="24dp"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/rgPlayMode"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent" />
|
|||
|
|
|
|||
|
|
<SeekBar
|
|||
|
|
android:id="@+id/seekBarInterval"
|
|||
|
|
android:layout_width="0dp"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:max="60"
|
|||
|
|
android:min="1"
|
|||
|
|
android:progress="30"
|
|||
|
|
android:layout_marginTop="8dp"
|
|||
|
|
android:layout_marginStart="16dp"
|
|||
|
|
android:layout_marginEnd="16dp"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/tvIntervalLabel"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent" />
|
|||
|
|
|
|||
|
|
<TextView
|
|||
|
|
android:id="@+id/tvIntervalValue"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="30 分钟"
|
|||
|
|
android:textSize="16sp"
|
|||
|
|
android:layout_marginTop="8dp"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/seekBarInterval"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent" />
|
|||
|
|
|
|||
|
|
<Button
|
|||
|
|
android:id="@+id/btnSave"
|
|||
|
|
android:layout_width="wrap_content"
|
|||
|
|
android:layout_height="wrap_content"
|
|||
|
|
android:text="保存"
|
|||
|
|
android:layout_marginTop="24dp"
|
|||
|
|
app:layout_constraintTop_toBottomOf="@id/tvIntervalValue"
|
|||
|
|
app:layout_constraintStart_toStartOf="parent"
|
|||
|
|
app:layout_constraintEnd_toEndOf="parent" />
|
|||
|
|
|
|||
|
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/res/layout/activity_settings.xml
|
|||
|
|
git commit -m "feat: add settings layout"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 2: 创建 SettingsActivity 类
|
|||
|
|
|
|||
|
|
```java
|
|||
|
|
package cn.hugh.bz.gk2.ui;
|
|||
|
|
|
|||
|
|
import android.os.Bundle;
|
|||
|
|
import android.widget.Button;
|
|||
|
|
import android.widget.RadioButton;
|
|||
|
|
import android.widget.RadioGroup;
|
|||
|
|
import android.widget.SeekBar;
|
|||
|
|
import android.widget.TextView;
|
|||
|
|
import android.widget.Toast;
|
|||
|
|
|
|||
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|||
|
|
|
|||
|
|
import cn.hugh.bz.gk2.model.AppConfig;
|
|||
|
|
import cn.hugh.bz.gk2.model.PlayMode;
|
|||
|
|
import cn.hugh.bz.gk2.utils.AlarmScheduler;
|
|||
|
|
import cn.hugh.bz.gk2.wallpaper.WallpaperPreferences;
|
|||
|
|
|
|||
|
|
public class SettingsActivity extends AppCompatActivity {
|
|||
|
|
|
|||
|
|
private RadioButton rbSequential;
|
|||
|
|
private RadioButton rbRandom;
|
|||
|
|
private SeekBar seekBarInterval;
|
|||
|
|
private TextView tvIntervalValue;
|
|||
|
|
private Button btnSave;
|
|||
|
|
|
|||
|
|
private WallpaperPreferences prefs;
|
|||
|
|
private AppConfig config;
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|||
|
|
super.onCreate(savedInstanceState);
|
|||
|
|
setContentView(R.layout.activity_settings);
|
|||
|
|
|
|||
|
|
prefs = new WallpaperPreferences(this);
|
|||
|
|
config = prefs.loadConfig();
|
|||
|
|
|
|||
|
|
initViews();
|
|||
|
|
loadConfig();
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void initViews() {
|
|||
|
|
rbSequential = findViewById(R.id.rbSequential);
|
|||
|
|
rbRandom = findViewById(R.id.rbRandom);
|
|||
|
|
seekBarInterval = findViewById(R.id.seekBarInterval);
|
|||
|
|
tvIntervalValue = findViewById(R.id.tvIntervalValue);
|
|||
|
|
btnSave = findViewById(R.id.btnSave);
|
|||
|
|
|
|||
|
|
seekBarInterval.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
|
|||
|
|
@Override
|
|||
|
|
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
|
|||
|
|
tvIntervalValue.setText(progress + " 分钟");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void onStartTrackingTouch(SeekBar seekBar) {}
|
|||
|
|
|
|||
|
|
@Override
|
|||
|
|
public void onStopTrackingTouch(SeekBar seekBar) {}
|
|||
|
|
});
|
|||
|
|
|
|||
|
|
btnSave.setOnClickListener(v -> saveConfig());
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void loadConfig() {
|
|||
|
|
rbSequential.setChecked(config.getPlayMode() == PlayMode.SEQUENTIAL);
|
|||
|
|
rbRandom.setChecked(config.getPlayMode() == PlayMode.RANDOM);
|
|||
|
|
seekBarInterval.setProgress(config.getIntervalMinutes());
|
|||
|
|
tvIntervalValue.setText(config.getIntervalMinutes() + " 分钟");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
private void saveConfig() {
|
|||
|
|
// Update play mode
|
|||
|
|
PlayMode playMode = rbSequential.isChecked() ? PlayMode.SEQUENTIAL : PlayMode.RANDOM;
|
|||
|
|
config.setPlayMode(playMode);
|
|||
|
|
|
|||
|
|
// Update interval
|
|||
|
|
int interval = seekBarInterval.getProgress();
|
|||
|
|
config.setIntervalMinutes(interval);
|
|||
|
|
|
|||
|
|
// Save
|
|||
|
|
prefs.saveConfig(config);
|
|||
|
|
|
|||
|
|
// Reschedule alarm if enabled
|
|||
|
|
if (config.isEnabled()) {
|
|||
|
|
AlarmScheduler scheduler = new AlarmScheduler(this);
|
|||
|
|
scheduler.scheduleWallpaperChange(interval);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
Toast.makeText(this, "设置已保存", Toast.LENGTH_SHORT).show();
|
|||
|
|
finish();
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/java/cn/hugh/bz/gk2/ui/SettingsActivity.java
|
|||
|
|
git commit -m "feat: add settings activity"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 10: 添加壁纸配置 XML
|
|||
|
|
|
|||
|
|
**目标:** 创建壁纸配置元数据文件。
|
|||
|
|
|
|||
|
|
**Files:**
|
|||
|
|
- Create: `app/src/main/res/xml/wallpaper_info.xml`
|
|||
|
|
|
|||
|
|
### Step 1: 创建 wallpaper_info.xml
|
|||
|
|
|
|||
|
|
```xml
|
|||
|
|
<?xml version="1.0" encoding="utf-8"?>
|
|||
|
|
<wallpaper
|
|||
|
|
xmlns:android="http://schemas.android.com/apk/res/android"
|
|||
|
|
android:description="@string/wallpaper_description"
|
|||
|
|
android:thumbnail="@mipmap/ic_launcher" />
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add app/src/main/res/xml/wallpaper_info.xml
|
|||
|
|
git commit -m "feat: add wallpaper metadata"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 11: 添加应用图标
|
|||
|
|
|
|||
|
|
**目标:** 添加应用图标资源。
|
|||
|
|
|
|||
|
|
**说明:** 由于无法创建图片文件,请在 Android Studio 中:
|
|||
|
|
1. 右键 `res/mipmap` 目录
|
|||
|
|
2. New → Image Asset
|
|||
|
|
3. 选择或创建应用图标
|
|||
|
|
|
|||
|
|
**临时方案:** 可以使用 Android 默认图标,或者添加占位符说明。
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Task 12: 测试和验证
|
|||
|
|
|
|||
|
|
**目标:** 编译并测试应用功能。
|
|||
|
|
|
|||
|
|
### Step 1: 编译项目
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
./gradlew assembleDebug
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
**Expected:** 编译成功,生成 `app/build/outputs/apk/debug/app-debug.apk`
|
|||
|
|
|
|||
|
|
**Commit:**
|
|||
|
|
```bash
|
|||
|
|
git add .
|
|||
|
|
git commit -m "chore: complete MVP implementation"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 2: 安装到设备/模拟器
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
adb install app/build/outputs/apk/debug/app-debug.apk
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### Step 3: 验证功能清单
|
|||
|
|
|
|||
|
|
- [ ] 应用可以正常启动
|
|||
|
|
- [ ] 可以选择文件夹
|
|||
|
|
- [ ] 扫描图片并显示数量
|
|||
|
|
- [ ] 启用壁纸服务
|
|||
|
|
- [ ] 壁纸自动更换
|
|||
|
|
- [ ] 可以禁用服务
|
|||
|
|
- [ ] 设置页面可以修改间隔
|
|||
|
|
- [ ] 设置页面可以切换模式
|
|||
|
|
- [ ] 在鸿蒙 4.2 设备上测试
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 测试策略
|
|||
|
|
|
|||
|
|
### 单元测试(未来)
|
|||
|
|
|
|||
|
|
为关键工具类添加单元测试:
|
|||
|
|
- `JsonHelperTest.java` - 测试 JSON 读写
|
|||
|
|
- `ImageScannerTest.java` - 测试图片扫描逻辑
|
|||
|
|
- `AlarmSchedulerTest.java` - 测试闹钟调度
|
|||
|
|
|
|||
|
|
### 集成测试
|
|||
|
|
|
|||
|
|
1. **文件夹选择测试**
|
|||
|
|
- 选择包含图片的文件夹
|
|||
|
|
- 验证扫描结果
|
|||
|
|
- 验证 JSON 缓存生成
|
|||
|
|
|
|||
|
|
2. **壁纸服务测试**
|
|||
|
|
- 启用服务
|
|||
|
|
- 验证壁纸显示
|
|||
|
|
- 等待定时触发
|
|||
|
|
- 验证壁纸自动更换
|
|||
|
|
|
|||
|
|
3. **配置持久化测试**
|
|||
|
|
- 修改设置
|
|||
|
|
- 重启应用
|
|||
|
|
- 验证配置保留
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 常见问题排查
|
|||
|
|
|
|||
|
|
### 问题 1: 壁纸不自动更换
|
|||
|
|
|
|||
|
|
**排查步骤:**
|
|||
|
|
1. 检查是否启用了服务
|
|||
|
|
2. 检查是否授予了 SCHEDULE_EXACT_ALARM 权限
|
|||
|
|
3. 检查 Logcat 日志
|
|||
|
|
4. 验证 AlarmManager 是否成功调度
|
|||
|
|
|
|||
|
|
### 问题 2: 扫描不到图片
|
|||
|
|
|
|||
|
|
**排查步骤:**
|
|||
|
|
1. 检查存储权限
|
|||
|
|
2. 验证文件夹路径正确
|
|||
|
|
3. 检查文件扩展名是否支持
|
|||
|
|
4. 查看 Logcat 扫描日志
|
|||
|
|
|
|||
|
|
### 问题 3: 鸿蒙 4.2 兼容性问题
|
|||
|
|
|
|||
|
|
**已知问题和解决方案:**
|
|||
|
|
1. **应用启动管理** - 引导用户允许后台活动
|
|||
|
|
2. **电池优化** - 引导用户禁用电池优化
|
|||
|
|
3. **权限被撤销** - 检查并重新请求权限
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## Git 提交规范
|
|||
|
|
|
|||
|
|
```bash
|
|||
|
|
# 功能开发
|
|||
|
|
git commit -m "feat: add wallpaper service core"
|
|||
|
|
|
|||
|
|
# Bug 修复
|
|||
|
|
git commit -m "fix: resolve image loading crash"
|
|||
|
|
|
|||
|
|
# 配置变更
|
|||
|
|
git commit -m "chore: update dependencies"
|
|||
|
|
|
|||
|
|
# 文档
|
|||
|
|
git commit -m "docs: add usage instructions"
|
|||
|
|
|
|||
|
|
# 重构
|
|||
|
|
git commit -m "refactor: simplify image scanner logic"
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 参考资料
|
|||
|
|
|
|||
|
|
### 技术文档
|
|||
|
|
- [WallpaperService API](https://developer.android.com/reference/android/service/wallpaper/WallpaperService)
|
|||
|
|
- [AlarmManager API](https://developer.android.com/reference/android/app/AlarmManager)
|
|||
|
|
- [鸿蒙动态壁纸开发](https://developer.huawei.com/consumer/cn/doc/livewallpaper-0000001073967005)
|
|||
|
|
|
|||
|
|
### 示例项目
|
|||
|
|
- [Muzei Live Wallpaper](https://github.com/muzei/muzei)
|
|||
|
|
|
|||
|
|
### 相关技能
|
|||
|
|
- @superpowers:systematic-debugging - 遇到 Bug 时使用
|
|||
|
|
- @superpowers:test-driven-development - 添加测试时使用
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
## 后续优化方向
|
|||
|
|
|
|||
|
|
1. **性能优化**
|
|||
|
|
- 图片预加载和缓存
|
|||
|
|
- 内存优化
|
|||
|
|
- 增量扫描优化
|
|||
|
|
|
|||
|
|
2. **用户体验**
|
|||
|
|
- 添加壁纸预览
|
|||
|
|
- 播放历史记录
|
|||
|
|
- 统计信息展示
|
|||
|
|
|
|||
|
|
3. **功能增强**
|
|||
|
|
- 支持多个文件夹权重
|
|||
|
|
- 智能推荐算法
|
|||
|
|
- 主题和深色模式
|
|||
|
|
|
|||
|
|
4. **鸿蒙适配**
|
|||
|
|
- 添加应用启动管理引导
|
|||
|
|
- 电池优化引导
|
|||
|
|
- 权限检查和请求
|
|||
|
|
|
|||
|
|
---
|
|||
|
|
|
|||
|
|
**计划完成时间:** 预计 4-6 小时完成 MVP 功能
|
|||
|
|
|
|||
|
|
**下一步:** 讨论是否需要调整任务顺序或添加其他功能
|