123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279 |
- /*
- * DSBDirect
- * Copyright (C) 2019 Fynn Godau
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- *
- * This software is not affiliated with heinekingmedia GmbH, the
- * developer of the DSB platform.
- */
- package godau.fynn.dsbdirect.activity;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.view.KeyEvent;
- import android.view.View;
- import android.widget.Button;
- import androidx.appcompat.app.AlertDialog;
- import androidx.appcompat.app.AppCompatActivity;
- import com.google.android.material.textfield.TextInputEditText;
- import godau.fynn.dsbdirect.Login;
- import godau.fynn.dsbdirect.NewsQuery;
- import godau.fynn.dsbdirect.R;
- import godau.fynn.dsbdirect.Utility;
- import godau.fynn.dsbdirect.activity.settings.MainSettingsActivity;
- import godau.fynn.dsbdirect.manager.download.DownloadManager;
- import godau.fynn.dsbdirect.manager.FileManager;
- import godau.fynn.dsbdirect.manager.LoginManager;
- import godau.fynn.dsbdirect.manager.download.exception.LoginFailureException;
- import godau.fynn.dsbdirect.manager.download.exception.NoContentException;
- import godau.fynn.dsbdirect.table.Table;
- import java.io.IOException;
- public class LoginActivity extends AppCompatActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_login);
- final TextInputEditText idEditText = findViewById(R.id.id);
- final TextInputEditText passEditText = findViewById(R.id.pass);
- final Button login = findViewById(R.id.login);
- final Utility u = new Utility(LoginActivity.this);
- login.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- String id = idEditText.getText().toString();
- String pass = passEditText.getText().toString();
- final Login login = new Login(id, pass);
- // Indicate that something is happening
- indicateProgress(true);
- final DownloadManager downloadManager = DownloadManager.getDownloadManager(LoginActivity.this);
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- Table[] tables = downloadManager.downloadTables(login);
- // If no exception occurred, the credentials are correct
- LoginManager loginManager = new LoginManager(LoginActivity.this);
- // Only prompt user for their preferences when they log in with their first login
- boolean promptPreferences = loginManager.getLoginCount() <= 0;
- loginManager.addLogin(login);
- setResult(RESULT_OK);
- if (promptPreferences) {
- potentiallyPromptForFilter(downloadManager, u, login.getId(), tables);
- } else {
- // Skip questions
- finish();
- }
- } catch (LoginFailureException e) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- // Credentials invalid
- new AlertDialog.Builder(LoginActivity.this, R.style.LoginTheme_AlertDialog)
- .setTitle(R.string.credentials_popup_title)
- .setMessage(R.string.credentials_popup_message)
- .setPositiveButton(R.string.ok, null)
- .setNeutralButton(R.string.news_fix, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- new Thread(new NewsQuery(LoginActivity.this, downloadManager))
- .start();
- }
- })
- .setNegativeButton(R.string.credentials_popup_open_preferences, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- startActivity(new Intent(LoginActivity.this, MainSettingsActivity.class));
- }
- })
- .show();
- indicateProgress(false);
- }
- });
- e.printStackTrace();
- } catch (IOException e) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- // Error
- new AlertDialog.Builder(LoginActivity.this, R.style.LoginTheme_AlertDialog)
- .setTitle(R.string.network_generic_error)
- .setMessage(R.string.network_generic_error_credentials)
- .setPositiveButton(R.string.ok, null)
- .show();
- indicateProgress(false);
- }
- });
- e.printStackTrace();
- }
- }
- }).start();
- }
- });
- // Handel enter presses
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) passEditText.setOnKeyListener(new View.OnKeyListener() {
- @Override
- public boolean onKey(View v, int keyCode, KeyEvent event) {
- if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
- login.callOnClick();
- return true;
- }
- return false;
- }
- });
- }
- private void potentiallyPromptForFilter(final DownloadManager downloadManager, final Utility u, final String id, Table[] tables) {
- try {
- if (tables[0].isHtml()) {
- FileManager fileManager = new FileManager(LoginActivity.this);
- // Download the first table
- String html = fileManager.getHtmlTable(tables[0], downloadManager);
- // If a reader can be gotten, parsing is probably possible
- if (u.getReader(html, id) != null) {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- indicateProgress(false);
- MainSettingsActivity.setFilterPopup(LoginActivity.this, R.style.LoginTheme_AlertDialog,
- new Handler(new Handler.Callback() {
- @Override
- public boolean handleMessage(Message msg) {
- // Ensure parse and filter are on
- u.getSharedPreferences().edit()
- .putBoolean("parse", true)
- .putBoolean("filter", true)
- .apply();
- promptForNotificationsOnUiThreadAndFinish();
- return false;
- }
- }));
- }
- });
- } else {
- // Parsing seems to be impossible
- u.getSharedPreferences().edit()
- .putBoolean("parse", false)
- .apply();
- promptForNotificationsOnUiThreadAndFinish();
- }
- } else {
- promptForNotificationsOnUiThreadAndFinish();
- }
- } catch (NoContentException e) {
- promptForNotificationsOnUiThreadAndFinish();
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- private void promptForNotificationsOnUiThreadAndFinish() {
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- indicateProgress(false);
- new AlertDialog.Builder(LoginActivity.this, R.style.LoginTheme_AlertDialog)
- .setTitle(R.string.notifications_popup_title)
- .setMessage(R.string.notifications_popup_message)
- .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- new Utility(LoginActivity.this).getSharedPreferences()
- .edit()
- .putBoolean("poll", true)
- .apply();
- finish();
- }
- })
- .setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- new Utility(LoginActivity.this).getSharedPreferences()
- .edit()
- .putBoolean("poll", false)
- .apply();
- finish();
- }
- })
- .show();
- }
- });
- }
- private void indicateProgress(boolean yes) {
- if (yes) {
- findViewById(R.id.login).setEnabled(false);
- findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
- } else {
- findViewById(R.id.login).setEnabled(true);
- findViewById(R.id.progressBar).setVisibility(View.GONE);
- }
- }
- @Override
- public void finish() {
- new Utility(LoginActivity.this)
- .getSharedPreferences()
- .edit()
- .putBoolean("login", true)
- .apply();
- super.finish();
- }
- }
|