DB changes + logout for user

This commit is contained in:
stom79 2019-01-29 11:52:54 +01:00
parent 2f9abc4873
commit cb4409df69
4 changed files with 72 additions and 1 deletions

View File

@ -82,6 +82,7 @@ public class Account implements Parcelable {
private String display_name, stored_displayname;
private boolean locked;
private Date created_at;
private Date updated_at;
private int followers_count;
private int following_count;
private int statuses_count;
@ -136,6 +137,7 @@ public class Account implements Parcelable {
dest.writeString(this.stored_displayname);
dest.writeByte(this.locked ? (byte) 1 : (byte) 0);
dest.writeLong(this.created_at != null ? this.created_at.getTime() : -1);
dest.writeLong(this.updated_at != null ? this.updated_at.getTime() : -1);
dest.writeInt(this.followers_count);
dest.writeInt(this.following_count);
dest.writeInt(this.statuses_count);
@ -186,6 +188,8 @@ public class Account implements Parcelable {
this.locked = in.readByte() != 0;
long tmpCreated_at = in.readLong();
this.created_at = tmpCreated_at == -1 ? null : new Date(tmpCreated_at);
long tmpUpdated_at = in.readLong();
this.updated_at = tmpUpdated_at == -1 ? null : new Date(tmpUpdated_at);
this.followers_count = in.readInt();
this.following_count = in.readInt();
this.statuses_count = in.readInt();
@ -372,6 +376,14 @@ public class Account implements Parcelable {
isAdmin = admin;
}
public Date getUpdated_at() {
return updated_at;
}
public void setUpdated_at(Date updated_at) {
this.updated_at = updated_at;
}
public enum followAction{
FOLLOW,

View File

@ -543,6 +543,45 @@ public class Helper {
editor.apply();
}
/**
* Log out the authenticated user by removing its token
* @param context Context
*/
public static void logoutCurrentUser(Context context) {
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
//Current user
String currentToken = sharedpreferences.getString(PREF_KEY_OAUTH_TOKEN, null);
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
Account account = new AccountDAO(context, db).getAccountByToken(currentToken);
account.setToken("null");
new AccountDAO(context, db).updateAccount(account);
Account newAccount = new AccountDAO(context, db).getLastUsedAccount();
SharedPreferences.Editor editor = sharedpreferences.edit();
if( newAccount == null){
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, null);
editor.putString(Helper.CLIENT_ID, null);
editor.putString(Helper.CLIENT_SECRET, null);
editor.putString(Helper.PREF_KEY_ID, null);
editor.putBoolean(Helper.PREF_IS_MODERATOR, false);
editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, false);
editor.putString(Helper.PREF_INSTANCE, null);
editor.putString(Helper.ID, null);
editor.apply();
}else{
editor.putString(Helper.PREF_KEY_OAUTH_TOKEN, newAccount.getToken());
editor.putString(Helper.PREF_KEY_ID, newAccount.getId());
editor.putString(Helper.PREF_INSTANCE, newAccount.getInstance().trim());
editor.putBoolean(Helper.PREF_IS_MODERATOR, newAccount.isModerator());
editor.putBoolean(Helper.PREF_IS_ADMINISTRATOR, newAccount.isAdmin());
editor.commit();
Intent changeAccount = new Intent(context, MainActivity.class);
changeAccount.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
((Activity)context).finish();
context.startActivity(changeAccount);
}
}
/**
* Convert String date from Mastodon

View File

@ -148,6 +148,20 @@ public class AccountDAO {
}
}
/**
* 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 an Account by its id and acct
* @param accountId String
@ -252,6 +266,7 @@ public class AccountDAO {
account.setAvatar_static(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR_STATIC)));
account.setHeader(c.getString(c.getColumnIndex(Sqlite.COL_HEADER)));
account.setHeader_static(c.getString(c.getColumnIndex(Sqlite.COL_HEADER_STATIC)));
account.setUpdated_at(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_UPDATED_AT))));
account.setCreated_at(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
account.setInstance(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
account.setEmojis(Helper.restoreEmojisFromString(c.getString(c.getColumnIndex(Sqlite.COL_EMOJIS))));
@ -295,6 +310,7 @@ public class AccountDAO {
account.setAvatar_static(c.getString(c.getColumnIndex(Sqlite.COL_AVATAR_STATIC)));
account.setHeader(c.getString(c.getColumnIndex(Sqlite.COL_HEADER)));
account.setHeader_static(c.getString(c.getColumnIndex(Sqlite.COL_HEADER_STATIC)));
account.setUpdated_at(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_UPDATED_AT))));
account.setCreated_at(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_CREATED_AT))));
account.setInstance(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
account.setToken(c.getString(c.getColumnIndex(Sqlite.COL_OAUTHTOKEN)));

View File

@ -26,7 +26,7 @@ import android.database.sqlite.SQLiteOpenHelper;
public class Sqlite extends SQLiteOpenHelper {
public static final int DB_VERSION = 25;
public static final int DB_VERSION = 26;
public static final String DB_NAME = "mastodon_etalab_db";
public static SQLiteDatabase db;
private static Sqlite sInstance;
@ -88,6 +88,7 @@ public class Sqlite extends SQLiteOpenHelper {
static final String COL_REFRESH_TOKEN = "REFRESH_TOKEN";
static final String COL_IS_MODERATOR = "IS_MODERATOR";
static final String COL_IS_ADMIN = "IS_ADMIN";
static final String COL_UPDATED_AT = "UPDATED_AT";
private static final String CREATE_TABLE_USER_ACCOUNT = "CREATE TABLE " + TABLE_USER_ACCOUNT + " ("
+ COL_USER_ID + " TEXT PRIMARY KEY, " + COL_USERNAME + " TEXT NOT NULL, " + COL_ACCT + " TEXT NOT NULL, "
@ -101,6 +102,7 @@ public class Sqlite extends SQLiteOpenHelper {
+ COL_IS_MODERATOR + " INTEGER DEFAULT 0, "
+ COL_IS_ADMIN + " INTEGER DEFAULT 0, "
+ COL_CLIENT_ID + " TEXT, " + COL_CLIENT_SECRET + " TEXT, " + COL_REFRESH_TOKEN + " TEXT,"
+ COL_UPDATED_AT + " TEXT, "
+ COL_INSTANCE + " TEXT NOT NULL, " + COL_OAUTHTOKEN + " TEXT NOT NULL, " + COL_CREATED_AT + " TEXT NOT NULL)";
@ -325,6 +327,8 @@ public class Sqlite extends SQLiteOpenHelper {
case 24:
db.execSQL("ALTER TABLE " + TABLE_USER_ACCOUNT + " ADD COLUMN " + COL_IS_MODERATOR + " INTEGER DEFAULT 0");
db.execSQL("ALTER TABLE " + TABLE_USER_ACCOUNT + " ADD COLUMN " + COL_IS_ADMIN + " INTEGER DEFAULT 0");
case 25:
db.execSQL("ALTER TABLE " + TABLE_USER_ACCOUNT + " ADD COLUMN " + COL_UPDATED_AT + " TEXT");
default:
break;
}