优化t4 extend;

尝试兼容站点style配置 {"ratio": 1.3330,"type": "rect"};
main
jun 12 months ago
parent c205064848
commit d2fd1892ce
  1. 7
      app/src/main/java/com/github/tvbox/osc/api/ApiConfig.java
  2. 6
      app/src/main/java/com/github/tvbox/osc/bean/SourceBean.java
  3. 62
      app/src/main/java/com/github/tvbox/osc/ui/adapter/GridAdapter.java
  4. 27
      app/src/main/java/com/github/tvbox/osc/ui/fragment/GridFragment.java
  5. 5
      app/src/main/java/com/github/tvbox/osc/util/DefaultConfig.java
  6. 76
      app/src/main/java/com/github/tvbox/osc/viewmodel/SourceViewModel.java
  7. 3
      gradle.properties

@ -447,15 +447,12 @@ public class ApiConfig {
sb.setFilterable(DefaultConfig.safeJsonInt(obj, "filterable", 1));
}
sb.setPlayerUrl(DefaultConfig.safeJsonString(obj, "playUrl", ""));
if(obj.has("ext") && (obj.get("ext").isJsonObject() || obj.get("ext").isJsonArray())){
sb.setExt(obj.get("ext").toString());
}else {
sb.setExt(DefaultConfig.safeJsonString(obj, "ext", ""));
}
sb.setExt(DefaultConfig.safeJsonString(obj, "ext", ""));
sb.setJar(DefaultConfig.safeJsonString(obj, "jar", ""));
sb.setPlayerType(DefaultConfig.safeJsonInt(obj, "playerType", -1));
sb.setCategories(DefaultConfig.safeJsonStringList(obj, "categories"));
sb.setClickSelector(DefaultConfig.safeJsonString(obj, "click", ""));
sb.setStyle(DefaultConfig.safeJsonString(obj, "style", ""));
if (firstSite == null && sb.getFilterable()==1)
firstSite = sb;
sourceBeanList.put(siteKey, sb);

@ -16,6 +16,7 @@ public class SourceBean {
private ArrayList<String> categories = null; // 分类&排序
private int playerType; // 0 system 1 ikj 2 exo 10 mxplayer -1 以参数设置页面的为准
private String clickSelector; // 需要点击播放的嗅探站点selector ddrk.me;#id
private String style; // 展示风格
public String getKey() {
return key;
@ -112,4 +113,9 @@ public class SourceBean {
public String getClickSelector() { return clickSelector; }
public void setClickSelector(String clickSelector) { this.clickSelector = clickSelector; }
public String getStyle() { return style; }
public void setStyle(String style) { this.style = style; }
}

@ -2,6 +2,7 @@ package com.github.tvbox.osc.ui.adapter;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
@ -20,16 +21,35 @@ import java.util.ArrayList;
import me.jessyan.autosize.utils.AutoSizeUtils;
/**
* @author pj567
* @date :2020/12/21
* @description:
* GridAdapter 支持传入 style 来设置图片的宽高比例
* 如果不传 style 则保留旧的默认风格XML item_grid.xml 定义的尺寸
*/
public class GridAdapter extends BaseQuickAdapter<Movie.Video, BaseViewHolder> {
private boolean mShowList = false;
private boolean mShowList ;
private final int defaultWidth = 340;
private final Style style; // 动态风格,传入时调整图片宽高比
public GridAdapter(boolean l) {
super( l ? R.layout.item_list:R.layout.item_grid, new ArrayList<>());
this.mShowList = l;
/**
* style 数据结构ratio 指定宽高比 / type 表示风格例如 rectlist
*/
public static class Style {
public float ratio;
public String type;
public Style(float ratio, String type) {
this.ratio = ratio;
this.type = type;
}
}
/**
* 如果 style null则采用 item_grid.xml 中的默认尺寸
*/
public GridAdapter(boolean showList, Style style) {
super( showList ? R.layout.item_list:R.layout.item_grid, new ArrayList<>());
this.mShowList = showList;
if(style.type.equals("list"))this.mShowList=true;
this.style = style;
}
@Override
@ -95,6 +115,14 @@ public class GridAdapter extends BaseQuickAdapter<Movie.Video, BaseViewHolder> {
helper.setText(R.id.tvName, item.name);
helper.setText(R.id.tvActor, item.actor);
ImageView ivThumb = helper.getView(R.id.ivThumb);
int newWidth = 240;
int newHeight = 336;
if(style!=null){
newWidth = defaultWidth;
newHeight = (int)(newWidth / style.ratio);
}
//由于部分电视机使用glide报错
if (!TextUtils.isEmpty(item.pic)) {
item.pic=item.pic.trim();
@ -106,7 +134,7 @@ public class GridAdapter extends BaseQuickAdapter<Movie.Video, BaseViewHolder> {
.load(DefaultConfig.checkReplaceProxy(item.pic))
.transform(new RoundTransformation(MD5.string2MD5(item.pic))
.centerCorp(true)
.override(AutoSizeUtils.mm2px(mContext, 240), AutoSizeUtils.mm2px(mContext, 336))
.override(AutoSizeUtils.mm2px(mContext,newWidth), AutoSizeUtils.mm2px(mContext,newHeight))
.roundRadius(AutoSizeUtils.mm2px(mContext, 10), RoundTransformation.RoundType.ALL))
.placeholder(R.drawable.img_loading_placeholder)
.noFade()
@ -117,5 +145,21 @@ public class GridAdapter extends BaseQuickAdapter<Movie.Video, BaseViewHolder> {
// ivThumb.setImageResource(R.drawable.img_loading_placeholder);
ivThumb.setImageDrawable(ImgUtil.createTextDrawable(item.name));
}
applyStyleToImage(ivThumb);//动态设置宽高
}
/**
* 根据传入的 style 动态设置 ImageView 的高度高度 = 宽度 / ratio
*/
private void applyStyleToImage(final ImageView ivThumb) {
ViewGroup container = (ViewGroup) ivThumb.getParent();
int width = defaultWidth;
if(style!=null){
int height = (int) (width / style.ratio);
ViewGroup.LayoutParams containerParams = container.getLayoutParams();
containerParams.height = AutoSizeUtils.mm2px(mContext, height); // 高度
containerParams.width = AutoSizeUtils.mm2px(mContext, width); // 宽度
container.setLayoutParams(containerParams);
}
}
}
}

@ -44,6 +44,8 @@ import android.widget.TextView;
import android.widget.Toast;
import org.greenrobot.eventbus.EventBus;
import org.json.JSONException;
import org.json.JSONObject;
/**
* @author pj567
@ -61,7 +63,9 @@ public class GridFragment extends BaseLazyFragment {
private boolean isLoad = false;
private boolean isTop = true;
private View focusedView = null;
private class GridInfo{
private String bStyle="";
private static class GridInfo{
public String sortID="";
public TvRecyclerView mGridView;
public GridAdapter gridAdapter;
@ -88,6 +92,7 @@ public class GridFragment extends BaseLazyFragment {
@Override
protected void init() {
bStyle=ApiConfig.get().getHomeSourceBean().getStyle();
initView();
initViewModel();
initData();
@ -95,7 +100,7 @@ public class GridFragment extends BaseLazyFragment {
private void changeView(String id,Boolean isFolder){
if(isFolder){
this.sortData.flag ="1"; // 修改sortData.flag
this.sortData.flag =bStyle.isEmpty()?"1":"2"; // 修改sortData.flag
}else {
this.sortData.flag ="2"; // 修改sortData.flag
}
@ -143,7 +148,7 @@ public class GridFragment extends BaseLazyFragment {
return true;
}
// 更改当前页面
private void createView(){
private void createView() {
this.saveCurrentView(); // 保存当前页面
if(mGridView == null){ // 从layout中拿view
mGridView = findViewById(R.id.mGridView);
@ -159,7 +164,19 @@ public class GridFragment extends BaseLazyFragment {
mGridView.setVisibility(View.VISIBLE);
}
mGridView.setHasFixedSize(true);
gridAdapter = new GridAdapter(isFolederMode());
GridAdapter.Style style = null;
if(!bStyle.isEmpty()){
try {
JSONObject jsonObject = new JSONObject(bStyle);
float ratio = (float) jsonObject.getDouble("ratio");
String type = jsonObject.getString("type");
style = new GridAdapter.Style(ratio, type);
}catch (JSONException e){
}
}
gridAdapter = new GridAdapter(isFolederMode(), style);
this.page =1;
this.maxPage =1;
this.isLoad = false;
@ -171,7 +188,7 @@ public class GridFragment extends BaseLazyFragment {
if(isFolederMode()){
mGridView.setLayoutManager(new V7LinearLayoutManager(this.mContext, 1, false));
}else{
mGridView.setLayoutManager(new V7GridLayoutManager(this.mContext, isBaseOnWidth() ? 5 : 6));
mGridView.setLayoutManager(new V7GridLayoutManager(this.mContext, bStyle.isEmpty()?(isBaseOnWidth()?5:6):3));
}
gridAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {

@ -135,8 +135,9 @@ public class DefaultConfig {
public static String safeJsonString(JsonObject obj, String key, String defaultVal) {
try {
if (obj.has(key))
return obj.getAsJsonPrimitive(key).getAsString().trim();
if (obj.has(key)){
return obj.get(key).isJsonObject() || obj.get(key).isJsonArray()?obj.get(key).toString().trim():obj.getAsJsonPrimitive(key).getAsString().trim();
}
else
return defaultVal;
} catch (Throwable th) {

@ -24,7 +24,9 @@ import com.github.tvbox.osc.util.DefaultConfig;
import com.github.tvbox.osc.util.FileUtils;
import com.github.tvbox.osc.util.HawkConfig;
import com.github.tvbox.osc.util.LOG;
import com.github.tvbox.osc.util.MD5;
import com.github.tvbox.osc.util.thunder.Thunder;
import com.github.tvbox.osc.util.urlhttp.OkHttpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
@ -54,6 +56,7 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
@ -76,6 +79,7 @@ public class SourceViewModel extends ViewModel {
public MutableLiveData<AbsXml> quickSearchResult;
public MutableLiveData<AbsXml> detailResult;
public MutableLiveData<JSONObject> playResult;
public Gson gson;
public SourceViewModel() {
sortResult = new MutableLiveData<>();
@ -84,6 +88,7 @@ public class SourceViewModel extends ViewModel {
quickSearchResult = new MutableLiveData<>();
detailResult = new MutableLiveData<>();
playResult = new MutableLiveData<>();
gson=new Gson();
}
public static final ExecutorService spThreadPool = Executors.newSingleThreadExecutor();
@ -91,7 +96,7 @@ public class SourceViewModel extends ViewModel {
//homeContent缓存,最多存储10个sourceKey的AbsSortXml对象
private static final Map<String, AbsSortXml> sortCache = new LinkedHashMap<String, AbsSortXml>(10, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, AbsSortXml> eldest) {
protected boolean removeEldestEntry(Entry<String, AbsSortXml> eldest) {
return size() > 10;
}
};
@ -375,7 +380,6 @@ public class SourceViewModel extends ViewModel {
String ext= "";
String extend=homeSourceBean.getExt();
extend=getFixUrl(extend);
if(URLEncoder.encode(extend).length()>1000)extend="";
if (sortData.filterSelect != null && sortData.filterSelect.size() > 0) {
try {
String selectExt = new JSONObject(sortData.filterSelect).toString();
@ -569,7 +573,6 @@ public class SourceViewModel extends ViewModel {
} else if (type == 0 || type == 1|| type == 4) {
String extend=sourceBean.getExt();
extend=getFixUrl(extend);
if(URLEncoder.encode(extend).length()>1000)extend="";
OkGo.<String>get(sourceBean.getApi())
.tag("detail")
.params("ac", type == 0 ? "videolist" : "detail")
@ -661,7 +664,6 @@ public class SourceViewModel extends ViewModel {
}else if (type == 4) {
String extend=sourceBean.getExt();
extend=getFixUrl(extend);
if(URLEncoder.encode(extend).length()>1000)extend="";
OkGo.<String>get(sourceBean.getApi())
.params("wd", wd)
.params("ac" ,"detail")
@ -743,7 +745,6 @@ public class SourceViewModel extends ViewModel {
}else if (type == 4) {
String extend=sourceBean.getExt();
extend=getFixUrl(extend);
if(URLEncoder.encode(extend).length()>1000)extend="";
OkGo.<String>get(sourceBean.getApi())
.params("wd", wd)
.params("ac" ,"detail")
@ -854,7 +855,6 @@ public class SourceViewModel extends ViewModel {
} else if (type == 4) {
String extend=sourceBean.getExt();
extend=getFixUrl(extend);
if(URLEncoder.encode(extend).length()>1000)extend="";
OkGo.<String>get(sourceBean.getApi())
.params("play", url)
.params("flag" ,playFlag)
@ -899,13 +899,57 @@ public class SourceViewModel extends ViewModel {
}
}
private String getFixUrl(String content){
if (content.startsWith("http://127.0.0.1")) {
String path = content.replaceAll("^http.+/file/", FileUtils.getRootPath()+"/");
path = path.replaceAll("localhost/", "/");
content = FileUtils.readFileToString(path,"UTF-8");
private static final ConcurrentHashMap<String, String> extendCache = new ConcurrentHashMap<>();
private String getFixUrl(final String extend) {
if(!extend.startsWith("http"))return extend;
final String key = MD5.string2MD5(extend);
if (extendCache.containsKey(key)) {
LOG.i("echo-getFixUrl Cache");
return extendCache.get(key);
}
LOG.i("echo-getFixUrl load");
Future<String> future = spThreadPool.submit(new Callable<String>() {
@Override
public String call() {
String result = extend;
if (extend.startsWith("http://127.0.0.1")) {
String path = extend.replaceAll("^http.+/file/", FileUtils.getRootPath() + "/");
path = path.replaceAll("localhost/", "/");
result = FileUtils.readFileToString(path, "UTF-8");
result = tryMinifyJson(result);
extendCache.putIfAbsent(key, result);
} else if (extend.startsWith("http")) {
result = OkHttpUtil.string(extend, null);
if (!result.isEmpty()) {
result = tryMinifyJson(result);
extendCache.putIfAbsent(key, result);
}
}
return result;
}
});
try {
return future.get(5, TimeUnit.SECONDS);
} catch (TimeoutException te) {
te.printStackTrace();
future.cancel(true);
return extend;
} catch (Exception e) {
e.printStackTrace();
return extend;
}
}
private String tryMinifyJson(String raw) {
try {
raw = raw.trim();
JsonElement jsonElement = JsonParser.parseString(raw);
return gson.toJson(jsonElement);
} catch (Exception e) {
return raw;
}
return content;
}
private MovieSort.SortFilter getSortFilter(JsonObject obj) {
@ -929,7 +973,7 @@ public class SourceViewModel extends ViewModel {
private AbsSortXml sortJson(MutableLiveData<AbsSortXml> result, String json) {
try {
JsonObject obj = JsonParser.parseString(json).getAsJsonObject();
AbsSortJson sortJson = new Gson().fromJson(obj, new TypeToken<AbsSortJson>() {
AbsSortJson sortJson = gson.fromJson(obj, new TypeToken<AbsSortJson>() {
}.getType());
AbsSortXml data = sortJson.toAbsSortXml();
try {
@ -1073,7 +1117,7 @@ public class SourceViewModel extends ViewModel {
String res = response.body();
if (!TextUtils.isEmpty(res)) {
try {
AbsJson absJson = new Gson().fromJson(res, new TypeToken<AbsJson>() {
AbsJson absJson = gson.fromJson(res, new TypeToken<AbsJson>() {
}.getType());
resData[0] = absJson.toAbsXml();
absXml(resData[0], sb.getKey());
@ -1099,7 +1143,7 @@ public class SourceViewModel extends ViewModel {
String res = sp.detailContent(ids);
if (!TextUtils.isEmpty(res)) {
try {
AbsJson absJson = new Gson().fromJson(res, new TypeToken<AbsJson>() {}.getType());
AbsJson absJson = gson.fromJson(res, new TypeToken<AbsJson>() {}.getType());
resData[0] = absJson.toAbsXml();
absXml(resData[0], sb.getKey());
} catch (Exception e) {
@ -1274,7 +1318,7 @@ public class SourceViewModel extends ViewModel {
// "\t\t\"vod_play_url\": \"0$magnet:?xt=urn:btih:e398ca38fb9d64897ed19b4d16efeea11af4d03b\"\n" +
// "\t}]\n" +
// "}";
AbsJson absJson = new Gson().fromJson(json, new TypeToken<AbsJson>() {
AbsJson absJson = gson.fromJson(json, new TypeToken<AbsJson>() {
}.getType());
AbsXml data = absJson.toAbsXml();
absXml(data, sourceKey);

@ -17,4 +17,5 @@ android.useAndroidX=true
# Automatically convert third-party libraries to use AndroidX
android.enableJetifier=true
IsDebug=true
org.gradle.jvmargs=-Xmx2048m --add-opens java.base/java.io=ALL-UNNAMED
#????
#org.gradle.jvmargs=-Xmx2048m --add-opens java.base/java.io=ALL-UNNAMED

Loading…
Cancel
Save