diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java index 3abd799e5..cbf4306c3 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/Sqlite.java @@ -23,10 +23,10 @@ import android.database.sqlite.SQLiteOpenHelper; * Created by Thomas on 23/04/2017. * Manage the DataBase */ -@SuppressWarnings("WeakerAccess") + public class Sqlite extends SQLiteOpenHelper { - public static final int DB_VERSION = 7; + public static final int DB_VERSION = 8; public static final String DB_NAME = "mastodon_etalab_db"; public static SQLiteDatabase db; private static Sqlite sInstance; @@ -43,23 +43,26 @@ public class Sqlite extends SQLiteOpenHelper { //Table for search static final String TABLE_SEARCH = "SEARCH"; - public static final String COL_USER_ID = "USER_ID"; - public static final String COL_USERNAME = "USERNAME"; - public static final String COL_ACCT = "ACCT"; - public static final String COL_DISPLAYED_NAME = "DISPLAYED_NAME"; - public static final String COL_LOCKED = "LOCKED"; - public static final String COL_CREATED_AT = "CREATED_AT"; - public static final String COL_FOLLOWERS_COUNT = "FOLLOWERS_COUNT"; - public static final String COL_FOLLOWING_COUNT = "FOLLOWING_COUNT"; - public static final String COL_STATUSES_COUNT = "STATUSES_COUNT"; - public static final String COL_NOTE = "NOTE"; - public static final String COL_URL = "URL"; - public static final String COL_AVATAR = "AVATAR"; - public static final String COL_AVATAR_STATIC = "AVATAR_STATIC"; - public static final String COL_HEADER = "HEADER"; - public static final String COL_HEADER_STATIC = "HEADER_STATIC"; - public static final String COL_INSTANCE = "INSTANCE"; - public static final String COL_OAUTHTOKEN = "OAUTH_TOKEN"; + //Table for temp muting + static final String TABLE_TEMP_MUTE = "TEMP_MUTE"; + + static final String COL_USER_ID = "USER_ID"; + static final String COL_USERNAME = "USERNAME"; + static final String COL_ACCT = "ACCT"; + static final String COL_DISPLAYED_NAME = "DISPLAYED_NAME"; + static final String COL_LOCKED = "LOCKED"; + static final String COL_CREATED_AT = "CREATED_AT"; + static final String COL_FOLLOWERS_COUNT = "FOLLOWERS_COUNT"; + static final String COL_FOLLOWING_COUNT = "FOLLOWING_COUNT"; + static final String COL_STATUSES_COUNT = "STATUSES_COUNT"; + static final String COL_NOTE = "NOTE"; + static final String COL_URL = "URL"; + static final String COL_AVATAR = "AVATAR"; + static final String COL_AVATAR_STATIC = "AVATAR_STATIC"; + static final String COL_HEADER = "HEADER"; + static final String COL_HEADER_STATIC = "HEADER_STATIC"; + static final String COL_INSTANCE = "INSTANCE"; + static final String COL_OAUTHTOKEN = "OAUTH_TOKEN"; @@ -73,14 +76,14 @@ public class Sqlite extends SQLiteOpenHelper { + COL_INSTANCE + " TEXT NOT NULL, " + COL_OAUTHTOKEN + " TEXT NOT NULL, " + COL_CREATED_AT + " TEXT NOT NULL)"; - public static final String COL_ID = "ID"; - public static final String COL_STATUS_SERIALIZED = "STATUS_SERIALIZED"; - public static final String COL_STATUS_REPLY_SERIALIZED = "STATUS_REPLY_SERIALIZED"; - public static final String COL_DATE_CREATION = "DATE_CREATION"; - public static final String COL_IS_SCHEDULED = "IS_SCHEDULED"; - public static final String COL_DATE_SCHEDULED = "DATE_SCHEDULED"; - public static final String COL_SENT = "SENT"; - public static final String COL_DATE_SENT = "DATE_SENT"; + static final String COL_ID = "ID"; + static final String COL_STATUS_SERIALIZED = "STATUS_SERIALIZED"; + static final String COL_STATUS_REPLY_SERIALIZED = "STATUS_REPLY_SERIALIZED"; + static final String COL_DATE_CREATION = "DATE_CREATION"; + static final String COL_IS_SCHEDULED = "IS_SCHEDULED"; + static final String COL_DATE_SCHEDULED = "DATE_SCHEDULED"; + static final String COL_SENT = "SENT"; + static final String COL_DATE_SENT = "DATE_SENT"; private static final String CREATE_TABLE_STATUSES_STORED = "CREATE TABLE " + TABLE_STATUSES_STORED + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " @@ -90,19 +93,26 @@ public class Sqlite extends SQLiteOpenHelper { + COL_SENT + " INTEGER NOT NULL, " + COL_DATE_SENT + " TEXT)"; - public static final String COL_SHORTCODE = "SHORTCODE"; - public static final String COL_URL_STATIC = "URL_STATIC"; + static final String COL_SHORTCODE = "SHORTCODE"; + static final String COL_URL_STATIC = "URL_STATIC"; private final String CREATE_TABLE_CUSTOM_EMOJI = "CREATE TABLE " + TABLE_CUSTOM_EMOJI + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_SHORTCODE + " TEXT NOT NULL, " + COL_INSTANCE + " TEXT NOT NULL, " + COL_URL + " TEXT NOT NULL, " + COL_URL_STATIC + " TEXT NOT NULL, " + COL_DATE_CREATION + " TEXT NOT NULL)"; - public static final String COL_KEYWORDS = "KEYWORDS"; + static final String COL_KEYWORDS = "KEYWORDS"; private final String CREATE_TABLE_SEARCH = "CREATE TABLE " + TABLE_SEARCH + " (" + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_KEYWORDS + " TEXT NOT NULL, " + COL_USER_ID + " TEXT NOT NULL, " + COL_DATE_CREATION + " TEXT NOT NULL)"; + static final String COL_TARGETED_USER_ID = "TARGETED_USER_ID"; + static final String COL_DATE_END = "DATE_END"; + private final String CREATE_TABLE_TEMP_MUTE = "CREATE TABLE " + TABLE_TEMP_MUTE + " (" + + COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + + COL_USER_ID + " TEXT NOT NULL, " + COL_TARGETED_USER_ID + " TEXT NOT NULL, " + COL_DATE_CREATION + " TEXT NOT NULL, " + COL_DATE_END + " TEXT NOT NULL)"; + + public Sqlite(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); } @@ -122,6 +132,7 @@ public class Sqlite extends SQLiteOpenHelper { db.execSQL(CREATE_TABLE_STATUSES_STORED); db.execSQL(CREATE_TABLE_CUSTOM_EMOJI); db.execSQL(CREATE_TABLE_SEARCH); + db.execSQL(CREATE_TABLE_TEMP_MUTE); } @Override @@ -143,6 +154,8 @@ public class Sqlite extends SQLiteOpenHelper { db.execSQL("delete from "+ TABLE_CUSTOM_EMOJI); //Reset table due to bugs case 6: db.execSQL(CREATE_TABLE_SEARCH); + case 7: + db.execSQL(CREATE_TABLE_TEMP_MUTE); default: break; } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/TempMuteDAO.java b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/TempMuteDAO.java new file mode 100644 index 000000000..f19c99a7c --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/sqlite/TempMuteDAO.java @@ -0,0 +1,144 @@ +package fr.gouv.etalab.mastodon.sqlite; +/* Copyright 2017 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 . */ + +import android.content.ContentValues; +import android.content.Context; +import android.content.SharedPreferences; +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import fr.gouv.etalab.mastodon.helper.Helper; + + +/** + * Created by Thomas on 04/01/2018. + * Manage temp mute in DB + */ +public class TempMuteDAO { + + private SQLiteDatabase db; + public Context context; + private String userId; + + public TempMuteDAO(Context context, SQLiteDatabase db) { + //Creation of the DB with tables + this.context = context; + this.db = db; + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + + } + + + //------- INSERTIONS ------- + + /** + * Insert a keyword in database + * @param targeted_id String + * @param date Date + */ + public void insert(String targeted_id, Date date) { + SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + ContentValues values = new ContentValues(); + values.put(Sqlite.COL_TARGETED_USER_ID, targeted_id); + values.put(Sqlite.COL_USER_ID, userId); + values.put(Sqlite.COL_DATE_CREATION, Helper.dateToString(context, new Date())); + values.put(Sqlite.COL_DATE_END, Helper.dateToString(context, date)); + //Inserts temp mute + try{ + db.insert(Sqlite.TABLE_TEMP_MUTE, null, values); + }catch (Exception ignored) {} + } + + + //------- REMOVE ------- + + /*** + * Remove mute by its id + * @return int + */ + public int remove(String targeted_id){ + return db.delete(Sqlite.TABLE_TEMP_MUTE, Sqlite.COL_TARGETED_USER_ID + " = \"" + targeted_id + "\" AND " + Sqlite.COL_USER_ID + " = \"" + userId+ "\"", null); + } + + //------- GETTERS ------- + + /** + * Returns all search in db for a user + * @return search List + */ + public List getAllSearch(){ + try { + Cursor c = db.query(Sqlite.TABLE_SEARCH, null, Sqlite.COL_USER_ID + " = '" + userId+ "'", null, null, null, Sqlite.COL_KEYWORDS + " ASC", null); + return cursorToListSearch(c); + } catch (Exception e) { + return null; + } + } + + + + /** + * Returns search by its keyword in db + * @return keywords List + */ + public List getSearchStartingBy(String keyword){ + try { + Cursor c = db.query(Sqlite.TABLE_SEARCH, null, Sqlite.COL_KEYWORDS + " LIKE \"%" + keyword + "%\" AND " + Sqlite.COL_USER_ID + " = \"" + userId+ "\"", null, null, null, null, null); + return cursorToListSearch(c); + } catch (Exception e) { + return null; + } + } + + /** + * Returns search by its keyword in db + * @return keywords List + */ + public List getSearchByKeyword(String keyword){ + try { + Cursor c = db.query(Sqlite.TABLE_SEARCH, null, Sqlite.COL_KEYWORDS + " = \"" + keyword + "\" AND " + Sqlite.COL_USER_ID + " = \"" + userId+ "\"", null, null, null, null, null); + return cursorToListSearch(c); + } catch (Exception e) { + return null; + } + } + + + /*** + * Method to hydrate stored search from database + * @param c Cursor + * @return List + */ + private List cursorToListSearch(Cursor c){ + //No element found + if (c.getCount() == 0) + return null; + List searches = new ArrayList<>(); + while (c.moveToNext() ) { + searches.add(c.getString(c.getColumnIndex(Sqlite.COL_KEYWORDS))); + } + //Close the cursor + c.close(); + //Search list is returned + return searches; + } +}