LoginActivity.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * DSBDirect
  3. * Copyright (C) 2019 Fynn Godau
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. *
  18. * This software is not affiliated with heinekingmedia GmbH, the
  19. * developer of the DSB platform.
  20. */
  21. package godau.fynn.dsbdirect.activity;
  22. import android.content.DialogInterface;
  23. import android.content.Intent;
  24. import android.os.Build;
  25. import android.os.Bundle;
  26. import android.os.Handler;
  27. import android.os.Message;
  28. import android.view.KeyEvent;
  29. import android.view.View;
  30. import android.widget.Button;
  31. import androidx.appcompat.app.AlertDialog;
  32. import androidx.appcompat.app.AppCompatActivity;
  33. import com.google.android.material.textfield.TextInputEditText;
  34. import godau.fynn.dsbdirect.Login;
  35. import godau.fynn.dsbdirect.NewsQuery;
  36. import godau.fynn.dsbdirect.R;
  37. import godau.fynn.dsbdirect.Utility;
  38. import godau.fynn.dsbdirect.activity.settings.MainSettingsActivity;
  39. import godau.fynn.dsbdirect.manager.download.DownloadManager;
  40. import godau.fynn.dsbdirect.manager.FileManager;
  41. import godau.fynn.dsbdirect.manager.LoginManager;
  42. import godau.fynn.dsbdirect.manager.download.exception.LoginFailureException;
  43. import godau.fynn.dsbdirect.manager.download.exception.NoContentException;
  44. import godau.fynn.dsbdirect.table.Table;
  45. import java.io.IOException;
  46. public class LoginActivity extends AppCompatActivity {
  47. @Override
  48. protected void onCreate(Bundle savedInstanceState) {
  49. super.onCreate(savedInstanceState);
  50. setContentView(R.layout.activity_login);
  51. final TextInputEditText idEditText = findViewById(R.id.id);
  52. final TextInputEditText passEditText = findViewById(R.id.pass);
  53. final Button login = findViewById(R.id.login);
  54. final Utility u = new Utility(LoginActivity.this);
  55. login.setOnClickListener(new View.OnClickListener() {
  56. @Override
  57. public void onClick(View v) {
  58. String id = idEditText.getText().toString();
  59. String pass = passEditText.getText().toString();
  60. final Login login = new Login(id, pass);
  61. // Indicate that something is happening
  62. indicateProgress(true);
  63. final DownloadManager downloadManager = DownloadManager.getDownloadManager(LoginActivity.this);
  64. new Thread(new Runnable() {
  65. @Override
  66. public void run() {
  67. try {
  68. Table[] tables = downloadManager.downloadTables(login);
  69. // If no exception occurred, the credentials are correct
  70. LoginManager loginManager = new LoginManager(LoginActivity.this);
  71. // Only prompt user for their preferences when they log in with their first login
  72. boolean promptPreferences = loginManager.getLoginCount() <= 0;
  73. loginManager.addLogin(login);
  74. setResult(RESULT_OK);
  75. if (promptPreferences) {
  76. potentiallyPromptForFilter(downloadManager, u, login.getId(), tables);
  77. } else {
  78. // Skip questions
  79. finish();
  80. }
  81. } catch (LoginFailureException e) {
  82. runOnUiThread(new Runnable() {
  83. @Override
  84. public void run() {
  85. // Credentials invalid
  86. new AlertDialog.Builder(LoginActivity.this, R.style.LoginTheme_AlertDialog)
  87. .setTitle(R.string.credentials_popup_title)
  88. .setMessage(R.string.credentials_popup_message)
  89. .setPositiveButton(R.string.ok, null)
  90. .setNeutralButton(R.string.news_fix, new DialogInterface.OnClickListener() {
  91. @Override
  92. public void onClick(DialogInterface dialog, int which) {
  93. new Thread(new NewsQuery(LoginActivity.this, downloadManager))
  94. .start();
  95. }
  96. })
  97. .setNegativeButton(R.string.credentials_popup_open_preferences, new DialogInterface.OnClickListener() {
  98. @Override
  99. public void onClick(DialogInterface dialog, int which) {
  100. startActivity(new Intent(LoginActivity.this, MainSettingsActivity.class));
  101. }
  102. })
  103. .show();
  104. indicateProgress(false);
  105. }
  106. });
  107. e.printStackTrace();
  108. } catch (IOException e) {
  109. runOnUiThread(new Runnable() {
  110. @Override
  111. public void run() {
  112. // Error
  113. new AlertDialog.Builder(LoginActivity.this, R.style.LoginTheme_AlertDialog)
  114. .setTitle(R.string.network_generic_error)
  115. .setMessage(R.string.network_generic_error_credentials)
  116. .setPositiveButton(R.string.ok, null)
  117. .show();
  118. indicateProgress(false);
  119. }
  120. });
  121. e.printStackTrace();
  122. }
  123. }
  124. }).start();
  125. }
  126. });
  127. // Handel enter presses
  128. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) passEditText.setOnKeyListener(new View.OnKeyListener() {
  129. @Override
  130. public boolean onKey(View v, int keyCode, KeyEvent event) {
  131. if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
  132. login.callOnClick();
  133. return true;
  134. }
  135. return false;
  136. }
  137. });
  138. }
  139. private void potentiallyPromptForFilter(final DownloadManager downloadManager, final Utility u, final String id, Table[] tables) {
  140. try {
  141. if (tables[0].isHtml()) {
  142. FileManager fileManager = new FileManager(LoginActivity.this);
  143. // Download the first table
  144. String html = fileManager.getHtmlTable(tables[0], downloadManager);
  145. // If a reader can be gotten, parsing is probably possible
  146. if (u.getReader(html, id) != null) {
  147. runOnUiThread(new Runnable() {
  148. @Override
  149. public void run() {
  150. indicateProgress(false);
  151. MainSettingsActivity.setFilterPopup(LoginActivity.this, R.style.LoginTheme_AlertDialog,
  152. new Handler(new Handler.Callback() {
  153. @Override
  154. public boolean handleMessage(Message msg) {
  155. // Ensure parse and filter are on
  156. u.getSharedPreferences().edit()
  157. .putBoolean("parse", true)
  158. .putBoolean("filter", true)
  159. .apply();
  160. promptForNotificationsOnUiThreadAndFinish();
  161. return false;
  162. }
  163. }));
  164. }
  165. });
  166. } else {
  167. // Parsing seems to be impossible
  168. u.getSharedPreferences().edit()
  169. .putBoolean("parse", false)
  170. .apply();
  171. promptForNotificationsOnUiThreadAndFinish();
  172. }
  173. } else {
  174. promptForNotificationsOnUiThreadAndFinish();
  175. }
  176. } catch (NoContentException e) {
  177. promptForNotificationsOnUiThreadAndFinish();
  178. e.printStackTrace();
  179. } catch (IOException e) {
  180. e.printStackTrace();
  181. }
  182. }
  183. private void promptForNotificationsOnUiThreadAndFinish() {
  184. runOnUiThread(new Runnable() {
  185. @Override
  186. public void run() {
  187. indicateProgress(false);
  188. new AlertDialog.Builder(LoginActivity.this, R.style.LoginTheme_AlertDialog)
  189. .setTitle(R.string.notifications_popup_title)
  190. .setMessage(R.string.notifications_popup_message)
  191. .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
  192. @Override
  193. public void onClick(DialogInterface dialog, int which) {
  194. new Utility(LoginActivity.this).getSharedPreferences()
  195. .edit()
  196. .putBoolean("poll", true)
  197. .apply();
  198. finish();
  199. }
  200. })
  201. .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
  202. @Override
  203. public void onClick(DialogInterface dialog, int which) {
  204. new Utility(LoginActivity.this).getSharedPreferences()
  205. .edit()
  206. .putBoolean("poll", false)
  207. .apply();
  208. finish();
  209. }
  210. })
  211. .show();
  212. }
  213. });
  214. }
  215. private void indicateProgress(boolean yes) {
  216. if (yes) {
  217. findViewById(R.id.login).setEnabled(false);
  218. findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
  219. } else {
  220. findViewById(R.id.login).setEnabled(true);
  221. findViewById(R.id.progressBar).setVisibility(View.GONE);
  222. }
  223. }
  224. @Override
  225. public void finish() {
  226. new Utility(LoginActivity.this)
  227. .getSharedPreferences()
  228. .edit()
  229. .putBoolean("login", true)
  230. .apply();
  231. super.finish();
  232. }
  233. }