From ea9d27292f133fdf23796bd1c62fdc34b195684b Mon Sep 17 00:00:00 2001 From: jhengazuki Date: Mon, 13 Oct 2025 16:03:44 +0800 Subject: [PATCH] Optimize wallpaper load --- .../tv/ui/activity/SettingActivity.java | 21 +++------ .../java/com/fongmi/android/tv/Setting.java | 8 ++++ .../android/tv/api/config/WallConfig.java | 44 ++++++++++++++++--- .../android/tv/ui/custom/CustomWallView.java | 22 +--------- .../tv/ui/fragment/SettingFragment.java | 21 +++------ 5 files changed, 60 insertions(+), 56 deletions(-) diff --git a/app/src/leanback/java/com/fongmi/android/tv/ui/activity/SettingActivity.java b/app/src/leanback/java/com/fongmi/android/tv/ui/activity/SettingActivity.java index cb1099538..3e5cb3aab 100644 --- a/app/src/leanback/java/com/fongmi/android/tv/ui/activity/SettingActivity.java +++ b/app/src/leanback/java/com/fongmi/android/tv/ui/activity/SettingActivity.java @@ -149,6 +149,7 @@ public class SettingActivity extends BaseActivity implements ConfigCallback, Sit mBinding.liveUrl.setText(config.getDesc()); break; case 2: + Setting.putWall(0); Notify.progress(this); WallConfig.load(config, getCallback(2)); mBinding.wallUrl.setText(config.getDesc()); @@ -269,24 +270,14 @@ public class SettingActivity extends BaseActivity implements ConfigCallback, Sit } private void setWallDefault(View view) { - WallConfig.refresh(Setting.getWall() == 4 ? 1 : Setting.getWall() + 1); + Setting.putWall(Setting.getWall() == 4 ? 1 : Setting.getWall() + 1); + RefreshEvent.wall(); } private void setWallRefresh(View view) { + Setting.putWall(0); Notify.progress(this); - WallConfig.get().load(new Callback() { - @Override - public void success() { - Notify.dismiss(); - setCacheText(); - } - - @Override - public void error(String msg) { - Notify.dismiss(); - Notify.show(msg); - } - }); + WallConfig.get().load(getCallback(2)); } private boolean onWallHistory(View view) { @@ -361,7 +352,7 @@ public class SettingActivity extends BaseActivity implements ConfigCallback, Sit } private void initConfig() { - WallConfig.get().init(); + WallConfig.get().init().load(); LiveConfig.get().init().load(); VodConfig.get().init().load(getCallback(0)); } diff --git a/app/src/main/java/com/fongmi/android/tv/Setting.java b/app/src/main/java/com/fongmi/android/tv/Setting.java index a2199aba3..140acbf88 100644 --- a/app/src/main/java/com/fongmi/android/tv/Setting.java +++ b/app/src/main/java/com/fongmi/android/tv/Setting.java @@ -51,6 +51,14 @@ public class Setting { Prefers.put("wall", wall); } + public static int getWallType() { + return Prefers.getInt("wall_type", 0); + } + + public static void putWallType(int type) { + Prefers.put("wall_type", type); + } + public static int getReset() { return Prefers.getInt("reset", 0); } diff --git a/app/src/main/java/com/fongmi/android/tv/api/config/WallConfig.java b/app/src/main/java/com/fongmi/android/tv/api/config/WallConfig.java index 9213b9261..cc9477cb7 100644 --- a/app/src/main/java/com/fongmi/android/tv/api/config/WallConfig.java +++ b/app/src/main/java/com/fongmi/android/tv/api/config/WallConfig.java @@ -1,6 +1,8 @@ package com.fongmi.android.tv.api.config; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.media.MediaMetadataRetriever; import android.text.TextUtils; import com.bumptech.glide.Glide; @@ -71,6 +73,10 @@ public class WallConfig { return config == null ? Config.wall() : config; } + public void load() { + load(new Callback()); + } + public void load(Callback callback) { if (executor != null) executor.shutdownNow(); executor = Executors.newSingleThreadExecutor(); @@ -80,33 +86,59 @@ public class WallConfig { private void loadConfig(Callback callback) { try { download(); - refresh(0); config.update(); + RefreshEvent.wall(); App.post(callback::success); } catch (Throwable e) { if (TextUtils.isEmpty(config.getUrl())) App.post(() -> callback.error("")); else App.post(() -> callback.error(Notify.getError(R.string.error_config_get, e))); + Setting.putWall(1); + RefreshEvent.wall(); e.printStackTrace(); } } private void download() throws Exception { + Path.clear(FileUtil.getWallCache()); + Path.clear(FileUtil.getWall(0)); File file = FileUtil.getWall(0); if (getUrl().startsWith("file")) Path.copy(Path.local(getUrl()), file); else Download.create(UrlUtil.convert(getUrl()), file).start(); if (!Path.exists(file)) throw new FileNotFoundException(); + createSnapshot(file); + Setting.putWallType(0); + if (isGif(file)) Setting.putWallType(1); + else if (isVideo(file)) Setting.putWallType(2); + } + + private void createSnapshot(File file) throws Exception { Bitmap bitmap = Glide.with(App.get()).asBitmap().load(file).override(ResUtil.getScreenWidth(), ResUtil.getScreenHeight()).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).submit().get(); try (FileOutputStream fos = new FileOutputStream(FileUtil.getWallCache())) { bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); } } - public boolean needSync(String url) { - return sync || TextUtils.isEmpty(config.getUrl()) || url.equals(config.getUrl()); + private boolean isVideo(File file) { + try (MediaMetadataRetriever retriever = new MediaMetadataRetriever()) { + retriever.setDataSource(file.getAbsolutePath()); + return "yes".equalsIgnoreCase(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO)); + } catch (Exception e) { + return false; + } } - public static void refresh(int index) { - Setting.putWall(index); - RefreshEvent.wall(); + private boolean isGif(File file) { + try { + BitmapFactory.Options options = new BitmapFactory.Options(); + options.inJustDecodeBounds = true; + BitmapFactory.decodeFile(file.getAbsolutePath(), options); + return "image/gif".equals(options.outMimeType); + } catch (Exception e) { + return false; + } + } + + public boolean needSync(String url) { + return sync || TextUtils.isEmpty(config.getUrl()) || url.equals(config.getUrl()); } } diff --git a/app/src/main/java/com/fongmi/android/tv/ui/custom/CustomWallView.java b/app/src/main/java/com/fongmi/android/tv/ui/custom/CustomWallView.java index 971bcc3bf..cd5cd7cd5 100644 --- a/app/src/main/java/com/fongmi/android/tv/ui/custom/CustomWallView.java +++ b/app/src/main/java/com/fongmi/android/tv/ui/custom/CustomWallView.java @@ -1,9 +1,7 @@ package com.fongmi.android.tv.ui.custom; import android.content.Context; -import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; -import android.media.MediaMetadataRetriever; import android.net.Uri; import android.util.AttributeSet; import android.view.LayoutInflater; @@ -64,22 +62,6 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser player.setVolume(0); } - private boolean isVideo(File file) { - try (MediaMetadataRetriever retriever = new MediaMetadataRetriever()) { - retriever.setDataSource(file.getAbsolutePath()); - return "yes".equalsIgnoreCase(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO)); - } catch (Exception e) { - return false; - } - } - - private boolean isGif(File file) { - BitmapFactory.Options options = new BitmapFactory.Options(); - options.inJustDecodeBounds = true; - BitmapFactory.decodeFile(file.getAbsolutePath(), options); - return "image/gif".equals(options.outMimeType); - } - @Subscribe(threadMode = ThreadMode.MAIN) public void onRefreshEvent(RefreshEvent event) { if (event.getType() == RefreshEvent.Type.WALL) refresh(); @@ -92,8 +74,8 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser private void load(File file) { if (!file.getName().endsWith("0")) loadRes(ResUtil.getDrawable(file.getName())); - else if (isVideo(file)) loadVideo(file); - else if (isGif(file)) loadGif(file); + else if (Setting.getWallType() == 2) loadVideo(file); + else if (Setting.getWallType() == 1) loadGif(file); else loadImage(); } diff --git a/app/src/mobile/java/com/fongmi/android/tv/ui/fragment/SettingFragment.java b/app/src/mobile/java/com/fongmi/android/tv/ui/fragment/SettingFragment.java index 42633c0db..b34a701e4 100644 --- a/app/src/mobile/java/com/fongmi/android/tv/ui/fragment/SettingFragment.java +++ b/app/src/mobile/java/com/fongmi/android/tv/ui/fragment/SettingFragment.java @@ -156,6 +156,7 @@ public class SettingFragment extends BaseFragment implements ConfigCallback, Sit mBinding.liveUrl.setText(config.getDesc()); break; case 2: + Setting.putWall(0); Notify.progress(requireActivity()); WallConfig.load(config, getCallback(2)); mBinding.wallUrl.setText(config.getDesc()); @@ -276,24 +277,14 @@ public class SettingFragment extends BaseFragment implements ConfigCallback, Sit } private void setWallDefault(View view) { - WallConfig.refresh(Setting.getWall() == 4 ? 1 : Setting.getWall() + 1); + Setting.putWall(Setting.getWall() == 4 ? 1 : Setting.getWall() + 1); + RefreshEvent.wall(); } private void setWallRefresh(View view) { + Setting.putWall(0); Notify.progress(requireActivity()); - WallConfig.get().load(new Callback() { - @Override - public void success() { - Notify.dismiss(); - setCacheText(); - } - - @Override - public void error(String msg) { - Notify.dismiss(); - Notify.show(msg); - } - }); + WallConfig.get().load(getCallback(2)); } private boolean onWallHistory(View view) { @@ -372,7 +363,7 @@ public class SettingFragment extends BaseFragment implements ConfigCallback, Sit } private void initConfig() { - WallConfig.get().init(); + WallConfig.get().init().load(); LiveConfig.get().init().load(); VodConfig.get().init().load(getCallback(0)); }