Search in sources :

Example 1 with Maintenance

use of com.giua.objects.Maintenance 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 Maintenance

use of com.giua.objects.Maintenance 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 3 with Maintenance

use of com.giua.objects.Maintenance in project Giua-Webscraper by Giua-app.

the class GiuaScraper method getMaintenanceInfo.

public Maintenance getMaintenanceInfo() {
    Maintenance maintenance;
    if (!isMaintenanceScheduled()) {
        maintenance = new Maintenance(new Date(), new Date(), false, false, false);
        return maintenance;
    }
    Document doc = getPage("login/form/");
    Elements maintenanceElm = doc.getElementsByClass("col-sm-12 bg-danger gs-mb-4 text-center");
    Elements dateEl = maintenanceElm.get(0).getElementsByTag("strong");
    // Togli dalla scritta tutto tranne le date
    String dateTxt = dateEl.get(0).text();
    String[] a = dateTxt.split("ore");
    String start = a[1].replace("del", "").replace("alle", "");
    String end = a[2].replace("del", "");
    // logln("getMaintenanceInfo: Non formattate: Inizio " + start + " | Fine " + end);
    // Crea dei format per fare il parsing di quelle stringhe
    SimpleDateFormat format1 = new SimpleDateFormat(" HH:mm  dd/MM/yyyy  ");
    SimpleDateFormat format2 = new SimpleDateFormat(" HH:mm  dd/MM/yyyy");
    Date startDate;
    Date endDate;
    try {
        startDate = format1.parse(start);
        endDate = format2.parse(end);
    } catch (Exception e) {
        throw new MaintenanceHasEmptyDates("Maintenance info did not find dates");
    }
    // logln("getMaintenanceInfo: formattate: Inizio " + startDate.toString() + " | Fine " + endDate.toString());
    Date currentDate = new Date();
    boolean isActive = false;
    boolean shouldBeActive = false;
    if (currentDate.after(startDate) && currentDate.before(endDate)) {
        lm.d("Secondo l'orario di inizio la manutenzione dovrebbe essere in corso");
        shouldBeActive = true;
    }
    if (isMaintenanceActive()) {
        isActive = true;
    }
    maintenance = new Maintenance(startDate, endDate, isActive, shouldBeActive, true);
    return maintenance;
}
Also used : Maintenance(com.giua.objects.Maintenance) Document(org.jsoup.nodes.Document) Elements(org.jsoup.select.Elements) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date) IOException(java.io.IOException)

Aggregations

Maintenance (com.giua.objects.Maintenance)3 SimpleDateFormat (java.text.SimpleDateFormat)3 GiuaScraperThread (com.giua.app.GiuaScraperThread)2 LoggerManager (com.giua.app.LoggerManager)2 GiuaScraper (com.giua.webscraper.GiuaScraper)2 GiuaScraperExceptions (com.giua.webscraper.GiuaScraperExceptions)2 Intent (android.content.Intent)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Document (org.jsoup.nodes.Document)1 Elements (org.jsoup.select.Elements)1