Skip to content

Commit

Permalink
Build home page and edit some pages
Browse files Browse the repository at this point in the history
Signed-off-by: gohj99 <[email protected]>
  • Loading branch information
gohj99 committed Aug 6, 2024
1 parent 899ee46 commit a38fae7
Show file tree
Hide file tree
Showing 16 changed files with 11,361 additions and 9,912 deletions.
13 changes: 12 additions & 1 deletion .idea/deploymentTargetSelector.xml

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

2 changes: 2 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ android {
defaultConfig {
applicationId = "com.gohj99.telewatch"
minSdk = 24
//noinspection OldTargetApi
targetSdk = 34
versionCode = 1
versionName = "1.0.0"
Expand Down Expand Up @@ -60,6 +61,7 @@ dependencies {
implementation(libs.androidx.ui.tooling.preview)
implementation(libs.androidx.material3)
testImplementation(libs.junit)
//noinspection UseTomlInstead
implementation("com.google.zxing:core:3.4.1")
implementation(project(":libtd"))
androidTestImplementation(libs.androidx.junit)
Expand Down
61 changes: 52 additions & 9 deletions app/src/main/java/com/gohj99/telewatch/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.util.TypedValue
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
Expand All @@ -26,6 +29,8 @@ import java.util.Properties

class LoginActivity : ComponentActivity() {
private lateinit var client: Client
private lateinit var LanguageCode: String
private lateinit var appVersion: String
private var qrCodeLink by mutableStateOf<String?>(null)
private var showPasswordScreen by mutableStateOf(false)
private var passwordHint by mutableStateOf("")
Expand All @@ -40,6 +45,8 @@ class LoginActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
LanguageCode = this.resources.configuration.locales[0].language
appVersion = getAppVersion(this)
setContent {
TelewatchTheme {
if (showPasswordScreen) {
Expand Down Expand Up @@ -73,6 +80,15 @@ class LoginActivity : ComponentActivity() {
return properties
}

private fun getAppVersion(context: Context): String {
return try {
val pInfo = context.packageManager.getPackageInfo(context.packageName, 0)
pInfo.versionName
} catch (e: Exception) {
"1.0.0"
}
}

// 处理 TDLib 更新的函数
private fun handleUpdate(update: TdApi.Object) {
when (update.constructor) {
Expand All @@ -91,17 +107,33 @@ class LoginActivity : ComponentActivity() {
useSecretChats = true
apiId = tdapiId
apiHash = tdapiHash
systemLanguageCode = "en"
deviceModel = "Desktop"
systemVersion = "Unknown"
applicationVersion = "1.0"
systemLanguageCode = LanguageCode
deviceModel = Build.MODEL
systemVersion = Build.VERSION.RELEASE
applicationVersion = appVersion
enableStorageOptimizer = true
}
client.send(TdApi.SetTdlibParameters(parameters), { })
}
TdApi.AuthorizationStateWaitEncryptionKey.CONSTRUCTOR -> {
// 提供加密密钥
client.send(TdApi.CheckDatabaseEncryptionKey(), { })
// 检查本地是否有加密密钥
val sharedPref = getSharedPreferences("LoginPref", Context.MODE_PRIVATE)
val encryptionKeyString = sharedPref.getString("encryption_key", null)
val encryptionKey: TdApi.CheckDatabaseEncryptionKey = if (encryptionKeyString != null) {
val keyBytes = encryptionKeyString.chunked(2).map { it.toInt(16).toByte() }.toByteArray()
TdApi.CheckDatabaseEncryptionKey(keyBytes)
} else {
// 生成一个新的加密密钥并保存
val newKeyBytes = ByteArray(32).apply { (0..31).forEach { this[it] = (it * 7).toByte() } }
val newKeyString = newKeyBytes.joinToString("") { "%02x".format(it) }
with(sharedPref.edit()) {
putString("encryption_key", newKeyString)
apply()
}
TdApi.CheckDatabaseEncryptionKey(newKeyBytes)
}

client.send(encryptionKey, { })
}
TdApi.AuthorizationStateWaitPhoneNumber.CONSTRUCTOR -> {
// 请求二维码认证
Expand All @@ -118,6 +150,7 @@ class LoginActivity : ComponentActivity() {
TdApi.AuthorizationStateReady.CONSTRUCTOR -> {
// 登录成功
println("Login Successful")
doneStr.value = getString(R.string.Login_Successful)
// 发送广播通知 WelcomeActivity 销毁自己
/*LocalBroadcastManager.getInstance(this).sendBroadcast(
Intent("ACTION_DESTROY_WELCOME_ACTIVITY")
Expand All @@ -129,9 +162,9 @@ class LoginActivity : ComponentActivity() {
apply()
}
runOnUiThread {
// 启动新的页面
startActivity(Intent(this, MainActivity::class.java))
finish()
// 重启软件
resetSelf()
//finish()
}
}
TdApi.AuthorizationStateWaitPassword.CONSTRUCTOR -> {
Expand Down Expand Up @@ -174,6 +207,16 @@ class LoginActivity : ComponentActivity() {
// 处理其他结果...
}
}

// 重启软件
private fun resetSelf(){
Handler(Looper.getMainLooper()).postDelayed({
val intent = packageManager.getLaunchIntentForPackage(packageName)
intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent)
android.os.Process.killProcess(android.os.Process.myPid())
}, 1000)
}
}

// 创建一个函数来生成二维码的 Bitmap
Expand Down
91 changes: 57 additions & 34 deletions app/src/main/java/com/gohj99/telewatch/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.gohj99.telewatch

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.os.Bundle
Expand All @@ -8,55 +9,77 @@ import android.os.Looper
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.runtime.*
import com.gohj99.telewatch.telegram.TgApi
import com.gohj99.telewatch.ui.main.ErrorScreen
import com.gohj99.telewatch.ui.main.MainScreen
import com.gohj99.telewatch.ui.theme.TelewatchTheme
import org.drinkless.td.libcore.telegram.TdApi

class MainActivity : ComponentActivity() {
private var tgApi: TgApi? = null
private var isLoggedIn: Boolean = false
private var exceptionState by mutableStateOf<Exception?>(null)
@SuppressLint("MutableCollectionMutableState")
private var chatsList = mutableStateOf(mutableListOf<TdApi.Chat>())

override fun onDestroy() {
super.onDestroy()
tgApi?.close()
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

enableEdgeToEdge()

val sharedPref = getSharedPreferences("LoginPref", Context.MODE_PRIVATE)
val isLoggedIn = sharedPref.getBoolean("isLoggedIn", false)
isLoggedIn = sharedPref.getBoolean("isLoggedIn", false)

if (!isLoggedIn) {
// 如果未登录,打开WelcomeActivity
Handler(Looper.getMainLooper()).postDelayed({
// 跳转逻辑
startActivity(Intent(this, WelcomeActivity::class.java))
finish()
}, 0) //启动画面显示的时间
}, 0)
} else {
try {
initMain()
} catch (e: Exception) {
exceptionState = e
setContent {
TelewatchTheme {
ErrorScreen(
onRetry = { retryInitialization() },
onSetting = {
startActivity(Intent(this, SettingActivity::class.java))
}
)
}
}
}
}
super.onCreate(savedInstanceState)
enableEdgeToEdge()
}

private fun initMain() {
//println("start")
tgApi = TgApi(this)
tgApi?.getChats(
limit = 10,
chatsList = chatsList
)
setContent {
TelewatchTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Greeting(
name = "Android",
modifier = Modifier.padding(innerPadding)
)
}
MainScreen(chatsList)
}
}
}
}

@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
TelewatchTheme {
Greeting("Android")
private fun retryInitialization() {
exceptionState = null
try {
initMain()
} catch (e: Exception) {
exceptionState = e
}
}
}
}
81 changes: 54 additions & 27 deletions app/src/main/java/com/gohj99/telewatch/SettingActivity.kt
Original file line number Diff line number Diff line change
@@ -1,47 +1,74 @@
package com.gohj99.telewatch

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.widget.Toast
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.gohj99.telewatch.ui.SettingItem
import com.gohj99.telewatch.ui.SplashSettingScreen
import com.gohj99.telewatch.ui.theme.TelewatchTheme
import java.io.File

class SettingActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

enableEdgeToEdge()

val settingsList = listOf(
SettingItem.NestedSetting(getString(R.string.Clearing_cache), onClick = {
cacheDir.deleteRecursively()
Toast.makeText(this, getString(R.string.Successful), Toast.LENGTH_SHORT).show()
}),
SettingItem.NestedSetting(getString(R.string.Reset_libtd), onClick = { reset_libtd() }),
SettingItem.NestedSetting(getString(R.string.Reset_self), onClick = { reset_self() })
)

setContent {
TelewatchTheme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Greeting3(
name = "Android",
modifier = Modifier.padding(innerPadding)
)
}
SplashSettingScreen(
settings = settingsList
)
}
}
}
}

@Composable
fun Greeting3(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
private fun reset_libtd(){
val dir = File(applicationContext.filesDir.absolutePath)
dir.listFiles()?.find { it.name == "tdlib" && it.isDirectory }?.deleteRecursively()
// 清除登录数据
getSharedPreferences("LoginPref", Context.MODE_PRIVATE).edit().clear().apply()
// Toast提醒
Toast.makeText(this, getString(R.string.Successful), Toast.LENGTH_SHORT).show()
// 重启软件
Handler(Looper.getMainLooper()).postDelayed({
val intent = packageManager.getLaunchIntentForPackage(packageName)
intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent)
android.os.Process.killProcess(android.os.Process.myPid())
}, 1000)
}

@Preview(showBackground = true)
@Composable
fun GreetingPreview4() {
TelewatchTheme {
Greeting3("Android")
private fun reset_self(){
// 清除缓存
cacheDir.deleteRecursively()
// 清空软件文件
filesDir.deleteRecursively()
// 清空 SharedPreferences
getSharedPreferences("LoginPref", Context.MODE_PRIVATE).edit().clear().apply()
// Toast提醒
Toast.makeText(this, getString(R.string.Successful), Toast.LENGTH_SHORT).show()
// 重启软件
Handler(Looper.getMainLooper()).postDelayed({
val intent = packageManager.getLaunchIntentForPackage(packageName)
intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK)
startActivity(intent)
android.os.Process.killProcess(android.os.Process.myPid())
}, 1000)
}
}
}
Loading

0 comments on commit a38fae7

Please sign in to comment.