Skip to content

Commit

Permalink
add https badges
Browse files Browse the repository at this point in the history
  • Loading branch information
tsynik committed Jun 14, 2024
1 parent ba39a0f commit ff33ef5
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.content.res.ColorStateList
import android.content.res.Configuration
import android.net.Uri
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.util.Log
import android.view.KeyEvent
import android.view.View
Expand Down Expand Up @@ -44,9 +46,11 @@ import ru.yourok.torrserve.ui.fragments.main.update.apk.ApkUpdateFragment
import ru.yourok.torrserve.ui.fragments.main.update.apk.UpdaterApk
import ru.yourok.torrserve.ui.fragments.main.update.server.ServerUpdateFragment
import ru.yourok.torrserve.ui.fragments.main.update.server.UpdaterServer
import ru.yourok.torrserve.utils.CImageSpan
import ru.yourok.torrserve.utils.Format.dp2px
import ru.yourok.torrserve.utils.Net
import ru.yourok.torrserve.utils.Permission
import ru.yourok.torrserve.utils.SpanFormat
import ru.yourok.torrserve.utils.ThemeUtil
import kotlin.system.exitProcess

Expand Down Expand Up @@ -161,7 +165,14 @@ class MainActivity : AppCompatActivity() {
val hostView = findViewById<TextView>(R.id.tvCurrentHost)
val hostColor = ThemeUtil.getColorFromAttr(this, R.attr.colorHost)
host.observe(this) {
hostView?.text = it.removePrefix("http://")
hostView?.text = if (it.startsWith("https", true)) {
val sIcon = SpannableString(" ")
AppCompatResources.getDrawable(this, R.drawable.ssl)?.let { icon ->
icon.setBounds(0, 0, icon.intrinsicWidth, icon.intrinsicHeight)
sIcon.setSpan(CImageSpan(icon), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
SpanFormat.format("%s ${it.removePrefix("https://")}", sIcon)
} else it.removePrefix("http://")
}
val data = viewModel.get()
data.observe(this) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
package ru.yourok.torrserve.ui.fragments.main.servfinder

import android.content.res.ColorStateList
import android.text.SpannableString
import android.text.Spanned
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.content.res.AppCompatResources
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.shape.CornerFamily
import com.google.android.material.shape.MaterialShapeDrawable
import com.google.android.material.shape.ShapeAppearanceModel
import ru.yourok.torrserve.R
import ru.yourok.torrserve.settings.Settings
import ru.yourok.torrserve.utils.CImageSpan
import ru.yourok.torrserve.utils.Format
import ru.yourok.torrserve.utils.SpanFormat
import ru.yourok.torrserve.utils.ThemeUtil

class HostAdapter : RecyclerView.Adapter<HostAdapter.ViewHolder>() {
Expand Down Expand Up @@ -84,21 +88,38 @@ class HostAdapter : RecyclerView.Adapter<HostAdapter.ViewHolder>() {
val hostView = holder.view.findViewById<TextView>(R.id.tvHost)

hostView.apply {
text = hosts[position].host.removePrefix("http://")
text = if (hosts[position].host.startsWith("https", true)) { // show https badge
val sIcon = SpannableString(" ")
AppCompatResources.getDrawable(holder.view.context, R.drawable.ssl)?.let { icon ->
icon.setBounds(0, 0, icon.intrinsicWidth, icon.intrinsicHeight)
sIcon.setSpan(CImageSpan(icon), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
SpanFormat.format("${hosts[position].host.removePrefix("https://")} %s", sIcon)
} else hosts[position].host.removePrefix("http://")
val shapeDrawable = MaterialShapeDrawable(shapeAppearanceModel)
shapeDrawable.fillColor = hostColor.withAlpha(10)
shapeDrawable.setStroke(2.0f, hostColor.withAlpha(240))
background = shapeDrawable
setTextColor(hostColor)
} // TODO: http|https badge
}

val version = hosts[position].version
// set online badge by added version
//if (version.contains("·", true) || version.startsWith("1.2.") || version.startsWith("MatriX"))
if (version.isNotBlank() && (version.startsWith("1.2.") || version.startsWith("MatriX")))
holder.view.findViewById<ImageView>(R.id.ivOnline)?.visibility = View.VISIBLE
else {
holder.view.findViewById<ImageView>(R.id.ivOnline)?.visibility = View.INVISIBLE
val onlineView = holder.view.findViewById<TextView>(R.id.tvOnline)
val onlineColor = AppCompatResources.getColorStateList(holder.view.context, R.color.green)
// set online and dim by added version
if (version.isNotBlank() && (version.startsWith("1.2.") || version.startsWith("MatriX"))) {
onlineView?.apply {
visibility = View.VISIBLE
hostView.alpha = 1.0f
val shapeDrawable = MaterialShapeDrawable(shapeAppearanceModel)
shapeDrawable.fillColor = onlineColor.withAlpha(10)
shapeDrawable.setStroke(2.0f, onlineColor)
background = shapeDrawable
setTextColor(onlineColor)
text = holder.view.context.getString(R.string.online).lowercase()
}
} else {
onlineView?.visibility = View.INVISIBLE
hostView.alpha = 0.6f
}
val status = hosts[position].status
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ class ServerFinderFragment : TSFragment() {
setHost()
}

//vi.findViewById<TextView>(R.id.tvConnectedHost)?.text = Settings.getHost().removePrefix("http://")
vi.findViewById<TextInputEditText>(R.id.etHost)?.setText(Settings.getHost().removePrefix("http://"))
return vi
}
Expand Down Expand Up @@ -166,11 +165,11 @@ class ServerFinderFragment : TSFragment() {
if (TorrService.isLocal())
status += " · ${App.context.getString(R.string.connected_host)}"
val localVersion: String = withContext(Dispatchers.IO) {
Api.remoteEcho(localhost).also {
if (it.isNotEmpty()) {
status += " · ${App.context.getString(R.string.online)}"
}
}
Api.remoteEcho(localhost) // .also {
// if (it.isNotEmpty()) {
// status += " · ${App.context.getString(R.string.online)}"
// }
// }
}
hostAdapter.add(ServerIp(localhost, localVersion, status))
// add saved
Expand All @@ -179,11 +178,11 @@ class ServerFinderFragment : TSFragment() {
if (host == Settings.getHost())
status = App.context.getString(R.string.connected_host)
val remoteVersion: String = withContext(Dispatchers.IO) {
Api.remoteEcho(host).also {
if (it.isNotEmpty()) {
status += " · ${App.context.getString(R.string.online)}"
}
}
Api.remoteEcho(host) // .also {
// if (it.isNotEmpty()) {
// status += " · ${App.context.getString(R.string.online)}"
// }
// }
}
hostAdapter.add(ServerIp(host, remoteVersion, status))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class ServerFinderViewModel : ViewModel() {
App.context.getString(R.string.new_server)
else ""
if (version.isNotEmpty() && (version.startsWith("1.2.") || version.startsWith("MatriX"))) {
status += " · ${App.context.getString(R.string.online)}"
// status += " · ${App.context.getString(R.string.online)}"
withContext(Dispatchers.Main) {
servers?.value = ServerIp(checkHost, version, status)
}
Expand Down
10 changes: 0 additions & 10 deletions app/src/main/res/drawable/ic_circle_online.xml

This file was deleted.

37 changes: 37 additions & 0 deletions app/src/main/res/drawable/ssl.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="18dp"
android:height="18dp"
android:tint="?attr/colorHost"
android:viewportWidth="32"
android:viewportHeight="32">
<group android:translateY="2.5">
<path
android:fillColor="#00000000"
android:pathData="M22,12l0,8l4,0"
android:strokeWidth="2"
android:strokeColor="#000000"
android:strokeLineCap="round"
android:strokeLineJoin="round" />
<path
android:fillColor="#00000000"
android:pathData="M29,24H3c-1.1,0 -2,-0.9 -2,-2V10c0,-1.1 0.9,-2 2,-2h26c1.1,0 2,0.9 2,2v12C31,23.1 30.1,24 29,24z"
android:strokeWidth="2"
android:strokeColor="#000000"
android:strokeLineCap="round"
android:strokeLineJoin="round" />
<path
android:fillColor="#00000000"
android:pathData="M10,14L10,14c0,-1.1 -0.9,-2 -2,-2h0c-1.1,0 -2,0.9 -2,2v0c0,1.1 0.9,2 2,2h0c1.1,0 2,0.9 2,2v0c0,1.1 -0.9,2 -2,2h0c-1.1,0 -2,-0.9 -2,-2v0"
android:strokeWidth="2"
android:strokeColor="#000000"
android:strokeLineCap="round"
android:strokeLineJoin="round" />
<path
android:fillColor="#00000000"
android:pathData="M18,14L18,14c0,-1.1 -0.9,-2 -2,-2h0c-1.1,0 -2,0.9 -2,2v0c0,1.1 0.9,2 2,2h0c1.1,0 2,0.9 2,2v0c0,1.1 -0.9,2 -2,2h0c-1.1,0 -2,-0.9 -2,-2v0"
android:strokeWidth="2"
android:strokeColor="#000000"
android:strokeLineCap="round"
android:strokeLineJoin="round" />
</group>
</vector>
18 changes: 18 additions & 0 deletions app/src/main/res/drawable/twotone_lock_16.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:tint="?attr/colorHost"
android:viewportWidth="24"
android:viewportHeight="24">

<path
android:fillAlpha="0.3"
android:fillColor="@android:color/white"
android:pathData="M6,20h12L18,10L6,10v10zM12,13c1.1,0 2,0.9 2,2s-0.9,2 -2,2 -2,-0.9 -2,-2 0.9,-2 2,-2z"
android:strokeAlpha="0.3" />

<path
android:fillColor="@android:color/white"
android:pathData="M18,8h-1L17,6c0,-2.76 -2.24,-5 -5,-5S7,3.24 7,6v2L6,8c-1.1,0 -2,0.9 -2,2v10c0,1.1 0.9,2 2,2h12c1.1,0 2,-0.9 2,-2L20,10c0,-1.1 -0.9,-2 -2,-2zM9,6c0,-1.66 1.34,-3 3,-3s3,1.34 3,3v2L9,8L9,6zM18,20L6,20L6,10h12v10zM12,17c1.1,0 2,-0.9 2,-2s-0.9,-2 -2,-2 -2,0.9 -2,2 0.9,2 2,2z" />

</vector>
16 changes: 11 additions & 5 deletions app/src/main/res/layout/host_item.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@

</LinearLayout>

<ImageView
android:id="@+id/ivOnline"
<TextView
android:id="@+id/tvOnline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
Expand All @@ -80,9 +80,15 @@
android:layout_gravity="center|end"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:background="@drawable/ic_circle_online"
android:contentDescription="@string/online"
android:visibility="invisible" />
<!-- can't use vectors with ?attr on 4.4 -->
android:includeFontPadding="false"
android:paddingStart="4dp"
android:paddingLeft="4dp"
android:paddingEnd="4dp"
android:paddingRight="4dp"
android:singleLine="true"
android:visibility="invisible"
tools:text="@string/online"
tools:visibility="visible" />

</RelativeLayout>

0 comments on commit ff33ef5

Please sign in to comment.