Skip to content

Commit

Permalink
Clear local database on logout.
Browse files Browse the repository at this point in the history
  • Loading branch information
aforge committed Feb 25, 2023
1 parent 310aa5a commit 8bd61dd
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 1 deletion.
16 changes: 16 additions & 0 deletions app/src/main/java/co/tinode/tindroid/db/AccountDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.util.Log;

/**
* List of Tinode accounts.
Expand All @@ -21,6 +22,7 @@ public class AccountDb implements BaseColumns {
private static final String COLUMN_NAME_HOST_URI = "host_uri";
private static final String COLUMN_NAME_CRED_METHODS = "cred_methods";
private static final String COLUMN_NAME_DEVICE_ID = "device_id";
private static final String TAG = "AccountDb";

/**
* Statements to drop accounts table and index
Expand Down Expand Up @@ -120,6 +122,20 @@ static void delete(SQLiteDatabase db, StoredAccount acc) {
db.delete(TABLE_NAME, _ID + "=" + acc.id, null);
}

/**
* Deletes all records from 'accounts' table.
*
* @param db Database to use.
*/
static void truncateTable(SQLiteDatabase db) {
try {
// 'DELETE FROM table' in SQLite is equivalent to truncation.
db.delete(TABLE_NAME, null, null);
} catch (SQLException ex) {
Log.w(TAG, "Delete failed", ex);
}
}

static StoredAccount getByUid(SQLiteDatabase db, String uid) {
if (uid == null) {
return null;
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/co/tinode/tindroid/db/BaseDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,15 @@ void setUid(String uid, String hostURI) {
}
}

void clearDb() {
SQLiteDatabase db = sInstance.getWritableDatabase();
MessageDb.truncateTable(db);
SubscriberDb.truncateTable(db);
TopicDb.truncateTable(db);
UserDb.truncateTable(db);
AccountDb.truncateTable(db);
}

void updateCredentials(String[] credMethods) {
if (mAcc != null) {
if (AccountDb.updateCredentials(sInstance.getWritableDatabase(), credMethods)) {
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/co/tinode/tindroid/db/MessageDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,20 @@ static void deleteAll(SQLiteDatabase db, long topicId) {
}
}

/**
* Deletes all records from 'messages' table.
*
* @param db Database to use.
*/
static void truncateTable(SQLiteDatabase db) {
try {
// 'DELETE FROM table' in SQLite is equivalent to truncation.
db.delete(TABLE_NAME, null, null);
} catch (SQLException ex) {
Log.w(TAG, "Delete failed", ex);
}
}

/**
* Delete failed messages in a given topic.
*
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/co/tinode/tindroid/db/SqlStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import android.util.Log;

import java.io.Closeable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
Expand Down Expand Up @@ -82,7 +81,9 @@ public boolean isReady() {
}

public void logout() {
// Clear the database.
mDbh.setUid(null, null);
mDbh.clearDb();
}

@Override
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/co/tinode/tindroid/db/SubscriberDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,20 @@ static boolean deleteForTopic(SQLiteDatabase db, long topicId) {
return db.delete(TABLE_NAME, COLUMN_NAME_TOPIC_ID + "=" + topicId, null) > 0;
}

/**
* Deletes all records from 'subscribers' table.
*
* @param db Database to use.
*/
static void truncateTable(SQLiteDatabase db) {
try {
// 'DELETE FROM table' in SQLite is equivalent to truncation.
db.delete(TABLE_NAME, null, null);
} catch (SQLException ex) {
Log.w(TAG, "Delete failed", ex);
}
}

/**
* Get the next available senderId for the given topic. Min Id == 1.
*
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/co/tinode/tindroid/db/TopicDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,20 @@ static void deleteAll(SQLiteDatabase db, long accId) {
db.delete(TABLE_NAME, COLUMN_NAME_ACCOUNT_ID + "=" + accId, null);
}

/**
* Deletes all records from 'topics' table.
*
* @param db Database to use.
*/
static void truncateTable(SQLiteDatabase db) {
try {
// 'DELETE FROM table' in SQLite is equivalent to truncation.
db.delete(TABLE_NAME, null, null);
} catch (SQLException ex) {
Log.w(TAG, "Delete failed", ex);
}
}

/**
* Given topic name, get it's database _id
*
Expand Down
17 changes: 17 additions & 0 deletions app/src/main/java/co/tinode/tindroid/db/UserDb.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.content.ContentValues;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.util.Log;

import java.util.Date;

Expand Down Expand Up @@ -80,6 +82,7 @@ public class UserDb implements BaseColumns {
*/
static final String DROP_INDEX =
"DROP INDEX IF EXISTS " + INDEX_NAME;
private static final String TAG = "UserDb";

/**
* Save user to DB
Expand Down Expand Up @@ -168,6 +171,20 @@ static void deleteAll(SQLiteDatabase db, long accId) {
db.delete(TABLE_NAME, COLUMN_NAME_ACCOUNT_ID + "=" + accId, null);
}

/**
* Deletes all records from 'users' table.
*
* @param db Database to use.
*/
static void truncateTable(SQLiteDatabase db) {
try {
// 'DELETE FROM table' in SQLite is equivalent to truncation.
db.delete(TABLE_NAME, null, null);
} catch (SQLException ex) {
Log.w(TAG, "Delete failed", ex);
}
}

/**
* Given UID, get it's database _id
*
Expand Down

0 comments on commit 8bd61dd

Please sign in to comment.