diff --git a/app/build.gradle b/app/build.gradle index 4b8b8f7..87a1530 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -7,8 +7,8 @@ android { applicationId "com.de.aldo_apps.aldo.mariokartcircuitselector" minSdkVersion 16 targetSdkVersion 25 - versionCode 5 - versionName "0.1.5" + versionCode 6 + versionName "0.1.6" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/Constants.java b/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/Constants.java index 919bd0e..e842141 100644 --- a/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/Constants.java +++ b/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/Constants.java @@ -168,4 +168,86 @@ public class Constants { * The global KEY for the number field in the database. */ public static final String KEY_NUMBER = "number"; + + /** + * The Projection to retrieve all Information of a game object from database. + */ + public static final String[] PROJECTION_GAME = { + KEY_ID, + KEY_NAME, + KEY_COVER_URI + }; + + /** + * The Projection to retrieve all Information of a track object from database. + */ + public static final String[] PROJECTION_TRACK = { + KEY_ID, + KEY_NAME, + KEY_GAME, + KEY_PACKAGE, + KEY_NUMBER, + KEY_AVAILABLE + }; + + /** + * The Projection to retrieve all Information of a character object from database. + */ + public static final String[] PROJECTION_CHARACTER = { + KEY_ID, + KEY_NAME, + KEY_GAME, + KEY_WEIGHT, + KEY_AVAILABLE + }; + + /** + * The Projection to retrieve all Information of a kart object from database. + */ + public static final String[] PROJECTION_KART = { + KEY_ID, + KEY_NAME, + KEY_GAME, + KEY_WEIGHT, + KEY_WHEELS, + KEY_ACCESSORY, + KEY_DEDICATED_DRIVER, + KEY_FREE_FOR_ALL, + KEY_AVAILABLE + }; + + /** + * The Projection to retrieve all Information of a ruleset object from database. + */ + public static final String[] PROJECTION_RULESET = { + KEY_ID, + KEY_GAME, + KEY_MIRROR_CLASS, + KEY_50_CCM, + KEY_100_CCM, + KEY_150_CCM, + KEY_200_CCM, + KEY_KART_FREE_FOR_ALL, + KEY_BIKES + }; + + /** + * The Projection to retrieve all Information of a accessory object from database. + */ + public static final String[] PROJECTION_ACCESSORY = { + KEY_ID, + KEY_NAME, + KEY_GAME, + KEY_AVAILABLE + }; + + /** + * The Projection to retrieve all Information of a wheels object from database. + */ + public static final String[] PROJECTION_WHEELS = { + KEY_ID, + KEY_NAME, + KEY_GAME, + KEY_AVAILABLE + }; } diff --git a/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/DatabaseHandler.java b/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/DatabaseHandler.java index aa95993..72c4dca 100644 --- a/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/DatabaseHandler.java +++ b/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/DatabaseHandler.java @@ -9,23 +9,42 @@ import android.util.Log; import com.de.aldo_apps.aldo.mariokartcircuitselector.database_models.Accessory; import com.de.aldo_apps.aldo.mariokartcircuitselector.database_models.Character; +import com.de.aldo_apps.aldo.mariokartcircuitselector.database_models.Game; import com.de.aldo_apps.aldo.mariokartcircuitselector.database_models.Kart; +import com.de.aldo_apps.aldo.mariokartcircuitselector.database_models.Ruleset; import com.de.aldo_apps.aldo.mariokartcircuitselector.database_models.Track; import com.de.aldo_apps.aldo.mariokartcircuitselector.database_models.Wheels; import com.readystatesoftware.sqliteasset.SQLiteAssetHelper; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; +import java.util.ArrayList; -import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.DATABASE_NAME; -import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.DATABASE_OLD; -import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.DATABASE_PATH; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_100_CCM; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_150_CCM; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_200_CCM; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_50_CCM; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_ACCESSORY; import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_AVAILABLE; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_BIKES; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_COVER_URI; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_FREE_FOR_ALL; import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_GAME; import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_ID; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_KART_FREE_FOR_ALL; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_MIRROR_CLASS; import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_NAME; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_NUMBER; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_PACKAGE; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_WEIGHT; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.KEY_WHEELS; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.PROJECTION_CHARACTER; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.PROJECTION_GAME; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.PROJECTION_KART; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.PROJECTION_RULESET; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.PROJECTION_TRACK; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.TABLE_CHARACTER; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.TABLE_GAME; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.TABLE_KART; +import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.TABLE_RULESET; import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.TABLE_TRACK; import static com.de.aldo_apps.aldo.mariokartcircuitselector.Constants.TABLE_WHEELS; @@ -179,9 +198,606 @@ public class DatabaseHandler extends SQLiteAssetHelper { new String[]{String.valueOf(wheels.getId())}); } + // ------------------------------------------------------------------------------------------- + // -------------------------- "G E T A L L" F U N C T I O N S ---------------------------- + // ------------------------------------------------------------------------------------------- + + /** + * Retrieves all Tracks from Database. + * + * @return List containing all Track Objects retrieved from Database. + */ + public ArrayList getAllTracks() { + final SQLiteDatabase database = this.getReadableDatabase(); + + final Cursor cursor = database.query(TABLE_TRACK, PROJECTION_TRACK, null, + null, null, null, null, null); + if (cursor != null) { + Log.d(TAG, "getAllTracks: Moving cursor to first."); + cursor.moveToFirst(); + } else { + Log.d(TAG, "getAllTracks: cursor is null"); + return null; + } + + final ArrayList allAvailableTrack = new ArrayList<>(); + + do { + final int idIdx = cursor.getColumnIndex(KEY_ID); + final int nameIdx = cursor.getColumnIndex(KEY_NAME); + final int gameIdx = cursor.getColumnIndex(KEY_GAME); + final int packageIdx = cursor.getColumnIndex(KEY_PACKAGE); + final int numberIdx = cursor.getColumnIndex(KEY_NUMBER); + final int availableIdx = cursor.getColumnIndex(KEY_AVAILABLE); + + final Track track = new Track(); + boolean isValid = true; + if (idIdx >= 0) { + track.setId(cursor.getInt(idIdx)); + } else { + Log.d(TAG, "getAllTracks: No such column [" + KEY_ID + "]"); + isValid = false; + } + + if (nameIdx >= 0) { + track.setName(cursor.getString(nameIdx)); + } else { + Log.d(TAG, "getAllTracks: No such column [" + KEY_NAME + "]"); + isValid = false; + } + + if (gameIdx >= 0) { + track.setGame(cursor.getString(gameIdx)); + } else { + Log.d(TAG, "getAllTracks: No such column [" + KEY_GAME + "]"); + isValid = false; + } + + if (packageIdx >= 0) { + track.setPackage(cursor.getString(packageIdx)); + } else { + Log.d(TAG, "getAllTracks: No such column [" + KEY_PACKAGE + "]"); + isValid = false; + } + + if (numberIdx >= 0) { + track.setPackage(cursor.getString(numberIdx)); + } else { + Log.d(TAG, "getAllTracks: No such column [" + KEY_NUMBER + "]"); + isValid = false; + } + + if (availableIdx < 0) { + track.setAvailable(cursor.getInt(availableIdx)); + } else { + Log.d(TAG, "getAllTracks: No such column [" + KEY_AVAILABLE + "]"); + isValid = false; + } + + if (isValid) { + allAvailableTrack.add(track); + } else { + Log.d(TAG, "getAllTracks: Do not add Track as one or more arguments are invalid."); + } + + } while (cursor.moveToNext()); + + cursor.close(); + database.close(); + + // return contact + return allAvailableTrack; + } + + /** + * Retrieves all Karts from Database. + * + * @return List containing all Kart Objects retrieved from Database. + */ + public ArrayList getAllKarts() { + final SQLiteDatabase database = this.getReadableDatabase(); + + final Cursor cursor = database.query(TABLE_KART, PROJECTION_KART, null, + null, null, null, null, null); + if (cursor != null) { + Log.d(TAG, "getAllKarts: Moving cursor to first."); + cursor.moveToFirst(); + } else { + Log.d(TAG, "getAllKarts: cursor is null"); + return null; + } + + final ArrayList allAvailableKarts = new ArrayList<>(); + + do { + final int idIdx = cursor.getColumnIndex(KEY_ID); + final int nameIdx = cursor.getColumnIndex(KEY_NAME); + final int gameIdx = cursor.getColumnIndex(KEY_GAME); + final int weightIdx = cursor.getColumnIndex(KEY_WEIGHT); + final int accessoryIdx = cursor.getColumnIndex(KEY_ACCESSORY); + final int wheelsIdx = cursor.getColumnIndex(KEY_WHEELS); + final int freeForAllIdx = cursor.getColumnIndex(KEY_FREE_FOR_ALL); + final int availableIdx = cursor.getColumnIndex(KEY_AVAILABLE); + + final Kart kart = new Kart(); + boolean isValid = true; + if (idIdx >= 0) { + kart.setId(cursor.getInt(idIdx)); + } else { + Log.d(TAG, "getAllKarts: No such column [" + KEY_ID + "]"); + isValid = false; + } + + if (nameIdx >= 0) { + kart.setName(cursor.getString(nameIdx)); + } else { + Log.d(TAG, "getAllKarts: No such column [" + KEY_NAME + "]"); + isValid = false; + } + + if (gameIdx >= 0) { + kart.setGame(cursor.getString(gameIdx)); + } else { + Log.d(TAG, "getAllKarts: No such column [" + KEY_GAME + "]"); + isValid = false; + } + + if (weightIdx >= 0) { + kart.setWeight(cursor.getString(weightIdx)); + } else { + Log.d(TAG, "getAllKarts: No such column [" + KEY_WEIGHT + "]"); + isValid = false; + } + + if (accessoryIdx >= 0) { + kart.setAccessory(cursor.getInt(accessoryIdx)); + } else { + Log.d(TAG, "getAllKarts: No such column [" + KEY_ACCESSORY + "]"); + isValid = false; + } + + if (wheelsIdx >= 0) { + kart.setWheels(cursor.getInt(wheelsIdx)); + } else { + Log.d(TAG, "getAllKarts: No such column [" + KEY_WHEELS + "]"); + isValid = false; + } + + if (freeForAllIdx >= 0) { + kart.setFreeForAll(cursor.getInt(freeForAllIdx)); + } else { + Log.d(TAG, "getAllKarts: No such column [" + KEY_FREE_FOR_ALL + "]"); + isValid = false; + } + + if (availableIdx < 0) { + kart.setAvailable(cursor.getInt(availableIdx)); + } else { + Log.d(TAG, "getAllKarts: No such column [" + KEY_AVAILABLE + "]"); + isValid = false; + } + + if (isValid) { + allAvailableKarts.add(kart); + } else { + Log.d(TAG, "getAllKarts: Do not add Kart as one or more arguments are invalid."); + } + + } while (cursor.moveToNext()); + + cursor.close(); + database.close(); + + // return contact + return allAvailableKarts; + } + + /** + * Retrieves all Characters from Database. + * + * @return List containing all Character Objects retrieved from Database. + */ + public ArrayList getAllCharacters() { + final SQLiteDatabase database = this.getReadableDatabase(); + + final Cursor cursor = database.query(TABLE_CHARACTER, PROJECTION_CHARACTER, null, + null, null, null, null, null); + if (cursor != null) { + Log.d(TAG, "getAllCharacters: Moving cursor to first."); + cursor.moveToFirst(); + } else { + Log.d(TAG, "getAllCharacters: cursor is null"); + return null; + } + + final ArrayList allAvailableCharacters = new ArrayList<>(); + + do { + final int idIdx = cursor.getColumnIndex(KEY_ID); + final int nameIdx = cursor.getColumnIndex(KEY_NAME); + final int gameIdx = cursor.getColumnIndex(KEY_GAME); + final int weightIdx = cursor.getColumnIndex(KEY_WEIGHT); + final int availableIdx = cursor.getColumnIndex(KEY_AVAILABLE); + + final Character character = new Character(); + boolean isValid = true; + + if (idIdx >= 0) { + character.setId(cursor.getInt(idIdx)); + } else { + Log.d(TAG, "getAllCharacters: No such column [" + KEY_ID + "]"); + isValid = false; + } + + if (nameIdx >= 0) { + character.setName(cursor.getString(nameIdx)); + } else { + Log.d(TAG, "getAllCharacters: No such column [" + KEY_NAME + "]"); + isValid = false; + } + + if (gameIdx >= 0) { + character.setGame(cursor.getString(gameIdx)); + } else { + Log.d(TAG, "getAllCharacters: No such column [" + KEY_GAME + "]"); + isValid = false; + } + + if (weightIdx >= 0) { + character.setWeight(cursor.getString(weightIdx)); + } else { + Log.d(TAG, "getAllCharacters: No such column [" + KEY_WEIGHT + "]"); + isValid = false; + } + + if (availableIdx < 0) { + character.setAvailable(cursor.getInt(availableIdx)); + } else { + Log.d(TAG, "getAllCharacters: No such column [" + KEY_AVAILABLE + "]"); + isValid = false; + } + + if (isValid) { + allAvailableCharacters.add(character); + } else { + Log.d(TAG, "getAllCharacters: Do not add Character as one or more arguments " + + "are invalid."); + } + + } while (cursor.moveToNext()); + + cursor.close(); + database.close(); + + // return contact + return allAvailableCharacters; + } + + /** + * Retrieves all Rulesets from Database. + * + * @return List containing all Ruleset Objects retrieved from Database. + */ + public ArrayList getAllRulesets() { + final SQLiteDatabase database = this.getReadableDatabase(); + + final Cursor cursor = database.query(TABLE_RULESET, PROJECTION_RULESET, null, + null, null, null, null, null); + if (cursor != null) { + Log.d(TAG, "getAllRulesets: Moving cursor to first."); + cursor.moveToFirst(); + } else { + Log.d(TAG, "getAllRulesets: cursor is null"); + return null; + } + + final ArrayList allAvailableRulesets = new ArrayList<>(); + do { + final int idIdx = cursor.getColumnIndex(KEY_ID); + final int gameIdx = cursor.getColumnIndex(KEY_GAME); + final int mirrorIdx = cursor.getColumnIndex(KEY_MIRROR_CLASS); + final int ccm50Idx = cursor.getColumnIndex(KEY_50_CCM); + final int ccm100Idx = cursor.getColumnIndex(KEY_100_CCM); + final int ccm150Idx = cursor.getColumnIndex(KEY_150_CCM); + final int ccm200Idx = cursor.getColumnIndex(KEY_200_CCM); + final int freeForAllIdx = cursor.getColumnIndex(KEY_KART_FREE_FOR_ALL); + final int bikesIdx = cursor.getColumnIndex(KEY_BIKES); + + final Ruleset ruleset = new Ruleset(); + boolean isValid = true; + + if (idIdx >= 0) { + ruleset.setId(cursor.getInt(idIdx)); + } else { + Log.d(TAG, "getAllRulesets: No such column [" + KEY_ID + "]"); + isValid = false; + } + + if (gameIdx >= 0) { + ruleset.setGame(cursor.getString(gameIdx)); + } else { + Log.d(TAG, "getAllRulesets: No such column [" + KEY_GAME + "]"); + isValid = false; + } + + if (mirrorIdx >= 0) { + ruleset.setGame(cursor.getString(gameIdx)); + } else { + Log.d(TAG, "getAllRulesets: No such column [" + KEY_GAME + "]"); + isValid = false; + } + + if (ccm50Idx >= 0) { + ruleset.set50CcmAvailable(cursor.getInt(ccm50Idx)); + } else { + Log.d(TAG, "getAllRulesets: No such column [" + KEY_50_CCM + "]"); + isValid = false; + } + + if (ccm100Idx >= 0) { + ruleset.set100CcmAvailable(cursor.getInt(ccm100Idx)); + } else { + Log.d(TAG, "getAllRulesets: No such column [" + KEY_100_CCM + "]"); + isValid = false; + } + + if (ccm150Idx >= 0) { + ruleset.set150CcmAvailable(cursor.getInt(ccm150Idx)); + } else { + Log.d(TAG, "getAllRulesets: No such column [" + KEY_150_CCM + "]"); + isValid = false; + } + + if (ccm200Idx >= 0) { + ruleset.set200CcmAvailable(cursor.getInt(ccm200Idx)); + } else { + Log.d(TAG, "getAllRulesets: No such column [" + KEY_200_CCM + "]"); + isValid = false; + } + + if (freeForAllIdx >= 0) { + ruleset.setKartsFreeForAll(cursor.getInt(freeForAllIdx)); + } else { + Log.d(TAG, "getAllRulesets: No such column [" + KEY_KART_FREE_FOR_ALL + "]"); + isValid = false; + } + + if (bikesIdx >= 0) { + ruleset.setBikesAvailable(cursor.getInt(bikesIdx)); + } else { + Log.d(TAG, "getAllRulesets: No such column [" + KEY_BIKES + "]"); + isValid = false; + } + + if (isValid) { + allAvailableRulesets.add(ruleset); + } else { + Log.d(TAG, "getAllRulesets: Do not add Ruleset as one or more arguments " + + "are invalid."); + } + + } while (cursor.moveToNext()); + + cursor.close(); + database.close(); + + // return contact + return allAvailableRulesets; + } + + /** + * Retrieves all Accessories from Database. + * + * @return List containing all Accessory Objects retrieved from Database. + */ + public ArrayList getAllAccessories() { + final SQLiteDatabase database = this.getReadableDatabase(); + + final Cursor cursor = database.query(TABLE_TRACK, PROJECTION_TRACK, null, + null, null, null, null, null); + if (cursor != null) { + Log.d(TAG, "getAllAccessories: Moving cursor to first."); + cursor.moveToFirst(); + } else { + Log.d(TAG, "getAllAccessories: cursor is null"); + return null; + } + + final ArrayList allAvailableAccessories = new ArrayList<>(); + + do { + final int idIdx = cursor.getColumnIndex(KEY_ID); + final int nameIdx = cursor.getColumnIndex(KEY_NAME); + final int gameIdx = cursor.getColumnIndex(KEY_GAME); + final int availableIdx = cursor.getColumnIndex(KEY_AVAILABLE); + + final Accessory accessory = new Accessory(); + boolean isValid = true; + if (idIdx >= 0) { + accessory.setId(cursor.getInt(idIdx)); + } else { + Log.d(TAG, "getAllAccessories: No such column [" + KEY_ID + "]"); + isValid = false; + } + + if (nameIdx >= 0) { + accessory.setName(cursor.getString(nameIdx)); + } else { + Log.d(TAG, "getAllAccessories: No such column [" + KEY_NAME + "]"); + isValid = false; + } + + if (gameIdx >= 0) { + accessory.setGame(cursor.getString(gameIdx)); + } else { + Log.d(TAG, "getAllAccessories: No such column [" + KEY_GAME + "]"); + isValid = false; + } + + if (availableIdx < 0) { + accessory.setAvailable(cursor.getInt(availableIdx)); + } else { + Log.d(TAG, "getAllAccessories: No such column [" + KEY_AVAILABLE + "]"); + isValid = false; + } + + if (isValid) { + allAvailableAccessories.add(accessory); + } else { + Log.d(TAG, "getAllAccessories: Do not add Accessory as one or more arguments " + + "are invalid."); + } + + } while (cursor.moveToNext()); + + cursor.close(); + database.close(); + + // return contact + return allAvailableAccessories; + } + + /** + * Retrieves all Wheels from Database. + * + * @return List containing all Wheels Objects retrieved from Database. + */ + public ArrayList getAllWheels() { + final SQLiteDatabase database = this.getReadableDatabase(); + + final Cursor cursor = database.query(TABLE_TRACK, PROJECTION_TRACK, null, + null, null, null, null, null); + if (cursor != null) { + Log.d(TAG, "getAllWheels: Moving cursor to first."); + cursor.moveToFirst(); + } else { + Log.d(TAG, "getAllWheels: cursor is null"); + return null; + } + + final ArrayList allAvailableWheels = new ArrayList<>(); + + do { + final int idIdx = cursor.getColumnIndex(KEY_ID); + final int nameIdx = cursor.getColumnIndex(KEY_NAME); + final int gameIdx = cursor.getColumnIndex(KEY_GAME); + final int availableIdx = cursor.getColumnIndex(KEY_AVAILABLE); + + final Wheels wheels = new Wheels(); + boolean isValid = true; + if (idIdx >= 0) { + wheels.setId(cursor.getInt(idIdx)); + } else { + Log.d(TAG, "getAllWheels: No such column [" + KEY_ID + "]"); + isValid = false; + } + + if (nameIdx >= 0) { + wheels.setName(cursor.getString(nameIdx)); + } else { + Log.d(TAG, "getAllWheels: No such column [" + KEY_NAME + "]"); + isValid = false; + } + + if (gameIdx >= 0) { + wheels.setGame(cursor.getString(gameIdx)); + } else { + Log.d(TAG, "getAllWheels: No such column [" + KEY_GAME + "]"); + isValid = false; + } + + if (availableIdx < 0) { + wheels.setAvailable(cursor.getInt(availableIdx)); + } else { + Log.d(TAG, "getAllWheels: No such column [" + KEY_AVAILABLE + "]"); + isValid = false; + } + + if (isValid) { + allAvailableWheels.add(wheels); + } else { + Log.d(TAG, "getAllWheels: Do not add Wheels as one or more arguments are invalid."); + } + + } while (cursor.moveToNext()); + + cursor.close(); + database.close(); + + // return contact + return allAvailableWheels; + } + + /** + * Retrieves all Games from Database. + * + * @return List containing all Game Objects retrieved from Database. + */ + public ArrayList getAllGames() { + final SQLiteDatabase database = this.getReadableDatabase(); + + final Cursor cursor = database.query(TABLE_GAME, PROJECTION_GAME, null, + null, null, null, null, null); + if (cursor != null) { + Log.d(TAG, "getAllGames: Moving cursor to first."); + cursor.moveToFirst(); + } else { + Log.d(TAG, "getAllGames: cursor is null"); + return null; + } + + final ArrayList allAvailableGames = new ArrayList<>(); + + do { + final int idIdx = cursor.getColumnIndex(KEY_ID); + final int nameIdx = cursor.getColumnIndex(KEY_NAME); + final int coverUriIdx = cursor.getColumnIndex(KEY_COVER_URI); + + final Game game = new Game(); + boolean isValid = true; + if (idIdx >= 0) { + game.setId(cursor.getInt(idIdx)); + } else { + Log.d(TAG, "getAllGames: No such column [" + KEY_ID + "]"); + isValid = false; + } + + if (nameIdx >= 0) { + game.setName(cursor.getString(nameIdx)); + } else { + Log.d(TAG, "getAllGames: No such column [" + KEY_NAME + "]"); + isValid = false; + } + + if (coverUriIdx >= 0) { + game.setCoverUri(cursor.getString(coverUriIdx)); + } else { + Log.d(TAG, "getAllGames: No such column [" + KEY_COVER_URI + "]"); + isValid = false; + } + + if (isValid) { + allAvailableGames.add(game); + } else { + Log.d(TAG, "getAllGames: Do not add Game as one or more arguments are invalid."); + } + + } while (cursor.moveToNext()); + + cursor.close(); + database.close(); + + // return contact + return allAvailableGames; + } + + // ------------------------------------------------------------------------------------------- + // ------------------------------ T E S T F U N C T I O N S ------------------------------- + // ------------------------------------------------------------------------------------------- + public Track testDBConnection(final int id) { Log.d(TAG, "testDBConnection: Opening Readable Database."); - final SQLiteDatabase database= this.getReadableDatabase(); + final SQLiteDatabase database = this.getReadableDatabase(); Log.d(TAG, "testDBConnection: Query a single track for id = [" + id + "]"); final Cursor cursor = database.query(TABLE_TRACK, new String[]{KEY_ID, @@ -227,6 +843,7 @@ public class DatabaseHandler extends SQLiteAssetHelper { return track; } + cursor.close(); database.close(); // return contact diff --git a/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/GameSelection.java b/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/GameSelection.java index e2db914..9141d9c 100644 --- a/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/GameSelection.java +++ b/app/src/main/java/com/de/aldo_apps/aldo/mariokartcircuitselector/GameSelection.java @@ -1,6 +1,5 @@ package com.de.aldo_apps.aldo.mariokartcircuitselector; -import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; @@ -13,7 +12,7 @@ import com.de.aldo_apps.aldo.mariokartcircuitselector.database_models.Track; public class GameSelection extends AppCompatActivity { @Override - protected void onCreate(Bundle savedInstanceState) { + protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game_selection); @@ -27,7 +26,7 @@ public class GameSelection extends AppCompatActivity { button.setOnClickListener(new View.OnClickListener() { @Override - public void onClick(View view) { + public void onClick(final View view) { final int id = Integer.parseInt(editText.getText().toString()); final Track track = handler.testDBConnection(id); if (track != null) {