From a74307e67908fcf053e485c688e73662a45ee99a Mon Sep 17 00:00:00 2001 From: lihongwei Date: Mon, 12 May 2025 15:57:34 +0800 Subject: [PATCH] =?UTF-8?q?V1.0.0=EF=BC=884=EF=BC=89=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E9=80=BB=E8=BE=91=E4=BC=98=E5=8C=96bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/build.gradle.kts | 2 +- .../room/dao/DynamicDataDao.java | 2 +- .../ui/activity/DynamicActivity.java | 100 ++++++++++-------- .../dynamicwallpaper/util/MediaFetcher.java | 27 +++-- app/src/main/res/layout/activity_dynamic.xml | 22 ++-- 5 files changed, 93 insertions(+), 60 deletions(-) diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 2ceb6cc..1eddcb0 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -14,7 +14,7 @@ android { applicationId = "com.live.dynamicwallpaper" minSdk = 23 targetSdk = 34 - versionCode = 3 + versionCode = 4 versionName = "1.0.0" setProperty( "archivesBaseName", diff --git a/app/src/main/java/com/live/dynamicwallpaper/room/dao/DynamicDataDao.java b/app/src/main/java/com/live/dynamicwallpaper/room/dao/DynamicDataDao.java index 50f08a4..ea9bab4 100644 --- a/app/src/main/java/com/live/dynamicwallpaper/room/dao/DynamicDataDao.java +++ b/app/src/main/java/com/live/dynamicwallpaper/room/dao/DynamicDataDao.java @@ -32,4 +32,4 @@ public interface DynamicDataDao { @Query("SELECT * FROM DynamicData WHERE wallpaperType = :type AND flowId = :flowId") LiveData getFavoriteStatus(int type, int flowId); -} +} \ No newline at end of file diff --git a/app/src/main/java/com/live/dynamicwallpaper/ui/activity/DynamicActivity.java b/app/src/main/java/com/live/dynamicwallpaper/ui/activity/DynamicActivity.java index e3d724a..e111c7e 100644 --- a/app/src/main/java/com/live/dynamicwallpaper/ui/activity/DynamicActivity.java +++ b/app/src/main/java/com/live/dynamicwallpaper/ui/activity/DynamicActivity.java @@ -18,11 +18,11 @@ import androidx.lifecycle.ViewModelProvider; import com.live.dynamicwallpaper.R; import com.live.dynamicwallpaper.callback.OnDownloadCallback; -import com.live.dynamicwallpaper.room.entity.DynamicData; import com.live.dynamicwallpaper.databinding.ActivityDynamicBinding; +import com.live.dynamicwallpaper.room.entity.DynamicData; import com.live.dynamicwallpaper.service.LiveWallpaperService; -import com.live.dynamicwallpaper.viewmodel.DynamicViewModel; import com.live.dynamicwallpaper.util.MediaFetcher; +import com.live.dynamicwallpaper.viewmodel.DynamicViewModel; import java.io.File; @@ -31,6 +31,7 @@ public class DynamicActivity extends AppCompatActivity { private DynamicData contentData; private DynamicViewModel viewModel; private MediaFetcher mediaFetcher; + private boolean isDownloading = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -41,7 +42,7 @@ public class DynamicActivity extends AppCompatActivity { ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (view, insets) -> { Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); - view.setPadding(systemBars.left, 0, systemBars.right, systemBars.bottom); + view.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); return insets; }); @@ -65,45 +66,50 @@ public class DynamicActivity extends AppCompatActivity { viewModel = new ViewModelProvider(this).get(DynamicViewModel.class); String format = contentData.getWallpaperType() == 2 ? "ViewShiftLive" : "ViewLive"; - downloadVideo(resourceId, imagePath, format); - - observeFavoriteState(); - } - - private void downloadVideo(int resourceId, String imagePath, String format) { - if (contentData.getWallpaperPath().isEmpty()) { - mediaFetcher.fetchVideoFile(this, resourceId, imagePath, format, new OnDownloadCallback() { - @Override - public void onSuccess(File localFile) { - if (ui == null) return; - contentData.setWallpaperPath(localFile.getAbsolutePath()); - viewModel.update(contentData); - prepareVideoPlayback(); - hideLoading(); - hideRetry(); - } - - @Override - public void onFailure(Exception e) { - if (ui == null) return; - hideLoading(); - showRetry(); - Log.e("LivePreview", "Media fetch failed", e); - } - - @Override - public void onProgress(int percent) { - if (isActive() && ui != null && percent >= 0 && percent <= 100) { - ui.downloadProgressBar.setProgress(percent); - ui.progressText.setText(percent + "%"); - } - } - }); + File localFile = new File(contentData.getWallpaperPath()); + if (contentData.getWallpaperPath().isEmpty() || !localFile.exists()) { + downloadVideo(resourceId, imagePath, format); } else { prepareVideoPlayback(); hideLoading(); hideRetry(); } + + observeFavoriteState(); + } + + private void downloadVideo(int resourceId, String imagePath, String format) { + isDownloading = true; + + mediaFetcher.fetchVideoFile(this, resourceId, imagePath, format, new OnDownloadCallback() { + @Override + public void onSuccess(File localFile) { + if (ui == null) return; + isDownloading = false; + contentData.setWallpaperPath(localFile.getAbsolutePath()); + viewModel.update(contentData); + prepareVideoPlayback(); + hideLoading(); + hideRetry(); + } + + @Override + public void onFailure(Exception e) { + if (ui == null) return; + isDownloading = false; + hideLoading(); + showRetry(); + Log.e("LivePreview", "Media fetch failed", e); + } + + @Override + public void onProgress(int percent) { + if (isActive() && ui != null && percent >= 0 && percent <= 100) { + ui.downloadProgressBar.setProgress(percent); + ui.progressText.setText(percent + "%"); + } + } + }); } private void showRetry() { @@ -136,16 +142,19 @@ public class DynamicActivity extends AppCompatActivity { } private void prepareVideoPlayback() { - if (ui == null || contentData.getWallpaperPath() == null) { - showErrorPrompt(); - return; - } File video = new File(contentData.getWallpaperPath()); if (video.exists()) { try { ui.videoView.setVideoPath(contentData.getWallpaperPath()); ui.videoView.start(); ui.videoView.setOnPreparedListener(media -> media.setLooping(true)); + ui.videoView.setOnErrorListener((mp, what, extra) -> { + Log.e("LivePreview", "VideoView error: " + what + ", " + extra); + showErrorPrompt(); + showRetry(); + return true; + }); + hideRetry(); } catch (Exception e) { Log.e("LivePreview", "Video playback failed", e); showErrorPrompt(); @@ -160,6 +169,7 @@ public class DynamicActivity extends AppCompatActivity { String videoPath = contentData.getWallpaperPath(); if (videoPath == null || !new File(videoPath).exists()) { showErrorPrompt(); + showRetry(); return; } SharedPreferences preferences = getSharedPreferences("WallpaperPrefs", MODE_PRIVATE); @@ -203,6 +213,7 @@ public class DynamicActivity extends AppCompatActivity { private void showLoading() { if (ui != null) { + hideRetry(); ui.downloadProgressBar.setVisibility(View.VISIBLE); ui.progressTip.setVisibility(View.VISIBLE); ui.progressText.setVisibility(View.VISIBLE); @@ -228,7 +239,12 @@ public class DynamicActivity extends AppCompatActivity { @Override protected void onResume() { super.onResume(); - if (contentData != null && !contentData.getWallpaperPath().isEmpty()) { + File localFile = new File(contentData.getWallpaperPath()); + if (contentData != null && !contentData.getWallpaperPath().isEmpty() && !localFile.exists()) { + if (!isDownloading) { + showRetry(); + } + } else if (contentData != null && !contentData.getWallpaperPath().isEmpty()) { prepareVideoPlayback(); } } diff --git a/app/src/main/java/com/live/dynamicwallpaper/util/MediaFetcher.java b/app/src/main/java/com/live/dynamicwallpaper/util/MediaFetcher.java index e313f01..4f813e6 100644 --- a/app/src/main/java/com/live/dynamicwallpaper/util/MediaFetcher.java +++ b/app/src/main/java/com/live/dynamicwallpaper/util/MediaFetcher.java @@ -25,7 +25,6 @@ import okhttp3.RequestBody; import okhttp3.Response; public class MediaFetcher { - private static final String ENDPOINT = "https://neutrolabgames.com/LiveLoop/AppData/jmywall.php"; private static final OkHttpClient httpClient = new OkHttpClient.Builder() @@ -36,9 +35,11 @@ public class MediaFetcher { private static final Handler uiHandler = new Handler(Looper.getMainLooper()); private Call activeCall; + private File currentOutputFile; public MediaFetcher() { activeCall = null; + currentOutputFile = null; } public void fetchVideoFile(Context context, int id, String assetName, String mode, OnDownloadCallback callback) { @@ -61,6 +62,10 @@ public class MediaFetcher { activeCall.enqueue(new Callback() { @Override public void onFailure(@NonNull Call call, @NonNull IOException exception) { + if (currentOutputFile != null && currentOutputFile.exists()) { + currentOutputFile.delete(); + currentOutputFile = null; + } runOnUi(() -> { if (callback != null) callback.onFailure(exception); }); @@ -88,10 +93,10 @@ public class MediaFetcher { } String uniqueName = "video_" + System.currentTimeMillis() + "_" + id + ".mp4"; - File outputFile = new File(storageDir, uniqueName); + currentOutputFile = new File(storageDir, uniqueName); try (InputStream in = response.body().byteStream(); - FileOutputStream out = new FileOutputStream(outputFile)) { + FileOutputStream out = new FileOutputStream(currentOutputFile)) { byte[] buffer = new byte[8192]; long writtenBytes = 0; @@ -99,7 +104,8 @@ public class MediaFetcher { while ((read = in.read(buffer)) != -1) { if (call.isCanceled()) { - outputFile.delete(); + currentOutputFile.delete(); + currentOutputFile = null; return; } out.write(buffer, 0, read); @@ -113,10 +119,15 @@ public class MediaFetcher { } runOnUi(() -> { - if (callback != null) callback.onSuccess(outputFile); + if (callback != null) callback.onSuccess(currentOutputFile); + currentOutputFile = null; }); } catch (IOException ex) { + if (currentOutputFile != null && currentOutputFile.exists()) { + currentOutputFile.delete(); + currentOutputFile = null; + } runOnUi(() -> { if (callback != null) callback.onFailure(ex); }); @@ -148,5 +159,9 @@ public class MediaFetcher { } activeCall = null; uiHandler.removeCallbacksAndMessages(null); + if (currentOutputFile != null && currentOutputFile.exists()) { + currentOutputFile.delete(); + currentOutputFile = null; + } } -} +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_dynamic.xml b/app/src/main/res/layout/activity_dynamic.xml index d45deac..59efad2 100644 --- a/app/src/main/res/layout/activity_dynamic.xml +++ b/app/src/main/res/layout/activity_dynamic.xml @@ -22,8 +22,8 @@ android:layout_height="wrap_content" android:layout_marginStart="32dp" android:layout_marginTop="32dp" - android:src="@drawable/back" android:background="@drawable/rounded_rectangle_gradient" + android:src="@drawable/back" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> @@ -127,31 +127,33 @@ android:id="@+id/error_container" android:layout_width="match_parent" android:layout_height="wrap_content" - android:orientation="vertical" + android:layout_centerInParent="true" android:gravity="center" + android:orientation="vertical" android:visibility="gone" - app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" - app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent" - android:layout_centerInParent="true"> + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent"> + + android:textSize="16sp" /> +