fix(Tracking): add null check

This commit is contained in:
FineFindus 2024-07-19 20:43:43 +02:00
parent 72d486e992
commit 499baeb496
No known key found for this signature in database
GPG Key ID: 64873EE210FF8E6B
1 changed files with 17 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import android.net.Uri;
import android.util.Patterns;
import androidx.annotation.NonNull;
import java.util.Arrays;
import java.util.regex.Matcher;
@ -11,7 +12,7 @@ import java.util.regex.Matcher;
public class Tracking{
/* https://github.com/brave/brave-core/blob/face8d58ab81422480c8c05b9ba5d518e1a2d227/components/query_filter/utils.cc#L23-L119 */
private static final String[] TRACKING_IDS = {
private static final String[] TRACKING_IDS={
// Strip any utm_ based ones
"utm_",
// https://github.com/brave/brave-browser/issues/4239
@ -61,14 +62,16 @@ public class Tracking{
* @return The URL with the tracking parameters removed.
*/
@NonNull
public static String removeTrackingParameters(@NonNull String url) {
Uri uri = Uri.parse(url);
Uri.Builder uriBuilder = uri.buildUpon().clearQuery();
public static String removeTrackingParameters(@NonNull String url){
Uri uri=Uri.parse(url);
if(uri==null)
return url;
Uri.Builder uriBuilder=uri.buildUpon().clearQuery();
// Iterate over existing parameters and add them back if they are not tracking parameters
for (String paramName : uri.getQueryParameterNames()) {
if (!isTrackingParameter(paramName)) {
for (String paramValue : uri.getQueryParameters(paramName)) {
for(String paramName : uri.getQueryParameterNames()){
if(!isTrackingParameter(paramName)){
for(String paramValue : uri.getQueryParameters(paramName)){
uriBuilder.appendQueryParameter(paramName, paramValue);
}
}
@ -83,12 +86,12 @@ public class Tracking{
* @param text The text that may contain URLs.
* @return The given text with cleaned URLs.
*/
public static String cleanUrlsInText(String text) {
Matcher matcher = Patterns.WEB_URL.matcher(text);
StringBuffer sb = new StringBuffer();
public static String cleanUrlsInText(String text){
Matcher matcher=Patterns.WEB_URL.matcher(text);
StringBuffer sb=new StringBuffer();
while (matcher.find()) {
String url = matcher.group();
while(matcher.find()){
String url=matcher.group();
matcher.appendReplacement(sb, removeTrackingParameters(url));
}
matcher.appendTail(sb);
@ -98,7 +101,7 @@ public class Tracking{
/**
* Returns true if the given parameter is used for tracking.
*/
private static boolean isTrackingParameter(String parameter) {
return Arrays.stream(TRACKING_IDS).anyMatch(trackingId -> parameter.toLowerCase().contains(trackingId));
private static boolean isTrackingParameter(String parameter){
return Arrays.stream(TRACKING_IDS).anyMatch(trackingId->parameter.toLowerCase().contains(trackingId));
}
}