fongmi
FongMi 2 weeks ago
parent 4ce398d8af
commit fc431c174b
  1. 46
      app/src/main/java/com/fongmi/android/tv/impl/NewPipeImpl.java
  2. 75
      app/src/main/java/com/fongmi/android/tv/ui/custom/CustomWallView.java

@ -18,7 +18,7 @@ import okhttp3.ResponseBody;
public final class NewPipeImpl extends Downloader {
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0";
public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0";
private static class Loader {
static volatile NewPipeImpl INSTANCE = new NewPipeImpl();
@ -30,41 +30,19 @@ public final class NewPipeImpl extends Downloader {
@Override
public Response execute(@NonNull Request request) throws IOException, ReCaptchaException {
String httpMethod = request.httpMethod();
String url = request.url();
Map<String, List<String>> headers = request.headers();
byte[] dataToSend = request.dataToSend();
RequestBody requestBody = null;
if (dataToSend != null) {
requestBody = RequestBody.create(null, dataToSend);
okhttp3.Request.Builder builder = new okhttp3.Request.Builder().url(request.url()).method(request.httpMethod(), body(request)).header("User-Agent", USER_AGENT);
for (Map.Entry<String, List<String>> entry : request.headers().entrySet()) {
builder.removeHeader(entry.getKey());
for (String value : entry.getValue()) builder.addHeader(entry.getKey(), value);
}
okhttp3.Request.Builder requestBuilder = new okhttp3.Request.Builder().method(httpMethod, requestBody).url(url).addHeader("User-Agent", USER_AGENT);
for (Map.Entry<String, List<String>> pair : headers.entrySet()) {
String headerName = pair.getKey();
List<String> headerValueList = pair.getValue();
if (headerValueList.size() > 1) {
requestBuilder.removeHeader(headerName);
for (String headerValue : headerValueList) {
requestBuilder.addHeader(headerName, headerValue);
}
} else if (headerValueList.size() == 1) {
requestBuilder.header(headerName, headerValueList.get(0));
}
}
okhttp3.Response response = OkHttp.client().newCall(requestBuilder.build()).execute();
if (response.code() == 429) {
response.close();
throw new ReCaptchaException("reCaptcha Challenge requested", url);
try (okhttp3.Response response = OkHttp.client().newCall(builder.build()).execute(); ResponseBody body = response.body()) {
if (response.code() == 429) throw new ReCaptchaException("reCaptcha Challenge requested", request.url());
return new Response(response.code(), response.message(), response.headers().toMultimap(), body.string(), response.request().url().toString());
}
}
ResponseBody body = response.body();
String responseBodyToReturn = body.string();
String latestUrl = response.request().url().toString();
return new Response(response.code(), response.message(), response.headers().toMultimap(), responseBodyToReturn, latestUrl);
private static RequestBody body(Request request) {
byte[] data = request.dataToSend();
return data == null ? null : RequestBody.create(data);
}
}

@ -34,11 +34,15 @@ import pl.droidsonroids.gif.GifDrawable;
public class CustomWallView extends FrameLayout implements DefaultLifecycleObserver {
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;
private ExoPlayer player;
private Drawable cache;
public CustomWallView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
@ -47,23 +51,12 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
if (!isInEditMode()) init();
}
private void init() {
if (isInEditMode()) return;
binding = ViewWallBinding.inflate(LayoutInflater.from(getContext()), this, true);
((ComponentActivity) getContext()).getLifecycle().addObserver(this);
refresh();
}
private void ensurePlayer() {
if (player != null) return;
player = new ExoPlayer.Builder(getContext()).build();
player.setRepeatMode(Player.REPEAT_MODE_ALL);
player.setPlayWhenReady(true);
player.mute();
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onConfigEvent(ConfigEvent event) {
if (event.type() == ConfigEvent.Type.WALL) refresh();
@ -86,24 +79,27 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
if (drawable != null) {
drawable.stop();
drawable.recycle();
drawable = null;
}
}
private void load() {
int wall = Setting.getWall();
File file = FileUtil.getWall(wall);
cache = Drawable.createFromPath(FileUtil.getWallCache().getAbsolutePath());
if (Setting.getWallType() == 0 && wall != 0) loadRes(wall);
else if (Setting.getWallType() == 2) loadVideo(file);
else if (Setting.getWallType() == 1) loadGif(file);
int type = Setting.getWallType();
if (type == TYPE_RES && wall > 0 && wall < BUILT_IN.length) loadRes(BUILT_IN[wall]);
else if (type == TYPE_VIDEO) loadVideo(FileUtil.getWall(wall));
else if (type == TYPE_GIF) loadGif(FileUtil.getWall(wall));
else loadImage();
}
private void loadRes(int wall) {
if (wall == 1) binding.image.setImageResource(R.drawable.wallpaper_1);
else if (wall == 2) binding.image.setImageResource(R.drawable.wallpaper_2);
else if (wall == 3) binding.image.setImageResource(R.drawable.wallpaper_3);
else if (wall == 4) binding.image.setImageResource(R.drawable.wallpaper_4);
private void loadRes(int resId) {
binding.image.setImageResource(resId);
}
private void loadImage() {
Drawable cache = cache();
if (cache != null) binding.image.setImageDrawable(cache);
else binding.image.setImageResource(R.drawable.wallpaper_1);
}
private void loadVideo(File file) {
@ -111,19 +107,20 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
ensureVideoView();
video.setPlayer(player);
video.setVisibility(VISIBLE);
binding.image.setImageDrawable(cache);
binding.image.setImageDrawable(cache());
player.setMediaItem(MediaItem.fromUri(Uri.fromFile(file)));
player.prepare();
}
private void loadGif(File file) {
binding.image.setImageDrawable(cache);
binding.image.setImageDrawable(drawable = gif(file));
drawable = gif(file);
if (drawable != null) binding.image.setImageDrawable(drawable);
else loadImage();
}
private void loadImage() {
if (cache != null) binding.image.setImageDrawable(cache);
else binding.image.setImageResource(R.drawable.wallpaper_1);
private Drawable cache() {
File file = FileUtil.getWallCache();
return file.exists() ? Drawable.createFromPath(file.getAbsolutePath()) : null;
}
private GifDrawable gif(File file) {
@ -134,13 +131,24 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
}
}
private void ensurePlayer() {
if (player != null) return;
player = new ExoPlayer.Builder(getContext()).build();
player.setRepeatMode(Player.REPEAT_MODE_ALL);
player.setPlayWhenReady(true);
player.mute();
}
private void ensureVideoView() {
if (video != null) return;
LayoutInflater inflater = LayoutInflater.from(getContext());
video = (PlayerView) inflater.inflate(R.layout.view_wall_video, this, false);
video = (PlayerView) LayoutInflater.from(getContext()).inflate(R.layout.view_wall_video, this, false);
addView(video, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
}
private boolean hasVideo() {
return player != null && video != null && video.getVisibility() == VISIBLE && player.getMediaItemCount() > 0;
}
@Override
public void onCreate(@NonNull LifecycleOwner owner) {
EventBus.getDefault().register(this);
@ -149,7 +157,7 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
@Override
public void onResume(@NonNull LifecycleOwner owner) {
if (drawable != null) drawable.start();
if (player == null || video == null || video.getVisibility() != VISIBLE || player.getMediaItemCount() == 0) return;
if (!hasVideo()) return;
video.setPlayer(player);
player.play();
}
@ -157,7 +165,7 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
@Override
public void onPause(@NonNull LifecycleOwner owner) {
if (drawable != null) drawable.pause();
if (player == null || video == null || video.getVisibility() != VISIBLE || player.getMediaItemCount() == 0) return;
if (!hasVideo()) return;
video.setPlayer(null);
player.pause();
}
@ -171,7 +179,6 @@ public class CustomWallView extends FrameLayout implements DefaultLifecycleObser
drawable = null;
binding = null;
player = null;
cache = null;
video = null;
}
}

Loading…
Cancel
Save