diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/activities/MainActivity.java b/app/src/main/java/com/luca0n/joguitos/pluck/activities/MainActivity.java index a29015a..0adeb1f 100644 --- a/app/src/main/java/com/luca0n/joguitos/pluck/activities/MainActivity.java +++ b/app/src/main/java/com/luca0n/joguitos/pluck/activities/MainActivity.java @@ -24,67 +24,92 @@ Contact us at . package com.luca0n.joguitos.pluck.activities; import android.content.Intent; +import android.os.Build; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.Spinner; +import androidx.appcompat.app.AlertDialog; import butterknife.BindView; import butterknife.ButterKnife; import com.luca0n.joguitos.pluck.R; +import com.luca0n.joguitos.pluck.trivia.TriviaSource; import com.luca0n.joguitos.pluck.trivia.TriviaCategory; import com.luca0n.joguitos.pluck.trivia.TriviaDifficulty; import com.luca0n.joguitos.pluck.trivia.TriviaQuery; public class MainActivity extends BaseActivity { - @BindView(R.id.button_play) - Button buttonPlay; - @BindView(R.id.spinner_number) - Spinner spinnerNumber; - @BindView(R.id.spinner_category) - Spinner spinnerCategory; - @BindView(R.id.spinner_difficulty) - Spinner spinnerDifficulty; + @BindView(R.id.button_play) + Button buttonPlay; + @BindView(R.id.spinner_source) + Spinner spinnerSource; + @BindView(R.id.spinner_number) + Spinner spinnerNumber; + @BindView(R.id.spinner_category) + Spinner spinnerCategory; + @BindView(R.id.spinner_difficulty) + Spinner spinnerDifficulty; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - ButterKnife.bind(this); + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + ButterKnife.bind(this); - buttonPlay.setOnClickListener(v -> { - int amount = (int) spinnerNumber.getSelectedItem(); - TriviaCategory category = (TriviaCategory) spinnerCategory.getSelectedItem(); - TriviaDifficulty difficulty = (TriviaDifficulty) spinnerDifficulty.getSelectedItem(); + buttonPlay.setOnClickListener(v -> { + int sourcePos = spinnerSource.getSelectedItemPosition(); + if (TriviaSource.values()[sourcePos] == TriviaSource.FILE){ + // Check the Android version. + if (Build.VERSION.SDK_INT >= 19){ + // TODO: Show the open document dialog and read the trivia file. + return; + } else { + // Unsupported version. + new AlertDialog.Builder(this) + .setIcon(android.R.drawable.ic_dialog_alert) + .setTitle(R.string.error) + .setMessage(R.string.error_source_file_android_version_unsupported) + .setPositiveButton(android.R.string.ok, null) + .show(); + } + return; + } + int amount = (int) spinnerNumber.getSelectedItem(); + TriviaCategory category = (TriviaCategory) spinnerCategory.getSelectedItem(); + TriviaDifficulty difficulty = (TriviaDifficulty) spinnerDifficulty.getSelectedItem(); - TriviaQuery query = new TriviaQuery.Builder(amount) - .category(category) - .difficulty(difficulty) - .build(); + TriviaQuery query = new TriviaQuery.Builder(amount) + .category(category) + .difficulty(difficulty) + .build(); - Intent intent = new Intent(getApplicationContext(), TriviaGameActivity.class); - intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_QUERY, query); - startActivity(intent); - }); + Intent intent = new Intent(getApplicationContext(), TriviaGameActivity.class); + intent.putExtra(TriviaGameActivity.EXTRA_TRIVIA_QUERY, query); + startActivity(intent); + }); + Integer[] numbers = new Integer[50]; + for (int i = 0; i < 50; ) { + numbers[i] = ++i; + } + spinnerSource.setAdapter( + new ArrayAdapter<>( + this, android.R.layout.simple_list_item_1, TriviaSource.values()) + ); + spinnerNumber.setAdapter( + new ArrayAdapter<>( + this, android.R.layout.simple_list_item_1, numbers) + ); + spinnerNumber.setSelection(9); - Integer[] numbers = new Integer[50]; - for (int i = 0; i < 50; ) { - numbers[i] = ++i; - } - spinnerNumber.setAdapter( - new ArrayAdapter<>( - this, android.R.layout.simple_list_item_1, numbers) - ); - spinnerNumber.setSelection(9); + spinnerCategory.setAdapter( + new ArrayAdapter<>( + this, android.R.layout.simple_list_item_1, TriviaCategory.values())); - spinnerCategory.setAdapter( - new ArrayAdapter<>( - this, android.R.layout.simple_list_item_1, TriviaCategory.values())); - - spinnerDifficulty.setAdapter( - new ArrayAdapter<>( - this, android.R.layout.simple_list_item_1, TriviaDifficulty.values())); - } + spinnerDifficulty.setAdapter( + new ArrayAdapter<>( + this, android.R.layout.simple_list_item_1, TriviaDifficulty.values())); + } } diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java b/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java index cece721..0167303 100644 --- a/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java +++ b/app/src/main/java/com/luca0n/joguitos/pluck/activities/TriviaGameActivity.java @@ -95,9 +95,23 @@ public class TriviaGameActivity extends BaseActivity progressBar.setVisibility(View.VISIBLE); - DownloadTriviaQuestionsTask task = new DownloadTriviaQuestionsTask(); - task.setReceiver(this); - task.execute(query); + // Gather information about the trivia location. + /*String triviaLocation; + + switch (triviaLocation){ + case "GET_FROM_URL":*/ + DownloadTriviaQuestionsTask task = new DownloadTriviaQuestionsTask(); + task.setReceiver(this); + task.execute(query);/* + break; + case "GET_FROM_FILE": + + break; + default: + // Unknown specified trivia location. + // TODO: Show error message to the user about this internal error. + break; + }*/ } } diff --git a/app/src/main/java/com/luca0n/joguitos/pluck/trivia/TriviaSource.java b/app/src/main/java/com/luca0n/joguitos/pluck/trivia/TriviaSource.java new file mode 100644 index 0000000..7a09cbb --- /dev/null +++ b/app/src/main/java/com/luca0n/joguitos/pluck/trivia/TriviaSource.java @@ -0,0 +1,66 @@ +/* +Pluck: an open source trivia game for Android + +Copyright (C) 2021 Joguitos do luca0N! + +This program is free software: you can redistribute it and/or modify it under +the terms of the GNU General Public License as published by the Free Software +Foundation, either version 3 of the License, or (at your option) any later +version. + +This program is distributed in the hope that it will be useful, but WITHOUT +ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + +You should have received a copy of the GNU General Public License along with +this program. If not, see . + +This game is a fork of LibreTrivia, and its source code is available at +. + +Contact us at . +*/ + +package com.luca0n.joguitos.pluck.trivia; + +import java.util.HashMap; +import java.util.Map; + +import com.luca0n.joguitos.pluck.PluckApplication; +import com.luca0n.joguitos.pluck.R; + +public enum TriviaSource { + SERVER("server", R.string.source_server), + FILE("file", R.string.source_file); + + // Name of source + private final String name; + // Display name of the source + private final int displayName; + + private static final Map lookup = new HashMap<>(); + + static { + for (TriviaSource source : TriviaSource.values()) { + lookup.put(source.getName(), source); + } + } + + TriviaSource(String name, int displayName) { + this.name = name; + this.displayName = displayName; + } + + public String getName() { + return this.name; + } + + public static TriviaSource get(String name) { + return lookup.get(name); + } + + @Override + public String toString() { + return PluckApplication.getAppContext().getResources().getString(this.displayName); + } +} diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index f726a49..3ac5954 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -61,6 +61,10 @@ Contact us at . Multiple Choice True / False + + Server + File + Settings About @@ -80,13 +84,17 @@ Contact us at . Correct Answers Wrong Answers Total Questions - Return To Menu + Return To Menu + Source + This value specifies where the game should retrieve trivia data. By default, the game gathers trivia data from a server, but you can load trivia files with the "File" source option. - + + Error Network error!\n\nCould not connect to a network. Check if your custom server address is correct. If Tor is enabled in the game settings, check if Orbot is running and then try again. No trivia results!\n\nWas not able to find trivia questions that satisfied all options. Server response error!\n\nCould not parse the server response. The server response may be in a unsupported version. If you are using a custom server, make sure the server address points to a valid address and then try again. An unknown error occurred! + Trivia files are not supported in this version of Android. Settings @@ -98,6 +106,6 @@ Contact us at . Network Connect Using Tor Retrieves trivia data via the Tor network. Requires Orbot (recommended) or a Tor daemon with a SOCKS5 proxy listening on port 9050. - Server address - Specifies the server location used by the game to fetch questions. Leave empty to use the default server. + Server Address + Specifies the server location used by the game to fetch questions when using a server as the trivia source. Leave this setting empty to use the default server.