Compare commits

..

No commits in common. "f32a0a7a4386948e75df582ab4342226c696d7b5" and "fb2d81550c5639c2b11c422a03802196d7631c01" have entirely different histories.

4 changed files with 166 additions and 196 deletions

View file

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

View file

@ -83,15 +83,11 @@ Contact us at <joguitos+pluck@luca0n.com>.
<string name="ui_return_to_menu">Return To Menu</string> <string name="ui_return_to_menu">Return To Menu</string>
<!-- Error Strings --> <!-- Error Strings -->
<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_network"><b>Network error!</b>\n Could not connect to a network.</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="error_no_trivia_results"><b>No trivia results!</b>\n Was not able to find trivia questions that satisfied all options.</string>
<string name="title_activity_settings">Settings</string> <string name="title_activity_settings">Settings</string>
<!-- Setting Strings--> <!-- Setting Strings-->
<string name="pref_category_sound_title">Sound</string> <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> </resources>

View file

@ -33,14 +33,6 @@ Contact us at <joguitos+pluck@luca0n.com>.
android:key="@string/pref_sound_answer" android:key="@string/pref_sound_answer"
android:title="@string/pref_sound_answer_title" /> 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> </PreferenceScreen>