Improve ads blocker
This commit is contained in:
parent
e604bcc661
commit
a4adef24ed
|
@ -496,6 +496,7 @@ public abstract class BaseMainActivity extends BaseActivity
|
|||
}
|
||||
//Display filter for notification when long pressing the tab
|
||||
final LinearLayout tabStrip = (LinearLayout) tabLayout.getChildAt(0);
|
||||
if( social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA )
|
||||
tabStrip.getChildAt(1).setOnLongClickListener(new View.OnLongClickListener() {
|
||||
@Override
|
||||
public boolean onLongClick(View v) {
|
||||
|
|
|
@ -17,10 +17,13 @@ package fr.gouv.etalab.mastodon.activities;
|
|||
|
||||
import android.Manifest;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.graphics.drawable.LayerDrawable;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
|
@ -28,6 +31,7 @@ import android.os.Bundle;
|
|||
import android.os.Handler;
|
||||
import android.support.v4.app.ActivityCompat;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
import android.support.v7.app.AlertDialog;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.view.View;
|
||||
|
@ -35,6 +39,7 @@ import android.view.ViewGroup;
|
|||
import android.view.WindowManager;
|
||||
import android.webkit.DownloadListener;
|
||||
import android.webkit.WebView;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.FrameLayout;
|
||||
import android.widget.Toast;
|
||||
|
||||
|
@ -46,6 +51,7 @@ import es.dmoral.toasty.Toasty;
|
|||
import fr.gouv.etalab.mastodon.R;
|
||||
import fr.gouv.etalab.mastodon.client.API;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Results;
|
||||
import fr.gouv.etalab.mastodon.helper.CountDrawable;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.sqlite.DomainBlockDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
|
@ -69,6 +75,8 @@ public class WebviewActivity extends BaseActivity {
|
|||
private boolean peertubeLink;
|
||||
private WebView webView;
|
||||
public static List<String> trackingDomains;
|
||||
private Menu defaultMenu;
|
||||
private MastalabWebViewClient mastalabWebViewClient;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
|
@ -130,7 +138,8 @@ public class WebviewActivity extends BaseActivity {
|
|||
}
|
||||
});
|
||||
webView.setWebChromeClient(mastalabWebChromeClient);
|
||||
webView.setWebViewClient(new MastalabWebViewClient(WebviewActivity.this));
|
||||
mastalabWebViewClient = new MastalabWebViewClient(WebviewActivity.this);
|
||||
webView.setWebViewClient(mastalabWebViewClient);
|
||||
webView.setDownloadListener(new DownloadListener() {
|
||||
@Override
|
||||
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
|
||||
|
@ -172,11 +181,42 @@ public class WebviewActivity extends BaseActivity {
|
|||
webView.loadUrl(url);
|
||||
}
|
||||
|
||||
|
||||
public void setCount(Context context, String count) {
|
||||
if( defaultMenu != null) {
|
||||
MenuItem menuItem = defaultMenu.findItem(R.id.action_block);
|
||||
LayerDrawable icon = (LayerDrawable) menuItem.getIcon();
|
||||
|
||||
CountDrawable badge;
|
||||
|
||||
// Reuse drawable if possible
|
||||
Drawable reuse = icon.findDrawableByLayerId(R.id.ic_block_count);
|
||||
if (reuse instanceof CountDrawable) {
|
||||
badge = (CountDrawable) reuse;
|
||||
} else {
|
||||
badge = new CountDrawable(context);
|
||||
}
|
||||
|
||||
badge.setCount(count);
|
||||
icon.mutate();
|
||||
icon.setDrawableByLayerId(R.id.ic_block_count, badge);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onPrepareOptionsMenu(Menu menu) {
|
||||
setCount(this, "0");
|
||||
defaultMenu = menu;
|
||||
return super.onPrepareOptionsMenu(menu);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
getMenuInflater().inflate(R.menu.main_webview, menu);
|
||||
defaultMenu = menu;
|
||||
if( peertubeLink ){
|
||||
menu.findItem(R.id.action_go).setVisible(false);
|
||||
menu.findItem(R.id.action_block).setVisible(false);
|
||||
menu.findItem(R.id.action_comment).setVisible(true);
|
||||
}
|
||||
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
|
||||
|
@ -190,6 +230,44 @@ public class WebviewActivity extends BaseActivity {
|
|||
switch (item.getItemId()) {
|
||||
case android.R.id.home:
|
||||
finish();
|
||||
return true;
|
||||
case R.id.action_block:
|
||||
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
|
||||
final int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
|
||||
|
||||
List<String> domains = mastalabWebViewClient.getDomains();
|
||||
|
||||
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(WebviewActivity.this, R.layout.domains_blocked);
|
||||
for (String domain: domains)
|
||||
arrayAdapter.add(domain);
|
||||
int style;
|
||||
if (theme == Helper.THEME_DARK) {
|
||||
style = R.style.DialogDark;
|
||||
} else if (theme == Helper.THEME_BLACK){
|
||||
style = R.style.DialogBlack;
|
||||
}else {
|
||||
style = R.style.Dialog;
|
||||
}
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(WebviewActivity.this, style);
|
||||
builder.setTitle(R.string.list_of_blocked_domains);
|
||||
|
||||
builder.setNegativeButton(R.string.close, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
dialog.dismiss();
|
||||
}
|
||||
});
|
||||
|
||||
builder.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(DialogInterface dialog, int which) {
|
||||
String strName = arrayAdapter.getItem(which);
|
||||
assert strName != null;
|
||||
Toasty.info(WebviewActivity.this, strName, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
});
|
||||
builder.show();
|
||||
|
||||
return true;
|
||||
case R.id.action_go:
|
||||
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
|
||||
|
|
|
@ -0,0 +1,103 @@
|
|||
package fr.gouv.etalab.mastodon.helper;
|
||||
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.ColorFilter;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.PixelFormat;
|
||||
import android.graphics.Rect;
|
||||
import android.graphics.Typeface;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.v4.content.ContextCompat;
|
||||
|
||||
import fr.gouv.etalab.mastodon.R;
|
||||
|
||||
|
||||
public class CountDrawable extends Drawable {
|
||||
|
||||
private Paint mBadgePaint;
|
||||
private Paint mTextPaint;
|
||||
private Rect mTxtRect = new Rect();
|
||||
|
||||
private String mCount = "";
|
||||
private boolean mWillDraw;
|
||||
|
||||
public CountDrawable(Context context) {
|
||||
float mTextSize = context.getResources().getDimension(R.dimen.badge_count_textsize);
|
||||
|
||||
mBadgePaint = new Paint();
|
||||
mBadgePaint.setColor(ContextCompat.getColor(context.getApplicationContext(), R.color.red_1));
|
||||
mBadgePaint.setAntiAlias(true);
|
||||
mBadgePaint.setStyle(Paint.Style.FILL);
|
||||
|
||||
mTextPaint = new Paint();
|
||||
mTextPaint.setColor(Color.WHITE);
|
||||
mTextPaint.setTypeface(Typeface.DEFAULT);
|
||||
mTextPaint.setTextSize(mTextSize);
|
||||
mTextPaint.setAntiAlias(true);
|
||||
mTextPaint.setTextAlign(Paint.Align.CENTER);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(@NonNull Canvas canvas) {
|
||||
|
||||
if (!mWillDraw) {
|
||||
return;
|
||||
}
|
||||
Rect bounds = getBounds();
|
||||
float width = bounds.right - bounds.left;
|
||||
float height = bounds.bottom - bounds.top;
|
||||
|
||||
// Position the badge in the top-right quadrant of the icon.
|
||||
|
||||
/*Using Math.max rather than Math.min */
|
||||
|
||||
float radius = ((Math.max(width, height) / 2)) / 2;
|
||||
float centerX = (width - radius - 1) +5;
|
||||
float centerY = radius -5;
|
||||
if(mCount.length() <= 2){
|
||||
// Draw badge circle.
|
||||
canvas.drawCircle(centerX, centerY, (int)(radius+5.5), mBadgePaint);
|
||||
}
|
||||
else{
|
||||
canvas.drawCircle(centerX, centerY, (int)(radius+6.5), mBadgePaint);
|
||||
}
|
||||
// Draw badge count text inside the circle.
|
||||
mTextPaint.getTextBounds(mCount, 0, mCount.length(), mTxtRect);
|
||||
float textHeight = mTxtRect.bottom - mTxtRect.top;
|
||||
float textY = centerY + (textHeight / 2f);
|
||||
if(mCount.length() > 2)
|
||||
canvas.drawText("99+", centerX, textY, mTextPaint);
|
||||
else
|
||||
canvas.drawText(mCount, centerX, textY, mTextPaint);
|
||||
}
|
||||
|
||||
/*
|
||||
Sets the count (i.e notifications) to display.
|
||||
*/
|
||||
public void setCount(String count) {
|
||||
mCount = count;
|
||||
|
||||
// Only draw a badge if there are notifications.
|
||||
mWillDraw = !count.equalsIgnoreCase("0");
|
||||
invalidateSelf();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setAlpha(int alpha) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColorFilter(ColorFilter cf) {
|
||||
// do nothing
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOpacity() {
|
||||
return PixelFormat.UNKNOWN;
|
||||
}
|
||||
}
|
|
@ -62,6 +62,14 @@ public class DomainBlockDAO {
|
|||
insert(domain);
|
||||
|
||||
}
|
||||
|
||||
public int getTotalCount(){
|
||||
Cursor mCount= db.rawQuery("SELECT Count(*) FROM " + Sqlite.TABLE_TRACKING_BLOCK, null);
|
||||
mCount.moveToFirst();
|
||||
int count = mCount.getInt(0);
|
||||
mCount.close();
|
||||
return count;
|
||||
}
|
||||
/***
|
||||
* Remove all domains
|
||||
*/
|
||||
|
|
|
@ -28,6 +28,8 @@ import android.widget.TextView;
|
|||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import fr.gouv.etalab.mastodon.R;
|
||||
import fr.gouv.etalab.mastodon.activities.WebviewActivity;
|
||||
|
@ -40,11 +42,12 @@ import fr.gouv.etalab.mastodon.activities.WebviewActivity;
|
|||
public class MastalabWebViewClient extends WebViewClient {
|
||||
|
||||
private Activity activity;
|
||||
private int count = 0;
|
||||
|
||||
public MastalabWebViewClient(Activity activity){
|
||||
this.activity = activity;
|
||||
}
|
||||
|
||||
public List<String> domains = new ArrayList<>();
|
||||
@Override
|
||||
public void onPageFinished(WebView view, String url) {
|
||||
super.onPageFinished(view, url);
|
||||
|
@ -57,8 +60,15 @@ public class MastalabWebViewClient extends WebViewClient {
|
|||
try {
|
||||
uri = new URI(url);
|
||||
String domain = uri.getHost();
|
||||
if( domain != null) {
|
||||
domain = domain.startsWith("www.") ? domain.substring(4) : domain;
|
||||
if (WebviewActivity.trackingDomains.contains(domain)) {
|
||||
}
|
||||
if (domain != null && WebviewActivity.trackingDomains.contains(domain)) {
|
||||
if( activity instanceof WebviewActivity){
|
||||
count++;
|
||||
domains.add(url);
|
||||
((WebviewActivity)activity).setCount(activity, String.valueOf(count));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
} catch (URISyntaxException e) {
|
||||
|
@ -67,9 +77,16 @@ public class MastalabWebViewClient extends WebViewClient {
|
|||
return super.shouldInterceptRequest(view, url);
|
||||
}
|
||||
|
||||
public List<String> getDomains(){
|
||||
return this.domains;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPageStarted (WebView view, String url, Bitmap favicon) {
|
||||
super.onPageStarted(view, url, favicon);
|
||||
count = 0;
|
||||
domains = new ArrayList<>();
|
||||
domains.clear();
|
||||
ActionBar actionBar = ((AppCompatActivity) activity).getSupportActionBar();
|
||||
LayoutInflater mInflater = LayoutInflater.from(activity);
|
||||
if( actionBar != null){
|
||||
|
|
|
@ -0,0 +1,9 @@
|
|||
<vector xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:width="24dp"
|
||||
android:height="24dp"
|
||||
android:viewportWidth="24.0"
|
||||
android:viewportHeight="24.0">
|
||||
<path
|
||||
android:fillColor="#FFFFFFFF"
|
||||
android:pathData="M12,2C6.48,2 2,6.48 2,12s4.48,10 10,10 10,-4.48 10,-10S17.52,2 12,2zM4,12c0,-4.42 3.58,-8 8,-8 1.85,0 3.55,0.63 4.9,1.69L5.69,16.9C4.63,15.55 4,13.85 4,12zM12,20c-1.85,0 -3.55,-0.63 -4.9,-1.69L18.31,7.1C19.37,8.45 20,10.15 20,12c0,4.42 -3.58,8 -8,8z"/>
|
||||
</vector>
|
|
@ -0,0 +1,12 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<item
|
||||
android:drawable="@drawable/ic_block_script"
|
||||
android:gravity="center" />
|
||||
|
||||
<item
|
||||
android:id="@+id/ic_block_count"
|
||||
android:drawable="@color/transparent" />
|
||||
|
||||
</layer-list>
|
|
@ -0,0 +1,10 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:maxLines="1"
|
||||
android:paddingLeft="5dp"
|
||||
android:paddingStart="5dp"
|
||||
android:paddingEnd="5dp"
|
||||
android:textColor="@color/red_1"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:paddingRight="5dp" />
|
|
@ -1,6 +1,11 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<menu xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<item
|
||||
android:id="@+id/action_block"
|
||||
android:icon="@drawable/browser_calls_blocked"
|
||||
android:title="@string/calls_blocked"
|
||||
app:showAsAction="always" />
|
||||
<item
|
||||
android:id="@+id/action_go"
|
||||
android:title="@string/open_with"
|
||||
|
|
|
@ -15,4 +15,5 @@
|
|||
<dimen name="settings_checkbox_margin">2dp</dimen>
|
||||
<dimen name="settings_category_label_margin">2dp</dimen>
|
||||
<dimen name="activity_vertical_margin_login">50dp</dimen>
|
||||
<dimen name="badge_count_textsize">12sp</dimen>
|
||||
</resources>
|
||||
|
|
|
@ -881,6 +881,8 @@
|
|||
<string name="set_security_provider">Patch the Security Provider</string>
|
||||
<string name="update_tracking_domains">Update tracking domains</string>
|
||||
<string name="tracking_db_updated">The tracking data base has been updated!</string>
|
||||
<string name="calls_blocked">http calls blocked by the application</string>
|
||||
<string name="list_of_blocked_domains">List of blocked calls</string>
|
||||
<!-- end languages -->
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue