Can clear local database from the application.

This commit is contained in:
Pierre 2018-04-28 17:38:26 +02:00
parent 1ba90958af
commit d48cdd1c32
6 changed files with 111 additions and 6 deletions

View File

@ -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;
}
}

View File

@ -127,9 +127,9 @@ public class DatabaseHelper extends SQLiteOpenHelper {
* *
* @param db Database object * @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); db.execSQL(SQL_CREATE_USERS_INFOS_TABLE);
//Create friends list table //Create friends list table
@ -147,8 +147,8 @@ public class DatabaseHelper extends SQLiteOpenHelper {
* *
* @param db The database * @param db The database
*/ */
public void clear_db(SQLiteDatabase db){ void clear_db(SQLiteDatabase db){
//Delete users informations table //Delete users information table
db.execSQL(SQL_DELETE_USERS_INFOS_TABLE); db.execSQL(SQL_DELETE_USERS_INFOS_TABLE);
//Delete friends list table //Delete friends list table

View File

@ -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;
}
}

View File

@ -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.crashreporter.CrashReporter;
import org.communiquons.android.comunic.client.data.helpers.APIRequestHelper; 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.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.utils.AccountUtils;
import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper; import org.communiquons.android.comunic.client.data.helpers.DatabaseHelper;
import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper; import org.communiquons.android.comunic.client.data.helpers.ConversationsListHelper;
@ -200,6 +201,12 @@ public class MainActivity extends AppCompatActivity implements openConversationL
return true; return true;
} }
//Check if user wants to clear database
if(id == R.id.action_clear_local_db){
clearLocalDatabase();
return true;
}
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
@ -495,4 +502,16 @@ public class MainActivity extends AppCompatActivity implements openConversationL
transaction.replace(R.id.main_fragment, singlePostFragment); transaction.replace(R.id.main_fragment, singlePostFragment);
transaction.commit(); 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();
}
} }

View File

@ -2,7 +2,7 @@
<menu xmlns:android="http://schemas.android.com/apk/res/android"> <menu xmlns:android="http://schemas.android.com/apk/res/android">
<item <item
android:id="@+id/clear_local_db" android:id="@+id/action_clear_local_db"
android:title="@string/menu_clear_local_db"/> android:title="@string/menu_clear_local_db"/>
</menu> </menu>

View File

@ -179,7 +179,9 @@
<string name="notice_no_friend">You do not have any friend yet.</string> <string name="notice_no_friend">You do not have any friend yet.</string>
<string name="err_get_older_conversation_messages">An error occurred while trying to get older messages for the conversation!</string> <string name="err_get_older_conversation_messages">An error occurred while trying to get older messages for the conversation!</string>
<string name="preference_debug_mode_title">Enable debug mode</string> <string name="preference_debug_mode_title">Enable debug mode</string>
<string name="preference_debug_mode_summary">Enable this option to get access to development / debug features.</string> <string name="preference_debug_mode_summary">Enable this option to get access to development / debug features. (Need application restart to make effect)</string>
<string name="menu_debug_title">Debug</string> <string name="menu_debug_title">Debug</string>
<string name="menu_clear_local_db">Clear local database</string> <string name="menu_clear_local_db">Clear local database</string>
<string name="success_clear_local_db">The local database has been successfully cleared! You may need to restart the application to make the changes being fully applied.</string>
<string name="err_clear_local_db">An error occurred while trying to clear local database!</string>
</resources> </resources>