From a00b1f64125f6d36b62f52048cd7d6f6e90cfb67 Mon Sep 17 00:00:00 2001 From: Caesar2011 Date: Fri, 28 Apr 2017 18:03:54 +0200 Subject: [PATCH] Settings and help implemented; login via UID, rename, auto-name refresh on change --- .idea/misc.xml | 1 + app/src/main/AndroidManifest.xml | 18 +- .../it14/postgrachelor/HighscoreActivity.java | 17 +- .../it14/postgrachelor/LoginFragment.java | 24 ++- .../it14/postgrachelor/MainActivity.java | 188 +++++++++++++++-- .../it14/postgrachelor/MainFragment.java | 19 +- .../postgrachelor/QuestionEndFragment.java | 1 - .../it14/postgrachelor/QuestionFragment.java | 6 +- .../postgrachelor/Services/GameService.java | 125 +++++++---- .../Services/HighscoreService.java | 19 +- .../postgrachelor/Services/LoginService.java | 196 ++++++++++-------- .../it14/postgrachelor/SettingActivity.java | 82 ++++++++ .../postgrachelor/Types/HighscoresUser.java | 7 - .../it14/postgrachelor/Types/Login.java | 23 ++ .../it14/postgrachelor/Types/Prefs.java | 18 ++ .../it14/postgrachelor/Types/Requestable.java | 4 +- .../it14/postgrachelor/Types/SettingItem.java | 53 +++++ .../it14/postgrachelor/Types/StoredLogin.java | 38 ++++ .../postgrachelor/Types/StoredNextEntity.java | 12 -- .../postgrachelor/Types/StoredObject.java | 9 +- .../it14/postgrachelor/Types/StoredPrefs.java | 33 +++ .../postgrachelor/Types/StoredQuestion.java | 16 -- .../postgrachelor/Types/StoredStates.java | 2 - .../postgrachelor/Types/StoredTimings.java | 15 +- .../postgrachelor/Utils/AsyncAdapter.java | 48 +++-- .../postgrachelor/Utils/HighscoreAdapter.java | 12 +- .../it14/postgrachelor/Utils/JsonRequest.java | 28 ++- .../postgrachelor/Utils/JsonRequestPG.java | 1 - .../Utils/ReverseInterpolator.java | 1 + .../postgrachelor/Utils/SettingAdapter.java | 194 +++++++++++++++++ .../main/res/drawable-v21/ic_menu_camera.xml | 12 -- .../main/res/drawable-v21/ic_menu_gallery.xml | 9 - .../main/res/drawable-v21/ic_menu_manage.xml | 9 - .../main/res/drawable-v21/ic_menu_send.xml | 9 - .../main/res/drawable-v21/ic_menu_share.xml | 9 - .../res/drawable-v21/ic_menu_slideshow.xml | 9 - .../circular_progress_drawable_red.xml | 7 - app/src/main/res/drawable/ic_play_arrow.xml | 10 - app/src/main/res/drawable/side_nav_bar.xml | 9 - .../main/res/layout/activity_highscore.xml | 1 - app/src/main/res/layout/activity_main.xml | 1 - app/src/main/res/layout/activity_settings.xml | 14 ++ .../res/layout/adapter_highscore_details.xml | 5 +- app/src/main/res/layout/adapter_setting.xml | 21 ++ app/src/main/res/layout/fragment_login.xml | 9 + app/src/main/res/layout/fragment_main.xml | 3 +- .../res/layout/fragment_main_category.xml | 1 - app/src/main/res/menu/menu_main.xml | 15 +- app/src/main/res/values-de/plurals.xml | 7 + app/src/main/res/values-de/strings.xml | 26 ++- app/src/main/res/values/dimens.xml | 3 - app/src/main/res/values/drawables.xml | 9 +- app/src/main/res/values/plurals.xml | 7 + app/src/main/res/values/strings.xml | 26 ++- app/src/main/res/values/styles.xml | 6 + 55 files changed, 1073 insertions(+), 374 deletions(-) create mode 100644 app/src/main/java/de/hwr_berlin/it14/postgrachelor/SettingActivity.java create mode 100644 app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Login.java create mode 100644 app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Prefs.java create mode 100644 app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/SettingItem.java create mode 100644 app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredLogin.java create mode 100644 app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredPrefs.java create mode 100644 app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/SettingAdapter.java delete mode 100644 app/src/main/res/drawable-v21/ic_menu_camera.xml delete mode 100644 app/src/main/res/drawable-v21/ic_menu_gallery.xml delete mode 100644 app/src/main/res/drawable-v21/ic_menu_manage.xml delete mode 100644 app/src/main/res/drawable-v21/ic_menu_send.xml delete mode 100644 app/src/main/res/drawable-v21/ic_menu_share.xml delete mode 100644 app/src/main/res/drawable-v21/ic_menu_slideshow.xml delete mode 100644 app/src/main/res/drawable/circular_progress_drawable_red.xml delete mode 100644 app/src/main/res/drawable/ic_play_arrow.xml delete mode 100644 app/src/main/res/drawable/side_nav_bar.xml create mode 100644 app/src/main/res/layout/activity_settings.xml create mode 100644 app/src/main/res/layout/adapter_setting.xml create mode 100644 app/src/main/res/values-de/plurals.xml create mode 100644 app/src/main/res/values/plurals.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 57b25ae..85072bb 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,6 +3,7 @@ + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 4c140c3..5e6aaa1 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,5 +1,6 @@ @@ -13,7 +14,7 @@ @@ -30,7 +31,20 @@ android:scheme="postgrachelor" /> - + + + + + + \ No newline at end of file diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/HighscoreActivity.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/HighscoreActivity.java index 556d2ea..7d23f75 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/HighscoreActivity.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/HighscoreActivity.java @@ -4,8 +4,10 @@ import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ListView; -import android.widget.TextView; +import de.hwr_berlin.it14.postgrachelor.Services.GameService; +import de.hwr_berlin.it14.postgrachelor.Services.HighscoreService; +import de.hwr_berlin.it14.postgrachelor.Services.LoginService; import de.hwr_berlin.it14.postgrachelor.Utils.Conversion; import de.hwr_berlin.it14.postgrachelor.Utils.HighscoreAdapter; @@ -16,6 +18,10 @@ public class HighscoreActivity extends AppCompatActivity { super.onCreate(savedInstanceState); setContentView(R.layout.activity_highscore); + HighscoreService.initialize(this); + LoginService.initialize(this); + GameService.initialize(this); + if (getActionBar() != null) { getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setDisplayShowHomeEnabled(true); @@ -51,6 +57,15 @@ public class HighscoreActivity extends AppCompatActivity { return true; } + @Override + protected void onResume() { + super.onResume(); + + HighscoreService.initialize(this); + LoginService.initialize(this); + GameService.initialize(this); + } + @Override public void onSaveInstanceState(Bundle savedInstanceState) { super.onSaveInstanceState(savedInstanceState); diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/LoginFragment.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/LoginFragment.java index 1ed135c..212c653 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/LoginFragment.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/LoginFragment.java @@ -3,6 +3,8 @@ package de.hwr_berlin.it14.postgrachelor; import android.content.Context; import android.os.Bundle; import android.app.Fragment; +import android.text.Editable; +import android.text.TextWatcher; import android.util.Log; import android.view.LayoutInflater; import android.view.View; @@ -45,7 +47,7 @@ public class LoginFragment extends Fragment { Bundle savedInstanceState) { // Inflate the layout for this fragment View layout = inflater.inflate(R.layout.fragment_login, container, false); - Button login_btn = (Button) layout.findViewById(R.id.login_btn); + final Button login_btn = (Button) layout.findViewById(R.id.login_btn); final EditText login_edit = (EditText) layout.findViewById(R.id.login_edit); login_btn.setOnClickListener(new View.OnClickListener() { @Override @@ -53,15 +55,31 @@ public class LoginFragment extends Fragment { Log.d(NAME, "Button click: "+login_edit.getText().toString()); try { - LoginService.doLogin(login_edit.getText().toString()); + String value = login_edit.getText().toString(); + if (value.length()==32) + LoginService.doLogin(value); + else + LoginService.doRegister(value); } catch (NotInitializedException e) { e.printStackTrace(); } InputMethodManager mgr = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); mgr.hideSoftInputFromWindow(v.getWindowToken(), 0); + } + }); + login_edit.addTextChangedListener(new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence s, int start, int count, int after) {} + @Override + public void onTextChanged(CharSequence s, int start, int before, int count) {} - + @Override + public void afterTextChanged(Editable s) { + if (s.length()==32) + login_btn.setText(login_btn.getResources().getString(R.string.login_user_btn)); + else + login_btn.setText(login_btn.getResources().getString(R.string.register_btn)); } }); diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/MainActivity.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/MainActivity.java index b21fbf2..f18cc72 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/MainActivity.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/MainActivity.java @@ -3,10 +3,15 @@ package de.hwr_berlin.it14.postgrachelor; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.app.ProgressDialog; +import android.content.DialogInterface; +import android.content.Intent; import android.os.Bundle; +import android.support.v7.app.AlertDialog; +import android.support.v7.view.ContextThemeWrapper; import android.util.Log; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; +import android.util.SparseBooleanArray; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; @@ -20,12 +25,16 @@ import de.hwr_berlin.it14.postgrachelor.Exceptions.NotLoggedInException; import de.hwr_berlin.it14.postgrachelor.Services.GameService; import de.hwr_berlin.it14.postgrachelor.Services.HighscoreService; import de.hwr_berlin.it14.postgrachelor.Services.LoginService; +import de.hwr_berlin.it14.postgrachelor.Types.Login; import de.hwr_berlin.it14.postgrachelor.Types.RequestActivityInterface; import de.hwr_berlin.it14.postgrachelor.Types.StoredStates; public class MainActivity extends AppCompatActivity implements RequestActivityInterface { - private HashMap dialogs = new HashMap<>(); + private final HashMap dialogs = new HashMap<>(); + private Menu menu; + private boolean isSaved = true; + private final SparseBooleanArray menuStates = new SparseBooleanArray(); @Override public String showDialog() { @@ -83,8 +92,9 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + this.isSaved = false; - Log.d("Activity fragment", "create"); + Log.d(NAME, "create"); setContentView(R.layout.activity_main); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); @@ -100,6 +110,10 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn if (savedInstanceState != null) { this.selectFragment(savedInstanceState.getInt("selected fragment")); } + + HighscoreService.initialize(this); + LoginService.initialize(this); + GameService.initialize(this); } @Override @@ -109,21 +123,21 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn final MainActivity that = this; LoginService.addLoginEventListener(NAME, new LoginService.OnLoginEventListener() { @Override - public void onLoginEvent(String name, String uid) { - Log.d("Activity fragment", "onLogin - name: " + name + " - uid: " + uid); + public void onLoginEvent(Login login) { + Log.d(NAME, "onLogin - name: " + LoginService.getLogin().getName() + " - uid: " + LoginService.getLogin().getUID()); that.selectFragment(FragmentState.MAIN); } @Override public void onLogoutEvent(int status, String message) { - Log.d("Activity fragment", "onLogout - name: " + status + " - uid: " + message); + Log.d(NAME, "onLogout - name: " + status + " - uid: " + message); that.selectFragment(FragmentState.LOGIN); } }); GameService.addGameStateChangeEventListener(NAME, new GameService.OnGameStateChangeEventListener() { @Override public void onGameStateChangeEvent(int previous, int state) { - Log.d("Activity fragment", "onGameStateChange - previous: " + previous + " - state: " + state); + Log.d(NAME, "onGameStateChange - previous: " + previous + " - state: " + state); if (state == StoredStates.RUNNING || state == StoredStates.ON_HOLD_LOADING || state == StoredStates.ON_HOLD_RESULT) that.selectFragment(FragmentState.QUESTION); else if (previous == StoredStates.ON_HOLD_RESULT && state == StoredStates.END) { @@ -131,11 +145,9 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn } else if (state == StoredStates.PAUSED || state == StoredStates.END) { that.selectFragment(FragmentState.MAIN); } + updateOptionMenu(); } }); - HighscoreService.instantiate(this); - LoginService.instantiate(this); - GameService.initialize(this); //this.selectFragment(this.fragmentState); } @@ -157,6 +169,12 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn @Override protected void onResume() { super.onResume(); + this.isSaved = false; + + HighscoreService.initialize(this); + LoginService.initialize(this); + GameService.initialize(this); + Log.d(NAME, "Resuming"); selectFragment(this.fragmentState); } @@ -164,10 +182,38 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. + this.menu = menu; getMenuInflater().inflate(R.menu.menu_main, menu); return true; } + @Override + public boolean onPrepareOptionsMenu(Menu menu) { + this.updateOptionMenu(); + for (int i = 0; i < menuStates.size(); i++) { + int key = menuStates.keyAt(i); + if (menuStates.get(key)) + this.showOption(key); + else + this.hideOption(key); + } + menuStates.clear(); + return super.onPrepareOptionsMenu(menu); + } + + private void updateOptionMenu() { + if (GameService.isFinished()) { + this.setOptionIcon(R.id.action_gameState, android.R.drawable.ic_media_play); + this.setOptionTitle(R.id.action_gameState, this.getResources().getString(R.string.start_test)); + } else if (GameService.isRunning()) { + this.setOptionIcon(R.id.action_gameState, android.R.drawable.ic_media_pause); + this.setOptionTitle(R.id.action_gameState, this.getResources().getString(R.string.pause_test)); + } else if (GameService.isPaused()) { + this.setOptionIcon(R.id.action_gameState, android.R.drawable.ic_media_pause); + this.setOptionTitle(R.id.action_gameState, this.getResources().getString(R.string.resume_test)); + } + } + @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will @@ -177,6 +223,61 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { + Intent intent = new Intent(this, SettingActivity.class); + startActivity(intent); + return true; + } else if (id == R.id.action_gameState) { + if (GameService.isRunning()) { + try { + GameService.pauseGame(); + } catch (NotInitializedException e) { + e.printStackTrace(); + } + } else if (GameService.isPaused()) { + try { + GameService.resumeGame(); + } catch (NotInitializedException e) { + e.printStackTrace(); + } + } else if (GameService.isFinished()) { + try { + GameService.startGame(); + } catch (NotInitializedException | NotLoggedInException e) { + e.printStackTrace(); + } + } + return true; + } else if (id == R.id.action_help) { + int text = 0; + switch (this.fragmentState) { + case FragmentState.LOGIN: + text = R.string.help_login; + break; + case FragmentState.MAIN: + text = R.string.help_main; + break; + case FragmentState.QUESTION: + text = R.string.help_question; + break; + case FragmentState.QUESTION_END: + text = R.string.help_question_end; + break; + } + if (text != 0) { + + AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(this, R.style.DialogTheme)); + builder.setTitle(builder.getContext().getResources().getString(R.string.help)); + builder.setMessage(builder.getContext().getResources().getString(text)); + + builder.setNegativeButton("Close", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + builder.show(); + + } return true; } @@ -184,19 +285,24 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn } private void selectFragment(int item) { - boolean change = this.fragmentState != item; - this.fragmentState = item; - // Handle navigation view item clicks here. - try { - boolean isLoggedIn = LoginService.isLoggedIn(); - if (!isLoggedIn && (item != FragmentState.LOGIN)) { - return; - } else if (isLoggedIn && (item == FragmentState.LOGIN)) { - return; - } - } catch (NotInitializedException e) { - e.printStackTrace(); + if (this.isSaved) return; + + boolean isLoggedIn = LoginService.isLoggedIn(); + if (!isLoggedIn) { + item = FragmentState.LOGIN; + } else if (item == FragmentState.LOGIN) { + item = FragmentState.MAIN; + } + + boolean change = this.fragmentState != item; + + if (item == FragmentState.LOGIN) { + this.hideOption(R.id.action_gameState); + this.hideOption(R.id.action_settings); + } else if (this.fragmentState == FragmentState.LOGIN) { + this.showOption(R.id.action_gameState); + this.showOption(R.id.action_settings); } if (item == FragmentState.LOGIN) { @@ -236,11 +342,51 @@ public class MainActivity extends AppCompatActivity implements RequestActivityIn fragmentTransaction.commit(); } } + + + this.fragmentState = item; + } + + private void hideOption(int id) + { + if (this.menu!=null) { + MenuItem item = this.menu.findItem(id); + item.setVisible(false); + } else { + menuStates.put(id, false); + } + } + + private void showOption(int id) + { + if (this.menu!=null) { + MenuItem item = this.menu.findItem(id); + item.setVisible(true); + } else { + menuStates.put(id, true); + } + } + + private void setOptionTitle(int id, String title) + { + if (this.menu!=null) { + MenuItem item = this.menu.findItem(id); + item.setTitle(title); + } + } + + private void setOptionIcon(int id, int iconRes) + { + if (this.menu!=null) { + MenuItem item = this.menu.findItem(id); + item.setIcon(iconRes); + } } @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putInt("selected fragment", this.fragmentState); + this.isSaved = true; super.onSaveInstanceState(savedInstanceState); } diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/MainFragment.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/MainFragment.java index 20a1fb0..f77e93f 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/MainFragment.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/MainFragment.java @@ -26,6 +26,7 @@ import de.hwr_berlin.it14.postgrachelor.Services.HighscoreService; import de.hwr_berlin.it14.postgrachelor.Services.LoginService; import de.hwr_berlin.it14.postgrachelor.Types.Highscores; import de.hwr_berlin.it14.postgrachelor.Types.HighscoresCategories; +import de.hwr_berlin.it14.postgrachelor.Types.Login; import de.hwr_berlin.it14.postgrachelor.Types.StoredStates; import de.hwr_berlin.it14.postgrachelor.Utils.JsonRequestPG; @@ -74,9 +75,9 @@ public class MainFragment extends Fragment { }; LoginService.addLoginEventListener(NAME, new LoginService.OnLoginEventListener() { @Override - public void onLoginEvent(String name, String uid) { + public void onLoginEvent(Login login) { TextView textView = (TextView) view.findViewById(R.id.fragment_main_status).findViewById(R.id.fragment_user_name); - textView.setText(name); + textView.setText(login.getName()); } @Override @@ -123,14 +124,16 @@ public class MainFragment extends Fragment { categoryView = inflater.inflate(R.layout.fragment_main_category, gridLayoutCategories, false); categoryView.setTag(R.id.category_id, category.getId()); categoryView.setTag(R.id.category_name, category.getName()); - // TODO automated count categoryView.setTag(R.id.category_count, scores.getAll()); categoryView.setOnClickListener(categoryClick); textView = (TextView) categoryView.findViewById(R.id.textViewCategory); textView.setText(category.getName()); textView = (TextView) categoryView.findViewById(R.id.textViewScore); - textView.setText(String.format(Locale.getDefault(), "%1$d", category.getScore())); + if (category.getScore() != -1) + textView.setText(String.format(Locale.getDefault(), "%1$d", category.getScore())); + else + textView.setText(view.getResources().getString(R.string.no_score)); catProgress = (ProgressBar) categoryView.findViewById(R.id.progressBarCategory); catProgress.setProgress(0); @@ -143,7 +146,11 @@ public class MainFragment extends Fragment { categoryView.setLayoutParams(param); } - userStatusTextViewScore.setText(String.format(Locale.getDefault(), "%1$d", scores.getScore())); + if (scores.getScore() != -1) + userStatusTextViewScore.setText(String.format(Locale.getDefault(), "%1$d", scores.getScore())); + else + userStatusTextViewScore.setText(view.getResources().getString(R.string.no_score)); + userStatusProgressBar.setMax(scores.getAll()); userStatusProgressBar.setProgress(scores.getAll() - scores.getPlace() + 1); userStatusProgressBar.setTag(R.id.category_id, -1); @@ -219,7 +226,7 @@ public class MainFragment extends Fragment { } catch (NotInitializedException ignored) { } catch (NotLoggedInException e) { try { - LoginService.setLogout(15999, "Not logged in"); + LoginService.doLogout(15999, "Not logged in"); } catch (NotInitializedException ignored) { } } diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/QuestionEndFragment.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/QuestionEndFragment.java index e29975b..eb090af 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/QuestionEndFragment.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/QuestionEndFragment.java @@ -14,7 +14,6 @@ import java.util.Locale; import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInitializedException; import de.hwr_berlin.it14.postgrachelor.Services.GameService; import de.hwr_berlin.it14.postgrachelor.Types.Scores; -import de.hwr_berlin.it14.postgrachelor.Types.StoredScores; import de.hwr_berlin.it14.postgrachelor.Utils.Conversion; diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/QuestionFragment.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/QuestionFragment.java index 1d1e66e..b13b505 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/QuestionFragment.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/QuestionFragment.java @@ -23,7 +23,6 @@ import de.hwr_berlin.it14.postgrachelor.Services.GameService; import de.hwr_berlin.it14.postgrachelor.Types.Question; import de.hwr_berlin.it14.postgrachelor.Types.Result; import de.hwr_berlin.it14.postgrachelor.Types.StoredStates; -import de.hwr_berlin.it14.postgrachelor.Types.StoredTimings; import de.hwr_berlin.it14.postgrachelor.Types.Timings; import de.hwr_berlin.it14.postgrachelor.Utils.Conversion; import de.hwr_berlin.it14.postgrachelor.Utils.ReverseInterpolator; @@ -101,7 +100,10 @@ public class QuestionFragment extends Fragment { } else if (GameService.getState().getState() != StoredStates.ON_HOLD_RESULT) { try { GameService.answer((int) v.getTag()); - } catch (NotInitializedException | NotLoggedInException | NoCurrentQuestionException e) { + } catch (NotInitializedException | NotLoggedInException e) { + e.printStackTrace(); + } catch (NoCurrentQuestionException e) { + GameService.haltGame(); e.printStackTrace(); } } else { diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/GameService.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/GameService.java index 7f0ab22..1569e1f 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/GameService.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/GameService.java @@ -20,6 +20,8 @@ import de.hwr_berlin.it14.postgrachelor.Types.States; import de.hwr_berlin.it14.postgrachelor.Types.StoredNextEntity; import de.hwr_berlin.it14.postgrachelor.Types.StoredQuestion; import de.hwr_berlin.it14.postgrachelor.Types.StoredScores; +import de.hwr_berlin.it14.postgrachelor.Types.StoredPrefs; +import de.hwr_berlin.it14.postgrachelor.Types.Prefs; import de.hwr_berlin.it14.postgrachelor.Types.Question; import de.hwr_berlin.it14.postgrachelor.Types.Result; import de.hwr_berlin.it14.postgrachelor.Types.StoredStates; @@ -35,7 +37,6 @@ import de.hwr_berlin.it14.postgrachelor.Utils.JsonRequestPG; public class GameService { private static final int GAME_TICK_INTERVAL = 100; - private static final int QUESTION_COUNT = 10; private static final String NAME = "SERVICE_GAME"; private static final String PREFS_NAME = "PrefsGame"; @@ -45,25 +46,17 @@ public class GameService { private static StoredScores scores = null; private static StoredNextEntity next = null; private static StoredQuestion question = null; + private static StoredPrefs prefs = null; private static boolean initialized = false; @SuppressLint("StaticFieldLeak") private static Activity activity = null; - private static SharedPreferences settings = null; private static Timer timer = null; private static final HashMap gameStateChangeEventListeners = new HashMap<>(); private static final HashMap questionUpdateEventListeners = new HashMap<>(); private static final HashMap gameEndEventListeners = new HashMap<>(); private static final HashMap gameTickEventListeners = new HashMap<>(); - - public static void haltGame() { - Log.d(NAME, "Halt game!", new Throwable("HAlt game!")); - try { - GameService.endGame(); - } catch (NotInitializedException e) { - e.printStackTrace(); - } - } + private static final HashMap prefsUpdateEventListeners = new HashMap<>(); public interface OnGameStateChangeEventListener { void onGameStateChangeEvent(int previous, int state); @@ -77,19 +70,23 @@ public class GameService { public interface OnTickEventListener { void onTickEvent(Timings timings); } + public interface OnPrefsUpdateEventListener { + void onPrefsUpdateEvent(Prefs prefs); + } private GameService() {} public static void initialize(Activity activity) { GameService.activity = activity; - GameService.settings = activity.getSharedPreferences(GameService.PREFS_NAME, 0); + SharedPreferences settings = activity.getSharedPreferences(GameService.PREFS_NAME, 0); GameService.initialized = true; - GameService.states = new StoredStates(GameService.settings, ""); - GameService.timings = new StoredTimings(GameService.settings, ""); - GameService.scores = new StoredScores(GameService.settings, ""); - GameService.next = new StoredNextEntity(GameService.settings, ""); - GameService.question = new StoredQuestion(GameService.settings, ""); + GameService.states = new StoredStates(settings, ""); + GameService.timings = new StoredTimings(settings, ""); + GameService.scores = new StoredScores(settings, ""); + GameService.next = new StoredNextEntity(settings, ""); + GameService.question = new StoredQuestion(settings, ""); + GameService.prefs = new StoredPrefs(settings, ""); if (GameService.getState().getState()==StoredStates.RUNNING) GameService.startTimer(); @@ -98,6 +95,7 @@ public class GameService { emitQuestionUpdateEvent(); emitGameEndEvent(); emitGameStateChangeEvent(); + emitPrefsUpdateEvent(); } public static void addGameStateChangeEventListener(String key, OnGameStateChangeEventListener listener) { @@ -152,6 +150,17 @@ public class GameService { } } + public static void addPrefsUpdateEventListener(String key, OnPrefsUpdateEventListener onPrefsUpdateEventListener) { + prefsUpdateEventListeners.put(key, onPrefsUpdateEventListener); + onPrefsUpdateEventListener.onPrefsUpdateEvent(GameService.getPrefs()); + } + + private static void emitPrefsUpdateEvent() { + for (OnPrefsUpdateEventListener listener: prefsUpdateEventListeners.values()) { + listener.onPrefsUpdateEvent(GameService.getPrefs()); + } + } + private static void startTimer() { if (GameService.timer==null) GameService.timer = new Timer(); @@ -211,6 +220,38 @@ public class GameService { return GameService.next.getEnd(); } + private static Prefs getPrefs() { + return GameService.prefs.get(); + } + + + public static boolean isRunning() { + if (GameService.initialized) { + int state = GameService.getState().getState(); + return state==StoredStates.RUNNING || state==StoredStates.ON_HOLD_LOADING || state==StoredStates.ON_HOLD_RESULT; + } + return false; + } + + public static boolean isPaused() { + if (GameService.initialized) { + int state = GameService.getState().getState(); + return state==StoredStates.PAUSED; + } + return false; + } + public static boolean isFinished() { + if (GameService.initialized) { + int state = GameService.getState().getState(); + return state==StoredStates.END || state==StoredStates.UNINITIALIZED; + } + return false; + } + + public static boolean isNextNeeded() { + return GameService.initialized && GameService.next.isLoaded(); + } + // SETTER private static void startTimings() { @@ -229,7 +270,7 @@ public class GameService { GameService.next.setEnd(scores); } - // UNSETTER + // REMOVERS private static void resetTimings() { GameService.timings.reset(); @@ -262,17 +303,13 @@ public class GameService { } } - public static boolean resumeGame() throws NotInitializedException { + public static void resumeGame() throws NotInitializedException { if (!GameService.initialized) throw new NotInitializedException(); int state = GameService.getState().getState(); - if (state == StoredStates.PAUSED) { + if (state == StoredStates.PAUSED) GameService.setState(GameService.getState().getPrevious()); - return true; - } else { - return false; - } } public static void endGame() throws NotInitializedException { @@ -287,26 +324,35 @@ public class GameService { } + public static void haltGame() { + Log.d(NAME, "Halt game!", new Throwable("Halt game!")); + try { + GameService.endGame(); + } catch (NotInitializedException e) { + e.printStackTrace(); + } + } + public static void answer(int id) throws NotInitializedException, NotLoggedInException, NoCurrentQuestionException { if (!GameService.initialized) throw new NotInitializedException(); if (GameService.isNextNeeded()) throw new IllegalStateException(); + if (!LoginService.isLoggedIn()) + throw new NotLoggedInException(); + if (GameService.getQuestion()==null || GameService.getAnswer()==-1) { + throw new NoCurrentQuestionException(); + } GameService.stopTimings(); GameService.setAnswer(id); GameService.setState(StoredStates.ON_HOLD_LOADING); - if (GameService.getQuestion()==null || GameService.getAnswer()==-1) { - GameService.haltGame(); - return; - } - final String currentToken = GameService.getQuestion().getToken(); HashMap params = new HashMap<>(); params.put("answer", Conversion.intToStr(GameService.getAnswer())); - params.put("uid", LoginService.getLoginUID()); + params.put("uid", LoginService.getLogin().getUID()); params.put("token", currentToken); params.put("time", String.valueOf(GameService.getTimings().getTimeDiff())); Log.d("GameServiceFragment", "answer/processFinish - before"); @@ -365,6 +411,8 @@ public class GameService { public static void startGame() throws NotInitializedException, NotLoggedInException { if (!GameService.initialized) throw new NotInitializedException(); + if (!LoginService.isLoggedIn()) + throw new NotLoggedInException(); GameService.unsetNext(); GameService.unsetScores(); @@ -373,9 +421,9 @@ public class GameService { HashMap params = new HashMap<>(); String uid; - uid = LoginService.getLoginUID(); + uid = LoginService.getLogin().getUID(); params.put("uid", uid); - params.put("length", Conversion.intToStr(GameService.QUESTION_COUNT)); + params.put("length", Conversion.intToStr(GameService.getPrefs().getGameLength())); JsonRequestPG requester = new JsonRequestPG("start.php", params, GameService.activity, new JsonRequestPG.AsyncResponse() { @Override public void processFinish(JSONObject data) { @@ -393,7 +441,7 @@ public class GameService { GameService.setState(StoredStates.END); if (status==12104) { // user uid not found try { - LoginService.setLogout(status, message); + LoginService.doLogout(status, message); } catch (NotInitializedException e) { e.printStackTrace(); } @@ -403,10 +451,6 @@ public class GameService { requester.execute(); } - public static boolean isNextNeeded() { - return GameService.initialized && GameService.next.isLoaded(); - } - public static void runNext() throws IllegalStateException, NotInitializedException { if (!GameService.initialized) throw new NotInitializedException(); @@ -425,6 +469,13 @@ public class GameService { GameService.unsetNext(); } + public static void setGameLength(int length) throws NotInitializedException { + if (!GameService.initialized) + throw new NotInitializedException(); + GameService.prefs.setGameLength(length); + GameService.emitPrefsUpdateEvent(); + } + private static void setQuestion(Question question) { Log.d(NAME, "token: "+question.getToken()); GameService.question.setQuestion(question); diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/HighscoreService.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/HighscoreService.java index ec2e871..c7790d1 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/HighscoreService.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/HighscoreService.java @@ -2,7 +2,6 @@ package de.hwr_berlin.it14.postgrachelor.Services; import android.annotation.SuppressLint; import android.app.Activity; -import android.util.Log; import org.json.JSONArray; import org.json.JSONException; @@ -26,8 +25,6 @@ import de.hwr_berlin.it14.postgrachelor.Utils.JsonRequestPG; public class HighscoreService { private static final String NAME = "HighscoreService"; private static final long MIN_REFRESH_RATE = 1000; - //private static final String PREFS_NAME = "PrefsHighscore"; - //private static SharedPreferences settings = null; private static Highscores latestScores = null; private static boolean instantiated = false; @SuppressLint("StaticFieldLeak") @@ -50,8 +47,7 @@ public class HighscoreService { } } - public static void instantiate(Activity activity) { - //settings = activity.getSharedPreferences(PREFS_NAME, 0); + public static void initialize(Activity activity) { HighscoreService.activity = activity; instantiated = true; } @@ -74,18 +70,19 @@ public class HighscoreService { } public static void updateHighscores(final JsonRequestPG.AsyncResponse asyncResponse) throws NotInitializedException, NotLoggedInException { + if (!instantiated) + throw new NotInitializedException(); + if (!LoginService.isLoggedIn()) + throw new NotLoggedInException(); + if (lastUpdate >= System.currentTimeMillis()-MIN_REFRESH_RATE && available()) { emitHighscoreUpdateEvent(); return; } lastUpdate = System.currentTimeMillis(); - if (!instantiated) - throw new NotInitializedException(); HashMap params = new HashMap<>(); - String uid = LoginService.getLoginUID(); - Log.d(NAME, uid); - params.put("uid", LoginService.getLoginUID()); + params.put("uid", LoginService.getLogin().getUID()); JsonRequestPG requester = new JsonRequestPG("highscores.php", params, /*activity*/null, new JsonRequestPG.AsyncResponse() { @Override public void processFinish(JSONObject output) { @@ -124,7 +121,7 @@ public class HighscoreService { setLatestScores(null); if (status==15102) { // user uid not found try { - LoginService.setLogout(status, message); + LoginService.doLogout(status, message); } catch (NotInitializedException e) { e.printStackTrace(); } diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/LoginService.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/LoginService.java index 4680a1e..bf6163e 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/LoginService.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Services/LoginService.java @@ -10,7 +10,8 @@ import org.json.JSONObject; import java.util.HashMap; import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInitializedException; -import de.hwr_berlin.it14.postgrachelor.Exceptions.NotLoggedInException; +import de.hwr_berlin.it14.postgrachelor.Types.Login; +import de.hwr_berlin.it14.postgrachelor.Types.StoredLogin; import de.hwr_berlin.it14.postgrachelor.Utils.JsonRequestPG; /** @@ -20,31 +21,28 @@ import de.hwr_berlin.it14.postgrachelor.Utils.JsonRequestPG; public class LoginService { private static final String PREFS_NAME = "PrefsLogin"; - private static SharedPreferences settings = null; + private static final String NAME = "SERVICE_GAME"; private static boolean instantiated = false; @SuppressLint("StaticFieldLeak") private static Activity activity = null; + private static StoredLogin login = null; private static final HashMap loginEventListeners = new HashMap<>(); public interface OnLoginEventListener { - void onLoginEvent(String name, String uid); + void onLoginEvent(Login login); void onLogoutEvent(int status, String message); } public static void addLoginEventListener(String key, OnLoginEventListener onLoginEventListener) { loginEventListeners.put(key, onLoginEventListener); - if (isLoggedInSave()) { - try { - onLoginEventListener.onLoginEvent(getLoginName(), getLoginUID()); - } catch (NotInitializedException | NotLoggedInException e) { - e.printStackTrace(); - } + if (isLoggedIn()) { + onLoginEventListener.onLoginEvent(getLogin()); } } - private static void emitLoginEvent(String name, String uid) { + private static void emitLoginEvent() { for (OnLoginEventListener listener: loginEventListeners.values()) { - listener.onLoginEvent(name, uid); + listener.onLoginEvent(getLogin()); } } @@ -54,112 +52,136 @@ public class LoginService { } } - public static void instantiate(Activity activity) { - settings = activity.getSharedPreferences(PREFS_NAME, 0); + public static void initialize(Activity activity) { + SharedPreferences settings = activity.getSharedPreferences(PREFS_NAME, 0); LoginService.activity = activity; instantiated = true; - String name = null; - String uid = null; - try { - name = getLoginName(); - uid = getLoginUIDSave(); - } catch (NotInitializedException e) { - e.printStackTrace(); - } - if (isLoggedInSave()) { - emitLoginEvent(name, uid); + + login = new StoredLogin(settings, ""); + + if (LoginService.isLoggedIn()) { + emitLoginEvent(); + try { + LoginService.doLogin(LoginService.getLogin().getUID()); + } catch (NotInitializedException e) { + e.printStackTrace(); + } } else { emitLogoutEvent(-5, null); } } - public static boolean isLoggedIn() throws NotInitializedException { - if (!instantiated) - throw new NotInitializedException(); - return settings.getBoolean("isLoggedIn", false); + public static boolean isLoggedIn() { + return login != null && login.isLoaded(); } - private static boolean isLoggedInSave() { - try { - return isLoggedIn(); - } catch (NotInitializedException e) { - return false; - } + public static Login getLogin() { + return login.get(); } - private static String getLoginName() throws NotInitializedException { - if (!LoginService.isLoggedIn()) - return ""; - return settings.getString("loginName", ""); + private static void setLogin(Login user) { + login.login(user); + emitLoginEvent(); } - static String getLoginUID() throws NotInitializedException, NotLoggedInException { - if (!LoginService.isLoggedIn()) - throw new NotLoggedInException(); - return settings.getString("loginUID", ""); - } - - private static String getLoginUIDSave() throws NotInitializedException { - if (!LoginService.isLoggedIn()) - return ""; - return settings.getString("loginUID", ""); - } - - private static void setLogin(String name, String uid) throws NotInitializedException { - if (!instantiated) - throw new NotInitializedException(); - SharedPreferences.Editor editor = settings.edit(); - editor.putString("loginName", name); - editor.putString("loginUID", uid); - editor.putBoolean("isLoggedIn", true); - editor.apply(); - emitLoginEvent(name, uid); - } - - public static void setLogout(int status, String message) throws NotInitializedException { - if (!instantiated) - throw new NotInitializedException(); - SharedPreferences.Editor editor = settings.edit(); - editor.clear(); - editor.apply(); + private static void setLogout(int status, String message) { + login.logout(); emitLogoutEvent(status, message); } - private static void setLoginSave(String name, String uid) { - try { - setLogin(name, uid); - } catch (NotInitializedException ignored) { - } - } - - private static void setLogoutSave(int status, String message) { - try { - setLogout(status, message); - } catch (NotInitializedException ignored) { - } - } - - public static void doLogin(String name) throws NotInitializedException { + public static void doRegister(String name) throws NotInitializedException { if (!instantiated) throw new NotInitializedException(); + HashMap params = new HashMap<>(); params.put("name", name); JsonRequestPG requester = new JsonRequestPG("register.php", params, activity, new JsonRequestPG.AsyncResponse() { @Override public void processFinish(JSONObject output) { - Log.d("Activity fragment", "output"); - Log.d("Activity fragment", output.toString()); + Log.d(NAME, "output"); + Log.d(NAME, output.toString()); // never reached if not instantiated - LoginService.setLoginSave(output.optString("name", ""), output.optString("uid", "")); + LoginService.setLogin(new Login( + output.optString("name", ""), + output.optString("uid", "") + )); } @Override public void processError(int status, String message) { - Log.d("Activity fragment", "error"); - Log.d("Activity fragment", "status: "+status+" - message: "+message); + Log.d(NAME, "error"); + Log.d(NAME, "status: "+status+" - message: "+message); + // never reached if not instantiated + LoginService.setLogout(status, message); + } + }); + requester.execute(); + } + + public static void doLogin(String uid) throws NotInitializedException { + if (!instantiated) + throw new NotInitializedException(); + + HashMap params = new HashMap<>(); + params.put("uid", uid); + JsonRequestPG requester = new JsonRequestPG("login.php", params, activity, new JsonRequestPG.AsyncResponse() { + @Override + public void processFinish(JSONObject output) { + Log.d(NAME, "output"); + Log.d(NAME, output.toString()); + + // never reached if not instantiated + LoginService.setLogin(new Login( + output.optString("name", ""), + output.optString("uid", "") + )); + } + + @Override + public void processError(int status, String message) { + Log.d(NAME, "error"); + Log.d(NAME, "status: "+status+" - message: "+message); + } + }); + requester.execute(); + } + + public static void doLogout(int status, String message) throws NotInitializedException { + if (!LoginService.instantiated) { + throw new NotInitializedException(); + } + + GameService.endGame(); + GameService.endGame(); + LoginService.setLogout(status, message); + } + + public static void doRename(String name) throws NotInitializedException { + if (!instantiated) + throw new NotInitializedException(); + + HashMap params = new HashMap<>(); + params.put("name", name); + params.put("uid", LoginService.getLogin().getUID()); + JsonRequestPG requester = new JsonRequestPG("rename.php", params, activity, new JsonRequestPG.AsyncResponse() { + @Override + public void processFinish(JSONObject output) { + Log.d(NAME, "output"); + Log.d(NAME, output.toString()); + + // never reached if not instantiated + LoginService.setLogin(new Login( + output.optString("name", ""), + output.optString("uid", "") + )); + } + + @Override + public void processError(int status, String message) { + Log.d(NAME, "error"); + Log.d(NAME, "status: "+status+" - message: "+message); // never reached if not instantiated - LoginService.setLogoutSave(status, message); } }); requester.execute(); diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/SettingActivity.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/SettingActivity.java new file mode 100644 index 0000000..879d358 --- /dev/null +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/SettingActivity.java @@ -0,0 +1,82 @@ +package de.hwr_berlin.it14.postgrachelor; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.widget.ListView; + +import de.hwr_berlin.it14.postgrachelor.Services.GameService; +import de.hwr_berlin.it14.postgrachelor.Services.HighscoreService; +import de.hwr_berlin.it14.postgrachelor.Services.LoginService; +import de.hwr_berlin.it14.postgrachelor.Types.Login; +import de.hwr_berlin.it14.postgrachelor.Utils.SettingAdapter; + +/** + * Created by Sebastian on 24.04.2017. + */ + +public class SettingActivity extends AppCompatActivity { + + private static final String NAME = "SettingActivity"; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_settings); + + if (getActionBar() != null) { + getActionBar().setDisplayHomeAsUpEnabled(true); + getActionBar().setDisplayShowHomeEnabled(true); + } else if (getSupportActionBar() != null) { + getSupportActionBar().setDisplayHomeAsUpEnabled(true); + getSupportActionBar().setDisplayShowHomeEnabled(true); + } + + ListView listView = (ListView) findViewById(R.id.setting_listview); + SettingAdapter adapter = new SettingAdapter(this); + listView.setAdapter(adapter); + + adapter.notifyDataSetChanged(); + + GameService.initialize(this); + LoginService.initialize(this); + HighscoreService.initialize(this); + LoginService.addLoginEventListener(NAME, new LoginService.OnLoginEventListener() { + @Override + public void onLoginEvent(Login login) { + + } + + @Override + public void onLogoutEvent(int status, String message) { + SettingActivity.this.finish(); + } + }); + } + + @Override + public boolean onSupportNavigateUp() { + onBackPressed(); + return true; + } + + @Override + public boolean onNavigateUp() { + onBackPressed(); + return true; + } + + @Override + protected void onResume() { + super.onResume(); + + HighscoreService.initialize(this); + LoginService.initialize(this); + GameService.initialize(this); + } + + @Override + public void onSaveInstanceState(Bundle savedInstanceState) { + super.onSaveInstanceState(savedInstanceState); + } + +} diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/HighscoresUser.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/HighscoresUser.java index ad5ff9a..a438f6c 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/HighscoresUser.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/HighscoresUser.java @@ -16,13 +16,6 @@ public class HighscoresUser extends Requestable { this.unrequested(); } - public HighscoresUser() { - super(); - this.name = ""; - this.score = 0; - this.place = 0; - } - public String getName() { return name; } diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Login.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Login.java new file mode 100644 index 0000000..64a1c99 --- /dev/null +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Login.java @@ -0,0 +1,23 @@ +package de.hwr_berlin.it14.postgrachelor.Types; + +/** + * Created by Sebastian on 24.04.2017. + */ + +public class Login { + private final String name; + private final String uid; + + public Login(String name, String uid) { + this.name = name; + this.uid = uid; + } + + public String getName() { + return name; + } + + public String getUID() { + return uid; + } +} diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Prefs.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Prefs.java new file mode 100644 index 0000000..20ea030 --- /dev/null +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Prefs.java @@ -0,0 +1,18 @@ +package de.hwr_berlin.it14.postgrachelor.Types; + +/** + * Created by Sebastian on 23.04.2017. + * Time and score measurement + */ + +public class Prefs { + private final int gameLength; + + Prefs(int gameLength) { + this.gameLength = gameLength; + } + + public int getGameLength() { + return gameLength; + } +} diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Requestable.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Requestable.java index c74d38e..a880c01 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Requestable.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/Requestable.java @@ -15,11 +15,11 @@ public class Requestable { return this.requested != 0; } - protected void unrequested() { + void unrequested() { this.requested = 0; } - protected void requested() { + private void requested() { this.requested = System.currentTimeMillis(); } } diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/SettingItem.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/SettingItem.java new file mode 100644 index 0000000..5c28538 --- /dev/null +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/SettingItem.java @@ -0,0 +1,53 @@ +package de.hwr_berlin.it14.postgrachelor.Types; + +import android.view.View; + + + +/** + * Created by Sebastian on 24.04.2017. + */ + +public class SettingItem { + private final String title; + private String name; + private final View.OnClickListener onClickListener; + private final String message; + private String value; + + public SettingItem(String title, String message, View.OnClickListener onClickListener) { + this.title = title; + this.name = ""; + this.message = message; + this.value = ""; + this.onClickListener = onClickListener; + } + + public String getTitle() { + return title; + } + + public View.OnClickListener getOnClickListener() { + return onClickListener; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getMessage() { + return message; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } +} diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredLogin.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredLogin.java new file mode 100644 index 0000000..9c65d39 --- /dev/null +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredLogin.java @@ -0,0 +1,38 @@ +package de.hwr_berlin.it14.postgrachelor.Types; + +import android.content.SharedPreferences; + +/** + * Created by Sebastian on 24.04.2017. + */ + +public class StoredLogin extends StoredObject { + private static final String STR_NAME = "name"; + private String name; + private static final String STR_UID = "uid"; + private String uid; + + public StoredLogin(SharedPreferences prefs, String prefix) { + super(prefs, prefix+".login"); + this.name = this.loadFromPref(STR_NAME, ""); + this.uid = this.loadFromPref(STR_UID, ""); + } + + public void login(Login login) { + this.name = login.getName(); + this.saveToPref(STR_NAME, this.name); + this.uid = login.getUID(); + this.saveToPref(STR_UID, this.uid); + this.unremove(); + } + + public void logout() { + this.remove(); + } + + public Login get() { + if (!this.isLoaded()) + return null; + return new Login(this.name, this.uid); + } +} diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredNextEntity.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredNextEntity.java index f00c2c0..9ecf56b 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredNextEntity.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredNextEntity.java @@ -70,10 +70,6 @@ public class StoredNextEntity extends StoredObject { this.answer3 }); } - - protected void remove() { - super.remove(); - } } private class End extends StoredObject { @@ -139,14 +135,6 @@ public class StoredNextEntity extends StoredObject { return this.end.get(); } - public boolean isNext() { - return this.next.isLoaded(); - } - - public boolean isEnd() { - return this.end.isLoaded(); - } - public void remove() { super.remove(); } diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredObject.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredObject.java index 8e41022..fc4d104 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredObject.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredObject.java @@ -2,9 +2,6 @@ package de.hwr_berlin.it14.postgrachelor.Types; import android.content.SharedPreferences; import android.util.Log; -import android.util.NoSuchPropertyException; - -import java.util.NoSuchElementException; /** * Created by Sebastian on 18.04.2017. @@ -12,15 +9,15 @@ import java.util.NoSuchElementException; */ abstract class StoredObject { - private SharedPreferences prefs; - private String prefix; + private final SharedPreferences prefs; + private final String prefix; StoredObject(SharedPreferences prefs, String prefix){ this.prefs = prefs; this.prefix = prefix; } - protected void remove() { + void remove() { SharedPreferences.Editor editor = this.prefs.edit(); editor.putBoolean("L"+this.prefix, false); editor.apply(); diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredPrefs.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredPrefs.java new file mode 100644 index 0000000..fae1444 --- /dev/null +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredPrefs.java @@ -0,0 +1,33 @@ +package de.hwr_berlin.it14.postgrachelor.Types; + +import android.content.SharedPreferences; + +/** + * Created by Sebastian on 06.04.2017. + * Timing results on each tick, when game is running + */ +public class StoredPrefs extends StoredObject { + private static final int DEFAULT_GAME_LENGTH = 10; + + private int gameLength; + private static final String STR_GAME_LENGTH = "gameLength"; + + public StoredPrefs(SharedPreferences prefs, String prefix) { + super(prefs, prefix+".gameLength"); + this.gameLength = this.loadFromPref(STR_GAME_LENGTH, DEFAULT_GAME_LENGTH); + this.unremove(); + } + + public void setGameLength(int gameLength) { + if (gameLength > 0) + this.gameLength = gameLength; + else + this.gameLength = DEFAULT_GAME_LENGTH; + + this.saveToPref(STR_GAME_LENGTH, this.gameLength); + } + + public Prefs get() { + return new Prefs(this.gameLength); + } +} diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredQuestion.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredQuestion.java index 6d28542..314a232 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredQuestion.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredQuestion.java @@ -69,10 +69,6 @@ public class StoredQuestion extends StoredObject { this.answer3 }); } - - protected void remove() { - super.remove(); - } } private class Answer extends StoredObject { @@ -189,18 +185,6 @@ public class StoredQuestion extends StoredObject { return this.result.get(); } - public boolean isQuestion() { - return this.question.isLoaded(); - } - - public boolean isAnswer() { - return this.answer.isLoaded(); - } - - public boolean isResult() { - return this.result.isLoaded(); - } - public void remove() { super.remove(); } diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredStates.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredStates.java index 1d15aab..948a0fa 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredStates.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredStates.java @@ -2,8 +2,6 @@ package de.hwr_berlin.it14.postgrachelor.Types; import android.content.SharedPreferences; -import java.util.NoSuchElementException; - /** * Created by Sebastian on 21.04.2017. * Stored game state object diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredTimings.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredTimings.java index 9075639..8782e4e 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredTimings.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Types/StoredTimings.java @@ -1,7 +1,6 @@ package de.hwr_berlin.it14.postgrachelor.Types; import android.content.SharedPreferences; -import android.provider.Settings; /** * Created by Sebastian on 06.04.2017. @@ -15,14 +14,14 @@ public class StoredTimings extends StoredObject { private static final int T = 100; private long startTime; - private static final String STR_STARTTIME = "startTime"; + private static final String STR_START_TIME = "startTime"; private long endTime; - private static final String STR_ENDTIME = "endTime"; + private static final String STR_END_TIME = "endTime"; public StoredTimings(SharedPreferences prefs, String prefix) { super(prefs, prefix+".timings"); - this.startTime = this.loadFromPref(STR_STARTTIME, (long) -1); - this.endTime = this.loadFromPref(STR_ENDTIME, (long) -1); + this.startTime = this.loadFromPref(STR_START_TIME, (long) -1); + this.endTime = this.loadFromPref(STR_END_TIME, (long) -1); if (this.startTime == -1) this.remove(); else @@ -31,9 +30,9 @@ public class StoredTimings extends StoredObject { public void start() { this.startTime = System.currentTimeMillis(); - this.saveToPref(STR_STARTTIME, this.startTime); + this.saveToPref(STR_START_TIME, this.startTime); this.endTime = -1; - this.saveToPref(STR_ENDTIME, this.endTime); + this.saveToPref(STR_END_TIME, this.endTime); this.unremove(); } @@ -41,7 +40,7 @@ public class StoredTimings extends StoredObject { if (this.endTime != -1) return; this.endTime = System.currentTimeMillis(); - this.saveToPref(STR_ENDTIME, this.endTime); + this.saveToPref(STR_END_TIME, this.endTime); } public void reset() { diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/AsyncAdapter.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/AsyncAdapter.java index 04afe89..b3b5e4f 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/AsyncAdapter.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/AsyncAdapter.java @@ -1,7 +1,6 @@ package de.hwr_berlin.it14.postgrachelor.Utils; import android.os.AsyncTask; -import android.test.suitebuilder.annotation.Suppress; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; @@ -14,14 +13,14 @@ import de.hwr_berlin.it14.postgrachelor.Types.Requestable; * Created by Sebastian on 10.04.2017. */ -public abstract class AsyncAdapter extends BaseAdapter { +abstract class AsyncAdapter extends BaseAdapter { private int count; private ArrayList items; private AsyncAdapter() {} - public AsyncAdapter(int count) { + AsyncAdapter(int count) { super(); items = new ArrayList<>(); this.setCount(count); @@ -51,7 +50,7 @@ public abstract class AsyncAdapter extends BaseAdapter { return convertView; } - @SuppressWarnings("unchecked") + @SuppressWarnings({"unchecked", "UnusedParameters"}) private void loadView(int position, View convertView, ViewGroup parent) { if (position >= this.items.size() || this.items.get(position) == null) { AsyncRequest request = new AsyncRequest(position, convertView); @@ -61,8 +60,10 @@ public abstract class AsyncAdapter extends BaseAdapter { } } + @SuppressWarnings("UnusedParameters") abstract void fillView(T item, int position, View convertView); + @SuppressWarnings("UnusedParameters") abstract View inflateView(int position, ViewGroup parent); abstract View clearView(int position, View convertView); @@ -78,10 +79,10 @@ public abstract class AsyncAdapter extends BaseAdapter { - class AsyncRequest extends AsyncTask> { + private class AsyncRequest extends AsyncTask> { private final int position; - private View convertView; + private final View convertView; AsyncRequest(int position, View convertView) { this.position = position; @@ -104,16 +105,21 @@ public abstract class AsyncAdapter extends BaseAdapter { @Override - protected ListSequence doInBackground(Void... arg0) { - return getAsyncListSequence(position); + protected ListSequence doInBackground(Void... arg0) { + ListSequence list = getAsyncListSequence(position); + ArrayList items = list.getItems(); + ArrayList requestables = new ArrayList<>(items.size()); + for (T item: items) { + requestables.add(item); + } + return new ListSequence<>(list.getStart(), requestables); } @Override - @SuppressWarnings("unchecked") - protected void onPostExecute(ListSequence result) { + protected void onPostExecute(ListSequence result) { super.onPostExecute(result); int start = result.getStart(); - ArrayList list = result.getItems(); + ArrayList list = result.getItems(); for (int i = items.size(); i < start; i++) { items.add(null); } @@ -124,17 +130,25 @@ public abstract class AsyncAdapter extends BaseAdapter { items.add(list.remove(0)); start++; } - if (position < items.size()) - fillView((T) items.get(position), position, convertView); + if (position < items.size()) { + try { + T item; + //noinspection unchecked + item = (T) items.get(position); + fillView(item, position, convertView); + } catch (ClassCastException e) { + fillView(null, position, convertView); + } + } notifyDataSetChanged(); } } - class ListSequence { + class ListSequence { private final int start; - private final ArrayList items; + private final ArrayList items; - public ListSequence(int start, int end, ArrayList items) { + ListSequence(int start, ArrayList items) { this.start = start; this.items = items; } @@ -144,7 +158,7 @@ public abstract class AsyncAdapter extends BaseAdapter { return start; } - public ArrayList getItems() { + ArrayList getItems() { return items; } } diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/HighscoreAdapter.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/HighscoreAdapter.java index 8947d5f..af48720 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/HighscoreAdapter.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/HighscoreAdapter.java @@ -18,8 +18,8 @@ import de.hwr_berlin.it14.postgrachelor.Types.HighscoresUser; */ public class HighscoreAdapter extends AsyncAdapter { - private Context context; - private int category; + private final Context context; + private final int category; public HighscoreAdapter(int count, int category, Context context) { super(count); @@ -35,12 +35,10 @@ public class HighscoreAdapter extends AsyncAdapter { TextView userNameView = (TextView) convertView.findViewById(R.id.tvName); TextView userPlaceView = (TextView) convertView.findViewById(R.id.tvPlace); TextView userScoreView = (TextView) convertView.findViewById(R.id.tvScore); - if (userNameView != null) { + if (userNameView != null) userNameView.setText(item.getName()); - } - if (userPlaceView != null) { + if (userPlaceView != null) userPlaceView.setText(convertView.getContext().getResources().getString(R.string.place, Conversion.intToStr(item.getPlace()))); - } if (userScoreView != null) { int score = item.getScore(); if (score != -1) @@ -77,7 +75,7 @@ public class HighscoreAdapter extends AsyncAdapter { ListSequence getAsyncListSequence(int position) { int limit = this.getRequestedLength(position); ArrayList items = HighscoreService.getHighscoreDetailsSync(limit, position, this.category, null); - return new ListSequence<>(position, position+items.size()-1, items); + return new ListSequence<>(position, items); } @Override diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/JsonRequest.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/JsonRequest.java index a4e1f7c..7bf81c2 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/JsonRequest.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/JsonRequest.java @@ -1,11 +1,9 @@ package de.hwr_berlin.it14.postgrachelor.Utils; import android.app.Activity; -import android.app.ProgressDialog; import android.net.Uri; import android.os.AsyncTask; import android.util.Log; -import android.widget.Toast; import org.json.JSONException; import org.json.JSONObject; @@ -20,9 +18,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Scanner; -import de.hwr_berlin.it14.postgrachelor.R; import de.hwr_berlin.it14.postgrachelor.Types.RequestActivityInterface; -import de.hwr_berlin.it14.postgrachelor.Types.Requestable; import static android.content.ContentValues.TAG; @@ -31,16 +27,18 @@ abstract class JsonRequest extends AsyncTask { private final String path; private final HashMap params; final Activity activity; + private final RequestActivityInterface requestInterface; private String uuid; JsonRequest(String path, HashMap params, Activity activity) { this.connectionURL = getConnectionURL(); this.path = path; this.params = params; + this.activity = activity; if (activity instanceof RequestActivityInterface) { - this.activity = activity; + this.requestInterface = ((RequestActivityInterface) this.activity); } else { - this.activity = null; + this.requestInterface = null; } Log.d("JSONRequest create", connectionURL+path); } @@ -48,7 +46,7 @@ abstract class JsonRequest extends AsyncTask { @SuppressWarnings("SameReturnValue") protected abstract String getConnectionURL(); - public JSONObject executeSync() { + JSONObject executeSync() { JSONObject result = this.doInBackground(); Log.d("JSONRequest excecute", result.toString()); return result; @@ -59,8 +57,8 @@ abstract class JsonRequest extends AsyncTask { super.onPreExecute(); Log.d("fragment, JSONRequest", "onPre1"); // Showing progress dialog - if (this.activity != null) - this.uuid = ((RequestActivityInterface) this.activity).showDialog(); + if (this.requestInterface != null) + this.uuid = this.requestInterface.showDialog(); } @@ -104,11 +102,11 @@ abstract class JsonRequest extends AsyncTask { jsonObj = new JSONObject(jsonText); } catch (final JSONException e) { Log.e(TAG, "Json parsing error: " + e.getMessage()); - if (this.activity != null) { + if (this.requestInterface != null) { activity.runOnUiThread(new Runnable() { @Override public void run() { - ((RequestActivityInterface) activity).errorJsonLoadingFailed(e.getMessage()); + requestInterface.errorJsonLoadingFailed(e.getMessage()); } }); @@ -116,11 +114,11 @@ abstract class JsonRequest extends AsyncTask { } } else { Log.e(TAG, "Couldn't get json from server."); - if (this.activity != null) { + if (this.requestInterface != null) { activity.runOnUiThread(new Runnable() { @Override public void run() { - ((RequestActivityInterface) activity).errorJsonParsing(); + requestInterface.errorJsonParsing(); } }); @@ -134,8 +132,8 @@ abstract class JsonRequest extends AsyncTask { @Override protected void onPostExecute(JSONObject result) { super.onPostExecute(result); - if (this.activity != null) - ((RequestActivityInterface) activity).hideDialog(this.uuid); + if (this.requestInterface != null) + requestInterface.hideDialog(this.uuid); } private String parseURL(String url, Map params) diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/JsonRequestPG.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/JsonRequestPG.java index 32fe861..ac8f886 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/JsonRequestPG.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/JsonRequestPG.java @@ -3,7 +3,6 @@ package de.hwr_berlin.it14.postgrachelor.Utils; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; -import android.util.Log; import org.json.JSONArray; import org.json.JSONException; diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/ReverseInterpolator.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/ReverseInterpolator.java index 501fc30..10addca 100644 --- a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/ReverseInterpolator.java +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/ReverseInterpolator.java @@ -14,6 +14,7 @@ public class ReverseInterpolator implements Interpolator { this.delegate = delegate; } + @SuppressWarnings("unused") public ReverseInterpolator(){ this(new LinearInterpolator()); } diff --git a/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/SettingAdapter.java b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/SettingAdapter.java new file mode 100644 index 0000000..0cab74d --- /dev/null +++ b/app/src/main/java/de/hwr_berlin/it14/postgrachelor/Utils/SettingAdapter.java @@ -0,0 +1,194 @@ +package de.hwr_berlin.it14.postgrachelor.Utils; + +import android.app.Activity; +import android.content.Context; +import android.content.DialogInterface; +import android.support.v7.app.AlertDialog; +import android.support.v7.view.ContextThemeWrapper; +import android.text.InputType; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.EditText; +import android.widget.TextView; + +import de.hwr_berlin.it14.postgrachelor.Exceptions.NotInitializedException; +import de.hwr_berlin.it14.postgrachelor.R; +import de.hwr_berlin.it14.postgrachelor.Services.GameService; +import de.hwr_berlin.it14.postgrachelor.Services.LoginService; +import de.hwr_berlin.it14.postgrachelor.Types.Login; +import de.hwr_berlin.it14.postgrachelor.Types.Prefs; +import de.hwr_berlin.it14.postgrachelor.Types.SettingItem; + +/** + * Created by Sebastian on 24.04.2017. + */ + +public class SettingAdapter extends BaseAdapter { + private static final String NAME = "SettingAdapter"; + private final Activity activity; + private final Context context; + private final SettingItem[] items; + + public SettingAdapter(final Activity activity) { + super(); + this.context = activity.getApplicationContext(); + this.activity = activity; + this.items = new SettingItem[] { + new SettingItem(context.getString(R.string.game_length), + context.getString(R.string.game_length_msg), + new View.OnClickListener() { + @Override + public void onClick(View v) { + final EditText input = getInputView(); + input.setInputType(InputType.TYPE_CLASS_NUMBER); + getInsertBox((SettingItem) v.getTag(), input, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + int len = Conversion.strToInt(input.getText().toString(), 0); + try { + GameService.setGameLength(len); + } catch (NotInitializedException e) { + e.printStackTrace(); + } + } + }).show(); + } + }), + new SettingItem(context.getString(R.string.change_user_name), + context.getString(R.string.change_user_name_msg), new View.OnClickListener() { + @Override + public void onClick(View v) { + final EditText input = getInputView(); + getInsertBox((SettingItem) v.getTag(), input, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + try { + LoginService.doRename(input.getText().toString()); + } catch (NotInitializedException e) { + e.printStackTrace(); + } + } + }).show(); + } + }), + new SettingItem(context.getString(R.string.copy_uid), + context.getString(R.string.copy_uid_msg), new View.OnClickListener() { + @Override + public void onClick(View v) { + final EditText input = getInputView(); + input.setSelectAllOnFocus(true); + getInsertBox((SettingItem) v.getTag(), input, null).show(); + } + }), + new SettingItem(context.getString(R.string.logout_user), + context.getString(R.string.logout_user_msg), new View.OnClickListener() { + @Override + public void onClick(View v) { + getInsertBox((SettingItem) v.getTag(), null, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + try { + LoginService.doLogout(-7, "User desired logout!"); + } catch (NotInitializedException e) { + e.printStackTrace(); + } + } + }).show(); + } + }) + }; + items[3].setName(context.getString(R.string.logout_user_value)); + + GameService.addPrefsUpdateEventListener(NAME, new GameService.OnPrefsUpdateEventListener() { + @Override + public void onPrefsUpdateEvent(Prefs prefs) { + items[0].setName(context.getResources().getQuantityString(R.plurals.current_length, prefs.getGameLength(), prefs.getGameLength())); + items[0].setValue(Conversion.intToStr(prefs.getGameLength())); + notifyDataSetChanged(); + } + }); + + LoginService.addLoginEventListener(NAME, new LoginService.OnLoginEventListener() { + @Override + public void onLoginEvent(Login login) { + items[1].setName(context.getString(R.string.current_name, login.getName())); + items[1].setValue(login.getName()); + items[2].setName(context.getString(R.string.current_uid, login.getUID())); + items[2].setValue(login.getUID()); + notifyDataSetChanged(); + } + + @Override + public void onLogoutEvent(int status, String message) { + items[1].setName(context.getString(R.string.not_logged_in)); + items[1].setValue(""); + items[2].setName(context.getString(R.string.not_logged_in)); + items[2].setValue(""); + notifyDataSetChanged(); + } + }); + } + + @Override + public int getCount() { + return items.length; + } + + @Override + public SettingItem getItem(int position) { + return this.items[position]; + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + if (convertView == null) { + LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + convertView = inflater.inflate(R.layout.adapter_setting, parent, false); + } + SettingItem item = getItem(position); + TextView textView = (TextView) convertView.findViewById(R.id.svTitle); + textView.setText(item.getTitle()); + textView = (TextView) convertView.findViewById(R.id.svName); + textView.setText(item.getName()); + convertView.setOnClickListener(item.getOnClickListener()); + convertView.setTag(item); + return convertView; + } + + private EditText getInputView() { + final EditText input = new EditText(context); + // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text + input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS); + return input; + } + + private AlertDialog.Builder getInsertBox(SettingItem item, EditText input, DialogInterface.OnClickListener onPositive) { + AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(activity, R.style.DialogTheme)); + builder.setTitle(item.getTitle()); + builder.setMessage(item.getMessage()); + + // Set up the input + if (input != null) { + input.setText(item.getValue()); + builder.setView(input); + } + // Set up the buttons + if (onPositive != null) + builder.setPositiveButton("OK", onPositive); + builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + dialog.cancel(); + } + }); + + return builder; + } +} diff --git a/app/src/main/res/drawable-v21/ic_menu_camera.xml b/app/src/main/res/drawable-v21/ic_menu_camera.xml deleted file mode 100644 index 0d9ea10..0000000 --- a/app/src/main/res/drawable-v21/ic_menu_camera.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - diff --git a/app/src/main/res/drawable-v21/ic_menu_gallery.xml b/app/src/main/res/drawable-v21/ic_menu_gallery.xml deleted file mode 100644 index f6872c4..0000000 --- a/app/src/main/res/drawable-v21/ic_menu_gallery.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/drawable-v21/ic_menu_manage.xml b/app/src/main/res/drawable-v21/ic_menu_manage.xml deleted file mode 100644 index c1be60b..0000000 --- a/app/src/main/res/drawable-v21/ic_menu_manage.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - \ No newline at end of file diff --git a/app/src/main/res/drawable-v21/ic_menu_send.xml b/app/src/main/res/drawable-v21/ic_menu_send.xml deleted file mode 100644 index 00c668c..0000000 --- a/app/src/main/res/drawable-v21/ic_menu_send.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/drawable-v21/ic_menu_share.xml b/app/src/main/res/drawable-v21/ic_menu_share.xml deleted file mode 100644 index a28fb9e..0000000 --- a/app/src/main/res/drawable-v21/ic_menu_share.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/drawable-v21/ic_menu_slideshow.xml b/app/src/main/res/drawable-v21/ic_menu_slideshow.xml deleted file mode 100644 index 209aa64..0000000 --- a/app/src/main/res/drawable-v21/ic_menu_slideshow.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - diff --git a/app/src/main/res/drawable/circular_progress_drawable_red.xml b/app/src/main/res/drawable/circular_progress_drawable_red.xml deleted file mode 100644 index 5683748..0000000 --- a/app/src/main/res/drawable/circular_progress_drawable_red.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_play_arrow.xml b/app/src/main/res/drawable/ic_play_arrow.xml deleted file mode 100644 index 663a87d..0000000 --- a/app/src/main/res/drawable/ic_play_arrow.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - diff --git a/app/src/main/res/drawable/side_nav_bar.xml b/app/src/main/res/drawable/side_nav_bar.xml deleted file mode 100644 index 458b4b0..0000000 --- a/app/src/main/res/drawable/side_nav_bar.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - \ No newline at end of file diff --git a/app/src/main/res/layout/activity_highscore.xml b/app/src/main/res/layout/activity_highscore.xml index 3e64254..6f30b4e 100644 --- a/app/src/main/res/layout/activity_highscore.xml +++ b/app/src/main/res/layout/activity_highscore.xml @@ -1,6 +1,5 @@ + + + + + diff --git a/app/src/main/res/layout/adapter_highscore_details.xml b/app/src/main/res/layout/adapter_highscore_details.xml index 7c1000f..eb31b41 100644 --- a/app/src/main/res/layout/adapter_highscore_details.xml +++ b/app/src/main/res/layout/adapter_highscore_details.xml @@ -9,12 +9,15 @@ style="@style/TextAppearance.AppCompat.Body1" android:textColor="@android:color/primary_text_light" android:padding="20dp" + android:paddingStart="0dp" + android:paddingLeft="0dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="end" android:width="70sp" android:maxWidth="70sp" - android:text="@string/_0" /> + tools:text="0000" + tools:ignore="RtlSymmetry" /> + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_login.xml b/app/src/main/res/layout/fragment_login.xml index 27d806a..7af0c70 100644 --- a/app/src/main/res/layout/fragment_login.xml +++ b/app/src/main/res/layout/fragment_login.xml @@ -47,6 +47,15 @@ android:layout_height="wrap_content" android:id="@+id/login_btn" android:text="@string/register_btn"/> + diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml index e4f6f7b..6021223 100644 --- a/app/src/main/res/layout/fragment_main.xml +++ b/app/src/main/res/layout/fragment_main.xml @@ -37,7 +37,8 @@ grid:alignmentMode="alignBounds" grid:columnCount="2" grid:rowOrderPreserved="false" - grid:useDefaultMargins="true"> + grid:useDefaultMargins="true" + tools:listitem="@layout/fragment_main_category"> + + android:icon="@android:drawable/ic_menu_preferences" + app:showAsAction="never|withText" /> + diff --git a/app/src/main/res/values-de/plurals.xml b/app/src/main/res/values-de/plurals.xml new file mode 100644 index 0000000..5b82f0a --- /dev/null +++ b/app/src/main/res/values-de/plurals.xml @@ -0,0 +1,7 @@ + + + + eine Frage + %d Fragen + + \ No newline at end of file diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 1163449..68a43d8 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -1,17 +1,14 @@ - Einstellungen - - Benutzername eingeben… - + Benutzername oder UID eingeben… Willkommen zu unserer sinnlosen App! Beweise, dass du schlauer bist als wir und beantworte unsere Fragen! Registrieren Test starten! Test beendet! Zurück android.studio@android.com - Highscores konnten nicht geladen werden! + Highscores konnten nicht geladen werden! Herunterziehen zum aktualisieren… Test wiederaufnehmen! Keine Punkte Keine Zeit @@ -21,4 +18,23 @@ Bitte warten… Lädt… %1$s. + Hilfe + Test pausieren! + Benutzernamen ändern + Gib Deinen neuen Benutzernamen ein: + Klicke, um deine UID zu kopieren + Kopiere deine UID und stelle dein Account auf einem anderen Account wieder her! + Aktueller Benutzername: %s + Aktuelle UID: %s + Spiellänge + Gib die Anzahl der Fragen in einem Test an: + Du bist gar nicht eingeloggt, Junge! + Melde dich aus deinem Account aus! + Möchtest du dich wirklich ausloggen? Stelle sicher, dass du deine UID extern GESICHERT hast! + Abmelden + Login mit deiner UID + Registriere Dich mit einem eindeutigen Namen mit 6–20 Zeichen. Falls du schon ein Account hast, kannst du auch deine 32 Zeichen lange UID eintragen, die du innerhalb der App in den Einstellungen findest. + Hier siehst Du Deine Highscores! Unter dem \"Spiel starten\"-Button findest Du sowohl deine Gesamt-Highscore als auch für die einzelnen Kategorien. Der Fortschrittsbalken zeigt live Deine Position zu anderen Spielern. Die angegebenen Punkte entsprechen gewichteten Durchschnittspunkten pro Frage. + Schnell! Je schneller du antwortest, desto mehr Punkte bekommst du! Für jede korrekte Antwort erhälst du mindestens 100 Punkte. Falsche Antworten werden mit 0 Punkten gewertet! + Herzlichen Glückwunsch! Du hast deinen Test erfolgreich beendet und deine Gesamtpunkte und deine für den Test benötigte Zeit werden nun angezeigt. diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index c2effc5..7d270e7 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -1,9 +1,6 @@ - 16dp - 160dp 16dp 16dp - 16dp diff --git a/app/src/main/res/values/drawables.xml b/app/src/main/res/values/drawables.xml index 9036d27..2c5844d 100644 --- a/app/src/main/res/values/drawables.xml +++ b/app/src/main/res/values/drawables.xml @@ -1,8 +1 @@ - - @android:drawable/ic_menu_camera - @android:drawable/ic_menu_gallery - @android:drawable/ic_menu_slideshow - @android:drawable/ic_menu_manage - @android:drawable/ic_menu_share - @android:drawable/ic_menu_send - + diff --git a/app/src/main/res/values/plurals.xml b/app/src/main/res/values/plurals.xml new file mode 100644 index 0000000..24747e1 --- /dev/null +++ b/app/src/main/res/values/plurals.xml @@ -0,0 +1,7 @@ + + + + 1 question + %d questions + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cbbc0ef..0018486 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,11 +1,8 @@ PostGrachelor - Settings - - Enter User Name… - + Enter User Name or UID… Welcome to our stupid app. Prove that you are smarter than we are and answer our questions! Register Start Test! @@ -14,7 +11,7 @@ Return Launcher Icon android.studio@android.com - Highscores could not be loaded! + Highscores could not be loaded! Pull down to refresh… Resume Test! 02:15 No Score @@ -26,4 +23,23 @@ Please wait… Loading… #%1$s + Help + Pause Test! + Current Name: %s + You are not logged in, dude! + Game Length + Change User Name + Click to copy your UID + Current UID: %s + Enter how many questions a game should contain: + Enter your new user name: + Copy your UID to restore your account on a different device! + Logout + Log out from your account! Be sure to COPY your UID externally before! + Log out from your account! + Login with your UID + Register yourself with a unique name with 6 to 20 characters. If you already have an account enter your 32 character long UID. You can find it in your settings screen inside the app. + This page shows your highscores! Underneath the test start button you can find your general highscore and your scores per category. The progress bar indicates your live place relative to other players. Points are valued average points per question. + Hurry up! The faster you answer your question, the more points you get! For any correct answer you receive at least 100 point. Wrong answers are rewarded with 0 points! + Congratulations! You finished your test successfully and your total score and total time of all questions are shown. diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 545b9c6..8ed65f1 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -17,4 +17,10 @@ +