Optimize load background

pull/497/head
FongMi 2 years ago
parent 5eaa484a08
commit 8bba8bcb04
  1. 32
      app/src/leanback/java/com/fongmi/android/tv/ui/base/BaseActivity.java
  2. 35
      app/src/main/java/com/fongmi/android/tv/api/config/WallConfig.java
  3. 41
      app/src/mobile/java/com/fongmi/android/tv/ui/base/BaseActivity.java

@ -3,25 +3,19 @@ package com.fongmi.android.tv.ui.base;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewbinding.ViewBinding;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.bumptech.glide.signature.ObjectKey;
import com.fongmi.android.tv.App;
import com.fongmi.android.tv.R;
import com.fongmi.android.tv.Setting;
import com.fongmi.android.tv.api.config.WallConfig;
import com.fongmi.android.tv.event.RefreshEvent;
import com.fongmi.android.tv.utils.FileUtil;
import com.fongmi.android.tv.utils.ResUtil;
@ -102,31 +96,13 @@ public abstract class BaseActivity extends AppCompatActivity {
try {
if (!customWall()) return;
File file = FileUtil.getWall(Setting.getWall());
if (file.exists() && file.length() > 0) loadWall(file);
if (file.exists() && file.length() > 0) getWindow().setBackgroundDrawable(WallConfig.drawable(file));
else getWindow().setBackgroundDrawableResource(ResUtil.getDrawable(file.getName()));
} catch (Exception e) {
getWindow().setBackgroundDrawableResource(R.drawable.wallpaper_1);
}
}
private void loadWall(File file) {
Glide.with(App.get()).load(file).centerCrop().override(ResUtil.getScreenWidth(), ResUtil.getScreenHeight()).signature(new ObjectKey(com.github.catvod.utils.Util.md5(file))).into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable drawable, @Nullable Transition<? super Drawable> transition) {
getWindow().setBackgroundDrawable(drawable);
}
@Override
public void onLoadFailed(@Nullable Drawable error) {
getWindow().setBackgroundDrawableResource(R.drawable.wallpaper_1);
}
@Override
public void onLoadCleared(@Nullable Drawable drawable) {
}
});
}
private Resources hackResources(Resources resources) {
try {
AutoSizeCompat.autoConvertDensityOfGlobal(resources);
@ -138,7 +114,9 @@ public abstract class BaseActivity extends AppCompatActivity {
@Subscribe(threadMode = ThreadMode.MAIN)
public void onRefreshEvent(RefreshEvent event) {
if (event.getType() == RefreshEvent.Type.WALL) refreshWall();
if (event.getType() != RefreshEvent.Type.WALL) return;
WallConfig.get().setDrawable(null);
refreshWall();
}
@Override

@ -1,7 +1,11 @@
package com.fongmi.android.tv.api.config;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.signature.ObjectKey;
import com.fongmi.android.tv.App;
import com.fongmi.android.tv.R;
import com.fongmi.android.tv.Setting;
@ -10,15 +14,18 @@ import com.fongmi.android.tv.event.RefreshEvent;
import com.fongmi.android.tv.impl.Callback;
import com.fongmi.android.tv.utils.FileUtil;
import com.fongmi.android.tv.utils.Notify;
import com.fongmi.android.tv.utils.ResUtil;
import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Asset;
import com.github.catvod.utils.Path;
import com.github.catvod.utils.Util;
import java.io.File;
import java.io.IOException;
import java.io.FileOutputStream;
public class WallConfig {
private Drawable drawable;
private Config config;
private boolean sync;
@ -38,6 +45,12 @@ public class WallConfig {
return get().getConfig().getDesc();
}
public static Drawable drawable(File file) {
if (get().drawable != null) return get().drawable;
get().setDrawable(Drawable.createFromPath(file.getAbsolutePath()));
return get().drawable;
}
public static void load(Config config, Callback callback) {
get().clear().config(config).load(callback);
}
@ -62,6 +75,10 @@ public class WallConfig {
return config == null ? Config.wall() : config;
}
public void setDrawable(Drawable drawable) {
this.drawable = drawable;
}
public void load(Callback callback) {
App.execute(() -> loadConfig(callback));
}
@ -80,12 +97,22 @@ public class WallConfig {
}
}
private File write(File file) throws IOException {
private File write(File file) throws Exception {
if (getUrl().startsWith("file")) Path.copy(Path.local(getUrl()), file);
else if (getUrl().startsWith("assets")) Path.copy(Asset.open(getUrl()), file);
else if (getUrl().startsWith("http")) Path.write(file, OkHttp.newCall(getUrl()).execute().body().bytes());
else file.delete();
return file;
return resize(file);
}
private File resize(File file) {
try {
Bitmap bitmap = Glide.with(App.get()).asBitmap().load(file).centerCrop().override(ResUtil.getScreenWidth(), ResUtil.getScreenHeight()).signature(new ObjectKey(Util.md5(file))).submit().get();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(file));
bitmap.recycle();
return file;
} catch (Exception e) {
return file;
}
}
public boolean needSync(String url) {

@ -1,9 +1,7 @@
package com.fongmi.android.tv.ui.base;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.DisplayCutout;
@ -12,22 +10,15 @@ import android.view.ViewGroup;
import android.view.WindowManager;
import androidx.activity.OnBackPressedCallback;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewbinding.ViewBinding;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.bumptech.glide.signature.ObjectKey;
import com.fongmi.android.tv.App;
import com.fongmi.android.tv.R;
import com.fongmi.android.tv.Setting;
import com.fongmi.android.tv.api.config.WallConfig;
import com.fongmi.android.tv.event.RefreshEvent;
import com.fongmi.android.tv.utils.FileUtil;
import com.fongmi.android.tv.utils.ResUtil;
import com.github.catvod.utils.Util;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
@ -129,40 +120,18 @@ public abstract class BaseActivity extends AppCompatActivity {
try {
if (!customWall()) return;
File file = FileUtil.getWall(Setting.getWall());
if (file.exists() && file.length() > 0) loadWall(file);
if (file.exists() && file.length() > 0) getWindow().setBackgroundDrawable(WallConfig.drawable(file));
else getWindow().setBackgroundDrawableResource(ResUtil.getDrawable(file.getName()));
} catch (Exception e) {
getWindow().setBackgroundDrawableResource(R.drawable.wallpaper_1);
}
}
private void loadWall(File file) {
Glide.with(App.get()).load(file).centerCrop().override(ResUtil.getScreenWidth(), ResUtil.getScreenHeight()).signature(new ObjectKey(Util.md5(file))).into(new CustomTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable drawable, @Nullable Transition<? super Drawable> transition) {
getWindow().setBackgroundDrawable(drawable);
}
@Override
public void onLoadFailed(@Nullable Drawable error) {
getWindow().setBackgroundDrawableResource(R.drawable.wallpaper_1);
}
@Override
public void onLoadCleared(@Nullable Drawable drawable) {
}
});
}
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
if (getRequestedOrientation() != newConfig.orientation) refreshWall();
super.onConfigurationChanged(newConfig);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onRefreshEvent(RefreshEvent event) {
if (event.getType() == RefreshEvent.Type.WALL) refreshWall();
if (event.getType() != RefreshEvent.Type.WALL) return;
WallConfig.get().setDrawable(null);
refreshWall();
}
@Override

Loading…
Cancel
Save