Skip to content

Commit

Permalink
add pro college
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethosa committed Mar 27, 2022
1 parent bf10c06 commit e86ed8d
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
22 changes: 22 additions & 0 deletions app/src/main/java/com/ethosa/ktc/college/AuthInterface.kt
Original file line number Diff line number Diff line change
@@ -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) {
}
}
47 changes: 47 additions & 0 deletions app/src/main/java/com/ethosa/ktc/college/ProCollege.kt
Original file line number Diff line number Diff line change
@@ -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('<html>'+document.getElementsByTagName('html')[0].innerHTML+'</html>');
window.Android.sendCookies(document.cookie);
""".trimIndent())
}
}
}

wb.loadUrl(LOGIN_PAGE)
}
}
Original file line number Diff line number Diff line change
@@ -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
}
}
95 changes: 95 additions & 0 deletions app/src/main/res/layout/fragment_pro_college.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.fragments.ProCollegeFragment">

<WebView
android:id="@+id/content"
android:layout_width="0dp"
android:layout_height="0dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/login"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/username"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/background_secondary"
android:hint="@string/username"
android:textColor="@color/text"
tools:ignore="TextContrastCheck" />
</com.google.android.material.textfield.TextInputLayout>

<com.google.android.material.textfield.TextInputLayout
android:id="@+id/password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/username"
app:passwordToggleEnabled="true"
app:passwordToggleTint="@color/primary">

<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="@color/background_secondary"
android:hint="@string/password"
android:inputType="textPassword"
android:textColor="@color/text"
tools:ignore="TextContrastCheck" />
</com.google.android.material.textfield.TextInputLayout>

<Button
android:id="@+id/auth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/auth"
android:textColor="@color/btn_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password"
tools:ignore="TextContrastCheck" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title_pro_college"
android:textColor="@color/text"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
12 changes: 12 additions & 0 deletions app/src/main/res/navigation/mobile_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
app:destination="@id/navigation_contact_info"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim" />
<action
android:id="@+id/action_navigation_settings_to_navigation_pro_college"
app:destination="@id/navigation_pro_college"
app:enterAnim="@anim/nav_default_enter_anim"
app:exitAnim="@anim/nav_default_exit_anim" />
</fragment>

<fragment
Expand All @@ -58,4 +63,11 @@
android:label="@string/title_contact_info"
tools:layout="@layout/fragment_contact_info" />

<fragment
android:id="@+id/navigation_pro_college"
android:tag="ProCollege"
android:name="com.ethosa.ktc.ui.fragments.ProCollegeFragment"
android:label="@string/title_pro_college"
tools:layout="@layout/fragment_pro_college" />

</navigation>
3 changes: 3 additions & 0 deletions app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="settings_titles">
<item>Личный кабинет</item>
<item>Контактная информация</item>
<item>О программе</item>
</string-array>

<array name="settings_icons">
<item>@drawable/ic_portrait</item>
<item>@drawable/ic_headset</item>
<item>@drawable/ic_info</item>
</array>

<array name="settings_actions">
<item>@id/action_navigation_settings_to_navigation_pro_college</item>
<item>@id/action_navigation_settings_to_navigation_contact_info</item>
<item>@id/action_navigation_settings_to_navigation_about_app</item>
</array>
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@
<string name="title_settings">Настройки</string>
<string name="title_about">О приложении</string>
<string name="title_contact_info">Контактная информация</string>
<string name="title_pro_college">ProCollege</string>
<string name="title_activity_album">Альбом</string>
<string name="title_activity_wall_post">Новость</string>

<string name="news_title">Название поста</string>
<string name="news_date">19 апр</string>
<string name="news_description">Краткое описание поста</string>

<string name="course_string">1 Курс</string>

<string name="username">Логин</string>
<string name="password">Пароль</string>
<string name="auth">Войти</string>

<string name="lesson_title">Проектирование и дизайн информационных систем</string>
<string name="lesson_number">1</string>
<string name="lesson_time">00:00</string>
Expand Down

0 comments on commit e86ed8d

Please sign in to comment.