|
|
|
|
@ -1,6 +1,8 @@ |
|
|
|
|
package com.fongmi.android.tv.ui.custom; |
|
|
|
|
|
|
|
|
|
import android.content.Context; |
|
|
|
|
import android.graphics.Bitmap; |
|
|
|
|
import android.graphics.BitmapFactory; |
|
|
|
|
import android.graphics.drawable.Drawable; |
|
|
|
|
import android.net.Uri; |
|
|
|
|
import android.util.AttributeSet; |
|
|
|
|
@ -16,10 +18,12 @@ import androidx.media3.common.MediaItem; |
|
|
|
|
import androidx.media3.common.Player; |
|
|
|
|
import androidx.media3.exoplayer.ExoPlayer; |
|
|
|
|
import androidx.media3.ui.PlayerView; |
|
|
|
|
import androidx.palette.graphics.Palette; |
|
|
|
|
|
|
|
|
|
import com.fongmi.android.tv.R; |
|
|
|
|
import com.fongmi.android.tv.databinding.ViewWallBinding; |
|
|
|
|
import com.fongmi.android.tv.event.ConfigEvent; |
|
|
|
|
import com.fongmi.android.tv.event.RefreshEvent; |
|
|
|
|
import com.fongmi.android.tv.setting.Setting; |
|
|
|
|
import com.fongmi.android.tv.utils.FileUtil; |
|
|
|
|
|
|
|
|
|
@ -34,11 +38,11 @@ import pl.droidsonroids.gif.GifDrawable; |
|
|
|
|
|
|
|
|
|
public class CustomWallView extends FrameLayout implements DefaultLifecycleObserver { |
|
|
|
|
|
|
|
|
|
private static final int[] WALL_PAPERS = {0, R.drawable.wallpaper_1, R.drawable.wallpaper_2, R.drawable.wallpaper_3, R.drawable.wallpaper_4}; |
|
|
|
|
private static final int[] WALL_COLORS = {0, 0xFF40C090, 0xFF4870E0, 0xFF48B0C0, 0xFF404040}; |
|
|
|
|
private static final int TYPE_RES = 0; |
|
|
|
|
private static final int TYPE_GIF = 1; |
|
|
|
|
private static final int TYPE_VIDEO = 2; |
|
|
|
|
private static final int[] BUILT_IN = {0, R.drawable.wallpaper_1, R.drawable.wallpaper_2, R.drawable.wallpaper_3, R.drawable.wallpaper_4}; |
|
|
|
|
|
|
|
|
|
private ViewWallBinding binding; |
|
|
|
|
private GifDrawable drawable; |
|
|
|
|
private PlayerView video; |
|
|
|
|
@ -65,6 +69,7 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser |
|
|
|
|
private void refresh() { |
|
|
|
|
stop(); |
|
|
|
|
load(); |
|
|
|
|
theme(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void stop() { |
|
|
|
|
@ -86,12 +91,17 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser |
|
|
|
|
private void load() { |
|
|
|
|
int wall = Setting.getWall(); |
|
|
|
|
int type = Setting.getWallType(); |
|
|
|
|
if (type == TYPE_RES && wall > 0 && wall < BUILT_IN.length) loadRes(BUILT_IN[wall]); |
|
|
|
|
if (isBuiltIn(wall, type)) loadRes(WALL_PAPERS[wall]); |
|
|
|
|
else if (type == TYPE_VIDEO) loadVideo(FileUtil.getWall(wall)); |
|
|
|
|
else if (type == TYPE_GIF) loadGif(FileUtil.getWall(wall)); |
|
|
|
|
else loadImage(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void theme() { |
|
|
|
|
Setting.putWallColor(getWallColor()); |
|
|
|
|
if (Setting.getThemeColor() == 0) RefreshEvent.theme(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void loadRes(int resId) { |
|
|
|
|
binding.image.setImageResource(resId); |
|
|
|
|
} |
|
|
|
|
@ -149,6 +159,38 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser |
|
|
|
|
return player != null && video != null && video.getVisibility() == VISIBLE && player.getMediaItemCount() > 0; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private int getWallColor() { |
|
|
|
|
int wall = Setting.getWall(); |
|
|
|
|
int type = Setting.getWallType(); |
|
|
|
|
if (isBuiltIn(wall, type)) return WALL_COLORS[wall]; |
|
|
|
|
File file = FileUtil.getWallCache(); |
|
|
|
|
return file.exists() ? paletteColor(file) : WALL_COLORS[1]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private int paletteColor(File file) { |
|
|
|
|
Bitmap bitmap = decodeBitmap(file); |
|
|
|
|
if (bitmap == null) return WALL_COLORS[1]; |
|
|
|
|
Palette palette = Palette.from(bitmap).maximumColorCount(8).generate(); |
|
|
|
|
bitmap.recycle(); |
|
|
|
|
return swatchColor(palette); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private Bitmap decodeBitmap(File file) { |
|
|
|
|
BitmapFactory.Options opts = new BitmapFactory.Options(); |
|
|
|
|
opts.inSampleSize = 8; |
|
|
|
|
return BitmapFactory.decodeFile(file.getAbsolutePath(), opts); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private int swatchColor(Palette palette) { |
|
|
|
|
Palette.Swatch swatch = palette.getVibrantSwatch(); |
|
|
|
|
if (swatch == null) swatch = palette.getDominantSwatch(); |
|
|
|
|
return swatch != null ? swatch.getRgb() : WALL_COLORS[1]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private boolean isBuiltIn(int wall, int type) { |
|
|
|
|
return type == TYPE_RES && wall > 0 && wall < WALL_PAPERS.length; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
public void onCreate(@NonNull LifecycleOwner owner) { |
|
|
|
|
EventBus.getDefault().register(this); |
|
|
|
|
|