2020-04-15 17:51:18 +02:00
|
|
|
package app.fedilab.nitterizeme.helpers;
|
2020-02-17 11:57:14 +01:00
|
|
|
/* Copyright 2020 Thomas Schneider
|
|
|
|
*
|
2020-04-10 17:25:19 +02:00
|
|
|
* This file is a part of UntrackMe
|
2020-02-17 11:57:14 +01:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
2020-04-10 17:25:19 +02:00
|
|
|
* UntrackMe is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
2020-02-17 11:57:14 +01:00
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
2020-04-10 17:25:19 +02:00
|
|
|
* You should have received a copy of the GNU General Public License along with UntrackMe; if not,
|
2020-02-17 11:57:14 +01:00
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2020-02-18 12:09:18 +01:00
|
|
|
|
2020-03-29 12:17:10 +02:00
|
|
|
|
2020-04-05 12:40:43 +02:00
|
|
|
import android.app.DownloadManager;
|
|
|
|
import android.content.Context;
|
2020-04-16 17:56:32 +02:00
|
|
|
import android.content.SharedPreferences;
|
2020-04-19 17:53:30 +02:00
|
|
|
import android.content.pm.PackageInfo;
|
|
|
|
import android.content.pm.PackageManager;
|
2020-04-05 12:40:43 +02:00
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.Environment;
|
|
|
|
|
2020-02-17 11:57:14 +01:00
|
|
|
import java.io.IOException;
|
2020-04-16 17:56:32 +02:00
|
|
|
import java.io.UnsupportedEncodingException;
|
2020-02-20 11:20:30 +01:00
|
|
|
import java.net.InetAddress;
|
2020-04-16 17:56:32 +02:00
|
|
|
import java.net.MalformedURLException;
|
2020-02-17 11:57:14 +01:00
|
|
|
import java.net.URL;
|
2020-04-16 17:56:32 +02:00
|
|
|
import java.net.URLEncoder;
|
2020-04-05 12:40:43 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
2020-02-18 12:09:18 +01:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Arrays;
|
2020-04-05 12:40:43 +02:00
|
|
|
import java.util.Date;
|
2020-04-19 19:41:18 +02:00
|
|
|
import java.util.HashSet;
|
2020-02-17 11:57:14 +01:00
|
|
|
import java.util.List;
|
2020-04-05 12:40:43 +02:00
|
|
|
import java.util.Locale;
|
2020-02-17 11:57:14 +01:00
|
|
|
import java.util.Map;
|
2020-04-16 17:56:32 +02:00
|
|
|
import java.util.Objects;
|
2020-04-19 19:41:18 +02:00
|
|
|
import java.util.Set;
|
2020-02-17 11:57:14 +01:00
|
|
|
import java.util.regex.Matcher;
|
|
|
|
import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
import javax.net.ssl.HttpsURLConnection;
|
|
|
|
|
2020-04-16 17:56:32 +02:00
|
|
|
import app.fedilab.nitterizeme.activities.MainActivity;
|
|
|
|
|
2020-04-05 12:40:43 +02:00
|
|
|
import static android.content.Context.DOWNLOAD_SERVICE;
|
2020-04-16 17:56:32 +02:00
|
|
|
import static app.fedilab.nitterizeme.activities.CheckAppActivity.instagram_domains;
|
2020-04-15 18:19:19 +02:00
|
|
|
import static app.fedilab.nitterizeme.activities.CheckAppActivity.shortener_domains;
|
2020-04-16 17:56:32 +02:00
|
|
|
import static app.fedilab.nitterizeme.activities.CheckAppActivity.twitter_domains;
|
|
|
|
import static app.fedilab.nitterizeme.activities.CheckAppActivity.youtube_domains;
|
|
|
|
import static app.fedilab.nitterizeme.activities.MainActivity.SET_BIBLIOGRAM_ENABLED;
|
|
|
|
import static app.fedilab.nitterizeme.activities.MainActivity.SET_INVIDIOUS_ENABLED;
|
|
|
|
import static app.fedilab.nitterizeme.activities.MainActivity.SET_NITTER_ENABLED;
|
2020-02-18 12:09:18 +01:00
|
|
|
|
2020-04-15 17:51:18 +02:00
|
|
|
public class Utils {
|
2020-02-17 11:57:14 +01:00
|
|
|
|
2020-04-16 18:05:39 +02:00
|
|
|
public static final String KILL_ACTIVITY = "kill_activity";
|
2020-04-19 17:31:07 +02:00
|
|
|
public static final String URL_APP_PICKER = "url_app_picker";
|
2020-04-16 17:56:32 +02:00
|
|
|
public static final Pattern youtubePattern = Pattern.compile("(www\\.|m\\.)?(youtube\\.com|youtu\\.be|youtube-nocookie\\.com)/(((?!([\"'<])).)*)");
|
|
|
|
public static final Pattern nitterPattern = Pattern.compile("(mobile\\.|www\\.)?twitter.com([\\w-/]+)");
|
|
|
|
public static final Pattern bibliogramPostPattern = Pattern.compile("(m\\.|www\\.)?instagram.com(/p/[\\w-/]+)");
|
|
|
|
public static final Pattern bibliogramAccountPattern = Pattern.compile("(m\\.|www\\.)?instagram.com(((?!/p/).)+)");
|
|
|
|
public static final Pattern maps = Pattern.compile("/maps/place/[^@]+@([\\d.,z]{3,}).*");
|
|
|
|
public static final Pattern ampExtract = Pattern.compile("amp/s/(.*)");
|
2020-04-15 17:51:18 +02:00
|
|
|
public static final String RECEIVE_STREAMING_URL = "receive_streaming_url";
|
2020-04-16 17:56:32 +02:00
|
|
|
private static final Pattern extractPlace = Pattern.compile("/maps/place/(((?!/data).)*)");
|
2020-03-29 12:17:10 +02:00
|
|
|
private static final String[] UTM_PARAMS = {
|
|
|
|
"utm_\\w+",
|
|
|
|
"ga_source",
|
|
|
|
"ga_medium",
|
|
|
|
"ga_term",
|
|
|
|
"ga_content",
|
|
|
|
"ga_campaign",
|
|
|
|
"ga_place",
|
|
|
|
"yclid",
|
|
|
|
"_openstat",
|
|
|
|
"fb_action_ids",
|
|
|
|
"fb_action_types",
|
|
|
|
"fb_source",
|
|
|
|
"fb_ref",
|
|
|
|
"fbclid",
|
|
|
|
"action_object_map",
|
|
|
|
"action_type_map",
|
|
|
|
"action_ref_map",
|
|
|
|
"gs_l",
|
|
|
|
"mkt_tok",
|
|
|
|
"hmb_campaign",
|
|
|
|
"hmb_medium",
|
|
|
|
"hmb_source",
|
2020-04-04 09:53:49 +02:00
|
|
|
"[\\?|&]ref[\\_]?",
|
2020-04-04 10:07:51 +02:00
|
|
|
"amp[_#\\w]+",
|
|
|
|
"[\\w]+"
|
2020-02-17 11:57:14 +01:00
|
|
|
|
2020-03-29 12:17:10 +02:00
|
|
|
};
|
2020-04-04 11:58:21 +02:00
|
|
|
private static String urlRegex = "(?i)\\b((?:[a-z][\\w-]+:(?:/{1,3}|[a-z0-9%])|www\\d{0,3}[.]|[a-z0-9.\\-]+[.][a-z]{2,10}/)(?:[^\\s()<>]+|\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\))+(?:\\(([^\\s()<>]+|(\\([^\\s()<>]+\\)))*\\)|[^\\s`!()\\[\\]{};:'\".,<>?«»“”‘’]))";
|
2020-04-15 17:51:18 +02:00
|
|
|
public static final Pattern urlPattern = Pattern.compile(
|
2020-04-04 11:58:21 +02:00
|
|
|
urlRegex,
|
|
|
|
Pattern.CASE_INSENSITIVE | Pattern.MULTILINE | Pattern.DOTALL);
|
2020-02-18 11:26:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the unshortened URL
|
2020-02-18 12:25:20 +01:00
|
|
|
*
|
|
|
|
* @param urls ArrayList<String> URL to check
|
2020-02-18 11:26:29 +01:00
|
|
|
*/
|
2020-04-16 17:56:32 +02:00
|
|
|
public static void checkUrl(Context context, ArrayList<String> urls) {
|
2020-02-17 11:57:14 +01:00
|
|
|
URL url;
|
2020-02-18 12:09:18 +01:00
|
|
|
String newURL = null;
|
|
|
|
String comingURl;
|
2020-02-17 11:57:14 +01:00
|
|
|
try {
|
2020-02-18 12:25:20 +01:00
|
|
|
comingURl = urls.get(urls.size() - 1);
|
2020-02-18 12:09:18 +01:00
|
|
|
|
2020-02-18 12:25:20 +01:00
|
|
|
if (comingURl.startsWith("http://")) {
|
2020-02-18 12:09:18 +01:00
|
|
|
comingURl = comingURl.replace("http://", "https://");
|
|
|
|
}
|
|
|
|
url = new URL(comingURl);
|
2020-02-17 11:57:14 +01:00
|
|
|
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
|
|
|
httpsURLConnection.setRequestProperty("http.keepAlive", "false");
|
|
|
|
httpsURLConnection.setInstanceFollowRedirects(false);
|
|
|
|
httpsURLConnection.setRequestMethod("HEAD");
|
2020-02-18 12:25:20 +01:00
|
|
|
if (httpsURLConnection.getResponseCode() == 301) {
|
2020-02-17 11:57:14 +01:00
|
|
|
Map<String, List<String>> map = httpsURLConnection.getHeaderFields();
|
|
|
|
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
|
|
|
|
if (entry.toString().toLowerCase().startsWith("location")) {
|
|
|
|
Matcher matcher = urlPattern.matcher(entry.toString());
|
|
|
|
if (matcher.find()) {
|
2020-03-29 12:17:10 +02:00
|
|
|
newURL = remove_tracking_param(matcher.group(1));
|
2020-04-16 17:56:32 +02:00
|
|
|
urls.add(transformUrl(context, newURL));
|
2020-02-17 11:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
httpsURLConnection.getInputStream().close();
|
2020-02-18 12:25:20 +01:00
|
|
|
if (newURL != null && newURL.compareTo(comingURl) != 0) {
|
2020-02-18 12:09:18 +01:00
|
|
|
URL redirectURL = new URL(newURL);
|
2020-02-17 11:57:14 +01:00
|
|
|
String host = redirectURL.getHost();
|
|
|
|
String protocol = redirectURL.getProtocol();
|
2020-02-18 12:25:20 +01:00
|
|
|
if (protocol != null && host != null) {
|
|
|
|
if (Arrays.asList(shortener_domains).contains(host)) {
|
2020-04-16 17:56:32 +02:00
|
|
|
checkUrl(context, urls);
|
2020-02-18 12:09:18 +01:00
|
|
|
}
|
2020-02-17 11:57:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (IOException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2020-02-20 11:20:30 +01:00
|
|
|
|
2020-04-16 17:56:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transform the URL to a Nitter, Invidious or OSM ones
|
|
|
|
*
|
|
|
|
* @param url String original URL
|
|
|
|
* @return String transformed URL
|
|
|
|
*/
|
|
|
|
public static String transformUrl(Context context, String url) {
|
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(MainActivity.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String newUrl = null;
|
|
|
|
URL url_;
|
|
|
|
String host = null;
|
|
|
|
try {
|
|
|
|
url_ = new URL(url);
|
|
|
|
host = url_.getHost();
|
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
if (Arrays.asList(twitter_domains).contains(host)) {
|
|
|
|
boolean nitter_enabled = sharedpreferences.getBoolean(SET_NITTER_ENABLED, true);
|
|
|
|
if (nitter_enabled) {
|
|
|
|
String nitterHost = sharedpreferences.getString(MainActivity.SET_NITTER_HOST, MainActivity.DEFAULT_NITTER_HOST).toLowerCase();
|
|
|
|
assert host != null;
|
|
|
|
if (host.compareTo("pbs.twimg.com") == 0 || host.compareTo("pic.twitter.com") == 0) {
|
|
|
|
try {
|
|
|
|
newUrl = "https://" + nitterHost + "/pic/" + URLEncoder.encode(url, "utf-8");
|
|
|
|
} catch (UnsupportedEncodingException e) {
|
|
|
|
newUrl = "https://" + nitterHost + "/pic/" + url;
|
|
|
|
}
|
|
|
|
} else if (url.contains("/search?")) {
|
|
|
|
newUrl = url.replace(host, nitterHost);
|
|
|
|
} else {
|
|
|
|
Matcher matcher = nitterPattern.matcher(url);
|
|
|
|
while (matcher.find()) {
|
|
|
|
final String nitter_directory = matcher.group(2);
|
|
|
|
newUrl = "https://" + nitterHost + nitter_directory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newUrl;
|
|
|
|
} else {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
} else if (Arrays.asList(instagram_domains).contains(host)) {
|
|
|
|
boolean bibliogram_enabled = sharedpreferences.getBoolean(SET_BIBLIOGRAM_ENABLED, true);
|
|
|
|
if (bibliogram_enabled) {
|
|
|
|
Matcher matcher = bibliogramPostPattern.matcher(url);
|
|
|
|
while (matcher.find()) {
|
|
|
|
final String bibliogram_directory = matcher.group(2);
|
|
|
|
String bibliogramHost = sharedpreferences.getString(MainActivity.SET_BIBLIOGRAM_HOST, MainActivity.DEFAULT_BIBLIOGRAM_HOST).toLowerCase();
|
|
|
|
newUrl = "https://" + bibliogramHost + bibliogram_directory;
|
|
|
|
}
|
|
|
|
matcher = bibliogramAccountPattern.matcher(url);
|
|
|
|
while (matcher.find()) {
|
|
|
|
final String bibliogram_directory = matcher.group(2);
|
|
|
|
String bibliogramHost = sharedpreferences.getString(MainActivity.SET_BIBLIOGRAM_HOST, MainActivity.DEFAULT_BIBLIOGRAM_HOST).toLowerCase();
|
|
|
|
if (bibliogram_directory != null && bibliogram_directory.compareTo("privacy") != 0) {
|
|
|
|
newUrl = "https://" + bibliogramHost + "/u" + bibliogram_directory;
|
|
|
|
} else {
|
|
|
|
newUrl = "https://" + bibliogramHost + bibliogram_directory;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newUrl;
|
|
|
|
} else {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
} else if (url.contains("/maps/place")) {
|
|
|
|
boolean osm_enabled = sharedpreferences.getBoolean(MainActivity.SET_OSM_ENABLED, true);
|
|
|
|
if (osm_enabled) {
|
|
|
|
Matcher matcher = maps.matcher(url);
|
|
|
|
while (matcher.find()) {
|
|
|
|
final String localization = matcher.group(1);
|
|
|
|
assert localization != null;
|
|
|
|
String[] data = localization.split(",");
|
|
|
|
if (data.length > 2) {
|
|
|
|
String zoom;
|
|
|
|
String[] details = data[2].split("\\.");
|
|
|
|
if (details.length > 0) {
|
|
|
|
zoom = details[0];
|
|
|
|
} else {
|
|
|
|
zoom = data[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
String osmHost = sharedpreferences.getString(MainActivity.SET_OSM_HOST, MainActivity.DEFAULT_OSM_HOST).toLowerCase();
|
|
|
|
boolean geo_uri_enabled = sharedpreferences.getBoolean(MainActivity.SET_GEO_URIS, false);
|
|
|
|
if (!geo_uri_enabled) {
|
|
|
|
newUrl = "https://" + osmHost + "/#map=" + zoom + "/" + data[0] + "/" + data[1];
|
|
|
|
} else {
|
|
|
|
newUrl = "geo:0,0?q=" + data[0] + "," + data[1] + ",z=" + zoom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (newUrl == null && url.contains("/data=")) {
|
|
|
|
matcher = extractPlace.matcher(url);
|
|
|
|
while (matcher.find()) {
|
|
|
|
final String search = matcher.group(1);
|
|
|
|
newUrl = "geo:0,0?q=" + search;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newUrl;
|
|
|
|
} else {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
} else if (Arrays.asList(youtube_domains).contains(host)) { //Youtube URL
|
|
|
|
boolean invidious_enabled = sharedpreferences.getBoolean(SET_INVIDIOUS_ENABLED, true);
|
|
|
|
if (invidious_enabled) {
|
|
|
|
Matcher matcher = youtubePattern.matcher(url);
|
|
|
|
while (matcher.find()) {
|
|
|
|
final String youtubeId = matcher.group(3);
|
|
|
|
String invidiousHost = sharedpreferences.getString(MainActivity.SET_INVIDIOUS_HOST, MainActivity.DEFAULT_INVIDIOUS_HOST).toLowerCase();
|
|
|
|
if (Objects.requireNonNull(matcher.group(2)).compareTo("youtu.be") == 0) {
|
|
|
|
newUrl = "https://" + invidiousHost + "/watch?v=" + youtubeId + "&local=true";
|
|
|
|
} else {
|
|
|
|
newUrl = "https://" + invidiousHost + "/" + youtubeId + "&local=true";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return newUrl;
|
|
|
|
} else {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2020-02-20 11:20:30 +01:00
|
|
|
/**
|
|
|
|
* Get time for reaching a domain
|
2020-02-20 15:25:41 +01:00
|
|
|
*
|
2020-02-20 11:20:30 +01:00
|
|
|
* @param domain String domain name
|
|
|
|
* @return long delay
|
|
|
|
*/
|
2020-04-15 17:51:18 +02:00
|
|
|
public static long ping(String domain) {
|
2020-02-20 11:20:30 +01:00
|
|
|
long timeDifference = -2;
|
|
|
|
try {
|
|
|
|
long beforeTime = System.currentTimeMillis();
|
|
|
|
//noinspection ResultOfMethodCallIgnored
|
|
|
|
InetAddress.getByName(domain).isReachable(10000);
|
|
|
|
long afterTime = System.currentTimeMillis();
|
|
|
|
timeDifference = afterTime - beforeTime;
|
2020-02-20 15:25:41 +01:00
|
|
|
} catch (IOException ignored) {
|
|
|
|
}
|
2020-02-20 11:20:30 +01:00
|
|
|
return timeDifference;
|
|
|
|
}
|
2020-03-29 12:17:10 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clean URLs from utm parameters
|
|
|
|
*
|
|
|
|
* @param url String URL
|
|
|
|
* @return cleaned URL String
|
|
|
|
*/
|
2020-04-18 17:44:34 +02:00
|
|
|
public static String remove_tracking_param(String url) {
|
2020-03-29 12:17:10 +02:00
|
|
|
if (url != null) {
|
|
|
|
for (String utm : UTM_PARAMS) {
|
|
|
|
url = url.replaceAll("&" + utm + "=[0-9a-zA-Z._-]*", "");
|
|
|
|
url = url.replaceAll("&" + utm + "=[0-9a-zA-Z._-]*", "");
|
|
|
|
url = url.replaceAll("\\?" + utm + "=[0-9a-zA-Z._-]*", "?");
|
2020-04-04 11:58:21 +02:00
|
|
|
url = url.replaceAll("/" + utm + "=" + urlRegex, "/");
|
|
|
|
url = url.replaceAll("#" + utm + "=" + urlRegex, "");
|
2020-03-29 12:17:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (url != null && url.endsWith("?")) {
|
|
|
|
url = url.substring(0, url.length() - 1);
|
|
|
|
}
|
|
|
|
return url;
|
|
|
|
}
|
2020-04-05 12:40:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage downloads with URLs
|
|
|
|
*
|
|
|
|
* @param context Context
|
|
|
|
* @param url String download url
|
|
|
|
*/
|
2020-04-15 17:51:18 +02:00
|
|
|
public static void manageDownloadsNoPopup(final Context context, final String url) {
|
2020-04-05 12:40:43 +02:00
|
|
|
|
|
|
|
final DownloadManager.Request request;
|
|
|
|
try {
|
|
|
|
request = new DownloadManager.Request(Uri.parse(url.trim()));
|
|
|
|
} catch (Exception e) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
SimpleDateFormat formatter = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss", Locale.ENGLISH);
|
|
|
|
Date now = new Date();
|
2020-04-10 17:25:19 +02:00
|
|
|
final String fileName = "UntrackMe_" + formatter.format(now) + ".mp4";
|
2020-04-05 12:40:43 +02:00
|
|
|
request.allowScanningByMediaScanner();
|
|
|
|
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
|
|
|
|
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
|
|
|
|
DownloadManager dm = (DownloadManager) context.getSystemService(DOWNLOAD_SERVICE);
|
|
|
|
assert dm != null;
|
|
|
|
dm.enqueue(request);
|
|
|
|
} catch (IllegalStateException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 17:53:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if an app is installed
|
|
|
|
*
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public static boolean isAppInstalled(Context context, String packageName) {
|
|
|
|
try {
|
|
|
|
context.getPackageManager().getPackageInfo(packageName, 0);
|
|
|
|
return true;
|
|
|
|
} catch (PackageManager.NameNotFoundException e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get PackageInfo for an app
|
|
|
|
*
|
|
|
|
* @return PackageInfo
|
|
|
|
*/
|
|
|
|
public static PackageInfo getPackageInfo(Context context, String packageName) {
|
|
|
|
PackageInfo packageInfo = null;
|
|
|
|
try {
|
|
|
|
packageInfo = context.getPackageManager().getPackageInfo(packageName, 0);
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
}
|
|
|
|
return packageInfo;
|
|
|
|
}
|
2020-04-19 19:41:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert an ArrayList to a string using coma
|
|
|
|
*
|
|
|
|
* @param arrayList ArrayList<String>
|
|
|
|
* @return String
|
|
|
|
*/
|
|
|
|
public static String arrayToString(ArrayList<String> arrayList) {
|
|
|
|
if (arrayList == null || arrayList.size() == 0) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
StringBuilder result = new StringBuilder();
|
|
|
|
for (String item : arrayList) {
|
|
|
|
result.append("\"").append(item).append("\"").append(",");
|
|
|
|
}
|
|
|
|
return result.substring(0, result.length() - 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert String items to Array
|
|
|
|
*
|
|
|
|
* @param items String
|
|
|
|
* @return ArrayList<String>
|
|
|
|
*/
|
|
|
|
public static ArrayList<String> stringToArray(String items) {
|
|
|
|
if (items == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
String[] result = items.split(",");
|
|
|
|
return new ArrayList<>(Arrays.asList(result));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static <T> ArrayList<T> union(ArrayList<T> list1, ArrayList<T> list2) {
|
|
|
|
Set<T> set = new HashSet<T>();
|
|
|
|
set.addAll(list1);
|
|
|
|
set.addAll(list2);
|
|
|
|
return new ArrayList<T>(set);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static <T> ArrayList<T> intersection(ArrayList<T> list1, ArrayList<T> list2) {
|
|
|
|
ArrayList<T> list = new ArrayList<T>();
|
|
|
|
for (T t : list1) {
|
|
|
|
if (list2.contains(t)) {
|
|
|
|
list.add(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:57:14 +01:00
|
|
|
}
|