From d48cdd1c3200cd0f1db8e0f72f2287d528232c8c Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 28 Apr 2018 17:38:26 +0200 Subject: [PATCH] Can clear local database from the application. --- .../client/data/helpers/BaseHelper.java | 38 +++++++++++++++ .../client/data/helpers/DatabaseHelper.java | 8 ++-- .../client/data/helpers/DebugHelper.java | 46 +++++++++++++++++++ .../client/ui/activities/MainActivity.java | 19 ++++++++ app/src/main/res/menu/debug_menu.xml | 2 +- app/src/main/res/values/strings.xml | 4 +- 6 files changed, 111 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/helpers/BaseHelper.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/data/helpers/DebugHelper.java diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/BaseHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/BaseHelper.java new file mode 100644 index 0000000..ec94211 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/BaseHelper.java @@ -0,0 +1,38 @@ +package org.communiquons.android.comunic.client.data.helpers; + +import android.content.Context; + +/** + * Base helper + * + * This helper is intended to be inherited by all the other helpers + * + * @author Pierre HUBERT + * Created by pierre on 4/28/18. + */ + +class BaseHelper { + + /** + * The context of the application + */ + private Context mContext; + + /** + * Package-private constructor + * + * @param context The context of the application + */ + BaseHelper(Context context){ + this.mContext = context.getApplicationContext(); + } + + /** + * Get the context of the application + * + * @return The context + */ + public Context getContext() { + return mContext; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/DatabaseHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/DatabaseHelper.java index c29988a..760d07b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/DatabaseHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/DatabaseHelper.java @@ -127,9 +127,9 @@ public class DatabaseHelper extends SQLiteOpenHelper { * * @param db Database object */ - public void init_db(SQLiteDatabase db){ + void init_db(SQLiteDatabase db){ - //Create user informations table + //Create user information table db.execSQL(SQL_CREATE_USERS_INFOS_TABLE); //Create friends list table @@ -147,8 +147,8 @@ public class DatabaseHelper extends SQLiteOpenHelper { * * @param db The database */ - public void clear_db(SQLiteDatabase db){ - //Delete users informations table + void clear_db(SQLiteDatabase db){ + //Delete users information table db.execSQL(SQL_DELETE_USERS_INFOS_TABLE); //Delete friends list table diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/DebugHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/DebugHelper.java new file mode 100644 index 0000000..f8a6e9c --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/helpers/DebugHelper.java @@ -0,0 +1,46 @@ +package org.communiquons.android.comunic.client.data.helpers; + +import android.content.Context; +import android.database.SQLException; +import android.database.sqlite.SQLiteDatabase; + +/** + * Debug helper + * + * This helpers handles all the debug operations made by the user other the application + * + * @author Pierre HUBERT + * Created by pierre on 4/28/18. + */ + +public class DebugHelper extends BaseHelper { + + /** + * Public constructor + * + * @param context The context of the application + */ + public DebugHelper(Context context) { + super(context); + } + + /** + * Clear the database of the application, setting it default values + * + * @return TRUE for a success / FALSE else + */ + public boolean clearLocalDatabase(){ + + try { + DatabaseHelper dbHelper = DatabaseHelper.getInstance(getContext()); + SQLiteDatabase db = dbHelper.getWritableDatabase(); + dbHelper.clear_db(db); + dbHelper.init_db(db); + } catch (SQLException e){ + e.printStackTrace(); + return false; + } + + return true; + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java index 2746d2a..c50daf2 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java @@ -21,6 +21,7 @@ import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.crashreporter.CrashReporter; import org.communiquons.android.comunic.client.data.helpers.APIRequestHelper; import org.communiquons.android.comunic.client.data.helpers.AccountHelper; +import org.communiquons.android.comunic.client.data.helpers.DebugHelper; import org.communiquons.android.comunic.client.data.utils.AccountUtils; import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper; @@ -200,6 +201,12 @@ public class MainActivity extends AppCompatActivity implements openConversationL return true; } + //Check if user wants to clear database + if(id == R.id.action_clear_local_db){ + clearLocalDatabase(); + return true; + } + return super.onOptionsItemSelected(item); } @@ -495,4 +502,16 @@ public class MainActivity extends AppCompatActivity implements openConversationL transaction.replace(R.id.main_fragment, singlePostFragment); transaction.commit(); } + + /** + * Clear the cache database of the application + */ + private void clearLocalDatabase() { + + if(!new DebugHelper(this).clearLocalDatabase()) + Toast.makeText(this, R.string.err_clear_local_db, Toast.LENGTH_SHORT).show(); + else + Toast.makeText(this, R.string.success_clear_local_db, Toast.LENGTH_SHORT).show(); + } + } diff --git a/app/src/main/res/menu/debug_menu.xml b/app/src/main/res/menu/debug_menu.xml index 9a3675b..79b228d 100644 --- a/app/src/main/res/menu/debug_menu.xml +++ b/app/src/main/res/menu/debug_menu.xml @@ -2,7 +2,7 @@ \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 4353ae8..a2eabd7 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -179,7 +179,9 @@ You do not have any friend yet. An error occurred while trying to get older messages for the conversation! Enable debug mode - Enable this option to get access to development / debug features. + Enable this option to get access to development / debug features. (Need application restart to make effect) Debug Clear local database + The local database has been successfully cleared! You may need to restart the application to make the changes being fully applied. + An error occurred while trying to clear local database!