mirror of
https://github.com/pierre42100/ComunicAndroid
synced 2024-11-23 22:09:30 +00:00
Automatically clean users information cache.
This commit is contained in:
parent
81f5a80ed8
commit
10d382ccb6
@ -15,7 +15,7 @@ public final class DatabaseContract {
|
|||||||
public DatabaseContract(){}
|
public DatabaseContract(){}
|
||||||
|
|
||||||
/* Database basic information */
|
/* Database basic information */
|
||||||
public static final int DATABASE_VERSION = 4;
|
public static final int DATABASE_VERSION = 5;
|
||||||
public static final String DATABASE_NAME = "database.db";
|
public static final String DATABASE_NAME = "database.db";
|
||||||
|
|
||||||
/* Users info table */
|
/* Users info table */
|
||||||
@ -23,6 +23,7 @@ public final class DatabaseContract {
|
|||||||
public static final String TABLE_NAME = "users_info";
|
public static final String TABLE_NAME = "users_info";
|
||||||
|
|
||||||
public static final String COLUMN_NAME_USER_ID = "user_id";
|
public static final String COLUMN_NAME_USER_ID = "user_id";
|
||||||
|
public static final String COLUMN_NAME_TIME_INSERT = "time_insert";
|
||||||
public static final String COLUMN_NAME_USER_FIRSTNAME = "first_name";
|
public static final String COLUMN_NAME_USER_FIRSTNAME = "first_name";
|
||||||
public static final String COLUMN_NAME_USER_LASTNAME = "last_name";
|
public static final String COLUMN_NAME_USER_LASTNAME = "last_name";
|
||||||
public static final String COLUMN_NAME_USER_ACCOUNT_IMAGE = "account_image";
|
public static final String COLUMN_NAME_USER_ACCOUNT_IMAGE = "account_image";
|
||||||
|
@ -33,6 +33,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
|
|||||||
"CREATE TABLE " + UsersInfoSchema.TABLE_NAME + " (" +
|
"CREATE TABLE " + UsersInfoSchema.TABLE_NAME + " (" +
|
||||||
UsersInfoSchema._ID + " INTEGER PRIMARY KEY," +
|
UsersInfoSchema._ID + " INTEGER PRIMARY KEY," +
|
||||||
UsersInfoSchema.COLUMN_NAME_USER_ID + INTEGER_TYPE + COMMA_SEP +
|
UsersInfoSchema.COLUMN_NAME_USER_ID + INTEGER_TYPE + COMMA_SEP +
|
||||||
|
UsersInfoSchema.COLUMN_NAME_TIME_INSERT + INTEGER_TYPE + COMMA_SEP +
|
||||||
UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME + TEXT_TYPE + COMMA_SEP +
|
UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME + TEXT_TYPE + COMMA_SEP +
|
||||||
UsersInfoSchema.COLUMN_NAME_USER_LASTNAME + TEXT_TYPE + COMMA_SEP +
|
UsersInfoSchema.COLUMN_NAME_USER_LASTNAME + TEXT_TYPE + COMMA_SEP +
|
||||||
UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE + TEXT_TYPE
|
UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE + TEXT_TYPE
|
||||||
|
@ -38,7 +38,7 @@ public class GetUsersHelper {
|
|||||||
/**
|
/**
|
||||||
* User information database helper
|
* User information database helper
|
||||||
*/
|
*/
|
||||||
private UsersInfosDbHelper udbHelper = null;
|
private UsersInfoDbHelper udbHelper = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Public constructor of the class
|
* Public constructor of the class
|
||||||
@ -55,7 +55,7 @@ public class GetUsersHelper {
|
|||||||
* @param context The context of execution of the application
|
* @param context The context of execution of the application
|
||||||
* @param udbHelper User database helper
|
* @param udbHelper User database helper
|
||||||
*/
|
*/
|
||||||
public GetUsersHelper(@NonNull Context context, @NonNull UsersInfosDbHelper udbHelper){
|
public GetUsersHelper(@NonNull Context context, @NonNull UsersInfoDbHelper udbHelper){
|
||||||
mContext = context.getApplicationContext();
|
mContext = context.getApplicationContext();
|
||||||
this.udbHelper = udbHelper;
|
this.udbHelper = udbHelper;
|
||||||
}
|
}
|
||||||
@ -68,7 +68,7 @@ public class GetUsersHelper {
|
|||||||
*/
|
*/
|
||||||
public GetUsersHelper(@NonNull Context context, @NonNull DatabaseHelper dbHelper){
|
public GetUsersHelper(@NonNull Context context, @NonNull DatabaseHelper dbHelper){
|
||||||
mContext = context;
|
mContext = context;
|
||||||
this.udbHelper = new UsersInfosDbHelper(dbHelper);
|
this.udbHelper = new UsersInfoDbHelper(dbHelper);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -6,6 +6,10 @@ import android.database.sqlite.SQLiteDatabase;
|
|||||||
|
|
||||||
import org.communiquons.android.comunic.client.data.DatabaseContract.UsersInfoSchema;
|
import org.communiquons.android.comunic.client.data.DatabaseContract.UsersInfoSchema;
|
||||||
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
import org.communiquons.android.comunic.client.data.models.UserInfo;
|
||||||
|
import org.communiquons.android.comunic.client.data.utils.TimeUtils;
|
||||||
|
|
||||||
|
import static org.communiquons.android.comunic.client.data.DatabaseContract.UsersInfoSchema.COLUMN_NAME_TIME_INSERT;
|
||||||
|
import static org.communiquons.android.comunic.client.data.DatabaseContract.UsersInfoSchema.TABLE_NAME;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Users information helpers
|
* Users information helpers
|
||||||
@ -16,7 +20,12 @@ import org.communiquons.android.comunic.client.data.models.UserInfo;
|
|||||||
* Created by pierre on 11/2/17.
|
* Created by pierre on 11/2/17.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class UsersInfosDbHelper {
|
class UsersInfoDbHelper {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Max age of user information in cache
|
||||||
|
*/
|
||||||
|
private static final int USER_INFO_MAX_AGE = 36000; //10 hours
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Database helper
|
* Database helper
|
||||||
@ -28,8 +37,9 @@ class UsersInfosDbHelper {
|
|||||||
*
|
*
|
||||||
* @param dbHelper Database helper object
|
* @param dbHelper Database helper object
|
||||||
*/
|
*/
|
||||||
UsersInfosDbHelper(DatabaseHelper dbHelper){
|
UsersInfoDbHelper(DatabaseHelper dbHelper){
|
||||||
this.dbHelper = dbHelper;
|
this.dbHelper = dbHelper;
|
||||||
|
clean();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,7 +61,7 @@ class UsersInfosDbHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check wether a user is present in the database or not
|
* Check whether a user is present in the database or not
|
||||||
*
|
*
|
||||||
* @param userID The user to research on the database
|
* @param userID The user to research on the database
|
||||||
* @return boolean True if the user exists / false else
|
* @return boolean True if the user exists / false else
|
||||||
@ -73,7 +83,7 @@ class UsersInfosDbHelper {
|
|||||||
|
|
||||||
//Perform the request on the database
|
//Perform the request on the database
|
||||||
Cursor c = db.query(
|
Cursor c = db.query(
|
||||||
UsersInfoSchema.TABLE_NAME,
|
TABLE_NAME,
|
||||||
projection,
|
projection,
|
||||||
selection,
|
selection,
|
||||||
selectionArgs,
|
selectionArgs,
|
||||||
@ -87,9 +97,6 @@ class UsersInfosDbHelper {
|
|||||||
//Close cursor
|
//Close cursor
|
||||||
c.close();
|
c.close();
|
||||||
|
|
||||||
//Close the database
|
|
||||||
//db.close();
|
|
||||||
|
|
||||||
return number_entries > 0;
|
return number_entries > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,17 +112,10 @@ class UsersInfosDbHelper {
|
|||||||
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
||||||
|
|
||||||
//Prepare the insertion
|
//Prepare the insertion
|
||||||
ContentValues newValues = new ContentValues();
|
ContentValues newValues = UserInfoToDb(user);
|
||||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_ID, user.getId());
|
|
||||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME, user.getFirstName());
|
|
||||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_LASTNAME, user.getLastName());
|
|
||||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE, user.getAcountImageURL());
|
|
||||||
|
|
||||||
//Insert it
|
//Insert it
|
||||||
long newRowId = db.insert(UsersInfoSchema.TABLE_NAME, null, newValues);
|
long newRowId = db.insert(TABLE_NAME, null, newValues);
|
||||||
|
|
||||||
//Close the database
|
|
||||||
//db.close();
|
|
||||||
|
|
||||||
return (int) newRowId;
|
return (int) newRowId;
|
||||||
}
|
}
|
||||||
@ -150,7 +150,7 @@ class UsersInfosDbHelper {
|
|||||||
|
|
||||||
//Perform the request
|
//Perform the request
|
||||||
Cursor c = db.query(
|
Cursor c = db.query(
|
||||||
UsersInfoSchema.TABLE_NAME,
|
TABLE_NAME,
|
||||||
requestedFields,
|
requestedFields,
|
||||||
selection,
|
selection,
|
||||||
selectionArgs,
|
selectionArgs,
|
||||||
@ -164,31 +164,14 @@ class UsersInfosDbHelper {
|
|||||||
result = null;
|
result = null;
|
||||||
else {
|
else {
|
||||||
|
|
||||||
//Initialize User object
|
|
||||||
result = new UserInfo();
|
|
||||||
|
|
||||||
c.moveToFirst();
|
|
||||||
|
|
||||||
//Extract the information and record them
|
//Extract the information and record them
|
||||||
result.setId(c.getInt(c.getColumnIndexOrThrow(
|
c.moveToFirst();
|
||||||
UsersInfoSchema.COLUMN_NAME_USER_ID)));
|
result = DbToUserInfo(c);
|
||||||
|
|
||||||
result.setFirstName(c.getString(c.getColumnIndexOrThrow(
|
|
||||||
UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME)));
|
|
||||||
|
|
||||||
result.setLastName(c.getString(c.getColumnIndexOrThrow(
|
|
||||||
UsersInfoSchema.COLUMN_NAME_USER_LASTNAME)));
|
|
||||||
|
|
||||||
result.setAccountImageURL(c.getString(c.getColumnIndexOrThrow(
|
|
||||||
UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE)));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//Close the cursor
|
//Close the cursor
|
||||||
c.close();
|
c.close();
|
||||||
|
|
||||||
//Close the database
|
|
||||||
//db.close();
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -208,10 +191,7 @@ class UsersInfosDbHelper {
|
|||||||
String[] conditionArgs = {""+userID};
|
String[] conditionArgs = {""+userID};
|
||||||
|
|
||||||
//Perform the request
|
//Perform the request
|
||||||
int result = db.delete(UsersInfoSchema.TABLE_NAME, condition, conditionArgs);
|
int result = db.delete(TABLE_NAME, condition, conditionArgs);
|
||||||
|
|
||||||
//Close database
|
|
||||||
//db.close();
|
|
||||||
|
|
||||||
return result > 0;
|
return result > 0;
|
||||||
}
|
}
|
||||||
@ -219,7 +199,7 @@ class UsersInfosDbHelper {
|
|||||||
/**
|
/**
|
||||||
* Update a user entry
|
* Update a user entry
|
||||||
*
|
*
|
||||||
* @param userInfo New informations about the user
|
* @param userInfo New information about the user
|
||||||
* @return True if the operation seems to be a success / false else
|
* @return True if the operation seems to be a success / false else
|
||||||
*/
|
*/
|
||||||
private boolean update(UserInfo userInfo){
|
private boolean update(UserInfo userInfo){
|
||||||
@ -228,10 +208,7 @@ class UsersInfosDbHelper {
|
|||||||
|
|
||||||
//Prepare the request
|
//Prepare the request
|
||||||
//Set the new values
|
//Set the new values
|
||||||
ContentValues newValues = new ContentValues();
|
ContentValues newValues = UserInfoToDb(userInfo);
|
||||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME, userInfo.getFirstName());
|
|
||||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_LASTNAME, userInfo.getLastName());
|
|
||||||
newValues.put(UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE, userInfo.getAcountImageURL());
|
|
||||||
|
|
||||||
//Set the condition
|
//Set the condition
|
||||||
String conditions = UsersInfoSchema.COLUMN_NAME_USER_ID + " = ?";
|
String conditions = UsersInfoSchema.COLUMN_NAME_USER_ID + " = ?";
|
||||||
@ -241,10 +218,51 @@ class UsersInfosDbHelper {
|
|||||||
|
|
||||||
|
|
||||||
//Perform the request
|
//Perform the request
|
||||||
int result = db.update(UsersInfoSchema.TABLE_NAME, newValues, conditions, conditionArgs);
|
return db.update(TABLE_NAME, newValues, conditions, conditionArgs) > 0;
|
||||||
|
|
||||||
//db.close();
|
}
|
||||||
|
|
||||||
return result > 0;
|
/**
|
||||||
|
* Remove old user data
|
||||||
|
*/
|
||||||
|
private void clean(){
|
||||||
|
SQLiteDatabase db = dbHelper.getWritableDatabase();
|
||||||
|
String whereClauses = COLUMN_NAME_TIME_INSERT + " < ?";
|
||||||
|
String[] whereArgs = {(TimeUtils.time() - USER_INFO_MAX_AGE)+""};
|
||||||
|
db.delete(TABLE_NAME, whereClauses, whereArgs);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn user information object into database entry
|
||||||
|
*
|
||||||
|
* @param user Information about the user
|
||||||
|
* @return Generated database entry
|
||||||
|
*/
|
||||||
|
private ContentValues UserInfoToDb(UserInfo user){
|
||||||
|
ContentValues values = new ContentValues();
|
||||||
|
values.put(UsersInfoSchema.COLUMN_NAME_USER_ID, user.getId());
|
||||||
|
values.put(UsersInfoSchema.COLUMN_NAME_TIME_INSERT, TimeUtils.time());
|
||||||
|
values.put(UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME, user.getFirstName());
|
||||||
|
values.put(UsersInfoSchema.COLUMN_NAME_USER_LASTNAME, user.getLastName());
|
||||||
|
values.put(UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE, user.getAcountImageURL());
|
||||||
|
return values;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn a database entry into a UserInfo object
|
||||||
|
*
|
||||||
|
* @param c The cursor to parse
|
||||||
|
* @return Generated user information
|
||||||
|
*/
|
||||||
|
private UserInfo DbToUserInfo(Cursor c){
|
||||||
|
UserInfo user = new UserInfo();
|
||||||
|
user.setId(c.getInt(c.getColumnIndexOrThrow(UsersInfoSchema.COLUMN_NAME_USER_ID)));
|
||||||
|
user.setFirstName(c.getString(c.getColumnIndexOrThrow(
|
||||||
|
UsersInfoSchema.COLUMN_NAME_USER_FIRSTNAME)));
|
||||||
|
user.setLastName(c.getString(c.getColumnIndexOrThrow(
|
||||||
|
UsersInfoSchema.COLUMN_NAME_USER_LASTNAME)));
|
||||||
|
user.setAccountImageURL(c.getString(c.getColumnIndexOrThrow(
|
||||||
|
UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE)));
|
||||||
|
return user;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user