forked from eycorsican/kitsunebi-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c29ebf8
commit 1c57eda
Showing
23 changed files
with
628 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
app/src/main/java/fun/kitsunebi/kitsunebi4android/common/utils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package `fun`.kitsunebi.kitsunebi4android.common | ||
|
||
open class SingletonHolder<out T, in A>(creator: (A) -> T) { | ||
private var creator: ((A) -> T)? = creator | ||
@Volatile private var instance: T? = null | ||
|
||
fun getInstance(arg: A): T { | ||
val i = instance | ||
if (i != null) { | ||
return i | ||
} | ||
|
||
return synchronized(this) { | ||
val i2 = instance | ||
if (i2 != null) { | ||
i2 | ||
} else { | ||
val created = creator!!(arg) | ||
instance = created | ||
creator = null | ||
created | ||
} | ||
} | ||
} | ||
} | ||
|
||
public fun humanReadableByteCount(bytes: Long, si: Boolean): String { | ||
val unit = if (si) 1000 else 1024 | ||
if (bytes < unit) return bytes.toString() + " B" | ||
val exp = (Math.log(bytes.toDouble()) / Math.log(unit.toDouble())).toInt() | ||
val pre = (if (si) "kMGTPE" else "KMGTPE")[exp - 1] + if (si) "" else "i" | ||
return String.format("%.1f %sB", bytes / Math.pow(unit.toDouble(), exp.toDouble()), pre) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/src/main/java/fun/kitsunebi/kitsunebi4android/storage/ProxyLog.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package `fun`.kitsunebi.kitsunebi4android.storage | ||
|
||
import androidx.room.* | ||
import `fun`.kitsunebi.kitsunebi4android.common.SingletonHolder | ||
import androidx.paging.DataSource | ||
import android.content.Context | ||
|
||
public const val PROXY_LOG_DB_NAME = "proxy_log.sqlite3" | ||
|
||
@Entity(tableName = "proxy_log") | ||
data class ProxyLog( | ||
@PrimaryKey(autoGenerate = true) var id: Int, | ||
@ColumnInfo(name = "target") var target: String?, | ||
@ColumnInfo(name = "tag") var tag: String?, | ||
@ColumnInfo(name = "start_time") var startTime: Long?, | ||
@ColumnInfo(name = "end_time") var endTime: Long?, | ||
@ColumnInfo(name = "upload_bytes") var uploadBytes: Int?, | ||
@ColumnInfo(name = "download_bytes") var downloadBytes: Int?, | ||
@ColumnInfo(name = "record_type") var recordType: Int?, | ||
@ColumnInfo(name = "dns_query_type") var dnsQueryType: Int?, | ||
@ColumnInfo(name = "dns_request") var dnsRequest: String?, | ||
@ColumnInfo(name = "dns_response") var dnsResponse: String?, | ||
@ColumnInfo(name = "dns_num_ips") var dnsNumIPs: Int? | ||
) | ||
|
||
@Dao | ||
interface ProxyLogDao { | ||
@Query("SELECT * FROM proxy_log") | ||
fun getAll(): List<ProxyLog> | ||
|
||
@Query("SELECT * FROM proxy_log ORDER BY end_time DESC") | ||
fun getAllPaged(): DataSource.Factory<Int, ProxyLog> | ||
|
||
@Insert | ||
fun insertAll(proxyLogs: ProxyLog) | ||
|
||
@Delete | ||
fun delete(proxyLog: ProxyLog) | ||
|
||
@Query("DELETE FROM proxy_log") | ||
fun deleteAll() | ||
|
||
@Query("SELECT COUNT(1) FROM proxy_log") | ||
fun getAllCount(): Int | ||
} | ||
|
||
@Database(entities = arrayOf(ProxyLog::class), version = 4) | ||
abstract class ProxyLogDatabase : RoomDatabase() { | ||
abstract fun proxyLogDao(): ProxyLogDao | ||
companion object : SingletonHolder<ProxyLogDatabase, Context>({ | ||
Room.databaseBuilder(it.applicationContext, | ||
ProxyLogDatabase::class.java, PROXY_LOG_DB_NAME) | ||
.fallbackToDestructiveMigration() | ||
.enableMultiInstanceInvalidation() | ||
.build() | ||
}) | ||
} |
88 changes: 88 additions & 0 deletions
88
app/src/main/java/fun/kitsunebi/kitsunebi4android/ui/LogcatActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package `fun`.kitsunebi.kitsunebi4android.ui | ||
|
||
import `fun`.kitsunebi.kitsunebi4android.R | ||
import android.content.ClipData | ||
import android.content.ClipboardManager | ||
import android.content.Context | ||
import android.os.Bundle | ||
import android.text.method.ScrollingMovementMethod | ||
import android.view.Menu | ||
import android.view.MenuItem | ||
import androidx.appcompat.app.AppCompatActivity | ||
import android.widget.TextView | ||
import java.io.BufferedReader | ||
import java.io.IOException | ||
import java.io.InputStreamReader | ||
import java.util.* | ||
import kotlin.concurrent.schedule | ||
import kotlin.concurrent.scheduleAtFixedRate | ||
|
||
|
||
class LogcatActivity : AppCompatActivity() { | ||
|
||
private lateinit var logcatTextView: TextView | ||
private lateinit var bgThread: Thread | ||
private lateinit var logBuilder: StringBuilder | ||
private lateinit var bgTimer: Timer | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_logcat) | ||
logcatTextView = findViewById<TextView>(R.id.logcat_text) | ||
logcatTextView.movementMethod = ScrollingMovementMethod() | ||
|
||
bgThread = object : Thread() { | ||
override fun run() { | ||
try { | ||
logBuilder = StringBuilder() | ||
bgTimer = Timer() | ||
val process = Runtime.getRuntime().exec("logcat") | ||
val bufferedReader = BufferedReader(InputStreamReader(process.inputStream)) | ||
fun readAndDisplay() { | ||
while (bufferedReader.ready()) { | ||
val line = bufferedReader.readLine() | ||
logBuilder.append(line + "\n") | ||
|
||
} | ||
runOnUiThread { | ||
logcatTextView.text = logBuilder.toString() | ||
} | ||
} | ||
bgTimer.schedule(1000) { | ||
readAndDisplay() | ||
} | ||
bgTimer.scheduleAtFixedRate(0, 5000) { | ||
readAndDisplay() | ||
} | ||
} catch (e: IOException) { | ||
println(e) | ||
} | ||
} | ||
} | ||
bgThread.start() | ||
} | ||
|
||
override fun onDestroy() { | ||
super.onDestroy() | ||
bgTimer.cancel() | ||
bgThread.interrupt() | ||
} | ||
|
||
|
||
override fun onCreateOptionsMenu(menu: Menu): Boolean { | ||
menuInflater.inflate(R.menu.menu_logcat, menu) | ||
return true | ||
} | ||
|
||
override fun onOptionsItemSelected(item: MenuItem): Boolean { | ||
return when (item.itemId) { | ||
R.id.copy_btn -> { | ||
val clipboard = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager | ||
val clip: ClipData = ClipData.newPlainText("logcat text", logBuilder.toString()) | ||
clipboard.primaryClip = clip | ||
return true | ||
} | ||
else -> super.onOptionsItemSelected(item) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.