123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- package com.github.tvbox.osc.util;
- import android.content.Context;
- import android.content.pm.PackageInfo;
- import android.content.pm.PackageManager;
- import android.net.Uri;
- import android.text.TextUtils;
- import com.github.tvbox.osc.api.ApiConfig;
- import com.github.tvbox.osc.bean.MovieSort;
- import com.github.tvbox.osc.bean.SourceBean;
- import com.github.tvbox.osc.server.ControlManager;
- import com.google.gson.JsonElement;
- import com.google.gson.JsonObject;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- import java.util.regex.Pattern;
- /**
- * @author pj567
- * @date :2020/12/21
- * @description:
- */
- public class DefaultConfig {
- public static List<MovieSort.SortData> adjustSort(String sourceKey, List<MovieSort.SortData> list, boolean withMy) {
- List<MovieSort.SortData> data = new ArrayList<>();
- if (sourceKey != null) {
- SourceBean sb = ApiConfig.get().getSource(sourceKey);
- ArrayList<String> categories = sb.getCategories();
- if (!categories.isEmpty()) {
- for (String cate : categories) {
- for (MovieSort.SortData sortData : list) {
- if (sortData.name.equals(cate)) {
- if (sortData.filters == null)
- sortData.filters = new ArrayList<>();
- data.add(sortData);
- }
- }
- }
- } else {
- for (MovieSort.SortData sortData : list) {
- if (sortData.filters == null)
- sortData.filters = new ArrayList<>();
- data.add(sortData);
- }
- }
- }
- if (withMy)
- data.add(0, new MovieSort.SortData("my0", "主页"));
- Collections.sort(data);
- return data;
- }
- public static int getAppVersionCode(Context mContext) {
- //包管理操作管理类
- PackageManager pm = mContext.getPackageManager();
- try {
- PackageInfo packageInfo = pm.getPackageInfo(mContext.getPackageName(), 0);
- return packageInfo.versionCode;
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
- return -1;
- }
- public static String getAppVersionName(Context mContext) {
- //包管理操作管理类
- PackageManager pm = mContext.getPackageManager();
- try {
- PackageInfo packageInfo = pm.getPackageInfo(mContext.getPackageName(), 0);
- return packageInfo.versionName;
- } catch (PackageManager.NameNotFoundException e) {
- e.printStackTrace();
- }
- return "";
- }
- /**
- * 后缀
- *
- * @param name
- * @return
- */
- public static String getFileSuffix(String name) {
- if (TextUtils.isEmpty(name)) {
- return "";
- }
- int endP = name.lastIndexOf(".");
- return endP > -1 ? name.substring(endP) : "";
- }
- /**
- * 获取文件的前缀
- *
- * @param fileName
- * @return
- */
- public static String getFilePrefixName(String fileName) {
- if (TextUtils.isEmpty(fileName)) {
- return "";
- }
- int start = fileName.lastIndexOf(".");
- return start > -1 ? fileName.substring(0, start) : fileName;
- }
- private static final Pattern snifferMatch = Pattern.compile(
- "http((?!http).){20,}?\\.(m3u8|mp4|mp3|m4a|flv|avi|mkv|rm|wmv|wma|mpg)\\?.*|" +
- "http((?!http).){20,}\\.(m3u8|mp4|mp3|m4a|flv|avi|mkv|rm|wmv|wma|mpg)|" +
- "http((?!http).)*?video/tos*|" +
- "http((?!http).){20,}?/m3u8\\?pt=m3u8.*|" +
- "http((?!http).)*?default\\.ixigua\\.com/.*|" +
- "http((?!http).)*?dycdn-tos\\.pstatp[^\\?]*|" +
- "http.*?/player/m3u8play\\.php\\?url=.*|" +
- "http.*?/player/.*?[pP]lay\\.php\\?url=.*|" +
- "http.*?/playlist/m3u8/\\?vid=.*|" +
- "http.*?\\.php\\?type=m3u8&.*|" +
- "http.*?/download.aspx\\?.*|" +
- "http.*?/api/up_api.php\\?.*|" +
- "https.*?\\.66yk\\.cn.*|" +
- "http((?!http).)*?netease\\.com/file/.*"
- );
- public static boolean isVideoFormat(String url) {
- if (url.contains("=http") || url.contains(".html")) {
- return false;
- }
- Uri uri = Uri.parse(url);
- String path = uri.getPath();
- if (TextUtils.isEmpty(path)) {
- return false;
- }
- if (path.endsWith(".js") || path.endsWith(".css") || path.endsWith(".html")) {
- return false;
- }
- if (snifferMatch.matcher(url).find()) return true;
- return false;
- }
- public static String safeJsonString(JsonObject obj, String key, String defaultVal) {
- try {
- if (obj.has(key))
- return obj.getAsJsonPrimitive(key).getAsString().trim();
- else
- return defaultVal;
- } catch (Throwable th) {
- }
- return defaultVal;
- }
- public static int safeJsonInt(JsonObject obj, String key, int defaultVal) {
- try {
- if (obj.has(key))
- return obj.getAsJsonPrimitive(key).getAsInt();
- else
- return defaultVal;
- } catch (Throwable th) {
- }
- return defaultVal;
- }
- public static ArrayList<String> safeJsonStringList(JsonObject obj, String key) {
- ArrayList<String> result = new ArrayList<>();
- try {
- if (obj.has(key)) {
- if (obj.get(key).isJsonObject()) {
- result.add(obj.get(key).getAsString());
- } else {
- for (JsonElement opt : obj.getAsJsonArray(key)) {
- result.add(opt.getAsString());
- }
- }
- }
- } catch (Throwable th) {
- }
- return result;
- }
- public static String checkReplaceProxy(String urlOri) {
- if (urlOri.startsWith("proxy://"))
- return urlOri.replace("proxy://", ControlManager.get().getAddress(true) + "proxy?");
- return urlOri;
- }
- }
|