fedilab-Android-App/app/src/main/java/app/fedilab/android/sqlite/DomainBlockDAO.java

122 lines
3.2 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.sqlite;
2019-02-14 17:46:38 +01:00
/* Copyright 2019 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2019-02-14 17:46:38 +01:00
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2019-02-14 17:46:38 +01:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2019-02-14 17:46:38 +01:00
* 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
*/
2020-05-07 17:02:07 +02:00
@SuppressWarnings("unused")
2019-02-14 17:46:38 +01:00
public class DomainBlockDAO {
public Context context;
2019-11-15 16:32:25 +01:00
private SQLiteDatabase db;
2019-02-14 17:46:38 +01:00
public DomainBlockDAO(Context context, SQLiteDatabase db) {
//Creation of the DB with tables
this.context = context;
this.db = db;
}
//------- INSERTIONS -------
/**
* Insert a domain in database
2019-09-06 17:55:14 +02:00
*
2019-02-14 17:46:38 +01:00
* @param domain String
*/
private void insert(String domain) {
ContentValues values = new ContentValues();
values.put(Sqlite.COL_DOMAIN, domain);
2019-09-06 17:55:14 +02:00
try {
2019-02-14 17:46:38 +01:00
db.insert(Sqlite.TABLE_TRACKING_BLOCK, null, values);
2020-05-16 11:32:09 +02:00
} catch (Exception ignored) {
}
2019-02-14 17:46:38 +01:00
}
/**
* Insert domains in database
2019-09-06 17:55:14 +02:00
*
* @param domains List<String>
2019-02-14 17:46:38 +01:00
*/
public void set(List<String> domains) {
2019-02-14 17:46:38 +01:00
removeAll();
for (String domain : domains)
insert(domain);
}
2019-02-15 18:19:32 +01:00
2019-09-06 17:55:14 +02:00
public int getTotalCount() {
Cursor mCount = db.rawQuery("SELECT Count(*) FROM " + Sqlite.TABLE_TRACKING_BLOCK, null);
2019-02-15 18:19:32 +01:00
mCount.moveToFirst();
int count = mCount.getInt(0);
mCount.close();
return count;
}
2019-09-06 17:55:14 +02:00
2019-02-14 17:46:38 +01:00
/***
* Remove all domains
*/
2019-09-06 17:55:14 +02:00
private void removeAll() {
2019-02-14 17:46:38 +01:00
db.delete(Sqlite.TABLE_TRACKING_BLOCK, null, null);
}
/**
* Returns all domains in db
2019-09-06 17:55:14 +02:00
*
2019-02-14 17:46:38 +01:00
* @return string domain List<String>
*/
2019-09-06 17:55:14 +02:00
public List<String> getAll() {
2019-02-14 17:46:38 +01:00
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>
*/
2019-09-06 17:55:14 +02:00
private List<String> cursorToDomain(Cursor c) {
2019-02-14 17:46:38 +01:00
//No element found
2019-08-17 14:34:33 +02:00
if (c.getCount() == 0) {
c.close();
2019-02-14 17:46:38 +01:00
return null;
2019-08-17 14:34:33 +02:00
}
2019-02-14 17:46:38 +01:00
List<String> domains = new ArrayList<>();
2019-09-06 17:55:14 +02:00
while (c.moveToNext()) {
2019-02-14 17:46:38 +01:00
domains.add(c.getString(c.getColumnIndex(Sqlite.COL_DOMAIN)));
}
//Close the cursor
c.close();
//domains list is returned
return domains;
}
}