Tracking database

This commit is contained in:
stom79 2019-02-14 17:46:38 +01:00
parent 5da0994eb2
commit 7bb586225c
8 changed files with 301 additions and 3 deletions

View File

@ -94,5 +94,6 @@ dependencies {
implementation 'com.github.mabbas007:TagsEditText:1.0.5'
implementation 'com.jaredrummler:material-spinner:1.3.1'
implementation 'com.github.stom79:SQLite2XL:1.0.5'
implementation "com.tonyodev.fetch2:fetch2:2.3.6"
playstoreImplementation "io.github.kobakei:ratethisapp:$ratethisappLibraryVersion"
}

View File

@ -1233,8 +1233,10 @@ public abstract class BaseMainActivity extends BaseActivity
//Defines the current locale of the device in a static variable
currentLocale = Helper.currentLocale(getApplicationContext());
if( tabLayout.getTabAt(0) == null)
Helper.logout(BaseMainActivity.thisL);
if( tabLayout.getTabAt(0) == null) {
Helper.logout(BaseMainActivity.this);
return;
}
tabLayout.getTabAt(0).select();
if( social == UpdateAccountInfoAsyncTask.SOCIAL.MASTODON || social == UpdateAccountInfoAsyncTask.SOCIAL.PLEROMA || social == UpdateAccountInfoAsyncTask.SOCIAL.GNU || social == UpdateAccountInfoAsyncTask.SOCIAL.FRIENDICA) {
toot.setOnClickListener(new View.OnClickListener() {

View File

@ -0,0 +1,37 @@
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Mastalab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
package fr.gouv.etalab.mastodon.client.Entities;
public class BlockedDomains {
private int id;
private String domain;
public String getDomain() {
return domain;
}
public void setDomain(String domain) {
this.domain = domain;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}

View File

@ -24,9 +24,11 @@ import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
@ -40,6 +42,7 @@ import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
@ -51,12 +54,33 @@ import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.tonyodev.fetch2.Download;
import com.tonyodev.fetch2.Error;
import com.tonyodev.fetch2.Fetch;
import com.tonyodev.fetch2.FetchConfiguration;
import com.tonyodev.fetch2.FetchListener;
import com.tonyodev.fetch2.NetworkType;
import com.tonyodev.fetch2.Priority;
import com.tonyodev.fetch2.Request;
import com.tonyodev.fetch2core.DownloadBlock;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import es.dmoral.toasty.Toasty;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.activities.MainActivity;
import fr.gouv.etalab.mastodon.client.Entities.Account;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
import fr.gouv.etalab.mastodon.sqlite.DomainBlockDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import static android.app.Activity.RESULT_OK;
@ -163,6 +187,115 @@ public class SettingsFragment extends Fragment {
}
});
Button update_tracking_domains = rootView.findViewById(R.id.update_tracking_domains);
update_tracking_domains.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FetchConfiguration fetchConfiguration = new FetchConfiguration.Builder(context)
.setDownloadConcurrentLimit(1)
.build();
update_tracking_domains.setEnabled(false);
Fetch fetch = Fetch.Impl.getInstance(fetchConfiguration);
String url = "https://sebsauvage.net/hosts/hosts";
File dir = context.getCacheDir();
String file = dir + "/tracking.txt";
FetchListener fetchListener = new FetchListener() {
@Override
public void onWaitingNetwork(@NotNull Download download) {
}
@Override
public void onStarted(@NotNull Download download, @NotNull List<? extends DownloadBlock> list, int i) {
}
@Override
public void onResumed(@NotNull Download download) {
}
@Override
public void onRemoved(@NotNull Download download) {
}
@Override
public void onQueued(@NotNull Download download, boolean b) {
}
@Override
public void onProgress(@NotNull Download download, long l, long l1) {
}
@Override
public void onPaused(@NotNull Download download) {
}
@Override
public void onError(@NotNull Download download, @NotNull Error error, @Nullable Throwable throwable) {
Toasty.error(context, context.getString(R.string.toast_error),Toast.LENGTH_LONG).show();
update_tracking_domains.setEnabled(true);
}
@Override
public void onDownloadBlockUpdated(@NotNull Download download, @NotNull DownloadBlock downloadBlock, int i) {
}
@Override
public void onDeleted(@NotNull Download download) {
}
@Override
public void onCompleted(@NotNull Download download) {
boolean canRecord = false;
if( download.getFileUri().getPath() != null) {
File file = new File(download.getFileUri().getPath());
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
ArrayList<String> domains = new ArrayList<>();
while ((line = br.readLine()) != null) {
if(!canRecord && line.contains("# Blocked domains"))
canRecord = true;
if( canRecord) {
String domain = line.replaceAll("0.0.0.0 ","").trim();
domains.add(domain);
}
}
br.close();
AsyncTask.execute(new Runnable() {
@Override
public void run() {
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
new DomainBlockDAO(context, db).insertAll(domains);
// Get a handler that can be used to post to the main thread
Handler mainHandler = new Handler(context.getMainLooper());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
Toasty.success(context, context.getString(R.string.tracking_db_updated), Toast.LENGTH_LONG).show();
}
};
mainHandler.post(myRunnable);
}
});
}
catch (IOException e) {
//You'll need to add proper error handling here
}
}
}
@Override
public void onCancelled(@NotNull Download download) {
}
@Override
public void onAdded(@NotNull Download download) {
}
};
fetch.addListener(fetchListener);
final Request request = new Request(url, file);
request.setPriority(Priority.HIGH);
request.setNetworkType(NetworkType.ALL);
fetch.enqueue(request, updatedRequest -> {
//Request was successfully enqueued for download.
}, error -> {
//An error occurred enqueuing the request.
});
}
});
//Manage download of attachments
RadioGroup radioGroup = rootView.findViewById(R.id.set_attachment_group);
int attachmentAction = sharedpreferences.getInt(Helper.SET_ATTACHMENT_ACTION, Helper.ATTACHMENT_ALWAYS);

View File

@ -0,0 +1,106 @@
package fr.gouv.etalab.mastodon.sqlite;
/* Copyright 2019 Thomas Schneider
*
* This file is a part of Mastalab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Mastalab; if not,
* see <http://www.gnu.org/licenses>. */
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Thomas on 14/02/2019.
* Manage domain block in DB
*/
public class DomainBlockDAO {
private SQLiteDatabase db;
public Context context;
public DomainBlockDAO(Context context, SQLiteDatabase db) {
//Creation of the DB with tables
this.context = context;
this.db = db;
}
//------- INSERTIONS -------
/**
* Insert a domain in database
* @param domain String
*/
private void insert(String domain) {
ContentValues values = new ContentValues();
values.put(Sqlite.COL_DOMAIN, domain);
try{
db.insert(Sqlite.TABLE_TRACKING_BLOCK, null, values);
}catch (Exception ignored) {}
}
/**
* Insert domains in database
* @param domains ArrayList<String>
*/
public void insertAll(ArrayList<String> domains) {
removeAll();
for (String domain : domains)
insert(domain);
}
/***
* Remove all domains
*/
private void removeAll(){
db.delete(Sqlite.TABLE_TRACKING_BLOCK, null, null);
}
/**
* Returns all domains in db
* @return string domain List<String>
*/
public List<String> getAll(){
try {
Cursor c = db.query(Sqlite.TABLE_TRACKING_BLOCK, null, null, null, null, null, null, null);
return cursorToDomain(c);
} catch (Exception e) {
return null;
}
}
/***
* Method to hydrate domain from database
* @param c Cursor
* @return List<String>
*/
private List<String> cursorToDomain(Cursor c){
//No element found
if (c.getCount() == 0)
return null;
List<String> domains = new ArrayList<>();
while (c.moveToNext() ) {
domains.add(c.getString(c.getColumnIndex(Sqlite.COL_DOMAIN)));
}
//Close the cursor
c.close();
//domains list is returned
return domains;
}
}

View File

@ -617,6 +617,15 @@
android:text="@string/embedded_browser"
android:layout_height="wrap_content" />
<Button
android:gravity="center"
android:textSize="16sp"
android:layout_gravity="center_horizontal"
android:id="@+id/update_tracking_domains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:text="@string/update_tracking_domains" />
<CheckBox
android:visibility="gone"

View File

@ -617,7 +617,15 @@
android:text="@string/embedded_browser"
android:layout_height="wrap_content" />
<Button
android:gravity="center"
android:textSize="16sp"
android:layout_gravity="center_horizontal"
android:id="@+id/update_tracking_domains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/Base.Widget.AppCompat.Button.Colored"
android:text="@string/update_tracking_domains" />
<CheckBox
android:visibility="gone"
android:layout_marginTop="10dp"

View File

@ -879,6 +879,8 @@
<string name="set_console">Console</string>
<string name="set_mode">Set display mode</string>
<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>
<!-- end languages -->