Skip to content

Commit

Permalink
fix(Tracking): add null check
Browse files Browse the repository at this point in the history
  • Loading branch information
FineFindus committed Jul 19, 2024
1 parent 72d486e commit 499baeb
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions mastodon/src/main/java/org/joinmastodon/android/utils/Tracking.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import android.util.Patterns;

import androidx.annotation.NonNull;

import java.util.Arrays;
import java.util.regex.Matcher;

// Inspired by https://github.com/GeopJr/Tuba/blob/91a036edff9ab1ffb38d5b54a33023e5db551051/src/Utils/Tracking.vala

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
Expand Down Expand Up @@ -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);
}
}
Expand All @@ -83,12 +86,12 @@ public static String removeTrackingParameters(@NonNull String url) {
* @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);
Expand All @@ -98,7 +101,7 @@ public static String cleanUrlsInText(String text) {
/**
* 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));
}
}

0 comments on commit 499baeb

Please sign in to comment.