157 lines
5.3 KiB
Java
157 lines
5.3 KiB
Java
package ru.vtosters.lite.utils;
|
|
|
|
import static android.text.TextUtils.isEmpty;
|
|
import static ru.vtosters.lite.utils.AndroidUtils.getGlobalContext;
|
|
import static ru.vtosters.lite.utils.Preferences.getBoolValue;
|
|
import static ru.vtosters.lite.utils.Preferences.isEnableExternalOpening;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.app.Activity;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.pm.ApplicationInfo;
|
|
import android.net.Uri;
|
|
import android.os.Build;
|
|
|
|
import com.vk.core.dialogs.alert.VkAlertDialog;
|
|
import com.vk.dto.common.VideoFile;
|
|
|
|
import java.util.Arrays;
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
public class ExternalLinkParser {
|
|
private static final String TAG = "ExternalLinkHandler";
|
|
|
|
private static final List<String> filters = Arrays.asList(
|
|
"youtube.com",
|
|
"www.youtube.com",
|
|
"youtu.be",
|
|
"m.youtube.com"
|
|
);
|
|
private static final Map<String, String> qualities = new HashMap<>();
|
|
|
|
public static boolean parseVideoFile(VideoFile file) {
|
|
return parseVideoFile(file, getGlobalContext(), isEnableExternalOpening());
|
|
}
|
|
|
|
public static boolean parseVideoFile(VideoFile file, Context context) {
|
|
return parseVideoFile(file, context, isEnableExternalOpening());
|
|
}
|
|
|
|
public static boolean parseVideoFile(VideoFile file, Activity activity) {
|
|
return parseVideoFile(file, activity, isEnableExternalOpening());
|
|
}
|
|
|
|
public static boolean parseVideoFile(VideoFile file, Context context, Boolean isEnabled) {
|
|
if (checkYoutubeLink(file)) {
|
|
try {
|
|
Intent intent = new Intent(Intent.ACTION_VIEW);
|
|
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
intent.setData(Uri.parse(file.G));
|
|
context.startActivity(intent);
|
|
return true;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
if (!isEnabled || file.isEmpty()) return false;
|
|
|
|
if (checkVkVideo(file)) {
|
|
if (qualities.size() == 1 || getBoolValue("maxquality", false)) {
|
|
startExternalVideo((String) qualities.values().toArray()[0], context);
|
|
} else {
|
|
String[] titles = qualities.keySet().toArray(new String[0]);
|
|
new VkAlertDialog.Builder(context)
|
|
.setItems(titles, ((dialog, which) -> startExternalVideo(qualities.get(titles[which]), context)))
|
|
.show();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private static boolean checkYoutubeLink(VideoFile file) {
|
|
if (!isEmpty(file.G)) {
|
|
for (String filter : filters) {
|
|
if (file.G.contains(filter))
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* this.e = var66.optString("mp4_240", var66.optString("src"));
|
|
* this.f = var66.optString("mp4_360");
|
|
* this.g = var66.optString("mp4_480");
|
|
* this.h = var66.optString("mp4_720");
|
|
* this.B = var66.optString("mp4_1080");
|
|
* this.C = var66.optString("mp4_1440");
|
|
* this.D = var66.optString("mp4_2160");
|
|
*/
|
|
public static boolean checkVkVideo(VideoFile videoFile) {
|
|
if (!isEmpty(videoFile.e)) {
|
|
qualities.put("240", videoFile.e);
|
|
}
|
|
if (!isEmpty(videoFile.f)) {
|
|
qualities.put("360", videoFile.f);
|
|
}
|
|
if (!isEmpty(videoFile.g)) {
|
|
qualities.put("480", videoFile.g);
|
|
}
|
|
if (!isEmpty(videoFile.h)) {
|
|
qualities.put("720", videoFile.h);
|
|
}
|
|
if (!isEmpty(videoFile.B)) {
|
|
qualities.put("1080", videoFile.B);
|
|
}
|
|
if (!isEmpty(videoFile.C)) {
|
|
qualities.put("1440", videoFile.C);
|
|
}
|
|
if (!isEmpty(videoFile.D)) {
|
|
qualities.put("2160", videoFile.D);
|
|
}
|
|
|
|
return qualities.size() > 0;
|
|
}
|
|
|
|
public static boolean startExternalVideo(String url, Context ctx) {
|
|
try {
|
|
qualities.clear();
|
|
|
|
Uri uri = Uri.parse(url);
|
|
Intent intent = new Intent();
|
|
|
|
String packageName = getMXPlayerPackageName();
|
|
if (!isEmpty(packageName)) {
|
|
intent.setPackage(packageName);
|
|
intent.setDataAndType(uri, "application/mp4");
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
} else {
|
|
intent.setAction(Intent.ACTION_VIEW);
|
|
intent.setDataAndType(uri, "video/mp4");
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
|
|
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
|
}
|
|
ctx.startActivity(intent);
|
|
return true;
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@SuppressLint("QueryPermissionsNeeded")
|
|
private static String getMXPlayerPackageName() {
|
|
for (ApplicationInfo info : getGlobalContext().getPackageManager().getInstalledApplications(0)) {
|
|
String packName = info.packageName;
|
|
if (packName.equals("com.mxtech.videoplayer.ad") || packName.equals("com.mxtech.videoplayer.pro"))
|
|
return packName;
|
|
}
|
|
return null;
|
|
}
|
|
}
|