diff --git a/.idea/misc.xml b/.idea/misc.xml index 795485e..4d198f6 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -46,6 +46,7 @@ + @@ -71,6 +72,11 @@ + + + + + diff --git a/app/build.gradle b/app/build.gradle index 0764e48..bd8e261 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -10,8 +10,8 @@ android { applicationId "com.ethosa.ktc" minSdk 21 targetSdk 32 - versionCode 6 - versionName "0.4.2" + versionCode 7 + versionName "0.5.0" ndk { abiFilters 'armeabi-v7a','arm64-v8a','x86','x86_64' } diff --git a/app/src/main/java/com/ethosa/ktc/college/AuthInterface.kt b/app/src/main/java/com/ethosa/ktc/college/AuthInterface.kt new file mode 100644 index 0000000..f190e66 --- /dev/null +++ b/app/src/main/java/com/ethosa/ktc/college/AuthInterface.kt @@ -0,0 +1,22 @@ +package com.ethosa.ktc.college + +import android.webkit.JavascriptInterface +import android.webkit.WebView + +/** + * Provides HTML processing + */ +class AuthInterface { + var webView: WebView? = null + + /** + * Processes HTML text + */ + @JavascriptInterface + fun processHtml(html: String) { + } + + @JavascriptInterface + fun getCookies(cookies: String) { + } +} \ No newline at end of file diff --git a/app/src/main/java/com/ethosa/ktc/college/ProCollege.kt b/app/src/main/java/com/ethosa/ktc/college/ProCollege.kt new file mode 100644 index 0000000..2a8656d --- /dev/null +++ b/app/src/main/java/com/ethosa/ktc/college/ProCollege.kt @@ -0,0 +1,47 @@ +package com.ethosa.ktc.college + +import android.annotation.SuppressLint +import android.content.Context +import android.webkit.WebView +import android.webkit.WebViewClient + +/** + * Provides work with pro college. + */ +class ProCollege( + private val wb: WebView +) { + private val authInterface = AuthInterface() + + companion object { + private const val LOGIN_PAGE = "https://pro.kansk-tc.ru/login/index.php" + } + + /** + * Authorization in pro college using WebView. + */ + @SuppressLint("SetJavaScriptEnabled") + fun auth(username: String, password: String) { + authInterface.webView = wb + // Setup WebView .. + wb.settings.javaScriptEnabled = true + wb.settings.userAgentString = "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" + wb.addJavascriptInterface(authInterface, "Android") + wb.webViewClient = object : WebViewClient() { + override fun onPageFinished(view: WebView?, url: String?) { + view?.evaluateJavascript("") { + wb.loadUrl(""" + javascript: + document.getElementsByName("username")[0].value = "$username"; + document.getElementsByName("password")[0].value = "$password"; + document.getElementsByClassName("btn-login")[0].click(); + window.Android.processHtml(''+document.getElementsByTagName('html')[0].innerHTML+''); + window.Android.sendCookies(document.cookie); + """.trimIndent()) + } + } + } + + wb.loadUrl(LOGIN_PAGE) + } +} \ No newline at end of file diff --git a/app/src/main/java/com/ethosa/ktc/ui/fragments/ProCollegeFragment.kt b/app/src/main/java/com/ethosa/ktc/ui/fragments/ProCollegeFragment.kt new file mode 100644 index 0000000..846150f --- /dev/null +++ b/app/src/main/java/com/ethosa/ktc/ui/fragments/ProCollegeFragment.kt @@ -0,0 +1,55 @@ +package com.ethosa.ktc.ui.fragments + +import android.content.Context +import android.content.SharedPreferences +import android.os.Bundle +import androidx.fragment.app.Fragment +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.ethosa.ktc.college.ProCollege +import com.ethosa.ktc.databinding.FragmentProCollegeBinding + + +/** + * Fragment which provides working with ProCollege. + */ +class ProCollegeFragment : Fragment() { + private var _binding: FragmentProCollegeBinding? = null + private val binding get() = _binding!! + private lateinit var proCollege: ProCollege + private lateinit var preferences: SharedPreferences + + companion object { + private const val USERNAME = "username" + private const val PASSWORD = "password" + } + + override fun onCreateView( + inflater: LayoutInflater, + container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + _binding = FragmentProCollegeBinding.inflate(inflater, container, false) + + proCollege = ProCollege(binding.content) + preferences = requireActivity().getPreferences(Context.MODE_PRIVATE) + + binding.username.editText?.setText(preferences.getString(USERNAME, "")) + binding.password.editText?.setText(preferences.getString(PASSWORD, "")) + + binding.auth.setOnClickListener { + // Auth in ProCollege + binding.login.visibility = View.GONE + binding.content.visibility = View.VISIBLE + preferences.edit().putString(USERNAME, binding.username.editText?.text.toString()).apply() + preferences.edit().putString(PASSWORD, binding.password.editText?.text.toString()).apply() + proCollege.auth( + binding.username.editText?.text.toString(), + binding.password.editText?.text.toString() + ) + } + + return binding.root + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_pro_college.xml b/app/src/main/res/layout/fragment_pro_college.xml new file mode 100644 index 0000000..1c9a681 --- /dev/null +++ b/app/src/main/res/layout/fragment_pro_college.xml @@ -0,0 +1,95 @@ + + + + + + + + + + + + + + + + + +