Search in sources :

Example 1 with GiuaScraperThread

use of com.giua.app.GiuaScraperThread in project Giua-App by Giua-app.

the class LoginActivity method login.

private void login() {
    if (GlobalVariables.gsThread == null || GlobalVariables.gsThread.isInterrupted())
        GlobalVariables.gsThread = new GiuaScraperThread();
    GlobalVariables.gsThread.addTask(() -> {
        try {
            if (isAddingAccount && AppData.getAllAccountUsernames(this).contains(etUsername.getText().toString().toLowerCase())) {
                runOnUiThread(() -> {
                    txtLayoutUsername.setError("Username già salvato");
                    etPassword.setText("");
                    pgProgressBar.setVisibility(View.INVISIBLE);
                    btnLogin.setVisibility(View.VISIBLE);
                });
                return;
            }
            loggerManager.d("Eseguo login...");
            GlobalVariables.gS = new GiuaScraper(etUsername.getText().toString(), etPassword.getText().toString(), true, SettingsData.getSettingBoolean(this, SettingKey.DEMO_MODE), new LoggerManager("GiuaScraper", this));
            GlobalVariables.gS.login();
            loggerManager.d("Devo ricordare le credenziali? " + chRememberCredentials.isChecked());
            if (GlobalVariables.gS.isSessionValid(GlobalVariables.gS.getCookie())) {
                if (chRememberCredentials.isChecked()) {
                    AccountData.setCredentials(this, etUsername.getText().toString(), etPassword.getText().toString(), GlobalVariables.gS.getCookie(), GlobalVariables.gS.getUserTypeString(), GlobalVariables.gS.getProfilePage(false).getProfileInformation()[2]);
                    AccountData.setSiteUrl(this, etUsername.getText().toString(), GlobalVariables.gS.getSiteUrl());
                    AppData.addAccountUsername(this, etUsername.getText().toString().toLowerCase());
                    AppData.saveActiveUsername(this, etUsername.getText().toString().toLowerCase());
                }
                startDrawerActivity();
            } else {
                loggerManager.e("Errore sconosciuto, login eseguito ma checkLogin ritorna false");
                setErrorMessage("Qualcosa e' andato storto!");
                this.runOnUiThread(() -> {
                    txtLayoutPassword.setError("Riprova");
                    etPassword.setText("");
                    btnLogin.setVisibility(View.VISIBLE);
                    pgProgressBar.setVisibility(View.INVISIBLE);
                });
            }
        } catch (GiuaScraperExceptions.SessionCookieEmpty e) {
            loggerManager.e("Errore: il sito dice: " + e.siteSays);
            if (e.siteSays.equals("Tipo di utente non ammesso: usare l'autenticazione tramite GSuite.")) {
                loggerManager.d("Rilevato credenziali account google");
                startStudentLoginActivity();
            } else {
                loggerManager.e("Errore: credenziali errate");
                setErrorMessage("Credenziali di login errate!");
                this.runOnUiThread(() -> {
                    etPassword.setText("");
                    txtLayoutPassword.setError("Password o Username errato");
                    pgProgressBar.setVisibility(View.INVISIBLE);
                    btnLogin.setVisibility(View.VISIBLE);
                });
            }
        } catch (GiuaScraperExceptions.UnableToLogin e) {
            loggerManager.e("Errore sconosciuto - " + e.getMessage());
            setErrorMessage("E' stato riscontrato qualche problema sconosciuto");
            this.runOnUiThread(() -> {
                etPassword.setText("");
                pgProgressBar.setVisibility(View.INVISIBLE);
                btnLogin.setVisibility(View.VISIBLE);
            });
        } catch (GiuaScraperExceptions.MaintenanceIsActiveException e) {
            loggerManager.e("Errore: Manutenzione attiva");
            try {
                Maintenance maintenance = GlobalVariables.gS.getMaintenanceInfo();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm", Locale.ITALY);
                runOnUiThread(() -> setErrorMessage("Il sito è in manutenzione sino alle " + simpleDateFormat.format(maintenance.end)));
            } catch (Exception e2) {
                runOnUiThread(() -> setErrorMessage(getString(R.string.maintenance_is_active_error)));
            }
            this.runOnUiThread(() -> {
                etPassword.setText("");
                txtLayoutPassword.setError("In manutenzione, riprova più tardi!");
                pgProgressBar.setVisibility(View.INVISIBLE);
                btnLogin.setVisibility(View.VISIBLE);
            });
        } catch (GiuaScraperExceptions.YourConnectionProblems e) {
            loggerManager.e("Errore di connessione dell'utente");
            setErrorMessage(getString(R.string.your_connection_error));
            this.runOnUiThread(() -> {
                etPassword.setText("");
                txtLayoutPassword.setError("C'è un problema con la tua rete");
                pgProgressBar.setVisibility(View.INVISIBLE);
                btnLogin.setVisibility(View.VISIBLE);
            });
        } catch (GiuaScraperExceptions.SiteConnectionProblems e) {
            loggerManager.e("Errore di connessione da parte del server");
            setErrorMessage(getString(R.string.site_connection_error));
            this.runOnUiThread(() -> {
                etPassword.setText("");
                txtLayoutPassword.setError("Errore di connesione al registro");
                pgProgressBar.setVisibility(View.INVISIBLE);
                btnLogin.setVisibility(View.VISIBLE);
            });
        }
    });
}
Also used : GiuaScraperThread(com.giua.app.GiuaScraperThread) LoggerManager(com.giua.app.LoggerManager) Maintenance(com.giua.objects.Maintenance) GiuaScraperExceptions(com.giua.webscraper.GiuaScraperExceptions) SimpleDateFormat(java.text.SimpleDateFormat) GiuaScraper(com.giua.webscraper.GiuaScraper)

Example 2 with GiuaScraperThread

use of com.giua.app.GiuaScraperThread in project Giua-App by Giua-app.

the class LoginActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    if (GlobalVariables.gsThread == null || GlobalVariables.gsThread.isInterrupted())
        GlobalVariables.gsThread = new GiuaScraperThread();
    isAddingAccount = getIntent().getBooleanExtra("addAccount", false);
    loggerManager = new LoggerManager("LoginActivity", this);
    loggerManager.d("onCreate chiamato");
    txtLayoutUsername = findViewById(R.id.login_txtlayout_user);
    txtLayoutPassword = findViewById(R.id.login_txtlayout_password);
    txtCardTitle = findViewById(R.id.login_card_title);
    etUsername = txtLayoutUsername.getEditText();
    etPassword = txtLayoutPassword.getEditText();
    pgProgressBar = findViewById(R.id.progressBar);
    btnLogin = findViewById(R.id.login_button);
    chRememberCredentials = findViewById(R.id.checkbox_remember_credentials);
    btnLoginAsStudent = findViewById(R.id.btn_student_login);
    btnLoginAsStudent.setText(Html.fromHtml("<p>Sei uno studente?\n<u><i>Clicca qui!</i></u></p>", 0));
    Objects.requireNonNull(txtLayoutUsername.getEditText()).addTextChangedListener(onTextChange(txtLayoutUsername));
    Objects.requireNonNull(txtLayoutPassword.getEditText()).addTextChangedListener(onTextChange(txtLayoutPassword));
    findViewById(R.id.login_btn_settings).setOnClickListener(this::btnSettingOnClick);
    btnLogin.setOnClickListener(this::btnLoginOnClick);
    btnLoginAsStudent.setOnClickListener(this::btnLoginAsStudentOnClick);
    checkDisplayMetrics();
    checkForUpdateChangelog();
    GlobalVariables.gsThread.addTask(() -> {
        try {
            if (!GiuaScraper.isGoogleLoginAvailable())
                runOnUiThread(() -> btnLoginAsStudent.setVisibility(View.INVISIBLE));
        } catch (Exception ignored) {
        }
    });
    new Thread(() -> {
        try {
            String str = GiuaScraper.getSchoolName();
            runOnUiThread(() -> txtCardTitle.setText("Accesso a " + str));
        } catch (Exception e) {
            runOnUiThread(() -> txtCardTitle.setText("Accesso al registro"));
        }
    }).start();
    if (isAddingAccount) {
        btnLogin.setText("Aggiungi account");
        chRememberCredentials.setChecked(true);
        chRememberCredentials.setVisibility(View.INVISIBLE);
    }
}
Also used : GiuaScraperThread(com.giua.app.GiuaScraperThread) LoggerManager(com.giua.app.LoggerManager) GiuaScraperThread(com.giua.app.GiuaScraperThread)

Example 3 with GiuaScraperThread

use of com.giua.app.GiuaScraperThread in project Giua-App by Giua-app.

the class AutomaticLoginActivity method loginWithPreviousCredentials.

private void loginWithPreviousCredentials() {
    if (GlobalVariables.gsThread == null || GlobalVariables.gsThread.isInterrupted() || GlobalVariables.gsThread.isInterrupting())
        GlobalVariables.gsThread = new GiuaScraperThread();
    loggerManager.d("Login automatico con credenziali salvate in corso");
    GlobalVariables.gsThread.addTask(() -> {
        runOnUiThread(() -> pbLoadingScreen.setVisibility(View.VISIBLE));
        try {
            String username = AppData.getActiveUsername(this);
            String password = AccountData.getPassword(this, username);
            String cookie = AccountData.getCookie(this, username);
            String savedSiteUrl = AccountData.getSiteUrl(this, username);
            GlobalVariables.gS = new GiuaScraper(username, password, cookie, true, SettingsData.getSettingBoolean(this, SettingKey.DEMO_MODE), new LoggerManager("GiuaScraper", this));
            if (!savedSiteUrl.equals(""))
                GlobalVariables.gS.setPrivateSiteUrl(savedSiteUrl);
            GlobalVariables.gS.login();
            AccountData.setCredentials(this, username, password, GlobalVariables.gS.getCookie(), GlobalVariables.gS.getUserTypeString(), GlobalVariables.gS.getProfilePage(false).getProfileInformation()[2]);
            AccountData.setSiteUrl(this, username, GlobalVariables.gS.getSiteUrl());
            if (!AppData.getAllAccountUsernames(this).contains(username))
                AppData.addAccountUsername(this, username);
            startDrawerActivity();
        } catch (GiuaScraperExceptions.YourConnectionProblems | GiuaScraperExceptions.SiteConnectionProblems e) {
            loggerManager.e("Errore di connessione - " + e.getMessage());
            loggerManager.e(Arrays.toString(e.getStackTrace()));
            runOnUiThread(() -> btnLogout.setVisibility(View.VISIBLE));
            runOnUiThread(() -> btnOffline.setVisibility(View.VISIBLE));
            if (SettingsData.getSettingBoolean(this, SettingKey.EXP_MODE))
                runOnUiThread(() -> btnOffline.setVisibility(View.VISIBLE));
            if (e.getClass() == GiuaScraperExceptions.YourConnectionProblems.class)
                runOnUiThread(() -> setErrorMessage(getString(R.string.your_connection_error)));
            else if (e.getClass() == GiuaScraperExceptions.SiteConnectionProblems.class)
                runOnUiThread(() -> setErrorMessage(getString(R.string.site_connection_error)));
            else
                runOnUiThread(() -> setErrorMessage("E' stato riscontrato qualche problema sconosciuto riguardo la rete"));
            runOnUiThread(() -> pbLoadingScreen.setVisibility(View.INVISIBLE));
            threadSleepWithTextUpdates();
            if (waitToReLogin < 30)
                waitToReLogin += 5;
            loginWithPreviousCredentials();
        } catch (GiuaScraperExceptions.SessionCookieEmpty sce) {
            // Se il login non dovesse funzionare lancia l acitvity di login ed elimina le credenziali salvate
            if (AppData.getActiveUsername(this).equals("gsuite")) {
                // Questa condizione si verifica quando è presente un acccount studente con il cookie scaduto
                loggerManager.w("Cookie gS dell'account studente scaduto, avvio StudentLoginActivity");
                startStudentLoginActivity();
            } else {
                loggerManager.w("Cookie gS scaduto, avvio ActivityManager");
                AppData.removeAccountUsername(this, AppData.getActiveUsername(this));
                AppData.saveActiveUsername(this, "");
                setErrorMessage("Le credenziali di accesso non sono più valide");
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                startActivity(new Intent(AutomaticLoginActivity.this, ActivityManager.class));
            }
        } catch (GiuaScraperExceptions.MaintenanceIsActiveException e) {
            loggerManager.e("Errore: sito in manutenzione");
            try {
                Maintenance maintenance = GlobalVariables.gS.getMaintenanceInfo();
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm");
                runOnUiThread(() -> setErrorMessage("Il sito è in manutenzione sino alle " + simpleDateFormat.format(maintenance.end)));
            } catch (Exception e2) {
                loggerManager.e("Errore nel ricevere informazioni manutenzione: " + e.getMessage());
                runOnUiThread(() -> setErrorMessage(getString(R.string.maintenance_is_active_error)));
            }
            runOnUiThread(() -> btnLogout.setVisibility(View.VISIBLE));
            if (SettingsData.getSettingBoolean(this, SettingKey.EXP_MODE))
                runOnUiThread(() -> btnOffline.setVisibility(View.VISIBLE));
            runOnUiThread(() -> pbLoadingScreen.setVisibility(View.GONE));
            runOnUiThread(() -> tvAutoLogin.setText("Accesso fallito."));
        }
    });
    if (SettingsData.getSettingBoolean(this, SettingKey.EXP_MODE)) {
        new Thread(() -> {
            // mostra il pulsante per l'offline dopo 5 secondi
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            runOnUiThread(() -> {
                btnOffline.setVisibility(View.INVISIBLE);
                btnOffline.setAlpha(0f);
                btnOffline.animate().alpha(1f).setDuration(400);
                btnOffline.setVisibility(View.VISIBLE);
            });
        }).start();
    }
}
Also used : Intent(android.content.Intent) Maintenance(com.giua.objects.Maintenance) GiuaScraperThread(com.giua.app.GiuaScraperThread) GiuaScraperThread(com.giua.app.GiuaScraperThread) LoggerManager(com.giua.app.LoggerManager) GiuaScraperExceptions(com.giua.webscraper.GiuaScraperExceptions) SimpleDateFormat(java.text.SimpleDateFormat) GiuaScraper(com.giua.webscraper.GiuaScraper)

Example 4 with GiuaScraperThread

use of com.giua.app.GiuaScraperThread in project Giua-App by Giua-app.

the class DrawerActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (savedInstanceState != null)
        savedInstanceState.clear();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer);
    loggerManager = new LoggerManager("DrawerActivity", this);
    loggerManager.d("onCreate chiamato");
    offlineMode = getIntent().getBooleanExtra("offline", false);
    demoMode = SettingsData.getSettingBoolean(this, SettingKey.DEMO_MODE);
    goTo = getIntent().getStringExtra("goTo");
    notificationsDBController = new NotificationsDBController(this);
    toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    if (GlobalVariables.gsThread == null || GlobalVariables.gsThread.isInterrupted())
        GlobalVariables.gsThread = new GiuaScraperThread();
    if (GlobalVariables.gS == null) {
        loggerManager.w("gs è null ma non dovrebbe esserlo quindi avvio AutomaticLogin");
        startActivityManager();
        return;
    }
    if (!offlineMode) {
        GlobalVariables.gsThread.addTask(() -> {
            if (!GlobalVariables.gS.isSessionValid(GlobalVariables.gS.getCookie())) {
                runOnUiThread(this::startActivityManager);
            }
        });
    }
    myFragmentManager = new MyFragmentManager(this, toolbar, getSupportFragmentManager(), offlineMode, demoMode, unstableFeatures);
    myDrawerManager = new MyDrawerManager(this, this::onChangeAccountFromDrawer, this::settingsItemOnClick, this::logoutItemOnClick, realUsername, userType, toolbar, myFragmentManager, demoMode);
    if (goTo == null || goTo.equals(""))
        myFragmentManager.changeFragment(R.id.nav_home);
    else if (goTo.equals(AppNotificationsParams.NEWSLETTERS_NOTIFICATION_GOTO))
        myFragmentManager.changeFragment(R.id.nav_newsletters);
    else if (goTo.equals(AppNotificationsParams.ALERTS_NOTIFICATION_GOTO))
        myFragmentManager.changeFragment(R.id.nav_alerts);
    else if (goTo.equals(AppNotificationsParams.VOTES_NOTIFICATION_GOTO))
        myFragmentManager.changeFragment(R.id.nav_votes);
    else if (goTo.equals(AppNotificationsParams.AGENDA_NOTIFICATION_GOTO))
        myFragmentManager.changeFragment(R.id.nav_agenda);
    else
        myFragmentManager.changeFragment(R.id.nav_home);
    setupAppNotifications();
    getAndSetupUsernameUsertype();
    new Thread(() -> {
        // Anche se siamo in offline mode, proviamo a scaricarle comunque (magari il registro è in manutenzione)
        loggerManager.d("Scarico le informazioni sulle funzionalità instabili");
        try {
            unstableFeatures = GiuaScraper.getExtPage("https://giua-app.github.io/unstable_features2.txt").text();
            myFragmentManager.unstableFeatures = unstableFeatures;
        } catch (Exception ignored) {
        }
    }).start();
    checkForUpdateChangelog();
}
Also used : NotificationsDBController(com.giua.app.NotificationsDBController) GiuaScraperThread(com.giua.app.GiuaScraperThread) MyFragmentManager(com.giua.app.MyFragmentManager) LoggerManager(com.giua.app.LoggerManager) MyDrawerManager(com.giua.app.MyDrawerManager) GiuaScraperThread(com.giua.app.GiuaScraperThread)

Aggregations

GiuaScraperThread (com.giua.app.GiuaScraperThread)4 LoggerManager (com.giua.app.LoggerManager)4 Maintenance (com.giua.objects.Maintenance)2 GiuaScraper (com.giua.webscraper.GiuaScraper)2 GiuaScraperExceptions (com.giua.webscraper.GiuaScraperExceptions)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Intent (android.content.Intent)1 MyDrawerManager (com.giua.app.MyDrawerManager)1 MyFragmentManager (com.giua.app.MyFragmentManager)1 NotificationsDBController (com.giua.app.NotificationsDBController)1