Skip to content

Commit

Permalink
Merge pull request #79 from GuoXiCheng/dev
Browse files Browse the repository at this point in the history
add updateAPK
  • Loading branch information
GuoXiCheng authored Nov 6, 2023
2 parents c7e9bb0 + ec17fd8 commit 8e9be02
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
branches:
- main
env:
LATEST_VERSION: "1.4.1"
LATEST_VERSION: "1.4.2"

jobs:
build-and-deploy:
Expand Down
1 change: 1 addition & 0 deletions .idea/misc.xml

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

Binary file removed apk/SKIP-v1.3.1.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.3.2.apk
Binary file not shown.
Binary file removed apk/SKIP-v1.3.4.apk
Binary file not shown.
Binary file renamed apk/SKIP-v1.3.3.apk → apk/SKIP-v1.4.2.apk
Binary file not shown.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
minSdk 24
targetSdk 32
versionCode 1
versionName "1.4.1"
versionName "1.4.2"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.android.skip">
<!-- <uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />-->

<uses-permission android:name="android.permission.INTERNET" />

<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>

<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -16,6 +18,17 @@
android:theme="@style/Theme.OneClick"
android:networkSecurityConfig="@xml/network_security_config"
tools:targetApi="31">

<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.android.skip.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

<activity
android:name="com.android.skip.MainActivity"
android:exported="true"
Expand Down
25 changes: 24 additions & 1 deletion app/src/main/java/com/android/skip/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.FileProvider
import com.android.skip.dataclass.PackageInfo
import com.android.skip.manager.*
import com.android.skip.ui.theme.OneClickTheme
Expand All @@ -38,6 +39,7 @@ import kotlinx.coroutines.launch
import okhttp3.OkHttpClient
import okhttp3.Request
import org.yaml.snakeyaml.Yaml
import java.io.File
import java.util.*
import kotlin.concurrent.thread

Expand Down Expand Up @@ -67,6 +69,9 @@ var isNeedUpdateAPK by mutableStateOf(false)
// 最新版本号
var latestVersionText by mutableStateOf("")

// 立即更新
var isUpdateAPKClicked by mutableStateOf(false)


class MainActivity : ComponentActivity() {

Expand Down Expand Up @@ -190,7 +195,7 @@ class MainActivity : ComponentActivity() {
negativeText = "暂不更新",
positiveText = "立即更新"
) {

isUpdateAPKClicked = true
}
isNeedUpdateAPK = false
}
Expand Down Expand Up @@ -222,11 +227,29 @@ class MainActivity : ComponentActivity() {
if (latestVersion != BuildConfig.VERSION_NAME.trim()) {
isNeedUpdateAPK = true
latestVersionText = latestVersion
} else {
ToastManager.showToast(this, "当前版本已是最新版")
}

isCheckUpdateBtnClicked = false
}
}
isUpdateAPKClicked -> {
thread {
HttpManager.downLoadNewAPK(latestVersionText, this)
val latestVersionAPK = "SKIP-v$latestVersionText.apk"
val apkFile = File(this.getExternalFilesDir(null), latestVersionAPK)
println(apkFile.name)
val apkUri = FileProvider.getUriForFile(this, this.applicationContext.packageName + ".provider", apkFile)

val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION
this.startActivity(intent)

isUpdateAPKClicked = false
}
}
}

}
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/android/skip/manager/HttpManager.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.android.skip.manager

import android.content.Context
import com.android.skip.BuildConfig
import okhttp3.OkHttpClient
import okhttp3.Request
import org.yaml.snakeyaml.Yaml
import java.io.File
import java.io.FileOutputStream

object HttpManager {
private const val BASE_URL = "https://guoxicheng.github.io/SKIP"
Expand Down Expand Up @@ -33,4 +36,19 @@ object HttpManager {
BuildConfig.VERSION_NAME.trim()
}
}

fun downLoadNewAPK(latestVersion: String, context: Context) {
try {
val latestVersionAPK = "SKIP-v$latestVersion.apk"
val request = Request.Builder().url("$BASE_URL/$latestVersionAPK").build()
client.newCall(request).execute().use { response ->
val fos = FileOutputStream(File(context.getExternalFilesDir(null), latestVersionAPK))
fos.use {
fos.write(response.body()?.bytes())
}
}
} catch (e: Exception) {
LogManager.i(e.toString())
}
}
}
3 changes: 3 additions & 0 deletions app/src/main/res/xml/file_paths.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="files" path="." />
</paths>

0 comments on commit 8e9be02

Please sign in to comment.