Use glide to load wallpaper

pull/466/head
okjack 2 years ago
parent 2e765b3675
commit 4131b35502
  1. 41
      app/src/leanback/java/com/fongmi/android/tv/ui/base/BaseActivity.java
  2. 15
      app/src/main/java/com/fongmi/android/tv/api/config/WallConfig.java
  3. 2
      app/src/main/java/com/fongmi/android/tv/player/Players.java
  4. 30
      app/src/main/java/com/fongmi/android/tv/utils/ImgUtil.java
  5. 64
      app/src/mobile/java/com/fongmi/android/tv/ui/base/BaseActivity.java

@ -9,14 +9,20 @@ 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.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
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.LanguageUtil;
@ -42,11 +48,16 @@ public abstract class BaseActivity extends AppCompatActivity {
EventBus.getDefault().register(this);
Util.hideSystemUI(this);
setBackCallback();
setWall();
initView();
initEvent();
}
@Override
public void setContentView(View view) {
super.setContentView(view);
refreshWall();
}
protected Activity getActivity() {
return this;
}
@ -93,17 +104,35 @@ public abstract class BaseActivity extends AppCompatActivity {
});
}
private void setWall() {
private void refreshWall() {
try {
if (!customWall()) return;
File file = FileUtil.getWall(Setting.getWall());
if (file.exists() && file.length() > 0) getWindow().setBackgroundDrawable(WallConfig.drawable(Drawable.createFromPath(file.getAbsolutePath())));
if (file.exists() && file.length() > 0) loadWall(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().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().override(ResUtil.getScreenWidth(), ResUtil.getScreenHeight())).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 {
LanguageUtil.setLanguage(resources, Setting.getLanguage());
@ -116,9 +145,7 @@ public abstract class BaseActivity extends AppCompatActivity {
@Subscribe(threadMode = ThreadMode.MAIN)
public void onRefreshEvent(RefreshEvent event) {
if (event.getType() != RefreshEvent.Type.WALL) return;
WallConfig.get().setDrawable(null);
setWall();
if (event.getType() == RefreshEvent.Type.WALL) refreshWall();
}
@Override

@ -1,6 +1,5 @@
package com.fongmi.android.tv.api.config;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import com.fongmi.android.tv.App;
@ -10,7 +9,6 @@ import com.fongmi.android.tv.bean.Config;
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.ImgUtil;
import com.fongmi.android.tv.utils.Notify;
import com.github.catvod.net.OkHttp;
import com.github.catvod.utils.Asset;
@ -21,7 +19,6 @@ import java.io.IOException;
public class WallConfig {
private Drawable drawable;
private Config config;
private boolean sync;
@ -41,12 +38,6 @@ public class WallConfig {
return get().getConfig().getDesc();
}
public static Drawable drawable(Drawable drawable) {
if (get().drawable != null) return drawable;
get().setDrawable(drawable);
return drawable;
}
public static void load(Config config, Callback callback) {
get().clear().config(config).load(callback);
}
@ -71,10 +62,6 @@ 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));
}
@ -96,7 +83,7 @@ public class WallConfig {
private File write(File file) throws IOException {
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, ImgUtil.resize(OkHttp.newCall(getUrl()).execute().body().bytes()));
else if (getUrl().startsWith("http")) Path.write(file, OkHttp.newCall(getUrl()).execute().body().bytes());
else file.delete();
return file;
}

@ -256,7 +256,7 @@ public class Players implements Player.Listener, IMediaPlayer.Listener, Analytic
}
public boolean canAdjustSpeed() {
return isIjk() || (isExo() && !Setting.isTunnel());
return isIjk() || !Setting.isTunnel();
}
public boolean haveTrack(int type) {

@ -1,9 +1,6 @@
package com.fongmi.android.tv.utils;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
@ -27,7 +24,6 @@ import com.fongmi.android.tv.Setting;
import com.github.catvod.utils.Json;
import com.google.common.net.HttpHeaders;
import java.io.ByteArrayOutputStream;
import java.util.Map;
import jahirfiquitiva.libs.textdrawable.TextDrawable;
@ -91,32 +87,6 @@ public class ImgUtil {
for (Map.Entry<String, String> entry : map.entrySet()) builder.addHeader(UrlUtil.fixHeader(entry.getKey()), entry.getValue());
}
public static byte[] resize(byte[] bytes) {
Bitmap bitmap = crop(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
return baos.toByteArray();
}
private static Bitmap crop(Bitmap source) {
int newWidth = ResUtil.getScreenWidth();
int newHeight = ResUtil.getScreenHeight();
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;
RectF rectF = new RectF(left, top, left + scaledWidth, top + scaledHeight);
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, rectF, null);
return dest;
}
private static RequestListener<Bitmap> getListener(ImageView view) {
return getListener(view, ImageView.ScaleType.CENTER);
}

@ -1,6 +1,7 @@
package com.fongmi.android.tv.ui.base;
import android.app.Activity;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
@ -12,9 +13,17 @@ 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.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
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;
@ -39,12 +48,17 @@ public abstract class BaseActivity extends AppCompatActivity {
if (transparent()) setTransparent(this);
setContentView(getBinding().getRoot());
EventBus.getDefault().register(this);
setBackCallback();
setWall();
initView(savedInstanceState);
setBackCallback();
initEvent();
}
@Override
public void setContentView(View view) {
super.setContentView(view);
refreshWall();
}
protected Activity getActivity() {
return this;
}
@ -107,24 +121,24 @@ public abstract class BaseActivity extends AppCompatActivity {
});
}
private void setWall() {
private void setTransparent(Activity activity) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
}
private void refreshWall() {
try {
if (!customWall()) return;
File file = FileUtil.getWall(Setting.getWall());
if (file.exists() && file.length() > 0) getWindow().setBackgroundDrawable(WallConfig.drawable(Drawable.createFromPath(file.getAbsolutePath())));
if (file.exists() && file.length() > 0) loadWall(file);
else getWindow().setBackgroundDrawableResource(ResUtil.getDrawable(file.getName()));
} catch (Exception e) {
getWindow().setBackgroundDrawableResource(R.drawable.wallpaper_1);
}
}
private void setTransparent(Activity activity) {
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
}
private Resources hackResources(Resources resources) {
try {
LanguageUtil.setLanguage(resources, Setting.getLanguage());
@ -139,11 +153,33 @@ public abstract class BaseActivity extends AppCompatActivity {
return hackResources(super.getResources());
}
private void loadWall(File file) {
Glide.with(App.get()).load(file).centerCrop().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).apply(new RequestOptions().override(ResUtil.getScreenWidth(), ResUtil.getScreenHeight())).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) return;
WallConfig.get().setDrawable(null);
setWall();
if (event.getType() == RefreshEvent.Type.WALL) refreshWall();
}
@Override

Loading…
Cancel
Save