Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AB-2916 using WhoTracksMe to show tracker informarions #304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatButton;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.TextView;

import com.cliqz.browser.R;
import com.cliqz.browser.main.Messages;
Expand All @@ -25,12 +26,16 @@
import com.cliqz.jsengine.AntiTracking;
import com.cliqz.nove.Bus;
import com.cliqz.nove.Subscribe;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.ReadableMapKeySetIterator;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.inject.Inject;

Expand Down Expand Up @@ -128,7 +133,6 @@ public void onPause() {
@Nullable
@Override
protected View onCreateThemedView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//noinspection ConstantConditions
ControlCenterDialog.getComponent().inject(this);
mAdapter = new TrackersListAdapter(mIsIncognito, this);
final @LayoutRes int layout;
Expand Down Expand Up @@ -171,10 +175,14 @@ void onChecked(boolean isEnabled) {
return;
}
setEnabled(isEnabled);
if (isEnabled) {
attrack.removeDomainFromWhitelist(Uri.parse(mUrl).getHost());
} else {
attrack.addDomainToWhitelist(Uri.parse(mUrl).getHost());
final Uri uri = Uri.parse(mUrl);
final String host = uri != null ? uri.getHost() : null;
if (host != null) {
if (isEnabled) {
attrack.removeDomainFromWhitelist(host);
} else {
attrack.addDomainToWhitelist(host);
}
}
telemetry.sendCCToggleSignal(isEnabled, TelemetryKeys.ATTRACK);
}
Expand Down Expand Up @@ -206,7 +214,7 @@ protected void setEnabled(boolean isEnabled) {
bus.post(new Messages.UpdateControlCenterIcon(status));
}

private void setTableVisibility(ArrayList<TrackerDetailsModel> details) {
private void setTableVisibility(List<TrackerDetailsModel> details) {
if (!preferenceManager.isAttrackEnabled()) {
antitrackingTable.setVisibility(View.GONE);
trackersList.setVisibility(View.GONE);
Expand Down Expand Up @@ -248,31 +256,74 @@ public void updateList(Messages.UpdateAntiTrackingList event) {
}

private void updateList() {
ArrayList<TrackerDetailsModel> details = getTrackerDetails();
final List<TrackerDetailsModel> details = getTrackerDetails();
counter.setText(String.format(Locale.getDefault(), "%d", mTrackerCount));
mAdapter.updateList(details);
setTableVisibility(details);
}

private ArrayList<TrackerDetailsModel> getTrackerDetails() {
private List<TrackerDetailsModel> getTrackerDetails() {
mTrackerCount = 0;
final ArrayList<TrackerDetailsModel> trackerDetails = new ArrayList<>();
final ReadableMap attrackData = attrack.getTabBlockingInfo(mHashCode);
if(attrackData == null) {
return trackerDetails;
return Collections.emptyList();
}
final ReadableMapKeySetIterator iterator = attrackData.keySetIterator();
while (iterator.hasNextKey()) {
final String companyName = iterator.nextKey();
final int trackersCount = attrackData.getInt(companyName);
mTrackerCount += trackersCount;
trackerDetails.add(new TrackerDetailsModel(companyName, trackersCount));

final ReadableMap companies = attrackData.getMap("companies");
final ReadableMap trackers = attrackData.getMap("trackers");
final ReadableMap companyInfo = attrackData.getMap("companyInfo");

if (companies == null || trackers == null || companyInfo == null) {
return Collections.emptyList();
}
Collections.sort(trackerDetails, (lhs, rhs) -> {

final Map<String, String> domainsToName = new HashMap<>();
final ReadableMapKeySetIterator companiesIterator = companies.keySetIterator();
while (companiesIterator.hasNextKey()) {
final String companyName = companiesIterator.nextKey();
final ReadableArray domains = companies.getArray(companyName);
if (domains == null) {
continue;
}
for (Object obj: domains.toArrayList()) {
domainsToName.put(obj.toString(), companyName);
}
}
final ReadableMapKeySetIterator trackersIterator = trackers.keySetIterator();
final Map<String, TrackerDetailsModel> trackerDetails = new HashMap<>();
while (trackersIterator.hasNextKey()) {
final String domain = trackersIterator.nextKey();
final ReadableMap trackerInfo = trackers.getMap(domain);
if (trackerInfo == null) {
continue;
}
final int c = trackerInfo.getInt("tokens_removed");
final String companyName = domainsToName.containsKey(domain) ?
domainsToName.get(domain) : domain;
mTrackerCount += c;
if (companyName == null) {
continue;
}
if (trackerDetails.containsKey(companyName)) {
final TrackerDetailsModel tdm = trackerDetails.get(companyName);
trackerDetails.put(companyName, new TrackerDetailsModel(
companyName, tdm.trackerCount + c, tdm.wtm));
} else {
final ReadableMap domainInfo = companyInfo.getMap(companyName);
if (domainInfo == null) {
continue;
}
final String wtm = domainInfo.getString("wtm");
trackerDetails.put(companyName,
new TrackerDetailsModel(companyName, c, wtm != null ? wtm : "unknown"));
}
}
final List<TrackerDetailsModel> result = new ArrayList<>(trackerDetails.values());
Collections.sort(result, (lhs, rhs) -> {
final int count = rhs.trackerCount - lhs.trackerCount;
return count != 0 ? count : lhs.companyName.compareToIgnoreCase(rhs.companyName);
});
return trackerDetails;
return result;
}

public static AntiTrackingFragment create(@NonNull String tabId, int hashCode, String url, boolean isIncognito) {
Expand All @@ -292,10 +343,14 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
return;
}
setEnabled(isChecked);
if (isChecked) {
attrack.removeDomainFromWhitelist(Uri.parse(mUrl).getHost());
} else {
attrack.addDomainToWhitelist(Uri.parse(mUrl).getHost());
final Uri uri = Uri.parse(mUrl);
final String host = uri != null ? uri.getHost() : null;
if (host != null) {
if (isChecked) {
attrack.removeDomainFromWhitelist(host);
} else {
attrack.addDomainToWhitelist(host);
}
}
telemetry.sendCCToggleSignal(isChecked, TelemetryKeys.ATTRACK);
bus.post(new Messages.ReloadPage());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package com.cliqz.browser.controlcenter;

import androidx.annotation.NonNull;

/**
* Created by Ravjit on 09/08/16.
*/
@SuppressWarnings("WeakerAccess")
public class TrackerDetailsModel {
public final String companyName;
public final int trackerCount;
public final String wtm;

public TrackerDetailsModel(String companyName, int trackerCount) {
public TrackerDetailsModel(@NonNull String companyName,
int trackerCount,
@NonNull String wtm) {
this.companyName = companyName;
this.trackerCount = trackerCount;
this.wtm = wtm;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.facebook.react.bridge.AssertionException;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import acr.browser.lightning.bus.BrowserEvents;
Expand All @@ -33,7 +34,7 @@
*/
class TrackersListAdapter extends RecyclerView.Adapter<TrackersListAdapter.ViewHolder> {

private ArrayList<TrackerDetailsModel> trackerDetails;
private List<TrackerDetailsModel> trackerDetails;
private final boolean isIncognito;
private final Bus bus;
private final Telemetry telemetry;
Expand Down Expand Up @@ -74,10 +75,8 @@ public void onBindViewHolder(final ViewHolder holder, int position) {
final int position1 = holder.getAdapterPosition();
telemetry.sendCCCompanyInfoSignal(position1, TelemetryKeys.ATTRACK);
bus.post(new Messages.DismissControlCenter());
final String companyName = details.companyName.replaceAll("\\s", "-");
final String url =
String.format("https://cliqz.com/whycliqz/anti-tracking/tracker#%s", companyName);
bus.post(new BrowserEvents.OpenUrlInNewTab(tabId, url, isIncognito));
final String url = String.format("https://whotracks.me/trackers/%s.html", details.wtm);
bus.post(new BrowserEvents.OpenUrlInNewTab(tabId, url, isIncognito, true));
});
}

Expand All @@ -101,7 +100,7 @@ class ViewHolder extends RecyclerView.ViewHolder {
}
}

void updateList(ArrayList<TrackerDetailsModel> trackerDetails) {
void updateList(List<TrackerDetailsModel> trackerDetails) {
this.trackerDetails = trackerDetails;
notifyDataSetChanged();
}
Expand Down
25 changes: 17 additions & 8 deletions app/src/main/java/com/cliqz/jsengine/AntiTracking.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.cliqz.jsengine;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.NoSuchKeyException;
import com.facebook.react.bridge.ReadableMap;

Expand All @@ -13,33 +16,35 @@ public class AntiTracking {

private final Engine engine;

public AntiTracking(Engine engine) {
public AntiTracking(@NonNull Engine engine) {
this.engine = engine;
}

public void setEnabled(boolean enabled) throws EngineNotYetAvailable {
this.engine.setPref("modules.antitracking.enabled", enabled);
}

@Nullable
public ReadableMap getTabBlockingInfo(final int tabId) {
try {
ReadableMap response = this.engine.getBridge().callAction("antitracking:getTrackerListForTab", tabId);
ReadableMap response = this.engine.getBridge().callAction("antitracking:getTabBlockingInfo", tabId, null);
return response.getMap("result");
} catch (EngineNotYetAvailable | ActionNotAvailable | EmptyResponseException | NoSuchKeyException e) {
Timber.e(e, "getTabBlockingInfo error");
}
return null;
}

public void getTabThirdPartyInfo(final int tabId, final JSBridge.Callback callback) {

public void getTabThirdPartyInfo(final int tabId, @NonNull final JSBridge.Callback callback) {
try {
this.engine.getBridge().callAction("aggregatedBlockingStats", callback, tabId);
} catch (EngineNotYetAvailable e) {
Timber.e(e, "getTabThirdPartyInfo error");
}
}

public boolean isWhitelisted(String url) {
public boolean isWhitelisted(@NonNull String url) {
try {
ReadableMap response = this.engine.getBridge().callAction("antitracking:isWhitelisted", url);
return response.hasKey("result") && response.getBoolean("result");
Expand All @@ -49,15 +54,19 @@ public boolean isWhitelisted(String url) {
return false;
}

public void addDomainToWhitelist(final String url) {
public void addDomainToWhitelist(@NonNull final String url) {
try {
this.engine.getBridge().callAction("antitracking:addSourceDomainToWhitelist", new JSBridge.NoopCallback(), url);
} catch (EngineNotYetAvailable e) {}
} catch (EngineNotYetAvailable e) {
Timber.e(e, "Can't add %s to the white list", url);
}
}

public void removeDomainFromWhitelist(final String url) {
public void removeDomainFromWhitelist(@NonNull final String url) {
try {
this.engine.getBridge().callAction("antitracking:removeSourceDomainFromWhitelist", new JSBridge.NoopCallback(), url);
} catch (EngineNotYetAvailable e) {}
} catch (EngineNotYetAvailable e) {
Timber.e(e, "Can't remove %s from the white list", url);
}
}
}
Loading