Skip to content

Commit

Permalink
update: 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
4o4E committed Jan 24, 2024
1 parent 0e5eade commit 0d56066
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 147 deletions.
199 changes: 96 additions & 103 deletions app/src/main/java/cc/microblock/TGStickerProvider/hook/HookEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -90,117 +90,114 @@ class HookEntry : IYukiHookXposedInit {
val data = cursor.getBlob(1)
val stream = SerializedData(data)

fun processStream(stream: SerializedData) {
try {
var constructorId = stream.readInt32(true)
var count = 1

// Compatibility with newer DB sheets(stickersets2, stickerset)
if (constructorId != 0x6e153f16) {
count = constructorId
constructorId = stream.readInt32(true)
}
if (constructorId != 0x6e153f16) {
YLog.error("constructorId != 0x6e153f16")
return
}
try {
var constructorId = stream.readInt32(true)
var count = 1

// Compatibility with newer DB sheets(stickersets2, stickerset)
if (constructorId != 0x6e153f16) {
count = constructorId
constructorId = stream.readInt32(true)
}
if (constructorId != 0x6e153f16) {
YLog.error("constructorId != 0x6e153f16")
continue
}

for (i in 0 until count) {
try {
if (i != 0) stream.readInt32(true)
val stickerSet =
TLRPC.TL_messages_stickerSet.TLdeserialize(
stream,
constructorId,
true
)
val hash = stickerSet.set.hash
if (dedupSet.contains(hash)) continue
dedupSet.add(hash)

// use .txt.jpg to bypass the file type check in android 11+
val stickerNameFile =
File(
stickerDataPath,
"${stickerSet.set.hash}.stickerData.txt.jpg"
)
if (!stickerNameFile.exists()) {
try {
File(stickerDataPath).mkdirs()
stickerNameFile.createNewFile()
stickerNameFile.writeText(stickerSet.set.short_name + "\n" + stickerSet.set.title + "\n" + stickerSet.set.count)
} catch (e: Exception) {
YLog.error(e.toString())
// has occupied by another app
continue
}
for (i in 0 until count) {
try {
if (i != 0) stream.readInt32(true)
val stickerSet =
TLRPC.TL_messages_stickerSet.TLdeserialize(
stream,
constructorId,
true
)
val hash = stickerSet.set.hash
if (dedupSet.contains(hash)) continue
dedupSet.add(hash)

// use .txt.jpg to bypass the file type check in android 11+
val stickerNameFile =
File(
stickerDataPath,
"${stickerSet.set.hash}.stickerData.txt.jpg"
)
if (!stickerNameFile.exists()) {
try {
File(stickerDataPath).mkdirs()
stickerNameFile.createNewFile()
stickerNameFile.writeText(stickerSet.set.short_name + "\n" + stickerSet.set.title + "\n" + stickerSet.set.count)
} catch (e: Exception) {
YLog.error(e.toString())
// has occupied by another app
continue
}
}

var fullSync = true
val destDir =
"${destDataPath}/tgSync_${stickerSet.set.short_name}"
File(destDir).mkdirs()

var lowQualityCount = 0
var highQualityCount = 0
var printLog =
false // only print log if there is a new sticker

for (sticker in stickerSet.documents) {
val localPath =
"${sticker.dc_id}_${sticker.id}.webp"
val localPathLowQuality =
"-${sticker.id}_1109.webp"
val stickerFile = File(tgCachePath, localPath)
val destFile =
File(destDir, "${sticker.id}_high.webp")
val destFileLowQuality =
File(destDir, "${sticker.id}_low.webp")

if (stickerFile.exists()) {
highQualityCount++
if (!destFile.exists()) {
printLog = true
stickerFile.copyTo(destFile)
if (destFileLowQuality.exists()) {
destFileLowQuality.delete()
}
var fullSync = true
val destDir =
"${destDataPath}/tgSync_${stickerSet.set.short_name}"
File(destDir).mkdirs()

var lowQualityCount = 0
var highQualityCount = 0
var printLog =
false // only print log if there is a new sticker

for (sticker in stickerSet.documents) {
val localPath =
"${sticker.dc_id}_${sticker.id}.webp"
val localPathLowQuality =
"-${sticker.id}_1109.webp"
val stickerFile = File(tgCachePath, localPath)
val destFile =
File(destDir, "${sticker.id}_high.webp")
val destFileLowQuality =
File(destDir, "${sticker.id}_low.webp")

if (stickerFile.exists()) {
highQualityCount++
if (!destFile.exists()) {
printLog = true
stickerFile.copyTo(destFile)
if (destFileLowQuality.exists()) {
destFileLowQuality.delete()
}
} else if (File(
}
} else if (File(
tgCachePath,
localPathLowQuality
).exists()
) {
lowQualityCount++
if (!destFileLowQuality.exists() && !destFile.exists()) {
printLog = true
File(
tgCachePath,
localPathLowQuality
).exists()
) {
lowQualityCount++
if (!destFileLowQuality.exists() && !destFile.exists()) {
printLog = true
File(
tgCachePath,
localPathLowQuality
).copyTo(destFileLowQuality)
}
} else {
if (!destFile.exists()) fullSync = false
).copyTo(destFileLowQuality)
}

} else {
if (!destFile.exists()) fullSync = false
}

if (printLog)
YLog.debug("*new* [${lowQualityCount + highQualityCount}(Low$lowQualityCount High${highQualityCount})/${stickerSet.set.count}] ${stickerSet.set.title} ${stickerSet.set.short_name} ${stickerSet.set.count} ${stickerSet.set.hash}")
if (fullSync) ignoreSet.add(stickerSet.set.hash)

} catch (e: Exception) {
YLog.warn("", e)
continue
}

if (printLog)
YLog.debug("*new* [${lowQualityCount + highQualityCount}(Low$lowQualityCount High${highQualityCount})/${stickerSet.set.count}] ${stickerSet.set.title} ${stickerSet.set.short_name} ${stickerSet.set.count} ${stickerSet.set.hash}")
if (fullSync) ignoreSet.add(stickerSet.set.hash)

} catch (e: Exception) {
YLog.warn("", e)
continue
}
} catch (e: Exception) {
YLog.warn("", e)
return
}
} catch (e: Exception) {
YLog.warn("", e)
continue
}

processStream(stream)
}
cursor.close()
cache4DBConn.close()
Expand All @@ -209,18 +206,14 @@ class HookEntry : IYukiHookXposedInit {
return true
}

fun checkDbSheets(dbPath: String): Boolean {
return checkDb(dbPath, "stickers_v2")
}

// Main DB
checkDbSheets("${dataPath}/files/cache4.db")
checkDb("${dataPath}/files/cache4.db", "stickers_v2")

// Separate DBs for each account
File("$dataPath/files/").listFiles()?.filter {
it.isDirectory && it.name.startsWith("account")
}?.forEach {
checkDbSheets("${it.path}/cache4.db")
checkDb("${it.path}/cache4.db", "stickers_v2")
}
} catch (e: Exception) {
YLog.debug(e.toString())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.lifecycle.MutableLiveData
Expand Down Expand Up @@ -45,16 +47,16 @@ data class StickerInfo(
val all: Int
)

class RecyclerAdapterStickerList(val act: MainActivity) :
class RecyclerAdapterStickerList(private val act: MainActivity) :
RecyclerView.Adapter<RecyclerAdapterStickerList.ViewHolder>() {
var stickerList = listOf<StickerInfo>()

class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val name = view.findViewById<android.widget.TextView>(R.id.sticker_name)
val id = view.findViewById<android.widget.TextView>(R.id.sticker_id)
val syncState = view.findViewById<android.widget.TextView>(R.id.syncState)
val syncBtn = view.findViewById<android.widget.Button>(R.id.syncBtn)
val rmBtn = view.findViewById<MaterialTextView>(R.id.rmBtn)
val name: TextView = view.findViewById(R.id.sticker_name)
val id: TextView = view.findViewById(R.id.sticker_id)
val syncState: TextView = view.findViewById(R.id.syncState)
val syncBtn: Button = view.findViewById(R.id.syncBtn)
val rmBtn: MaterialTextView = view.findViewById(R.id.rmBtn)
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
Expand Down
59 changes: 21 additions & 38 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,18 @@
tools:context=".ui.activity.MainActivity"
tools:ignore="UselessParent,UseCompoundDrawables,ContentDescription">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="0dp"
android:gravity="center|start"
<TextView
android:paddingLeft="15dp"
android:paddingTop="13dp"
android:paddingRight="15dp"
android:paddingBottom="5dp">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:singleLine="true"
android:text="@string/app_name"
android:textColor="@color/colorTextGray"
android:textSize="25sp"
android:textStyle="bold" />

</LinearLayout>
android:paddingBottom="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:text="@string/app_name"
android:textColor="@color/colorTextGray"
android:textSize="25sp"
android:textStyle="bold" />

<LinearLayout
android:id="@+id/main_lin_status"
Expand Down Expand Up @@ -141,29 +132,21 @@
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/usage" />

<LinearLayout
<Button
android:id="@+id/button2"
android:layout_marginLeft="50dp"
android:layout_marginTop="10dp"
android:layout_marginRight="50dp"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="81dp"
android:layout_weight="1"
android:orientation="horizontal"
android:paddingHorizontal="30dp"
android:paddingVertical="10dp">

<Button
android:id="@+id/button2"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#656565"
android:text="@string/ok"
android:textColor="#fff"
android:textSize="20sp"
tools:text="得行" />
</LinearLayout>
android:layout_height="wrap_content"
android:backgroundTint="#656565"
android:text="@string/ok"
android:textColor="#fff"
android:textSize="20sp"
tools:text="得行" />

</LinearLayout>

Expand Down

0 comments on commit 0d56066

Please sign in to comment.