Optimize wallpaper load

pull/605/head
jhengazuki 7 months ago
parent 14afaa8c5f
commit ea9d27292f
  1. 21
      app/src/leanback/java/com/fongmi/android/tv/ui/activity/SettingActivity.java
  2. 8
      app/src/main/java/com/fongmi/android/tv/Setting.java
  3. 44
      app/src/main/java/com/fongmi/android/tv/api/config/WallConfig.java
  4. 22
      app/src/main/java/com/fongmi/android/tv/ui/custom/CustomWallView.java
  5. 21
      app/src/mobile/java/com/fongmi/android/tv/ui/fragment/SettingFragment.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));
}

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

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

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

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

Loading…
Cancel
Save