V1.0.0(4)修改逻辑优化bug

This commit is contained in:
lihongwei 2025-05-12 15:57:34 +08:00
parent f64da0aaec
commit a74307e679
5 changed files with 93 additions and 60 deletions

View File

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

View File

@ -18,11 +18,11 @@ import androidx.lifecycle.ViewModelProvider;
import com.live.dynamicwallpaper.R; import com.live.dynamicwallpaper.R;
import com.live.dynamicwallpaper.callback.OnDownloadCallback; import com.live.dynamicwallpaper.callback.OnDownloadCallback;
import com.live.dynamicwallpaper.room.entity.DynamicData;
import com.live.dynamicwallpaper.databinding.ActivityDynamicBinding; import com.live.dynamicwallpaper.databinding.ActivityDynamicBinding;
import com.live.dynamicwallpaper.room.entity.DynamicData;
import com.live.dynamicwallpaper.service.LiveWallpaperService; import com.live.dynamicwallpaper.service.LiveWallpaperService;
import com.live.dynamicwallpaper.viewmodel.DynamicViewModel;
import com.live.dynamicwallpaper.util.MediaFetcher; import com.live.dynamicwallpaper.util.MediaFetcher;
import com.live.dynamicwallpaper.viewmodel.DynamicViewModel;
import java.io.File; import java.io.File;
@ -31,6 +31,7 @@ public class DynamicActivity extends AppCompatActivity {
private DynamicData contentData; private DynamicData contentData;
private DynamicViewModel viewModel; private DynamicViewModel viewModel;
private MediaFetcher mediaFetcher; private MediaFetcher mediaFetcher;
private boolean isDownloading = false;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
@ -41,7 +42,7 @@ public class DynamicActivity extends AppCompatActivity {
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (view, insets) -> { ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (view, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); 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; return insets;
}); });
@ -65,17 +66,26 @@ public class DynamicActivity extends AppCompatActivity {
viewModel = new ViewModelProvider(this).get(DynamicViewModel.class); viewModel = new ViewModelProvider(this).get(DynamicViewModel.class);
String format = contentData.getWallpaperType() == 2 ? "ViewShiftLive" : "ViewLive"; String format = contentData.getWallpaperType() == 2 ? "ViewShiftLive" : "ViewLive";
File localFile = new File(contentData.getWallpaperPath());
if (contentData.getWallpaperPath().isEmpty() || !localFile.exists()) {
downloadVideo(resourceId, imagePath, format); downloadVideo(resourceId, imagePath, format);
} else {
prepareVideoPlayback();
hideLoading();
hideRetry();
}
observeFavoriteState(); observeFavoriteState();
} }
private void downloadVideo(int resourceId, String imagePath, String format) { private void downloadVideo(int resourceId, String imagePath, String format) {
if (contentData.getWallpaperPath().isEmpty()) { isDownloading = true;
mediaFetcher.fetchVideoFile(this, resourceId, imagePath, format, new OnDownloadCallback() { mediaFetcher.fetchVideoFile(this, resourceId, imagePath, format, new OnDownloadCallback() {
@Override @Override
public void onSuccess(File localFile) { public void onSuccess(File localFile) {
if (ui == null) return; if (ui == null) return;
isDownloading = false;
contentData.setWallpaperPath(localFile.getAbsolutePath()); contentData.setWallpaperPath(localFile.getAbsolutePath());
viewModel.update(contentData); viewModel.update(contentData);
prepareVideoPlayback(); prepareVideoPlayback();
@ -86,6 +96,7 @@ public class DynamicActivity extends AppCompatActivity {
@Override @Override
public void onFailure(Exception e) { public void onFailure(Exception e) {
if (ui == null) return; if (ui == null) return;
isDownloading = false;
hideLoading(); hideLoading();
showRetry(); showRetry();
Log.e("LivePreview", "Media fetch failed", e); Log.e("LivePreview", "Media fetch failed", e);
@ -99,11 +110,6 @@ public class DynamicActivity extends AppCompatActivity {
} }
} }
}); });
} else {
prepareVideoPlayback();
hideLoading();
hideRetry();
}
} }
private void showRetry() { private void showRetry() {
@ -136,16 +142,19 @@ public class DynamicActivity extends AppCompatActivity {
} }
private void prepareVideoPlayback() { private void prepareVideoPlayback() {
if (ui == null || contentData.getWallpaperPath() == null) {
showErrorPrompt();
return;
}
File video = new File(contentData.getWallpaperPath()); File video = new File(contentData.getWallpaperPath());
if (video.exists()) { if (video.exists()) {
try { try {
ui.videoView.setVideoPath(contentData.getWallpaperPath()); ui.videoView.setVideoPath(contentData.getWallpaperPath());
ui.videoView.start(); ui.videoView.start();
ui.videoView.setOnPreparedListener(media -> media.setLooping(true)); 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) { } catch (Exception e) {
Log.e("LivePreview", "Video playback failed", e); Log.e("LivePreview", "Video playback failed", e);
showErrorPrompt(); showErrorPrompt();
@ -160,6 +169,7 @@ public class DynamicActivity extends AppCompatActivity {
String videoPath = contentData.getWallpaperPath(); String videoPath = contentData.getWallpaperPath();
if (videoPath == null || !new File(videoPath).exists()) { if (videoPath == null || !new File(videoPath).exists()) {
showErrorPrompt(); showErrorPrompt();
showRetry();
return; return;
} }
SharedPreferences preferences = getSharedPreferences("WallpaperPrefs", MODE_PRIVATE); SharedPreferences preferences = getSharedPreferences("WallpaperPrefs", MODE_PRIVATE);
@ -203,6 +213,7 @@ public class DynamicActivity extends AppCompatActivity {
private void showLoading() { private void showLoading() {
if (ui != null) { if (ui != null) {
hideRetry();
ui.downloadProgressBar.setVisibility(View.VISIBLE); ui.downloadProgressBar.setVisibility(View.VISIBLE);
ui.progressTip.setVisibility(View.VISIBLE); ui.progressTip.setVisibility(View.VISIBLE);
ui.progressText.setVisibility(View.VISIBLE); ui.progressText.setVisibility(View.VISIBLE);
@ -228,7 +239,12 @@ public class DynamicActivity extends AppCompatActivity {
@Override @Override
protected void onResume() { protected void onResume() {
super.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(); prepareVideoPlayback();
} }
} }

View File

@ -25,7 +25,6 @@ import okhttp3.RequestBody;
import okhttp3.Response; import okhttp3.Response;
public class MediaFetcher { public class MediaFetcher {
private static final String ENDPOINT = "https://neutrolabgames.com/LiveLoop/AppData/jmywall.php"; private static final String ENDPOINT = "https://neutrolabgames.com/LiveLoop/AppData/jmywall.php";
private static final OkHttpClient httpClient = new OkHttpClient.Builder() 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 static final Handler uiHandler = new Handler(Looper.getMainLooper());
private Call activeCall; private Call activeCall;
private File currentOutputFile;
public MediaFetcher() { public MediaFetcher() {
activeCall = null; activeCall = null;
currentOutputFile = null;
} }
public void fetchVideoFile(Context context, int id, String assetName, String mode, OnDownloadCallback callback) { public void fetchVideoFile(Context context, int id, String assetName, String mode, OnDownloadCallback callback) {
@ -61,6 +62,10 @@ public class MediaFetcher {
activeCall.enqueue(new Callback() { activeCall.enqueue(new Callback() {
@Override @Override
public void onFailure(@NonNull Call call, @NonNull IOException exception) { public void onFailure(@NonNull Call call, @NonNull IOException exception) {
if (currentOutputFile != null && currentOutputFile.exists()) {
currentOutputFile.delete();
currentOutputFile = null;
}
runOnUi(() -> { runOnUi(() -> {
if (callback != null) callback.onFailure(exception); if (callback != null) callback.onFailure(exception);
}); });
@ -88,10 +93,10 @@ public class MediaFetcher {
} }
String uniqueName = "video_" + System.currentTimeMillis() + "_" + id + ".mp4"; String uniqueName = "video_" + System.currentTimeMillis() + "_" + id + ".mp4";
File outputFile = new File(storageDir, uniqueName); currentOutputFile = new File(storageDir, uniqueName);
try (InputStream in = response.body().byteStream(); try (InputStream in = response.body().byteStream();
FileOutputStream out = new FileOutputStream(outputFile)) { FileOutputStream out = new FileOutputStream(currentOutputFile)) {
byte[] buffer = new byte[8192]; byte[] buffer = new byte[8192];
long writtenBytes = 0; long writtenBytes = 0;
@ -99,7 +104,8 @@ public class MediaFetcher {
while ((read = in.read(buffer)) != -1) { while ((read = in.read(buffer)) != -1) {
if (call.isCanceled()) { if (call.isCanceled()) {
outputFile.delete(); currentOutputFile.delete();
currentOutputFile = null;
return; return;
} }
out.write(buffer, 0, read); out.write(buffer, 0, read);
@ -113,10 +119,15 @@ public class MediaFetcher {
} }
runOnUi(() -> { runOnUi(() -> {
if (callback != null) callback.onSuccess(outputFile); if (callback != null) callback.onSuccess(currentOutputFile);
currentOutputFile = null;
}); });
} catch (IOException ex) { } catch (IOException ex) {
if (currentOutputFile != null && currentOutputFile.exists()) {
currentOutputFile.delete();
currentOutputFile = null;
}
runOnUi(() -> { runOnUi(() -> {
if (callback != null) callback.onFailure(ex); if (callback != null) callback.onFailure(ex);
}); });
@ -148,5 +159,9 @@ public class MediaFetcher {
} }
activeCall = null; activeCall = null;
uiHandler.removeCallbacksAndMessages(null); uiHandler.removeCallbacksAndMessages(null);
if (currentOutputFile != null && currentOutputFile.exists()) {
currentOutputFile.delete();
currentOutputFile = null;
}
} }
} }

View File

@ -22,8 +22,8 @@
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginStart="32dp" android:layout_marginStart="32dp"
android:layout_marginTop="32dp" android:layout_marginTop="32dp"
android:src="@drawable/back"
android:background="@drawable/rounded_rectangle_gradient" android:background="@drawable/rounded_rectangle_gradient"
android:src="@drawable/back"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent" />
@ -127,29 +127,31 @@
android:id="@+id/error_container" android:id="@+id/error_container"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:orientation="vertical" android:layout_centerInParent="true"
android:gravity="center" android:gravity="center"
android:orientation="vertical"
android:visibility="gone" android:visibility="gone"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
android:layout_centerInParent="true"> app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView <TextView
android:id="@+id/error_text" android:id="@+id/error_text"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center" android:layout_gravity="center"
android:layout_marginBottom="16dp"
android:gravity="center" android:gravity="center"
android:text="Download failed. \nPlease check the network and try again." android:text="The video download fails or does not exist, \nplease check the network and download it again"
android:textColor="#FF0000" android:textColor="#FF0000"
android:textSize="16sp" android:textSize="16sp" />
android:layout_marginBottom="16dp"/>
<Button <Button
android:id="@+id/retry_button" android:id="@+id/retry_button"
android:layout_marginTop="8dp"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="#FF0000" android:backgroundTint="#FF0000"
android:text="Retry" /> android:text="Retry" />
</LinearLayout> </LinearLayout>