diff --git a/app/src/main/java/leszcz_team/wulkanowy/activity/main/Login.java b/app/src/main/java/leszcz_team/wulkanowy/activity/main/Login.java index 6e8209032..69eb85891 100644 --- a/app/src/main/java/leszcz_team/wulkanowy/activity/main/Login.java +++ b/app/src/main/java/leszcz_team/wulkanowy/activity/main/Login.java @@ -7,8 +7,8 @@ import android.widget.Toast; import org.jsoup.Connection; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; -import org.jsoup.nodes.Element; import org.jsoup.select.Elements; +import org.jsoup.helper.HttpConnection.Response; import java.io.IOException; import java.io.OutputStreamWriter; @@ -19,15 +19,15 @@ public class Login extends AsyncTask { String email; String password; String county; + + Map loginCookies; + Activity activity; String userMesage; - String wresults; - String wa; - Document doc4; - String htmlDefault = "https://cufs.vulcan.net.pl/Default/Account/LogOn"; - String htmlStage2 = "https://cufs.vulcan.net.pl/{locationID}/FS/LS?wa=wsignin1.0&wtrealm=https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx&wctx=https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx"; - String htmlStage3 = "https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx"; + String urlForStepOne = "https://cufs.vulcan.net.pl/Default/Account/LogOn"; + String urlForStepTwo = "https://cufs.vulcan.net.pl/{locationID}/FS/LS?wa=wsignin1.0&wtrealm=https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx&wctx=https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx"; + String urlForStepThree = "https://uonetplus.vulcan.net.pl/{locationID}/LoginEndpoint.aspx"; public Login(String emailT, String passwordT, String countyT, Activity mainAC){ @@ -42,39 +42,23 @@ public class Login extends AsyncTask { protected Void doInBackground(Void... params) { try { - Connection.Response initial = Jsoup - .connect(htmlDefault) - .data("LoginName", email) - .data("Password", password) - .method(Connection.Method.POST) - .execute(); + if (!stepOne()) { + return null; + } - Map loginCookies = initial.cookies(); + Document certificate = stepTwo(); - CheckPass checkPass = new CheckPass(initial); - userMesage = checkPass.start(); - - county = county.replace("ł", "l"); - htmlStage2 = htmlStage2.replace("{locationID}", county); - - Document doc = Jsoup.connect(htmlStage2) - .cookies(loginCookies) - .get(); - - Elements wresultsInput = doc.select("input[name=wresult]"); - wresults = wresultsInput.attr("value"); - - Elements waInput = doc.select("input[name=wa]"); - wa = waInput.attr("value"); - - htmlStage3 = htmlStage3.replace("{locationID}", county); - - doc4 = Jsoup.connect(htmlStage3) - .data("wa", wa) - .data("wresults", wresults) - .post(); + Connection.Response step3 = stepThree(certificate); + Document dashboardHtml = step3.parse(); + Elements mesageAlert = dashboardHtml.getElementsByClass("welcome"); + String helloText = dashboardHtml.getElementsByClass("welcome").text(); + if (helloText.equals("Dzień dobry!")) { + userMesage = "Zalogowano pomyślnie! " + helloText; + } else { + userMesage = "Coś poszło nie tak :/"; + } } catch (IOException e){ userMesage = e.toString(); @@ -83,6 +67,49 @@ public class Login extends AsyncTask { return null; } + private boolean stepOne() throws IOException { + Connection.Response initial = Jsoup + .connect(urlForStepOne) + .data("LoginName", email) + .data("Password", password) + .method(Connection.Method.POST) + .execute(); + + loginCookies = initial.cookies(); + + CheckPass checkPass = new CheckPass(initial); + userMesage = checkPass.start(); + + return userMesage.isEmpty(); + } + + private Document stepTwo() throws IOException { + county = county.replace("ł", "l"); + urlForStepTwo = urlForStepTwo.replace("{locationID}", county); + + return Jsoup.connect(urlForStepTwo) + .cookies(loginCookies) + .get(); + } + + private Connection.Response stepThree(Document certificate) throws IOException { + Elements wresultsInput = certificate.select("input[name=wresult]"); + String wresults = wresultsInput.attr("value"); + + Elements waInput = certificate.select("input[name=wa]"); + String wa = waInput.attr("value"); + + urlForStepThree = urlForStepThree.replace("{locationID}", county); + + return Jsoup.connect(urlForStepThree) + .data("wa", wa) + .data("wresult", wresults) + .cookies(loginCookies) + .followRedirects(true) + .method(Connection.Method.POST) + .execute(); + } + protected void onPostExecute(Void result) { super.onPostExecute(result); if (!userMesage.isEmpty()){ diff --git a/app/src/main/java/leszcz_team/wulkanowy/activity/main/MainActivity.java b/app/src/main/java/leszcz_team/wulkanowy/activity/main/MainActivity.java index d6f326125..0b992e78d 100644 --- a/app/src/main/java/leszcz_team/wulkanowy/activity/main/MainActivity.java +++ b/app/src/main/java/leszcz_team/wulkanowy/activity/main/MainActivity.java @@ -54,7 +54,7 @@ public class MainActivity extends Activity { String email = adressEmail.getText().toString(); String county = countyText.getText().toString(); - if (password.isEmpty()|| email.isEmpty() || county.isEmpty()){ + if (password.isEmpty() || email.isEmpty() || county.isEmpty()){ Toast.makeText(this, R.string.data_text, Toast.LENGTH_SHORT).show(); } else{ diff --git a/app/src/main/java/leszcz_team/wulkanowy/activity/started/LoadingTask.java b/app/src/main/java/leszcz_team/wulkanowy/activity/started/LoadingTask.java index fcaf429bb..4e785b0c7 100644 --- a/app/src/main/java/leszcz_team/wulkanowy/activity/started/LoadingTask.java +++ b/app/src/main/java/leszcz_team/wulkanowy/activity/started/LoadingTask.java @@ -18,7 +18,7 @@ public class LoadingTask extends AsyncTask { @Override protected Void doInBackground(Void... voids) { try { - Thread.sleep(5000); + Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); }