- js相关bug修复 (by okjack)

- 预览窗口全屏返回后的焦点顺序问题 (by okjack)
pull/83/head^2
okjackcaptain 3 years ago
parent 31f940523f
commit b5e1daf218
  1. 33
      app/src/main/java/com/github/tvbox/osc/js/DrpyMethods.java
  2. 7
      app/src/main/java/com/github/tvbox/osc/ui/activity/DetailActivity.java
  3. 57
      app/src/main/java/com/github/tvbox/osc/util/CharsetUtils.java

@ -3,21 +3,12 @@ package com.github.tvbox.osc.js;
import android.text.TextUtils;
import android.util.Base64;
import android.webkit.JavascriptInterface;
import com.github.tvbox.osc.util.CharsetUtils;
import com.github.tvbox.osc.util.Json;
import com.github.tvbox.osc.util.OkGoHelper;
import com.github.tvbox.osc.util.UnicodeReader;
import com.quickjs.JSArray;
import com.quickjs.JSObject;
import org.apache.commons.io.input.ReaderInputStream;
import org.json.JSONObject;
import org.mozilla.universalchardet.UniversalDetector;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
@ -99,23 +90,13 @@ public class DrpyMethods {
} else if (isBuffer == 2) {
result.set("content", Base64.encodeToString(response.body().bytes(), 0));
} else {
byte[] bytes = response.body().bytes();
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(bytes, 0, bytes.length);
detector.dataEnd();
String encoding = detector.getDetectedCharset();
encoding = encoding == null ? "UTF-8" : encoding;
String content = new String(bytes, encoding);
InputStream is = new ByteArrayInputStream(content.getBytes());
Reader reader = new UnicodeReader(is); //处理有BOM头的utf8
InputStream newInputStream = new ReaderInputStream(reader, Charset.defaultCharset());
ByteArrayOutputStream resultBs = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
for (int l; (l = newInputStream.read(buffer)) != -1; ) {
resultBs.write(buffer, 0, l);
String content = response.body().string();
byte[] bytes = content.getBytes();
Charset charset = CharsetUtils.detect(bytes);
if (!charset.name().toLowerCase().startsWith("utf")) {
content = new String(bytes, charset.name());
}
result.set("content", resultBs.toString("UTF-8"));
result.set("content", content);
}
return result;
} catch (Exception e) {

@ -435,10 +435,17 @@ public class DetailActivity extends BaseActivity {
currentSeriesGroupView.isSelected();
}
});
mGridView.setOnFocusChangeListener((view, b) -> onGridViewFocusChange(view, b));
setLoadSir(llLayout);
}
private void onGridViewFocusChange(View view, boolean hasFocus) {
if (llPlayerFragmentContainerBlock.getVisibility() != View.VISIBLE) return;
llPlayerFragmentContainerBlock.setFocusable(!hasFocus);
}
private void initCheckedSourcesForSearch() {
mCheckSources = SearchHelper.getSourcesForSearch();
}

@ -0,0 +1,57 @@
package com.github.tvbox.osc.util;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.mozilla.universalchardet.UniversalDetector;
/**
* 字符集工具类提供了检测字符集的工具方法
* 首先当然是使用mozilla的开源工具包universalchardet进行字符集检测对于检测失败的使用中文常用字进行再次检测
*/
public class CharsetUtils {
/**
* 中文常用字符集
*/
public static final String[] AVAILABLE_CHINESE_CHARSET_NAMES = new String[] { "GBK", "gb2312", "GB18030", "UTF-8", "Big5" };
/**
* 中文常用字
*/
private static final Pattern CHINESE_COMMON_CHARACTER_PATTERN = Pattern.compile("的|一|是|了|我|不|人|在|他|有|这|个|上|们|来|到|时|大|地|为|子|中|你|说|生|国|年|着|就|那|和|要");
public static Charset detect(byte[] content) {
String charset = universalDetect(content);
if (charset != null && !charset.isEmpty()) {
return Charset.forName(charset);
}
int longestMatch = 0;
for (String cs : AVAILABLE_CHINESE_CHARSET_NAMES) {
String temp = new String(content, Charset.forName(cs));
Matcher matcher = CHINESE_COMMON_CHARACTER_PATTERN.matcher(temp);
int count = 0;
while (matcher.find()) {
count += 1;
}
if (count > longestMatch) {
longestMatch = count;
charset = cs;
}
}
return charset == null ? Charset.forName("GB18030") : Charset.forName(charset);
}
/**
* 使用mozilla的开源工具包universalchardet进行字符集检测不一定能完全检测中文字符集
*/
public static String universalDetect(byte[] content) {
UniversalDetector detector = new UniversalDetector(null);
detector.handleData(content, 0, content.length);
detector.dataEnd();
return detector.getDetectedCharset();
}
}
Loading…
Cancel
Save