diff --git a/app/src/main/java/org/blockinger/game/activities/MainActivity.java b/app/src/main/java/org/blockinger/game/activities/MainActivity.java index 05291e1..73e62a1 100644 --- a/app/src/main/java/org/blockinger/game/activities/MainActivity.java +++ b/app/src/main/java/org/blockinger/game/activities/MainActivity.java @@ -39,7 +39,6 @@ import android.app.AlertDialog; import android.app.ListActivity; -import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; @@ -63,231 +62,233 @@ public class MainActivity extends ListActivity { - public static final int SCORE_REQUEST = 0x0; + public static final int SCORE_REQUEST = 0x0; + + /** + * This key is used to access the player name, which is returned as an Intent from the gameactivity upon completion (gameover). + * The Package Prefix is mandatory for Intent data + */ + public static final String PLAYERNAME_KEY = "org.blockinger.game.activities.playername"; + + /** + * This key is used to access the player name, which is returned as an Intent from the gameactivity upon completion (gameover). + * The Package Prefix is mandatory for Intent data + */ + public static final String SCORE_KEY = "org.blockinger.game.activities.score"; + + public ScoreDataSource datasource; + private SimpleCursorAdapter adapter; + private AlertDialog.Builder startLevelDialog; + private AlertDialog.Builder donateDialog; + private int startLevel; + private View dialogView; + private SeekBar leveldialogBar; + private TextView leveldialogtext; + private Sound sound; - /** This key is used to access the player name, which is returned as an Intent from the gameactivity upon completion (gameover). - * The Package Prefix is mandatory for Intent data - */ - public static final String PLAYERNAME_KEY = "org.blockinger.game.activities.playername"; - - /** This key is used to access the player name, which is returned as an Intent from the gameactivity upon completion (gameover). - * The Package Prefix is mandatory for Intent data - */ - public static final String SCORE_KEY = "org.blockinger.game.activities.score"; - - public ScoreDataSource datasource; - private SimpleCursorAdapter adapter; - private AlertDialog.Builder startLevelDialog; - private AlertDialog.Builder donateDialog; - private int startLevel; - private View dialogView; - private SeekBar leveldialogBar; - private TextView leveldialogtext; - private Sound sound; + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + setActionBar(findViewById(R.id.toolbar)); + } + PreferenceManager.setDefaultValues(this, R.xml.simple_preferences, true); + PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, true); + + /* Create Music */ + sound = new Sound(this); + sound.startMusic(Sound.MENU_MUSIC, 0); + + /* Database Management */ + Cursor mc; + datasource = new ScoreDataSource(this); + datasource.open(); + mc = datasource.getCursor(); + // Use the SimpleCursorAdapter to show the + // elements in a ListView + adapter = new SimpleCursorAdapter( + this, + R.layout.blockinger_list_item, + mc, + new String[]{HighscoreOpenHelper.COLUMN_SCORE, HighscoreOpenHelper.COLUMN_PLAYERNAME}, + new int[]{R.id.text1, R.id.text2}, + SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); + setListAdapter(adapter); + + /* Create Startlevel Dialog */ + startLevel = 0; + startLevelDialog = new AlertDialog.Builder(this); + startLevelDialog.setTitle(R.string.startLevelDialogTitle); + startLevelDialog.setCancelable(false); + startLevelDialog.setNegativeButton(R.string.startLevelDialogCancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + startLevelDialog.setPositiveButton(R.string.startLevelDialogStart, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + MainActivity.this.start(); + } + }); + + /* Create Donate Dialog */ + donateDialog = new AlertDialog.Builder(this); + donateDialog.setTitle(R.string.pref_donate_title); + donateDialog.setMessage(R.string.pref_donate_summary); + donateDialog.setNegativeButton(R.string.startLevelDialogCancel, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.dismiss(); + } + }); + donateDialog.setPositiveButton(R.string.donate_button, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + String url = getResources().getString(R.string.donation_url); + Intent i = new Intent(Intent.ACTION_VIEW); + i.setData(Uri.parse(url)); + startActivity(i); + } + }); + } - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { - setActionBar(findViewById(R.id.toolbar)); - } - PreferenceManager.setDefaultValues(this, R.xml.simple_preferences, true); - PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, true); - - /* Create Music */ - sound = new Sound(this); - sound.startMusic(Sound.MENU_MUSIC, 0); - - /* Database Management */ - Cursor mc; - datasource = new ScoreDataSource(this); - datasource.open(); - mc = datasource.getCursor(); - // Use the SimpleCursorAdapter to show the - // elements in a ListView - adapter = new SimpleCursorAdapter( - (Context)this, - R.layout.blockinger_list_item, - mc, - new String[] {HighscoreOpenHelper.COLUMN_SCORE, HighscoreOpenHelper.COLUMN_PLAYERNAME}, - new int[] {R.id.text1, R.id.text2}, - SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); - setListAdapter(adapter); - - /* Create Startlevel Dialog */ - startLevel = 0; - startLevelDialog = new AlertDialog.Builder(this); - startLevelDialog.setTitle(R.string.startLevelDialogTitle); - startLevelDialog.setCancelable(false); - startLevelDialog.setNegativeButton(R.string.startLevelDialogCancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - } - }); - startLevelDialog.setPositiveButton(R.string.startLevelDialogStart, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - MainActivity.this.start(); - } - }); - - /* Create Donate Dialog */ - donateDialog = new AlertDialog.Builder(this); - donateDialog.setTitle(R.string.pref_donate_title); - donateDialog.setMessage(R.string.pref_donate_summary); - donateDialog.setNegativeButton(R.string.startLevelDialogCancel, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - dialog.dismiss(); - } - }); - donateDialog.setPositiveButton(R.string.donate_button, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - String url = getResources().getString(R.string.donation_url); - Intent i = new Intent(Intent.ACTION_VIEW); - i.setData(Uri.parse(url)); - startActivity(i); - } - }); - } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + // Inflate the menu; this adds items to the action bar if it is present. + getMenuInflater().inflate(R.menu.main, menu); + return true; + } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - // Inflate the menu; this adds items to the action bar if it is present. - getMenuInflater().inflate(R.menu.main, menu); - return true; - } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.action_settings: + Intent intent = new Intent(this, SettingsActivity.class); + startActivity(intent); + return true; + case R.id.action_about: + Intent intent1 = new Intent(this, AboutActivity.class); + startActivity(intent1); + return true; + case R.id.action_donate: + donateDialog.show(); + return true; + case R.id.action_help: + Intent intent2 = new Intent(this, HelpActivity.class); + startActivity(intent2); + return true; + case R.id.action_exit: + GameState.destroy(); + MainActivity.this.finish(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.action_settings: - Intent intent = new Intent(this, SettingsActivity.class); - startActivity(intent); - return true; - case R.id.action_about: - Intent intent1 = new Intent(this, AboutActivity.class); - startActivity(intent1); - return true; - case R.id.action_donate: - donateDialog.show(); - return true; - case R.id.action_help: - Intent intent2 = new Intent(this, HelpActivity.class); - startActivity(intent2); - return true; - case R.id.action_exit: - GameState.destroy(); - MainActivity.this.finish(); - return true; - default: - return super.onOptionsItemSelected(item); - } - } - - public void start() { - Intent intent = new Intent(this, GameActivity.class); - Bundle b = new Bundle(); - b.putInt("mode", GameActivity.NEW_GAME); //Your id - b.putInt("level", startLevel); //Your id - b.putString("playername", ((TextView)findViewById(R.id.nicknameEditView)).getText().toString()); //Your id - intent.putExtras(b); //Put your id to your next Intent - startActivityForResult(intent,SCORE_REQUEST); - } + public void start() { + Intent intent = new Intent(this, GameActivity.class); + Bundle b = new Bundle(); + b.putInt("mode", GameActivity.NEW_GAME); //Your id + b.putInt("level", startLevel); //Your id + b.putString("playername", ((TextView) findViewById(R.id.nicknameEditView)).getText().toString()); //Your id + intent.putExtras(b); //Put your id to your next Intent + startActivityForResult(intent, SCORE_REQUEST); + } - @Override - protected void onActivityResult(int requestCode, int resultCode, Intent data) { - if(requestCode != SCORE_REQUEST) - return; - if(resultCode != RESULT_OK) - return; + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + if (requestCode != SCORE_REQUEST) + return; + if (resultCode != RESULT_OK) + return; - String playerName = data.getStringExtra(PLAYERNAME_KEY); - long score = data.getLongExtra(SCORE_KEY,0); + String playerName = data.getStringExtra(PLAYERNAME_KEY); + long score = data.getLongExtra(SCORE_KEY, 0); - datasource.open(); - datasource.createScore(score, playerName); - } + datasource.open(); + datasource.createScore(score, playerName); + } public void onClickStart(View view) { - dialogView = getLayoutInflater().inflate(R.layout.seek_bar_dialog, null); - leveldialogtext = ((TextView)dialogView.findViewById(R.id.leveldialogleveldisplay)); - leveldialogBar = ((SeekBar)dialogView.findViewById(R.id.levelseekbar)); - leveldialogBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { - - @Override - public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { - leveldialogtext.setText("" + arg1); - startLevel = arg1; - } - - @Override - public void onStartTrackingTouch(SeekBar arg0) { - } - - @Override - public void onStopTrackingTouch(SeekBar arg0) { - } - - }); - leveldialogBar.setProgress(startLevel); - leveldialogtext.setText("" + startLevel); - startLevelDialog.setView(dialogView); - startLevelDialog.show(); + dialogView = getLayoutInflater().inflate(R.layout.seek_bar_dialog, null); + leveldialogtext = dialogView.findViewById(R.id.leveldialogleveldisplay); + leveldialogBar = dialogView.findViewById(R.id.levelseekbar); + leveldialogBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { + + @Override + public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) { + leveldialogtext.setText("" + arg1); + startLevel = arg1; + } + + @Override + public void onStartTrackingTouch(SeekBar arg0) { + } + + @Override + public void onStopTrackingTouch(SeekBar arg0) { + } + + }); + leveldialogBar.setProgress(startLevel); + leveldialogtext.setText("" + startLevel); + startLevelDialog.setView(dialogView); + startLevelDialog.show(); } public void onClickResume(View view) { - Intent intent = new Intent(this, GameActivity.class); - Bundle b = new Bundle(); - b.putInt("mode", GameActivity.RESUME_GAME); //Your id - b.putString("playername", ((TextView)findViewById(R.id.nicknameEditView)).getText().toString()); //Your id - intent.putExtras(b); //Put your id to your next Intent - startActivityForResult(intent,SCORE_REQUEST); + Intent intent = new Intent(this, GameActivity.class); + Bundle b = new Bundle(); + b.putInt("mode", GameActivity.RESUME_GAME); //Your id + b.putString("playername", ((TextView) findViewById(R.id.nicknameEditView)).getText().toString()); //Your id + intent.putExtras(b); //Put your id to your next Intent + startActivityForResult(intent, SCORE_REQUEST); } @Override protected void onPause() { - super.onPause(); - sound.pause(); - sound.setInactive(true); - }; + super.onPause(); + sound.pause(); + sound.setInactive(true); + } - @Override + @Override protected void onStop() { - super.onStop(); - sound.pause(); - sound.setInactive(true); - datasource.close(); - }; + super.onStop(); + sound.pause(); + sound.setInactive(true); + datasource.close(); + } - @Override + @Override protected void onDestroy() { - super.onDestroy(); - sound.release(); - sound = null; - datasource.close(); - }; + super.onDestroy(); + sound.release(); + sound = null; + datasource.close(); + } - @Override + @Override protected void onResume() { - super.onResume(); - sound.setInactive(false); - sound.resume(); - datasource.open(); - Cursor cursor = datasource.getCursor(); - adapter.changeCursor(cursor); - - if(!GameState.isFinished()) { - ((Button)findViewById(R.id.resumeButton)).setEnabled(true); - ((Button)findViewById(R.id.resumeButton)).setTextColor(getResources().getColor(R.color.square_error)); - } else { - ((Button)findViewById(R.id.resumeButton)).setEnabled(false); - ((Button)findViewById(R.id.resumeButton)).setTextColor(getResources().getColor(R.color.holo_grey)); - } - }; + super.onResume(); + sound.setInactive(false); + sound.resume(); + datasource.open(); + Cursor cursor = datasource.getCursor(); + adapter.changeCursor(cursor); + + if (!GameState.isFinished()) { + findViewById(R.id.resumeButton).setEnabled(true); + ((Button) findViewById(R.id.resumeButton)).setTextColor(getResources().getColor(R.color.square_error)); + } else { + findViewById(R.id.resumeButton).setEnabled(false); + ((Button) findViewById(R.id.resumeButton)).setTextColor(getResources().getColor(R.color.holo_grey)); + } + } } diff --git a/app/src/main/java/org/blockinger/game/db/Score.java b/app/src/main/java/org/blockinger/game/db/Score.java index b515911..de426f6 100644 --- a/app/src/main/java/org/blockinger/game/db/Score.java +++ b/app/src/main/java/org/blockinger/game/db/Score.java @@ -38,45 +38,7 @@ package org.blockinger.game.db; public class Score { - private long id; - private long score; - private String playerName; - - public Score() { - - } - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - - public long getScore() { - return score; - } - - public String getScoreString() { - return String.valueOf(score); - } - - public void setScore(long comment) { - this.score = comment; - } - - public String getName() { - return playerName; - } - - public void setName(String comment) { - this.playerName = comment; - } - - // Will be used by the ArrayAdapter in the ListView - @Override - public String toString() { - return String.valueOf(score) + "@" + playerName; - } + public long id; + public long score; + public String playerName; } diff --git a/app/src/main/java/org/blockinger/game/db/ScoreDataSource.java b/app/src/main/java/org/blockinger/game/db/ScoreDataSource.java index e204fb5..d810763 100644 --- a/app/src/main/java/org/blockinger/game/db/ScoreDataSource.java +++ b/app/src/main/java/org/blockinger/game/db/ScoreDataSource.java @@ -45,57 +45,47 @@ public class ScoreDataSource { - // Database fields - private SQLiteDatabase database; - private HighscoreOpenHelper dbHelper; - private String[] allColumns = { HighscoreOpenHelper.COLUMN_ID, - HighscoreOpenHelper.COLUMN_SCORE, - HighscoreOpenHelper.COLUMN_PLAYERNAME}; - - public ScoreDataSource(Context context) { - dbHelper = new HighscoreOpenHelper(context); - } - - public void open() throws SQLException { - database = dbHelper.getWritableDatabase(); - } - - public void close() { - dbHelper.close(); - } - - public Score createScore(long score, String playerName) { - ContentValues values = new ContentValues(); - values.put(HighscoreOpenHelper.COLUMN_SCORE, score); - values.put(HighscoreOpenHelper.COLUMN_PLAYERNAME, playerName); - long insertId = database.insert(HighscoreOpenHelper.TABLE_HIGHSCORES, null, values); - Cursor cursor = database.query(HighscoreOpenHelper.TABLE_HIGHSCORES, - allColumns, HighscoreOpenHelper.COLUMN_ID + " = " + insertId, null, - null, null, HighscoreOpenHelper.COLUMN_SCORE + " DESC"); - cursor.moveToFirst(); - Score newScore = cursorToScore(cursor); - cursor.close(); - return newScore; - } - - public void deleteScore(Score score) { - long id = score.getId(); - //System.out.println("Comment deleted with id: " + id); - database.delete(HighscoreOpenHelper.TABLE_HIGHSCORES, HighscoreOpenHelper.COLUMN_ID - + " = " + id, null); - } - - private Score cursorToScore(Cursor cursor) { - Score score = new Score(); - score.setId(cursor.getLong(0)); - score.setScore(cursor.getLong(1)); - score.setName(cursor.getString(2)); - return score; - } - - public Cursor getCursor() { - return database.query(HighscoreOpenHelper.TABLE_HIGHSCORES, - allColumns, null, null, null, null, HighscoreOpenHelper.COLUMN_SCORE + " DESC"); - } + // Database fields + private SQLiteDatabase database; + private final HighscoreOpenHelper dbHelper; + private final String[] allColumns = {HighscoreOpenHelper.COLUMN_ID, + HighscoreOpenHelper.COLUMN_SCORE, + HighscoreOpenHelper.COLUMN_PLAYERNAME}; + + public ScoreDataSource(Context context) { + dbHelper = new HighscoreOpenHelper(context); + } + + public void open() throws SQLException { + database = dbHelper.getWritableDatabase(); + } + + public void close() { + dbHelper.close(); + } + + public void createScore(long score, String playerName) { + ContentValues values = new ContentValues(); + values.put(HighscoreOpenHelper.COLUMN_SCORE, score); + values.put(HighscoreOpenHelper.COLUMN_PLAYERNAME, playerName); + long insertId = database.insert(HighscoreOpenHelper.TABLE_HIGHSCORES, null, values); + Cursor cursor = database.query(HighscoreOpenHelper.TABLE_HIGHSCORES, + allColumns, HighscoreOpenHelper.COLUMN_ID + " = " + insertId, null, + null, null, HighscoreOpenHelper.COLUMN_SCORE + " DESC"); + cursor.moveToFirst(); + cursor.close(); + } + + public void deleteScore(Score score) { + long id = score.id; + //System.out.println("Comment deleted with id: " + id); + database.delete(HighscoreOpenHelper.TABLE_HIGHSCORES, HighscoreOpenHelper.COLUMN_ID + + " = " + id, null); + } + + public Cursor getCursor() { + return database.query(HighscoreOpenHelper.TABLE_HIGHSCORES, + allColumns, null, null, null, null, HighscoreOpenHelper.COLUMN_SCORE + " DESC"); + } }