mirror of
				https://github.com/pierre42100/ComunicAndroid
				synced 2025-11-04 11:34:06 +00:00 
			
		
		
		
	Automatically clean users information cache.
This commit is contained in:
		@@ -15,7 +15,7 @@ public final class DatabaseContract {
 | 
			
		||||
    public DatabaseContract(){}
 | 
			
		||||
 | 
			
		||||
    /* 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";
 | 
			
		||||
 | 
			
		||||
    /* Users info table */
 | 
			
		||||
@@ -23,6 +23,7 @@ public final class DatabaseContract {
 | 
			
		||||
        public static final String TABLE_NAME = "users_info";
 | 
			
		||||
 | 
			
		||||
        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_LASTNAME = "last_name";
 | 
			
		||||
        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 + " (" +
 | 
			
		||||
                    UsersInfoSchema._ID + " INTEGER PRIMARY KEY," +
 | 
			
		||||
                    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_LASTNAME + TEXT_TYPE + COMMA_SEP +
 | 
			
		||||
                    UsersInfoSchema.COLUMN_NAME_USER_ACCOUNT_IMAGE + TEXT_TYPE
 | 
			
		||||
 
 | 
			
		||||
@@ -38,7 +38,7 @@ public class GetUsersHelper {
 | 
			
		||||
    /**
 | 
			
		||||
     * User information database helper
 | 
			
		||||
     */
 | 
			
		||||
    private UsersInfosDbHelper udbHelper = null;
 | 
			
		||||
    private UsersInfoDbHelper udbHelper = null;
 | 
			
		||||
 | 
			
		||||
    /**
 | 
			
		||||
     * Public constructor of the class
 | 
			
		||||
@@ -55,7 +55,7 @@ public class GetUsersHelper {
 | 
			
		||||
     * @param context The context of execution of the application
 | 
			
		||||
     * @param udbHelper User database helper
 | 
			
		||||
     */
 | 
			
		||||
    public GetUsersHelper(@NonNull Context context, @NonNull UsersInfosDbHelper udbHelper){
 | 
			
		||||
    public GetUsersHelper(@NonNull Context context, @NonNull UsersInfoDbHelper udbHelper){
 | 
			
		||||
        mContext = context.getApplicationContext();
 | 
			
		||||
        this.udbHelper = udbHelper;
 | 
			
		||||
    }
 | 
			
		||||
@@ -68,7 +68,7 @@ public class GetUsersHelper {
 | 
			
		||||
     */
 | 
			
		||||
    public GetUsersHelper(@NonNull Context context, @NonNull DatabaseHelper dbHelper){
 | 
			
		||||
        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.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
 | 
			
		||||
@@ -16,7 +20,12 @@ import org.communiquons.android.comunic.client.data.models.UserInfo;
 | 
			
		||||
 * 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
 | 
			
		||||
@@ -28,8 +37,9 @@ class UsersInfosDbHelper {
 | 
			
		||||
     *
 | 
			
		||||
     * @param dbHelper Database helper object
 | 
			
		||||
     */
 | 
			
		||||
    UsersInfosDbHelper(DatabaseHelper dbHelper){
 | 
			
		||||
    UsersInfoDbHelper(DatabaseHelper 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
 | 
			
		||||
     * @return boolean True if the user exists / false else
 | 
			
		||||
@@ -73,7 +83,7 @@ class UsersInfosDbHelper {
 | 
			
		||||
 | 
			
		||||
        //Perform the request on the database
 | 
			
		||||
        Cursor c = db.query(
 | 
			
		||||
                UsersInfoSchema.TABLE_NAME,
 | 
			
		||||
                TABLE_NAME,
 | 
			
		||||
                projection,
 | 
			
		||||
                selection,
 | 
			
		||||
                selectionArgs,
 | 
			
		||||
@@ -87,9 +97,6 @@ class UsersInfosDbHelper {
 | 
			
		||||
        //Close cursor
 | 
			
		||||
        c.close();
 | 
			
		||||
 | 
			
		||||
        //Close the database
 | 
			
		||||
        //db.close();
 | 
			
		||||
 | 
			
		||||
        return number_entries > 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -105,17 +112,10 @@ class UsersInfosDbHelper {
 | 
			
		||||
        SQLiteDatabase db = dbHelper.getWritableDatabase();
 | 
			
		||||
 | 
			
		||||
        //Prepare the insertion
 | 
			
		||||
        ContentValues newValues = new ContentValues();
 | 
			
		||||
        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());
 | 
			
		||||
        ContentValues newValues = UserInfoToDb(user);
 | 
			
		||||
 | 
			
		||||
        //Insert it
 | 
			
		||||
        long newRowId = db.insert(UsersInfoSchema.TABLE_NAME, null, newValues);
 | 
			
		||||
 | 
			
		||||
        //Close the database
 | 
			
		||||
        //db.close();
 | 
			
		||||
        long newRowId = db.insert(TABLE_NAME, null, newValues);
 | 
			
		||||
 | 
			
		||||
        return (int) newRowId;
 | 
			
		||||
    }
 | 
			
		||||
@@ -150,7 +150,7 @@ class UsersInfosDbHelper {
 | 
			
		||||
 | 
			
		||||
        //Perform the request
 | 
			
		||||
        Cursor c = db.query(
 | 
			
		||||
                UsersInfoSchema.TABLE_NAME,
 | 
			
		||||
                TABLE_NAME,
 | 
			
		||||
                requestedFields,
 | 
			
		||||
                selection,
 | 
			
		||||
                selectionArgs,
 | 
			
		||||
@@ -164,31 +164,14 @@ class UsersInfosDbHelper {
 | 
			
		||||
            result = null;
 | 
			
		||||
        else {
 | 
			
		||||
 | 
			
		||||
            //Initialize User object
 | 
			
		||||
            result = new UserInfo();
 | 
			
		||||
 | 
			
		||||
            c.moveToFirst();
 | 
			
		||||
 | 
			
		||||
            //Extract the information and record them
 | 
			
		||||
            result.setId(c.getInt(c.getColumnIndexOrThrow(
 | 
			
		||||
                                UsersInfoSchema.COLUMN_NAME_USER_ID)));
 | 
			
		||||
 | 
			
		||||
            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)));
 | 
			
		||||
            c.moveToFirst();
 | 
			
		||||
            result = DbToUserInfo(c);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        //Close the cursor
 | 
			
		||||
        c.close();
 | 
			
		||||
 | 
			
		||||
        //Close the database
 | 
			
		||||
        //db.close();
 | 
			
		||||
 | 
			
		||||
        return result;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -208,10 +191,7 @@ class UsersInfosDbHelper {
 | 
			
		||||
        String[] conditionArgs = {""+userID};
 | 
			
		||||
 | 
			
		||||
        //Perform the request
 | 
			
		||||
        int result = db.delete(UsersInfoSchema.TABLE_NAME, condition, conditionArgs);
 | 
			
		||||
 | 
			
		||||
        //Close database
 | 
			
		||||
        //db.close();
 | 
			
		||||
        int result = db.delete(TABLE_NAME, condition, conditionArgs);
 | 
			
		||||
 | 
			
		||||
        return result > 0;
 | 
			
		||||
    }
 | 
			
		||||
@@ -219,7 +199,7 @@ class UsersInfosDbHelper {
 | 
			
		||||
    /**
 | 
			
		||||
     * 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
 | 
			
		||||
     */
 | 
			
		||||
    private boolean update(UserInfo userInfo){
 | 
			
		||||
@@ -228,10 +208,7 @@ class UsersInfosDbHelper {
 | 
			
		||||
 | 
			
		||||
        //Prepare the request
 | 
			
		||||
        //Set the new values
 | 
			
		||||
        ContentValues newValues = new ContentValues();
 | 
			
		||||
        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());
 | 
			
		||||
        ContentValues newValues = UserInfoToDb(userInfo);
 | 
			
		||||
 | 
			
		||||
        //Set the condition
 | 
			
		||||
        String conditions = UsersInfoSchema.COLUMN_NAME_USER_ID + " = ?";
 | 
			
		||||
@@ -241,10 +218,51 @@ class UsersInfosDbHelper {
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
        //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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user