Commit 9bbac59e authored by Adrien Oliva's avatar Adrien Oliva

feat(debug): Allow user to grab database

Add option in top-right menu to copy internal database on sdcard for
debug purpose

GTLAB #2Signed-off-by: Adrien Oliva's avatarAdrien Oliva <olivaa+gitlab@yapbreak.fr>
parent 509a100b
Pipeline #475 passed with stage
in 0 seconds
......@@ -13,12 +13,20 @@ import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import fr.yapbreak.skyrimquest.data.DataHelper;
import fr.yapbreak.skyrimquest.quests.SkyrimCursorAdapter;
......@@ -114,18 +122,11 @@ public class QuestList extends AppCompatActivity
/*******************************************************************************************
Set up database
******************************************************************************************/
if (checkSelfPermission(permission[0]) == PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(permission[1]) == PackageManager.PERMISSION_GRANTED) {
quest_data = new DataHelper(this);
quest_data.populate();
instance = quest_data;
SkyrimCursorAdapter adapter = new SkyrimCursorAdapter(this, quest_data.getAllQuests());
mainListView.setAdapter(adapter);
} else {
requestPermissions(permission, EXTERNAL_CODE);
}
quest_data = new DataHelper(this);
quest_data.populate();
instance = quest_data;
SkyrimCursorAdapter adapter = new SkyrimCursorAdapter(this, quest_data.getAllQuests());
mainListView.setAdapter(adapter);
}
......@@ -135,12 +136,7 @@ public class QuestList extends AppCompatActivity
case EXTERNAL_CODE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
quest_data = new DataHelper(this);
quest_data.populate();
instance = quest_data;
SkyrimCursorAdapter adapter = new SkyrimCursorAdapter(this, quest_data.getAllQuests());
mainListView.setAdapter(adapter);
copyDatabaseOnExternalData();
}
}
}
......@@ -176,11 +172,63 @@ public class QuestList extends AppCompatActivity
Intent i = new Intent(this, CharacterCreation.class);
startActivity(i);
return true;
} else if (id == R.id.action_debug_grab_database) {
if (checkSelfPermission(permission[0]) == PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(permission[1]) == PackageManager.PERMISSION_GRANTED) {
copyDatabaseOnExternalData();
} else {
requestPermissions(permission, EXTERNAL_CODE);
}
}
return super.onOptionsItemSelected(item);
}
void copyDatabaseOnExternalData()
{
String path = quest_data.getFilename();
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(path);
String outputBase = Environment.getExternalStorageDirectory() + "/quest_data.db";
out = new FileOutputStream(outputBase);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
String toast = getString(R.string.action_debug_grab_database_result);
Toast.makeText(this, toast + outputBase, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e("SKYRIM", e.getMessage());
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e("SKYRIM", e.getMessage());
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
Log.e("SKYRIM", e.getMessage());
}
}
}
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
......
......@@ -27,7 +27,7 @@ public class DataHelper extends SQLiteOpenHelper {
}
}
private final static String DATABASE_NAME = Environment.getExternalStorageDirectory().getPath()+"/quest_database.db";
private final static String DATABASE_NAME = "quest_database";
private final static int DATABASE_VERSION = 1;
private final static String QUEST_TABLE = "tbl_quest";
......@@ -90,7 +90,7 @@ public class DataHelper extends SQLiteOpenHelper {
public DataHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME, null);
//SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME, null);
}
@Override
......@@ -367,4 +367,9 @@ public class DataHelper extends SQLiteOpenHelper {
return null;
}
}
public String getFilename()
{
return getWritableDatabase().getPath();
}
}
......@@ -11,4 +11,10 @@
android:orderInCategory="200"
android:title="@string/action_character_create"
app:showAsAction="never" />
<item
android:id="@+id/action_debug_grab_database"
android:orderInCategory="1000"
android:title="@string/action_debug_grab_database"
app:showAsAction="never" />
</menu>
......@@ -58,4 +58,6 @@
<string name="quest_fallen">Le déchu</string>
<string name="quest_fallen_desc">Capturer et interroger un des alliés d’Alduin.</string>
<string name="avatar_description">Avatar</string>
<string name="action_debug_grab_database">Copie base de données</string>
<string name="action_debug_grab_database_result">"Base de données disponnible sous "</string>
</resources>
\ No newline at end of file
......@@ -19,4 +19,7 @@
<string name="avatar_description">Avatar picture</string>
<string name="plus" translatable="false">+</string>
<string name="minus" translatable="false">-</string>
<string name="action_debug_grab_database">Copy database</string>
<string name="action_debug_grab_database_result">Database available at </string>
</resources>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment