From 39c8a743bbeff5c2534ffcf5d91767b605517bb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kuba=20Szczodrzy=C5=84ski?= Date: Tue, 5 Nov 2019 21:42:16 +0100 Subject: [PATCH] [Login/Librus] Add Librus captcha activity/dialog. --- app/src/main/AndroidManifest.xml | 52 ++++---- .../ui/modules/home/HomeFragment.java | 10 ++ .../login/LoginLibrusCaptchaActivity.kt | 116 ++++++++++++++++++ app/src/main/res/layout/fragment_home.xml | 32 ++++- app/src/main/res/values/strings.xml | 1 + 5 files changed, 180 insertions(+), 31 deletions(-) create mode 100644 app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginLibrusCaptchaActivity.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2d1ad42c..dd139fc5 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -15,10 +15,14 @@ android:theme="@style/SplashTheme" android:usesCleartextTraffic="true" tools:ignore="UnusedAttribute"> + @@ -29,7 +33,7 @@ @@ -39,7 +43,7 @@ android:label="@string/app_name" android:theme="@style/AppTheme" /> @@ -101,22 +105,18 @@ android:excludeFromRecents="true" android:noHistory="true" android:theme="@style/AppTheme.NoDisplay" /> - - - - @@ -169,7 +169,6 @@ android:name="android.appwidget.provider" android:resource="@xml/widget_notifications_info" /> - @@ -188,7 +187,6 @@ - @@ -196,14 +194,7 @@ - - - - - - + + + + + + + + + + + + + + @@ -222,14 +230,6 @@ - - - - - - @@ -244,4 +244,4 @@ - + \ No newline at end of file diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.java b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.java index d5aca94b..8f606cac 100644 --- a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.java +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/home/HomeFragment.java @@ -30,6 +30,7 @@ import androidx.fragment.app.Fragment; import androidx.work.WorkManager; import com.afollestad.materialdialogs.MaterialDialog; +import com.chuckerteam.chucker.api.Chucker; import com.mikepenz.iconics.IconicsColor; import com.mikepenz.iconics.IconicsDrawable; import com.mikepenz.iconics.IconicsSize; @@ -50,6 +51,7 @@ import pl.szczodrzynski.edziennik.databinding.CardLuckyNumberBinding; import pl.szczodrzynski.edziennik.databinding.CardUpdateBinding; import pl.szczodrzynski.edziennik.databinding.FragmentHomeBinding; import pl.szczodrzynski.edziennik.receivers.BootReceiver; +import pl.szczodrzynski.edziennik.ui.modules.login.LoginLibrusCaptchaActivity; import pl.szczodrzynski.edziennik.ui.modules.messages.MessagesComposeActivity; import pl.szczodrzynski.edziennik.utils.Colors; import pl.szczodrzynski.edziennik.utils.Themes; @@ -104,6 +106,14 @@ public class HomeFragment extends Fragment { b.pruneWorkButton.setOnClickListener((v -> WorkManager.getInstance(app).pruneWork())); + b.runChucker.setOnClickListener((v -> { + startActivity(Chucker.getLaunchIntent(activity, Chucker.SCREEN_HTTP)); + })); + + b.librusCaptchaButton.setOnClickListener((v -> { + startActivity(new Intent(activity, LoginLibrusCaptchaActivity.class)); + })); + //((TextView)v.findViewById(R.id.nextSync)).setText(getString(R.string.next_sync_format,Time.fromMillis(app.appJobs.syncJobTime).getStringHMS())); diff --git a/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginLibrusCaptchaActivity.kt b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginLibrusCaptchaActivity.kt new file mode 100644 index 00000000..3db5ca45 --- /dev/null +++ b/app/src/main/java/pl/szczodrzynski/edziennik/ui/modules/login/LoginLibrusCaptchaActivity.kt @@ -0,0 +1,116 @@ +package pl.szczodrzynski.edziennik.ui.modules.login + +import android.annotation.SuppressLint +import android.graphics.Color +import android.os.Build +import android.os.Bundle +import android.util.Base64 +import android.webkit.JavascriptInterface +import android.webkit.WebView +import androidx.appcompat.app.AlertDialog +import androidx.appcompat.app.AppCompatActivity +import com.afollestad.materialdialogs.MaterialDialog +import com.google.android.material.dialog.MaterialAlertDialogBuilder +import pl.szczodrzynski.edziennik.R +import pl.szczodrzynski.edziennik.api.v2.LIBRUS_USER_AGENT +import pl.szczodrzynski.edziennik.utils.Themes +import pl.szczodrzynski.edziennik.utils.Utils.hexFromColorInt +import java.nio.charset.Charset + +class LoginLibrusCaptchaActivity : AppCompatActivity() { + companion object { + private const val TAG = "LoginLibrusCaptchaActivity" + } + + private lateinit var webView: WebView + private lateinit var dialog: AlertDialog + private lateinit var jsInterface: CaptchaCallbackInterface + + @SuppressLint("AddJavascriptInterface", "SetJavaScriptEnabled") + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setTheme(Themes.appThemeNoDisplay) + setFinishOnTouchOutside(false) + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { + WebView.setWebContentsDebuggingEnabled(true) + } + + val base64Content = """ +PCFET0NUWVBFIGh0bWw+PGh0bWw+PGhlYWQ+PHNjcmlwdCBzcmM9Imh0dHBzOi8vd3d3Lmdvb2ds +ZS5jb20vcmVjYXB0Y2hhL2FwaS5qcz9vbmxvYWQ9cmVhZHkmcmVuZGVyPWV4cGxpY2l0Ij48L3Nj +cmlwdD48L2hlYWQ+PGJvZHk+PGJyPjxjZW50ZXIgaWQ9ImdyIj48L2NlbnRlcj48YnI+PHNjcmlw +dD5mdW5jdGlvbiByZWFkeSgpe2dyZWNhcHRjaGEucmVuZGVyKCdncicse3NpdGVrZXk6JzZMZjQ4 +bW9VQUFBQUFCOUNsaGR2SHI0NmdSV1ItQ04zMUNYUVBHMlUnLHRoZW1lOidUSEVNRScsY2FsbGJh +Y2s6ZnVuY3Rpb24oZSl7d2luZG93LmlmLmNhbGxiYWNrKGUpO30sImV4cGlyZWQtY2FsbGJhY2si +OmZ1bmN0aW9uKCl7d2luZG93LmlmLmV4cGlyZWRDYWxsYmFjayhlKTt9LCJlcnJvci1jYWxsYmFj +ayI6ZnVuY3Rpb24oKXt3aW5kb3cuaWYuZXJyb3JDYWxsYmFjayhlKTt9fSk7fTwvc2NyaXB0Pjwv +Ym9keT48L2h0bWw+""" + + val backgroundColor = if (Themes.isDark) 0x424242 else 0xffffff + val backgroundColorString = hexFromColorInt(backgroundColor) + + val htmlContent = Base64.decode(base64Content, Base64.DEFAULT) + .toString(Charset.defaultCharset()) + .replace("COLOR", backgroundColorString, true) + .replace("THEME", if (Themes.isDark) "dark" else "light") + + jsInterface = object : CaptchaCallbackInterface { + @JavascriptInterface + override fun callback(recaptchaResponse: String) { + MaterialDialog.Builder(this@LoginLibrusCaptchaActivity) + .title("Captcha checked") + .content("Response: $recaptchaResponse") + .positiveText("OK") + .show() + } + + @JavascriptInterface + override fun expiredCallback() { + MaterialDialog.Builder(this@LoginLibrusCaptchaActivity) + .title("Captcha expired") + .content("Captcha expired") + .positiveText("OK") + .show() + } + + @JavascriptInterface + override fun errorCallback() { + MaterialDialog.Builder(this@LoginLibrusCaptchaActivity) + .title("Captcha error") + .content("Captcha error") + .positiveText("OK") + .show() + } + } + + webView = WebView(this).apply { + //setBackgroundColor((backgroundColor.toLong() or 0xff000000).toInt()) + setBackgroundColor(Color.TRANSPARENT) + settings.javaScriptEnabled = true + settings.userAgentString = LIBRUS_USER_AGENT + addJavascriptInterface(jsInterface, "if") + loadDataWithBaseURL("https://portal.librus.pl/rodzina/login/", htmlContent, "text/html", "UTF-8", null) + setLayerType(WebView.LAYER_TYPE_SOFTWARE, null) + } + + dialog = MaterialAlertDialogBuilder(this) + .setTitle(R.string.login_librus_captcha_title) + .setView(webView) + .setNegativeButton(R.string.cancel) { dialog, _ -> + dialog.dismiss() + finish() + } + .setCancelable(false) + .show() + } + + interface CaptchaCallbackInterface { + @JavascriptInterface + fun callback(recaptchaResponse: String) + @JavascriptInterface + fun expiredCallback() + @JavascriptInterface + fun errorCallback() + } +} diff --git a/app/src/main/res/layout/fragment_home.xml b/app/src/main/res/layout/fragment_home.xml index cedad80b..10b278a4 100644 --- a/app/src/main/res/layout/fragment_home.xml +++ b/app/src/main/res/layout/fragment_home.xml @@ -36,24 +36,46 @@ android:layout_height="wrap_content" android:visibility="gone" android:orientation="vertical" - tools:visibility="visible"> + tools:visibility="visible" + tools:ignore="HardcodedText"> + + + + + + android:layout_height="wrap_content" + android:gravity="center"> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a08886af..dafa8099 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -984,4 +984,5 @@ Nie pytaj ponownie Nie udało się otworzyć ustawień Tworzenie powiadomień + Librus - logowanie