V1.0.0(5)增加文件缺失判断
This commit is contained in:
parent
a74307e679
commit
beadad3104
@ -14,7 +14,7 @@ android {
|
|||||||
applicationId = "com.live.dynamicwallpaper"
|
applicationId = "com.live.dynamicwallpaper"
|
||||||
minSdk = 23
|
minSdk = 23
|
||||||
targetSdk = 34
|
targetSdk = 34
|
||||||
versionCode = 4
|
versionCode = 5
|
||||||
versionName = "1.0.0"
|
versionName = "1.0.0"
|
||||||
setProperty(
|
setProperty(
|
||||||
"archivesBaseName",
|
"archivesBaseName",
|
||||||
|
|||||||
@ -4,6 +4,7 @@ import android.app.WallpaperManager;
|
|||||||
import android.content.ComponentName;
|
import android.content.ComponentName;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
|
import android.media.MediaPlayer;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
@ -25,6 +26,7 @@ import com.live.dynamicwallpaper.util.MediaFetcher;
|
|||||||
import com.live.dynamicwallpaper.viewmodel.DynamicViewModel;
|
import com.live.dynamicwallpaper.viewmodel.DynamicViewModel;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
public class DynamicActivity extends AppCompatActivity {
|
public class DynamicActivity extends AppCompatActivity {
|
||||||
private ActivityDynamicBinding ui;
|
private ActivityDynamicBinding ui;
|
||||||
@ -32,6 +34,7 @@ public class DynamicActivity extends AppCompatActivity {
|
|||||||
private DynamicViewModel viewModel;
|
private DynamicViewModel viewModel;
|
||||||
private MediaFetcher mediaFetcher;
|
private MediaFetcher mediaFetcher;
|
||||||
private boolean isDownloading = false;
|
private boolean isDownloading = false;
|
||||||
|
private Toast toast;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
@ -97,6 +100,7 @@ public class DynamicActivity extends AppCompatActivity {
|
|||||||
public void onFailure(Exception e) {
|
public void onFailure(Exception e) {
|
||||||
if (ui == null) return;
|
if (ui == null) return;
|
||||||
isDownloading = false;
|
isDownloading = false;
|
||||||
|
contentData.setWallpaperPath("");
|
||||||
hideLoading();
|
hideLoading();
|
||||||
showRetry();
|
showRetry();
|
||||||
Log.e("LivePreview", "Media fetch failed", e);
|
Log.e("LivePreview", "Media fetch failed", e);
|
||||||
@ -115,6 +119,7 @@ public class DynamicActivity extends AppCompatActivity {
|
|||||||
private void showRetry() {
|
private void showRetry() {
|
||||||
if (ui == null) return;
|
if (ui == null) return;
|
||||||
ui.errorContainer.setVisibility(View.VISIBLE);
|
ui.errorContainer.setVisibility(View.VISIBLE);
|
||||||
|
ui.view.setVisibility(View.VISIBLE);
|
||||||
ui.retryButton.setOnClickListener(v -> {
|
ui.retryButton.setOnClickListener(v -> {
|
||||||
ui.errorContainer.setVisibility(View.GONE);
|
ui.errorContainer.setVisibility(View.GONE);
|
||||||
showLoading();
|
showLoading();
|
||||||
@ -126,6 +131,7 @@ public class DynamicActivity extends AppCompatActivity {
|
|||||||
private void hideRetry() {
|
private void hideRetry() {
|
||||||
if (ui != null) {
|
if (ui != null) {
|
||||||
ui.errorContainer.setVisibility(View.GONE);
|
ui.errorContainer.setVisibility(View.GONE);
|
||||||
|
ui.view.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -142,6 +148,18 @@ public class DynamicActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void prepareVideoPlayback() {
|
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());
|
File video = new File(contentData.getWallpaperPath());
|
||||||
if (video.exists()) {
|
if (video.exists()) {
|
||||||
try {
|
try {
|
||||||
@ -150,6 +168,7 @@ public class DynamicActivity extends AppCompatActivity {
|
|||||||
ui.videoView.setOnPreparedListener(media -> media.setLooping(true));
|
ui.videoView.setOnPreparedListener(media -> media.setLooping(true));
|
||||||
ui.videoView.setOnErrorListener((mp, what, extra) -> {
|
ui.videoView.setOnErrorListener((mp, what, extra) -> {
|
||||||
Log.e("LivePreview", "VideoView error: " + what + ", " + extra);
|
Log.e("LivePreview", "VideoView error: " + what + ", " + extra);
|
||||||
|
mp.reset();
|
||||||
showErrorPrompt();
|
showErrorPrompt();
|
||||||
showRetry();
|
showRetry();
|
||||||
return true;
|
return true;
|
||||||
@ -172,6 +191,15 @@ public class DynamicActivity extends AppCompatActivity {
|
|||||||
showRetry();
|
showRetry();
|
||||||
return;
|
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);
|
SharedPreferences preferences = getSharedPreferences("WallpaperPrefs", MODE_PRIVATE);
|
||||||
preferences.edit().putString("video_path", videoPath).apply();
|
preferences.edit().putString("video_path", videoPath).apply();
|
||||||
|
|
||||||
@ -196,7 +224,7 @@ public class DynamicActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
private void showErrorPrompt() {
|
private void showErrorPrompt() {
|
||||||
if (isActive() && ui != null) {
|
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.progressText.setVisibility(View.VISIBLE);
|
||||||
ui.loadingSpinner.setVisibility(View.VISIBLE);
|
ui.loadingSpinner.setVisibility(View.VISIBLE);
|
||||||
ui.view.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 (contentData != null && !contentData.getWallpaperPath().isEmpty() && !localFile.exists()) {
|
||||||
if (!isDownloading) {
|
if (!isDownloading) {
|
||||||
showRetry();
|
showRetry();
|
||||||
|
Log.d("Bynamic" , "is");
|
||||||
}
|
}
|
||||||
} else if (contentData != null && !contentData.getWallpaperPath().isEmpty()) {
|
} else if (contentData != null && !contentData.getWallpaperPath().isEmpty()) {
|
||||||
prepareVideoPlayback();
|
prepareVideoPlayback();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void showToast(String message) {
|
||||||
|
if (toast != null) {
|
||||||
|
toast.cancel();
|
||||||
|
}
|
||||||
|
toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
|
||||||
|
toast.show();
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onDestroy() {
|
protected void onDestroy() {
|
||||||
super.onDestroy();
|
super.onDestroy();
|
||||||
|
|||||||
@ -104,10 +104,13 @@ public class MediaFetcher {
|
|||||||
|
|
||||||
while ((read = in.read(buffer)) != -1) {
|
while ((read = in.read(buffer)) != -1) {
|
||||||
if (call.isCanceled()) {
|
if (call.isCanceled()) {
|
||||||
|
if (currentOutputFile != null && currentOutputFile.exists()) {
|
||||||
currentOutputFile.delete();
|
currentOutputFile.delete();
|
||||||
|
}
|
||||||
currentOutputFile = null;
|
currentOutputFile = null;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
out.write(buffer, 0, read);
|
out.write(buffer, 0, read);
|
||||||
writtenBytes += read;
|
writtenBytes += read;
|
||||||
|
|
||||||
|
|||||||
@ -63,8 +63,10 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
android:background="@color/gray"
|
android:background="@color/gray"
|
||||||
|
android:visibility="gone"
|
||||||
|
android:clickable="true"
|
||||||
android:focusable="true"
|
android:focusable="true"
|
||||||
android:visibility="gone" />
|
android:focusableInTouchMode="true" />
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
android:id="@+id/progress_tip"
|
android:id="@+id/progress_tip"
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user