feat: 添加主界面 Activity
实现了主界面的所有功能 - 服务启用/禁用切换 - 立即更换壁纸 - 打开文件夹选择器 - 打开设置页面 - 动态更新 UI 状态 - 显示文件夹和图片统计信息 - 使用 ActivityResultLauncher 处理文件夹选择结果 Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
535ce9a22d
commit
e331b0251c
1 file changed
+167
@@ -0,0 +1,167 @@
|
||||
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.R;
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user