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