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"
minSdk = 23
targetSdk = 34
versionCode = 3
versionCode = 4
versionName = "1.0.0"
setProperty(
"archivesBaseName",

View File

@ -32,4 +32,4 @@ public interface DynamicDataDao {
@Query("SELECT * FROM DynamicData WHERE wallpaperType = :type AND flowId = :flowId")
LiveData<DynamicData> getFavoriteStatus(int type, int flowId);
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}
}
}

View File

@ -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">
<TextView
android:id="@+id/error_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
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:textSize="16sp"
android:layout_marginBottom="16dp"/>
android:textSize="16sp" />
<Button
android:id="@+id/retry_button"
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="#FF0000"
android:text="Retry"/>
android:text="Retry" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>