Compare commits

...

2 Commits

Author SHA1 Message Date
luca0N! f32a0a7a43
Added missing Tor preference res files
Based on #2.
2021-03-03 22:18:38 -03:00
luca0N! e8c2504a33
Added back button on game activity 2021-03-03 22:18:11 -03:00
4 changed files with 196 additions and 166 deletions

View File

@ -33,6 +33,7 @@ import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
@ -40,6 +41,7 @@ import android.widget.TextView;
import java.io.IOException;
import androidx.appcompat.app.ActionBar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;
@ -56,198 +58,213 @@ import com.luca0n.joguitos.pluck.util.ApiUtil;
import com.luca0n.joguitos.pluck.util.SoundUtil;
public class TriviaGameActivity extends BaseActivity
implements IDownloadTriviaQuestionReceiver {
static final String EXTRA_TRIVIA_QUERY = "extra_trivia_query";
private final String STATE_TRIVIA_GAME = "state_trivia_game";
implements IDownloadTriviaQuestionReceiver {
static final String EXTRA_TRIVIA_QUERY = "extra_trivia_query";
private final String STATE_TRIVIA_GAME = "state_trivia_game";
private TriviaGame game;
private TriviaGame game;
@BindView(R.id.progress_bar)
ProgressBar progressBar;
@BindView(R.id.trivia_status_bar)
LinearLayout triviaStatusBar;
@BindView(R.id.text_question_category)
TextView textViewQuestionCategory;
@BindView(R.id.text_question_difficulty)
TextView textViewQuestionDifficulty;
@BindView(R.id.text_question_progress)
TextView textViewQuestionProgress;
@BindView(R.id.progress_bar)
ProgressBar progressBar;
@BindView(R.id.trivia_status_bar)
LinearLayout triviaStatusBar;
@BindView(R.id.text_question_category)
TextView textViewQuestionCategory;
@BindView(R.id.text_question_difficulty)
TextView textViewQuestionDifficulty;
@BindView(R.id.text_question_progress)
TextView textViewQuestionProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trivia_game);
ButterKnife.bind(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_trivia_game);
ButterKnife.bind(this);
if (savedInstanceState != null) {
this.game = (TriviaGame) savedInstanceState.getSerializable(STATE_TRIVIA_GAME);
} else {
Bundle bundle = getIntent().getExtras();
assert bundle != null;
TriviaQuery query = (TriviaQuery) bundle.get(EXTRA_TRIVIA_QUERY);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null)
actionBar.setDisplayHomeAsUpEnabled(true);
progressBar.setVisibility(View.VISIBLE);
if (savedInstanceState != null) {
this.game = (TriviaGame) savedInstanceState.getSerializable(STATE_TRIVIA_GAME);
} else {
Bundle bundle = getIntent().getExtras();
assert bundle != null;
TriviaQuery query = (TriviaQuery) bundle.get(EXTRA_TRIVIA_QUERY);
DownloadTriviaQuestionsTask task = new DownloadTriviaQuestionsTask();
task.setReceiver(this);
task.execute(query);
}
}
progressBar.setVisibility(View.VISIBLE);
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(STATE_TRIVIA_GAME, this.game);
}
DownloadTriviaQuestionsTask task = new DownloadTriviaQuestionsTask();
task.setReceiver(this);
task.execute(query);
}
}
@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_trivia_game);
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putSerializable(STATE_TRIVIA_GAME, this.game);
}
if (fragment instanceof TriviaGameErrorFragment) {
super.onBackPressed();
} else {
new AlertDialog.Builder(this)
.setTitle(R.string.ui_quit_game)
.setMessage(R.string.ui_quit_game_msg)
.setPositiveButton(android.R.string.yes, (dialog, which) ->
TriviaGameActivity.super.onBackPressed())
.setNegativeButton(android.R.string.no, (dialog, which) -> {
})
.show();
}
}
@Override
public void onBackPressed() {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.frame_trivia_game);
public void onTriviaQuestionsDownloaded(String json) {
if (json == null) {
onNetworkError();
return;
} else {
try {
this.game = new TriviaGame(ApiUtil.jsonToQuestionArray(json));
} catch (NoTriviaResultsException e) {
onNoTriviaResults();
return;
}
}
if (fragment instanceof TriviaGameErrorFragment) {
super.onBackPressed();
} else {
new AlertDialog.Builder(this)
.setTitle(R.string.ui_quit_game)
.setMessage(R.string.ui_quit_game_msg)
.setPositiveButton(android.R.string.yes, (dialog, which) ->
TriviaGameActivity.super.onBackPressed())
.setNegativeButton(android.R.string.no, (dialog, which) -> {
})
.show();
}
}
// Setup game layout
progressBar.setVisibility(View.GONE);
triviaStatusBar.setVisibility(View.VISIBLE);
updateStatusBar();
updateTriviaQuestion();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void updateStatusBar() {
String progress = getResources().getString(R.string.ui_question_progress,
game.getQuestionProgress(), game.getQuestionsCount());
public void onTriviaQuestionsDownloaded(String json) {
if (json == null) {
onNetworkError();
return;
} else {
try {
this.game = new TriviaGame(ApiUtil.jsonToQuestionArray(json));
} catch (NoTriviaResultsException e) {
onNoTriviaResults();
return;
}
}
String category = (game.getCurrentQuestion().getCategory() != null)
? game.getCurrentQuestion().getCategory().toString() : "";
// Setup game layout
progressBar.setVisibility(View.GONE);
triviaStatusBar.setVisibility(View.VISIBLE);
updateStatusBar();
updateTriviaQuestion();
}
String difficulty = game.getCurrentQuestion().getDifficulty().toString();
private void updateStatusBar() {
String progress = getResources().getString(R.string.ui_question_progress,
game.getQuestionProgress(), game.getQuestionsCount());
textViewQuestionProgress.setText(progress);
textViewQuestionCategory.setText(category);
textViewQuestionDifficulty.setText(difficulty);
}
String category = (game.getCurrentQuestion().getCategory() != null)
? game.getCurrentQuestion().getCategory().toString() : "";
private void updateTriviaQuestion() {
Fragment fragment = TriviaQuestionFragment.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_trivia_game, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
}
String difficulty = game.getCurrentQuestion().getDifficulty().toString();
private void onNetworkError() {
String msg = getResources().getString(R.string.error_network);
Fragment errorFragment = TriviaGameErrorFragment.newInstance(msg);
textViewQuestionProgress.setText(progress);
textViewQuestionCategory.setText(category);
textViewQuestionDifficulty.setText(difficulty);
}
// Hide the progress bar.
progressBar.setVisibility(View.GONE);
private void updateTriviaQuestion() {
Fragment fragment = TriviaQuestionFragment.newInstance();
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_trivia_game, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.commit();
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_trivia_game, errorFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
private void onNetworkError() {
String msg = getResources().getString(R.string.error_network);
Fragment errorFragment = TriviaGameErrorFragment.newInstance(msg);
private void onNoTriviaResults() {
String msg = getResources().getString(R.string.error_no_trivia_results);
Fragment errorFragment = TriviaGameErrorFragment.newInstance(msg);
// Hide the progress bar.
progressBar.setVisibility(View.GONE);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_trivia_game, errorFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_trivia_game, errorFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
public TriviaQuestion getCurrentQuestion() {
return this.game.getCurrentQuestion();
}
private void onNoTriviaResults() {
String msg = getResources().getString(R.string.error_no_trivia_results);
Fragment errorFragment = TriviaGameErrorFragment.newInstance(msg);
public void onAnswerClick(Button answer, Button correctAnswer) {
boolean guess = game.nextQuestion(answer.getText().toString());
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.frame_trivia_game, errorFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.commit();
}
final int green = getResources().getColor(R.color.colorAccentGreen);
int color = guess ? green
: getResources().getColor(R.color.colorAccentRed);
public TriviaQuestion getCurrentQuestion() {
return this.game.getCurrentQuestion();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ColorStateList stateList = ColorStateList.valueOf(color);
answer.setBackgroundTintList(stateList);
public void onAnswerClick(Button answer, Button correctAnswer) {
boolean guess = game.nextQuestion(answer.getText().toString());
if (!guess) {
final ColorStateList greenStateList = ColorStateList.valueOf(green);
correctAnswer.setBackgroundTintList(greenStateList);
}
} else {
answer.getBackground().getCurrent().setColorFilter(
new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
final int green = getResources().getColor(R.color.colorAccentGreen);
int color = guess ? green
: getResources().getColor(R.color.colorAccentRed);
if (!guess)
correctAnswer.getBackground().getCurrent().setColorFilter(
new PorterDuffColorFilter(green, PorterDuff.Mode.MULTIPLY));
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ColorStateList stateList = ColorStateList.valueOf(color);
answer.setBackgroundTintList(stateList);
SoundUtil.playSound(this, guess ?
SoundUtil.SOUND_ANSWER_CORRECT : SoundUtil.SOUND_ANSWER_WRONG);
if (!guess) {
final ColorStateList greenStateList = ColorStateList.valueOf(green);
correctAnswer.setBackgroundTintList(greenStateList);
}
} else {
answer.getBackground().getCurrent().setColorFilter(
new PorterDuffColorFilter(color, PorterDuff.Mode.MULTIPLY));
new Handler().postDelayed(() -> {
if (game.isDone()) {
Intent intent = new Intent(getApplicationContext(), TriviaGameResultsActivity.class);
intent.putExtra(TriviaGameResultsActivity.EXTRA_TRIVIA_GAME, game);
startActivity(intent);
finish();
} else {
updateStatusBar();
updateTriviaQuestion();
}
}, 500);
}
if (!guess)
correctAnswer.getBackground().getCurrent().setColorFilter(
new PorterDuffColorFilter(green, PorterDuff.Mode.MULTIPLY));
}
private static class DownloadTriviaQuestionsTask extends AsyncTask<TriviaQuery, Integer, String> {
private IDownloadTriviaQuestionReceiver receiver;
SoundUtil.playSound(this, guess ?
SoundUtil.SOUND_ANSWER_CORRECT : SoundUtil.SOUND_ANSWER_WRONG);
@Override
protected String doInBackground(TriviaQuery... query) {
String json;
try {
json = ApiUtil.GET(query[0]);
} catch (IOException e) {
return null;
}
return json;
}
new Handler().postDelayed(() -> {
if (game.isDone()) {
Intent intent = new Intent(getApplicationContext(), TriviaGameResultsActivity.class);
intent.putExtra(TriviaGameResultsActivity.EXTRA_TRIVIA_GAME, game);
startActivity(intent);
finish();
} else {
updateStatusBar();
updateTriviaQuestion();
}
}, 500);
}
@Override
protected void onPostExecute(String json) {
receiver.onTriviaQuestionsDownloaded(json);
}
private static class DownloadTriviaQuestionsTask extends AsyncTask<TriviaQuery, Integer, String> {
private IDownloadTriviaQuestionReceiver receiver;
private void setReceiver(IDownloadTriviaQuestionReceiver receiver) {
this.receiver = receiver;
}
}
@Override
protected String doInBackground(TriviaQuery... query) {
String json;
try {
json = ApiUtil.GET(query[0]);
} catch (IOException e) {
return null;
}
return json;
}
@Override
protected void onPostExecute(String json) {
receiver.onTriviaQuestionsDownloaded(json);
}
private void setReceiver(IDownloadTriviaQuestionReceiver receiver) {
this.receiver = receiver;
}
}
}

View File

@ -24,5 +24,6 @@ Contact us at <joguitos+pluck@luca0n.com>.
<resources>
<string name="pref_category_sound">pref_key_category_sound</string>
<string name="pref_sound_answer">pref_sound_answer</string>
<string name="pref_sound_answer">pref_sound_answer</string>
<string name="pref_network_tor">pref_network_tor</string>
</resources>

View File

@ -83,11 +83,15 @@ Contact us at <joguitos+pluck@luca0n.com>.
<string name="ui_return_to_menu">Return To Menu</string>
<!-- Error Strings -->
<string name="error_network"><b>Network error!</b>\n Could not connect to a network.</string>
<string name="error_no_trivia_results"><b>No trivia results!</b>\n Was not able to find trivia questions that satisfied all options.</string>
<string name="error_network"><b>Network error!</b>\nCould not connect to a network.\n\nIf Tor is enabled in the game settings, check if Orbot is running and then try again.</string>
<string name="error_no_trivia_results"><b>No trivia results!</b>\nWas not able to find trivia questions that satisfied all options.</string>
<string name="title_activity_settings">Settings</string>
<!-- Setting Strings-->
<string name="pref_category_sound_title">Sound</string>
<string name="pref_sound_answer_title">Answer Sound Effect</string>
<string name="pref_sound_answer_title">Answer Sound Effect</string>
<string name="pref_category_network_title">Network</string>
<string name="pref_network_tor_title">Connect using Tor</string>
<string name="pref_network_tor_summary">Retrieves trivia data via the Tor network. Requires Orbot.</string>
</resources>

View File

@ -33,6 +33,14 @@ Contact us at <joguitos+pluck@luca0n.com>.
android:key="@string/pref_sound_answer"
android:title="@string/pref_sound_answer_title" />
</PreferenceCategory>
</PreferenceCategory>
<PreferenceCategory
android:title="@string/pref_category_network_title">
<CheckBoxPreference
android:defaultValue="false"
android:key="@string/pref_network_tor"
android:title="@string/pref_network_tor_title"
android:summary="@string/pref_network_tor_summary" />
</PreferenceCategory>
</PreferenceScreen>