Skip to content

Commit

Permalink
feat: show subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
stitchrs committed Dec 10, 2022
1 parent d2f5503 commit e06c901
Show file tree
Hide file tree
Showing 12 changed files with 282 additions and 40 deletions.
9 changes: 0 additions & 9 deletions .idea/codeStyles/Project.xml

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

Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,12 @@ fun Long.toBytesString(): String {
else ->
"$this Bytes"
}
}

fun Double.toProgress(): Int {
return this.toInt()
}
fun Long.toDateStr(): String {
val simpleDateFormat =SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
return simpleDateFormat.format(Date(this*1000))
}
12 changes: 12 additions & 0 deletions design/src/main/res/drawable/bg_b.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="?attr/colorSurface" />
<corners
android:radius="8dp"
/>
</shape>
</item>

</layer-list>
51 changes: 44 additions & 7 deletions design/src/main/res/layout/adapter_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@
<variable
name="menu"
type="android.view.View.OnClickListener" />

<import type="android.view.View" />
<import type="com.github.kr328.clash.design.util.I18nKt" />
<import type="com.github.kr328.clash.design.util.IntervalKt" />
</data>

<RelativeLayout
android:elevation="5dp"
android:id="@+id/root_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:layout_margin="5dp"
android:background="@drawable/bg_b"
android:clickable="true"
android:focusable="true"
android:minHeight="@dimen/item_min_height"
Expand Down Expand Up @@ -61,6 +63,43 @@
android:layout_marginTop="@dimen/item_text_margin"
android:text="@{profile.pending ? @string/format_type_unsaved(I18nKt.toString(profile.type, context)) : I18nKt.toString(profile.type, context)}"
android:textAppearance="@style/TextAppearance.MaterialComponents.Body2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>

<TextView
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{profile.download ==0 ? View.GONE : View.VISIBLE}"
android:text='@{profile.download >0 ?I18nKt.toBytesString(profile.download+profile.upload)+"/" :""}'
android:textAppearance="@style/TextAppearance.MaterialComponents.Body1" />

<TextView
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{profile.download ==0 ? View.GONE : View.VISIBLE}"
android:text='@{profile.download >0 ?I18nKt.toBytesString(profile.total) : ""}'
android:textAppearance="@style/TextAppearance.MaterialComponents.Body1" />
</LinearLayout>
<TextView
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{profile.expire==0 ? View.GONE : View.VISIBLE}"
android:text='@{profile.expire>0 ? I18nKt.toDateStr(profile.expire):""}'
android:textAppearance="@style/TextAppearance.MaterialComponents.Body1" />
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="1000"
android:visibility="@{profile.download==0 ? View.GONE : View.VISIBLE}"
android:progress="@{profile.download>0 ?I18nKt.toProgress ((profile.download+profile.upload)/(profile.total/1000)) :0}"
/>
</LinearLayout>

<TextView
Expand All @@ -74,12 +113,10 @@
android:textAppearance="@style/TextAppearance.MaterialComponents.Tooltip" />

<View
android:layout_width="@dimen/divider_size"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_toStartOf="@id/menu_view"
android:background="?attr/colorControlHighlight"
android:minHeight="@{@dimen/item_tailing_component_size * 1.5f}" />
android:background="@color/color_clash_dark" />

<FrameLayout
android:id="@+id/menu_view"
Expand Down
5 changes: 5 additions & 0 deletions service/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ dependencies {
implementation(libs.androidx.room.ktx)
implementation(libs.kaidl.runtime)
implementation(libs.rikkax.multiprocess)
implementation(platform("com.squareup.okhttp3:okhttp-bom:4.10.0"))

// define any required OkHttp artifacts without version
implementation("com.squareup.okhttp3:okhttp")
implementation("com.squareup.okhttp3:logging-interceptor")
}

afterEvaluate {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.github.kr328.clash.service

import android.content.Context
import com.github.kr328.clash.service.data.Database
import com.github.kr328.clash.service.data.Imported
import com.github.kr328.clash.service.data.ImportedDao
import com.github.kr328.clash.service.data.Pending
import com.github.kr328.clash.service.data.PendingDao
Expand All @@ -13,10 +14,13 @@ import com.github.kr328.clash.service.util.directoryLastModified
import com.github.kr328.clash.service.util.generateProfileUUID
import com.github.kr328.clash.service.util.importedDir
import com.github.kr328.clash.service.util.pendingDir
import com.github.kr328.clash.service.util.sendProfileChanged
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
import java.io.FileNotFoundException
import java.util.*

Expand All @@ -40,6 +44,10 @@ class ProfileManager(private val context: Context) : IProfileManager,
type = type,
source = source,
interval = 0,
upload = 0,
total = 0,
download = 0,
expire = 0,
)

PendingDao().insert(pending)
Expand Down Expand Up @@ -68,6 +76,10 @@ class ProfileManager(private val context: Context) : IProfileManager,
type = Profile.Type.File,
source = imported.source,
interval = imported.interval,
upload = imported.upload,
total = imported.total,
download = imported.download,
expire = imported.expire,
)

cloneImportedFiles(uuid, newUUID)
Expand All @@ -93,13 +105,21 @@ class ProfileManager(private val context: Context) : IProfileManager,
type = imported.type,
source = source,
interval = interval,
upload = 0,
total = 0,
download = 0,
expire = 0,
)
)
} else {
val newPending = pending.copy(
name = name,
source = source,
interval = interval
interval = interval,
upload = 0,
total = 0,
download = 0,
expire = 0,
)

PendingDao().update(newPending)
Expand All @@ -108,6 +128,81 @@ class ProfileManager(private val context: Context) : IProfileManager,

override suspend fun update(uuid: UUID) {
scheduleUpdate(uuid, true)
ImportedDao().queryByUUID(uuid)?.let {
if (it.type == Profile.Type.Url) {
updateFlow(it)
}
}
}

suspend fun updateFlow(old: Imported) {
val client = OkHttpClient()
try {
val request = Request.Builder()
.url(old.source)
.header("User-Agent", "ClashforWindows/0.19.23")
.build()

client.newCall(request).execute().use { response ->
if (!response.isSuccessful || response.headers["subscription-userinfo"] == null) return

var upload: Long = 0
var download: Long = 0
var total: Long = 0
var expire: Long = 0

val userinfo = response.headers["subscription-userinfo"]
if (response.isSuccessful && userinfo != null) {

val flags = userinfo.split(";")
for (flag in flags) {
val info = flag.split("=")
when {
info[0].contains("upload") -> upload =
info[1].toLong()

info[0].contains("download") -> download =
info[1].toLong()

info[0].contains("total") -> total =
info[1].toLong()

info[0].contains("expire") -> {
if (info[1].isNotEmpty()) {
expire = info[1].toLong()
}
}
}
}
}

val new = Imported(
old.uuid,
old.name,
old.type,
old.source,
old.interval,
upload,
download,
total,
expire,
old?.createdAt ?: System.currentTimeMillis()
)

if (old != null) {
ImportedDao().update(new)
} else {
ImportedDao().insert(new)
}

PendingDao().remove(new.uuid)
context.sendProfileChanged(new.uuid)
// println(response.body!!.string())
}

} catch (e: Exception) {
System.out.println(e)
}
}

override suspend fun commit(uuid: UUID, callback: IFetchObserver?) {
Expand Down Expand Up @@ -163,6 +258,10 @@ class ProfileManager(private val context: Context) : IProfileManager,
val type = pending?.type ?: imported?.type ?: return null
val source = pending?.source ?: imported?.source ?: return null
val interval = pending?.interval ?: imported?.interval ?: return null
val upload = pending?.upload ?: imported?.upload ?: return null
val download = pending?.download ?: imported?.download ?: return null
val total = pending?.total ?: imported?.total ?: return null
val expire = pending?.expire ?: imported?.expire ?: return null

return Profile(
uuid,
Expand All @@ -171,6 +270,10 @@ class ProfileManager(private val context: Context) : IProfileManager,
source,
active != null && imported?.uuid == active,
interval,
upload,
download,
total,
expire,
resolveUpdatedAt(uuid),
imported != null,
pending != null
Expand Down
Loading

0 comments on commit e06c901

Please sign in to comment.