2020-06-26 08:50:49 +02:00
|
|
|
package app.fedilab.fedilabtube.sqlite;
|
2020-07-01 16:36:08 +02:00
|
|
|
/* Copyright 2020 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of TubeLab
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* TubeLab 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 TubeLab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
2020-06-26 08:50:49 +02:00
|
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
|
import android.content.Context;
|
2020-09-29 17:42:15 +02:00
|
|
|
import android.content.SharedPreferences;
|
2020-06-26 08:50:49 +02:00
|
|
|
import android.database.Cursor;
|
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-09-25 18:58:04 +02:00
|
|
|
import app.fedilab.fedilabtube.client.data.AccountData.Account;
|
|
|
|
import app.fedilab.fedilabtube.client.entities.Avatar;
|
2020-09-27 18:33:51 +02:00
|
|
|
import app.fedilab.fedilabtube.client.entities.Token;
|
2020-06-26 08:50:49 +02:00
|
|
|
import app.fedilab.fedilabtube.helper.Helper;
|
|
|
|
|
|
|
|
|
2020-09-25 18:58:04 +02:00
|
|
|
@SuppressWarnings("UnusedReturnValue")
|
2020-06-26 08:50:49 +02:00
|
|
|
public class AccountDAO {
|
|
|
|
|
|
|
|
public Context context;
|
|
|
|
private SQLiteDatabase db;
|
|
|
|
|
|
|
|
|
|
|
|
public AccountDAO(Context context, SQLiteDatabase db) {
|
|
|
|
//Creation of the DB with tables
|
|
|
|
this.context = context;
|
|
|
|
this.db = db;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Insert an Account in database
|
|
|
|
*
|
|
|
|
* @param account Account
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public boolean insertAccount(Account account) {
|
|
|
|
ContentValues values = new ContentValues();
|
2020-09-25 18:58:04 +02:00
|
|
|
if (account.getCreatedAt() == null)
|
|
|
|
account.setCreatedAt(new Date());
|
|
|
|
if (account.getDescription() == null)
|
|
|
|
account.setDescription("");
|
2020-06-26 08:50:49 +02:00
|
|
|
values.put(Sqlite.COL_USER_ID, account.getId());
|
2020-09-27 16:33:43 +02:00
|
|
|
values.put(Sqlite.COL_USERNAME, account.getUsername());
|
|
|
|
values.put(Sqlite.COL_ACCT, account.getUsername() + "@" + account.getHost());
|
|
|
|
values.put(Sqlite.COL_DISPLAYED_NAME, account.getDisplayName() != null ? account.getDisplayName() : account.getUsername());
|
2020-09-25 18:58:04 +02:00
|
|
|
values.put(Sqlite.COL_FOLLOWERS_COUNT, account.getFollowersCount());
|
|
|
|
values.put(Sqlite.COL_FOLLOWING_COUNT, account.getFollowingCount());
|
|
|
|
values.put(Sqlite.COL_NOTE, account.getDescription());
|
2020-06-26 08:50:49 +02:00
|
|
|
values.put(Sqlite.COL_URL, account.getUrl());
|
2020-09-27 16:33:43 +02:00
|
|
|
values.put(Sqlite.COL_AVATAR, account.getAvatar() != null ? account.getAvatar().getPath() : "null");
|
2020-09-25 18:58:04 +02:00
|
|
|
values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(account.getCreatedAt()));
|
|
|
|
values.put(Sqlite.COL_INSTANCE, account.getHost());
|
2020-09-27 16:33:43 +02:00
|
|
|
values.put(Sqlite.COL_LOCKED, false);
|
|
|
|
values.put(Sqlite.COL_STATUSES_COUNT, 0);
|
|
|
|
values.put(Sqlite.COL_URL, "");
|
|
|
|
values.put(Sqlite.COL_AVATAR_STATIC, "");
|
|
|
|
values.put(Sqlite.COL_HEADER, "");
|
|
|
|
values.put(Sqlite.COL_HEADER_STATIC, "");
|
2020-09-03 18:56:48 +02:00
|
|
|
if (account.getClient_id() != null && account.getClient_secret() != null) {
|
2020-06-26 08:50:49 +02:00
|
|
|
values.put(Sqlite.COL_CLIENT_ID, account.getClient_id());
|
|
|
|
values.put(Sqlite.COL_CLIENT_SECRET, account.getClient_secret());
|
2020-07-25 19:15:26 +02:00
|
|
|
}
|
2020-09-03 18:56:48 +02:00
|
|
|
if (account.getRefresh_token() != null) {
|
2020-06-26 08:50:49 +02:00
|
|
|
values.put(Sqlite.COL_REFRESH_TOKEN, account.getRefresh_token());
|
|
|
|
}
|
|
|
|
if (account.getToken() != null)
|
|
|
|
values.put(Sqlite.COL_OAUTHTOKEN, account.getToken());
|
|
|
|
|
|
|
|
//Inserts account
|
|
|
|
try {
|
|
|
|
db.insertOrThrow(Sqlite.TABLE_USER_ACCOUNT, null, values);
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update an Account in database
|
|
|
|
*
|
|
|
|
* @param account Account
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public int updateAccount(Account account) {
|
|
|
|
ContentValues values = new ContentValues();
|
2020-09-25 18:58:04 +02:00
|
|
|
if (account.getCreatedAt() == null)
|
|
|
|
account.setCreatedAt(new Date());
|
|
|
|
if (account.getDescription() == null)
|
|
|
|
account.setDescription("");
|
2020-09-27 16:33:43 +02:00
|
|
|
values.put(Sqlite.COL_USERNAME, account.getUsername());
|
|
|
|
values.put(Sqlite.COL_ACCT, account.getUsername() + "@" + account.getHost());
|
2020-09-25 18:58:04 +02:00
|
|
|
values.put(Sqlite.COL_DISPLAYED_NAME, account.getDisplayName());
|
|
|
|
values.put(Sqlite.COL_FOLLOWERS_COUNT, account.getFollowersCount());
|
|
|
|
values.put(Sqlite.COL_FOLLOWING_COUNT, account.getFollowingCount());
|
|
|
|
values.put(Sqlite.COL_NOTE, account.getDescription());
|
2020-06-26 08:50:49 +02:00
|
|
|
values.put(Sqlite.COL_URL, account.getUrl());
|
2020-09-25 18:58:04 +02:00
|
|
|
values.put(Sqlite.COL_AVATAR, account.getAvatar().getPath());
|
|
|
|
values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(account.getCreatedAt()));
|
2020-07-25 19:15:26 +02:00
|
|
|
if (account.getClient_id() != null && account.getClient_secret() != null) {
|
2020-06-26 08:50:49 +02:00
|
|
|
values.put(Sqlite.COL_CLIENT_ID, account.getClient_id());
|
|
|
|
values.put(Sqlite.COL_CLIENT_SECRET, account.getClient_secret());
|
2020-07-25 19:15:26 +02:00
|
|
|
}
|
2020-09-03 18:56:48 +02:00
|
|
|
if (account.getRefresh_token() != null) {
|
2020-06-26 08:50:49 +02:00
|
|
|
values.put(Sqlite.COL_REFRESH_TOKEN, account.getRefresh_token());
|
|
|
|
}
|
|
|
|
if (account.getToken() != null)
|
|
|
|
values.put(Sqlite.COL_OAUTHTOKEN, account.getToken());
|
|
|
|
return db.update(Sqlite.TABLE_USER_ACCOUNT,
|
|
|
|
values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?",
|
2020-09-25 18:58:04 +02:00
|
|
|
new String[]{account.getId(), account.getHost()});
|
2020-06-26 08:50:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-27 18:33:51 +02:00
|
|
|
/**
|
|
|
|
* Update an Account token in database
|
|
|
|
*
|
|
|
|
* @param token Token
|
|
|
|
* @return boolean
|
|
|
|
*/
|
2020-09-29 17:42:15 +02:00
|
|
|
public int updateAccountToken(Token token) {
|
2020-09-27 18:33:51 +02:00
|
|
|
ContentValues values = new ContentValues();
|
|
|
|
if (token.getRefresh_token() != null) {
|
|
|
|
values.put(Sqlite.COL_REFRESH_TOKEN, token.getRefresh_token());
|
|
|
|
}
|
|
|
|
if (token.getAccess_token() != null)
|
|
|
|
values.put(Sqlite.COL_OAUTHTOKEN, token.getAccess_token());
|
2020-09-29 17:42:15 +02:00
|
|
|
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
|
|
|
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
|
|
|
String instance = Helper.getLiveInstance(context);
|
|
|
|
try {
|
|
|
|
return db.update(Sqlite.TABLE_USER_ACCOUNT,
|
|
|
|
values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?",
|
|
|
|
new String[]{userId, instance});
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
return -1;
|
2020-09-27 18:33:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-26 08:50:49 +02:00
|
|
|
/**
|
|
|
|
* Update an Account in database
|
|
|
|
*
|
|
|
|
* @param account Account
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public int updateAccountCredential(Account account) {
|
|
|
|
ContentValues values = new ContentValues();
|
2020-09-25 18:58:04 +02:00
|
|
|
if (account.getCreatedAt() == null)
|
|
|
|
account.setCreatedAt(new Date());
|
|
|
|
if (account.getDescription() == null)
|
|
|
|
account.setDescription("");
|
2020-09-27 16:33:43 +02:00
|
|
|
values.put(Sqlite.COL_USERNAME, account.getUsername());
|
|
|
|
values.put(Sqlite.COL_ACCT, account.getUsername() + "@" + account.getHost());
|
2020-09-25 18:58:04 +02:00
|
|
|
values.put(Sqlite.COL_DISPLAYED_NAME, account.getDisplayName());
|
|
|
|
values.put(Sqlite.COL_FOLLOWERS_COUNT, account.getFollowersCount());
|
|
|
|
values.put(Sqlite.COL_FOLLOWING_COUNT, account.getFollowingCount());
|
|
|
|
values.put(Sqlite.COL_NOTE, account.getDescription());
|
2020-06-26 08:50:49 +02:00
|
|
|
values.put(Sqlite.COL_URL, account.getUrl());
|
2020-09-25 18:58:04 +02:00
|
|
|
values.put(Sqlite.COL_AVATAR, account.getAvatar().getPath());
|
|
|
|
values.put(Sqlite.COL_CREATED_AT, Helper.dateToString(account.getCreatedAt()));
|
2020-06-26 08:50:49 +02:00
|
|
|
|
2020-07-25 19:15:26 +02:00
|
|
|
if (account.getClient_id() != null && account.getClient_secret() != null) {
|
2020-06-26 08:50:49 +02:00
|
|
|
values.put(Sqlite.COL_CLIENT_ID, account.getClient_id());
|
|
|
|
values.put(Sqlite.COL_CLIENT_SECRET, account.getClient_secret());
|
2020-07-25 19:15:26 +02:00
|
|
|
}
|
2020-09-03 18:56:48 +02:00
|
|
|
if (account.getRefresh_token() != null) {
|
2020-06-26 08:50:49 +02:00
|
|
|
values.put(Sqlite.COL_REFRESH_TOKEN, account.getRefresh_token());
|
|
|
|
}
|
|
|
|
if (account.getToken() != null)
|
|
|
|
values.put(Sqlite.COL_OAUTHTOKEN, account.getToken());
|
|
|
|
return db.update(Sqlite.TABLE_USER_ACCOUNT,
|
|
|
|
values, Sqlite.COL_USER_ID + " = ? AND " + Sqlite.COL_INSTANCE + " =?",
|
2020-09-25 18:58:04 +02:00
|
|
|
new String[]{account.getId(), account.getHost()});
|
2020-06-26 08:50:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public int removeUser(Account account) {
|
|
|
|
return db.delete(Sqlite.TABLE_USER_ACCOUNT, Sqlite.COL_USER_ID + " = '" + account.getId() +
|
2020-09-25 18:58:04 +02:00
|
|
|
"' AND " + Sqlite.COL_INSTANCE + " = '" + account.getHost() + "'", null);
|
2020-06-26 08:50:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns last used account
|
|
|
|
*
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public Account getLastUsedAccount() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_OAUTHTOKEN + " != 'null'", null, null, null, Sqlite.COL_UPDATED_AT + " DESC", "1");
|
|
|
|
return cursorToUser(c);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns all Account in db
|
|
|
|
*
|
|
|
|
* @return Account List<Account>
|
|
|
|
*/
|
|
|
|
public List<Account> getAllAccount() {
|
|
|
|
|
|
|
|
try {
|
|
|
|
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, null, null, null, null, Sqlite.COL_INSTANCE + " ASC", null);
|
|
|
|
return cursorToListUser(c);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an Account by token
|
|
|
|
*
|
|
|
|
* @param token String
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public Account getAccountByToken(String token) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_OAUTHTOKEN + " = \"" + token + "\"", null, null, null, null, "1");
|
|
|
|
return cursorToUser(c);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an Account by token
|
|
|
|
*
|
|
|
|
* @param userId String
|
|
|
|
* @param instance String
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
public Account getUniqAccount(String userId, String instance) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_USER_ID + " = \"" + userId + "\" AND " + Sqlite.COL_INSTANCE + " = \"" + instance + "\"", null, null, null, null, "1");
|
|
|
|
return cursorToUser(c);
|
|
|
|
} catch (Exception e) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test if the current user is already stored in data base
|
|
|
|
*
|
|
|
|
* @param account Account
|
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public boolean userExist(Account account) {
|
|
|
|
Cursor mCount = db.rawQuery("select count(*) from " + Sqlite.TABLE_USER_ACCOUNT
|
2020-09-27 16:33:43 +02:00
|
|
|
+ " where " + Sqlite.COL_USERNAME + " = '" + account.getUsername() + "' AND " + Sqlite.COL_INSTANCE + " = '" + account.getHost() + "'", null);
|
2020-06-26 08:50:49 +02:00
|
|
|
mCount.moveToFirst();
|
|
|
|
int count = mCount.getInt(0);
|
|
|
|
mCount.close();
|
|
|
|
return (count > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-25 18:58:04 +02:00
|
|
|
/***
|
|
|
|
* Method to hydrate an Accounts from database
|
|
|
|
* @param c Cursor
|
|
|
|
* @return List<Account>
|
|
|
|
*/
|
|
|
|
private List<Account> cursorToListUser(Cursor c) {
|
|
|
|
//No element found
|
|
|
|
if (c.getCount() == 0) {
|
|
|
|
c.close();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
List<Account> accounts = new ArrayList<>();
|
|
|
|
while (c.moveToNext()) {
|
|
|
|
Account account = cursorToUser(c);
|
|
|
|
accounts.add(account);
|
|
|
|
}
|
|
|
|
//Close the cursor
|
|
|
|
c.close();
|
|
|
|
//Users list is returned
|
|
|
|
return accounts;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-26 08:50:49 +02:00
|
|
|
/***
|
|
|
|
* Method to hydrate an Account from database
|
|
|
|
* @param c Cursor
|
|
|
|
* @return Account
|
|
|
|
*/
|
|
|
|
private Account cursorToUser(Cursor c) {
|
|
|
|
//No element found
|
|
|
|
if (c.getCount() == 0) {
|
|
|
|
c.close();
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
//Take the first element
|
|
|
|
c.moveToFirst();
|
|
|
|
//New user
|
|
|
|
Account account = new Account();
|
|
|
|
account.setId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
|
2020-09-27 16:33:43 +02:00
|
|
|
account.setUsername(c.getString(c.getColumnIndex(Sqlite.COL_USERNAME)));
|
2020-09-25 18:58:04 +02:00
|
|
|
account.setDisplayName(c.getString(c.getColumnIndex(Sqlite.COL_DISPLAYED_NAME)));
|
|
|
|
account.setFollowersCount(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWERS_COUNT)));
|
|
|
|
account.setFollowingCount(c.getInt(c.getColumnIndex(Sqlite.COL_FOLLOWING_COUNT)));
|
|
|
|
account.setDescription(c.getString(c.getColumnIndex(Sqlite.COL_NOTE)));
|
2020-06-26 08:50:49 +02:00
|
|
|
account.setUrl(c.getString(c.getColumnIndex(Sqlite.COL_URL)));
|
2020-09-25 18:58:04 +02:00
|
|
|
Avatar avatar = new Avatar();
|
|
|
|
avatar.setPath(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR)));
|
|
|
|
account.setAvatar(avatar);
|
|
|
|
account.setUpdatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_UPDATED_AT))));
|
|
|
|
account.setCreatedAt(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
|
|
|
|
account.setHost(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
|
2020-06-26 08:50:49 +02:00
|
|
|
account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN)));
|
|
|
|
account.setClient_id(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_ID)));
|
|
|
|
account.setClient_secret(c.getString(c.getColumnIndex(Sqlite.COL_CLIENT_SECRET)));
|
|
|
|
account.setRefresh_token(c.getString(c.getColumnIndex(Sqlite.COL_REFRESH_TOKEN)));
|
|
|
|
//Close the cursor
|
|
|
|
c.close();
|
|
|
|
|
|
|
|
//User is returned
|
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|