V1.0.0(5)增加文件缺失判断

This commit is contained in:
lihongwei 2025-05-13 10:46:21 +08:00
parent a74307e679
commit beadad3104
4 changed files with 48 additions and 4 deletions

View File

@ -14,7 +14,7 @@ android {
applicationId = "com.live.dynamicwallpaper"
minSdk = 23
targetSdk = 34
versionCode = 4
versionCode = 5
versionName = "1.0.0"
setProperty(
"archivesBaseName",

View File

@ -4,6 +4,7 @@ import android.app.WallpaperManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
@ -25,6 +26,7 @@ import com.live.dynamicwallpaper.util.MediaFetcher;
import com.live.dynamicwallpaper.viewmodel.DynamicViewModel;
import java.io.File;
import java.io.IOException;
public class DynamicActivity extends AppCompatActivity {
private ActivityDynamicBinding ui;
@ -32,6 +34,7 @@ public class DynamicActivity extends AppCompatActivity {
private DynamicViewModel viewModel;
private MediaFetcher mediaFetcher;
private boolean isDownloading = false;
private Toast toast;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -97,6 +100,7 @@ public class DynamicActivity extends AppCompatActivity {
public void onFailure(Exception e) {
if (ui == null) return;
isDownloading = false;
contentData.setWallpaperPath("");
hideLoading();
showRetry();
Log.e("LivePreview", "Media fetch failed", e);
@ -115,6 +119,7 @@ public class DynamicActivity extends AppCompatActivity {
private void showRetry() {
if (ui == null) return;
ui.errorContainer.setVisibility(View.VISIBLE);
ui.view.setVisibility(View.VISIBLE);
ui.retryButton.setOnClickListener(v -> {
ui.errorContainer.setVisibility(View.GONE);
showLoading();
@ -126,6 +131,7 @@ public class DynamicActivity extends AppCompatActivity {
private void hideRetry() {
if (ui != null) {
ui.errorContainer.setVisibility(View.GONE);
ui.view.setVisibility(View.GONE);
}
}
@ -142,6 +148,18 @@ public class DynamicActivity extends AppCompatActivity {
}
private void prepareVideoPlayback() {
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(contentData.getWallpaperPath());
mediaPlayer.prepare();
mediaPlayer.release();
} catch (IOException e) {
Log.e("LivePreview", "Invalid video file", e);
showErrorPrompt();
showRetry();
return;
}
File video = new File(contentData.getWallpaperPath());
if (video.exists()) {
try {
@ -150,6 +168,7 @@ public class DynamicActivity extends AppCompatActivity {
ui.videoView.setOnPreparedListener(media -> media.setLooping(true));
ui.videoView.setOnErrorListener((mp, what, extra) -> {
Log.e("LivePreview", "VideoView error: " + what + ", " + extra);
mp.reset();
showErrorPrompt();
showRetry();
return true;
@ -172,6 +191,15 @@ public class DynamicActivity extends AppCompatActivity {
showRetry();
return;
}
File localFile = new File(contentData.getWallpaperPath());
if (contentData == null || contentData.getWallpaperPath().isEmpty() ||
!localFile.exists() || !localFile.canRead() || localFile.length() < 1024) {
showErrorPrompt();
showRetry();
return;
}
SharedPreferences preferences = getSharedPreferences("WallpaperPrefs", MODE_PRIVATE);
preferences.edit().putString("video_path", videoPath).apply();
@ -196,7 +224,7 @@ public class DynamicActivity extends AppCompatActivity {
private void showErrorPrompt() {
if (isActive() && ui != null) {
Toast.makeText(this, "Cannot play video. Please try again later.", Toast.LENGTH_LONG).show();
showToast("Cannot play video. Please try again later.");
}
}
@ -219,6 +247,8 @@ public class DynamicActivity extends AppCompatActivity {
ui.progressText.setVisibility(View.VISIBLE);
ui.loadingSpinner.setVisibility(View.VISIBLE);
ui.view.setVisibility(View.VISIBLE);
ui.downloadProgressBar.setProgress(0);
ui.progressText.setText("0%");
}
}
@ -243,12 +273,21 @@ public class DynamicActivity extends AppCompatActivity {
if (contentData != null && !contentData.getWallpaperPath().isEmpty() && !localFile.exists()) {
if (!isDownloading) {
showRetry();
Log.d("Bynamic" , "is");
}
} else if (contentData != null && !contentData.getWallpaperPath().isEmpty()) {
prepareVideoPlayback();
}
}
private void showToast(String message) {
if (toast != null) {
toast.cancel();
}
toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.show();
}
@Override
protected void onDestroy() {
super.onDestroy();

View File

@ -104,10 +104,13 @@ public class MediaFetcher {
while ((read = in.read(buffer)) != -1) {
if (call.isCanceled()) {
currentOutputFile.delete();
if (currentOutputFile != null && currentOutputFile.exists()) {
currentOutputFile.delete();
}
currentOutputFile = null;
return;
}
out.write(buffer, 0, read);
writtenBytes += read;

View File

@ -63,8 +63,10 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/gray"
android:visibility="gone"
android:clickable="true"
android:focusable="true"
android:visibility="gone" />
android:focusableInTouchMode="true" />
<TextView
android:id="@+id/progress_tip"