From d45e2af1c2b2ad04f6411593b19debefcbe40d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=96=E7=95=8C?= Date: Thu, 9 Nov 2023 13:05:56 +0800 Subject: [PATCH] Split play and other version --- app/build.gradle | 12 ++- .../io/nekohasekai/sfa/ui/MainActivity.kt | 63 ++------------- .../sfa/ui/main/SettingsFragment.kt | 8 ++ .../nekohasekai/sfa/vendor/VendorInterface.kt | 9 +++ app/src/main/res/layout/fragment_settings.xml | 27 +++++-- app/src/main/res/values/strings.xml | 5 +- .../java/io/nekohasekai/sfa/vendor/Vendor.kt | 15 ++++ .../java/io/nekohasekai/sfa/vendor/Vendor.kt | 80 +++++++++++++++++++ 8 files changed, 154 insertions(+), 65 deletions(-) create mode 100644 app/src/main/java/io/nekohasekai/sfa/vendor/VendorInterface.kt create mode 100644 app/src/other/java/io/nekohasekai/sfa/vendor/Vendor.kt create mode 100644 app/src/play/java/io/nekohasekai/sfa/vendor/Vendor.kt diff --git a/app/build.gradle b/app/build.gradle index 463b777..c24f377 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -48,6 +48,14 @@ android { } } + flavorDimensions "vendor" + productFlavors { + play { + } + other { + } + } + splits { abi { enable true @@ -68,6 +76,8 @@ android { applicationVariants.configureEach { variant -> variant.outputs.configureEach { outputFileName = (outputFileName as String).replace("-release", "") + outputFileName = (outputFileName as String).replace("-play", "") + outputFileName = (outputFileName as String).replace("-other", "") } } } @@ -99,7 +109,7 @@ dependencies { exclude group: 'com.google.guava', module: 'guava' } implementation 'com.google.guava:guava:32.1.2-android' - implementation 'com.google.android.play:app-update-ktx:2.1.0' + playImplementation 'com.google.android.play:app-update-ktx:2.1.0' } if (getProps("APPCENTER_TOKEN") != "") { diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/MainActivity.kt b/app/src/main/java/io/nekohasekai/sfa/ui/MainActivity.kt index 9870316..b07a2d0 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/MainActivity.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/MainActivity.kt @@ -6,7 +6,6 @@ import android.content.Context import android.content.Intent import android.net.VpnService import android.os.Bundle -import android.util.Log import androidx.activity.result.contract.ActivityResultContract import androidx.activity.result.contract.ActivityResultContracts import androidx.core.content.ContextCompat @@ -17,11 +16,6 @@ import androidx.navigation.ui.AppBarConfiguration import androidx.navigation.ui.setupActionBarWithNavController import androidx.navigation.ui.setupWithNavController import com.google.android.material.dialog.MaterialAlertDialogBuilder -import com.google.android.play.core.appupdate.AppUpdateManagerFactory -import com.google.android.play.core.appupdate.AppUpdateOptions -import com.google.android.play.core.install.model.AppUpdateType -import com.google.android.play.core.install.model.InstallStatus -import com.google.android.play.core.install.model.UpdateAvailability import io.nekohasekai.libbox.Libbox import io.nekohasekai.libbox.ProfileContent import io.nekohasekai.sfa.Application @@ -39,6 +33,7 @@ import io.nekohasekai.sfa.databinding.ActivityMainBinding import io.nekohasekai.sfa.ktx.errorDialogBuilder import io.nekohasekai.sfa.ui.profile.NewProfileActivity import io.nekohasekai.sfa.ui.shared.AbstractActivity +import io.nekohasekai.sfa.vendor.Vendor import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -177,59 +172,11 @@ class MainActivity : AbstractActivity(), ServiceConnection.Callback { } private fun startIntegration() { - lifecycleScope.launch(Dispatchers.IO) { - if (Settings.checkUpdateEnabled) { - checkUpdate() - } - } - } - - private fun checkUpdate() { - val appUpdateManager = AppUpdateManagerFactory.create(this) - val appUpdateInfoTask = appUpdateManager.appUpdateInfo - appUpdateInfoTask.addOnSuccessListener { appUpdateInfo -> - when (appUpdateInfo.updateAvailability()) { - UpdateAvailability.UPDATE_NOT_AVAILABLE -> { - Log.d(TAG, "checkUpdate: not available") + if (Vendor.checkUpdateAvailable()) { + lifecycleScope.launch(Dispatchers.IO) { + if (Settings.checkUpdateEnabled) { + Vendor.checkUpdate(this@MainActivity, false) } - - UpdateAvailability.DEVELOPER_TRIGGERED_UPDATE_IN_PROGRESS -> { - Log.d(TAG, "checkUpdate: in progress, status: ${appUpdateInfo.installStatus()}") - when (appUpdateInfo.installStatus()) { - InstallStatus.DOWNLOADED -> { - appUpdateManager.completeUpdate() - } - } - } - - UpdateAvailability.UPDATE_AVAILABLE -> { - Log.d(TAG, "checkUpdate: available") - if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.FLEXIBLE)) { - appUpdateManager.startUpdateFlow( - appUpdateInfo, - this, - AppUpdateOptions.newBuilder(AppUpdateType.FLEXIBLE).build() - ) - } else if (appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE)) { - appUpdateManager.startUpdateFlow( - appUpdateInfo, - this, - AppUpdateOptions.newBuilder(AppUpdateType.IMMEDIATE).build() - ) - } - } - - UpdateAvailability.UNKNOWN -> { - Log.d(TAG, "checkUpdate: unknown") - } - } - } - appUpdateInfoTask.addOnFailureListener { - Log.e(TAG, "checkUpdate: ", it) - } - appUpdateManager.registerListener { state -> - if (state.installStatus() == InstallStatus.DOWNLOADED) { - appUpdateManager.completeUpdate() } } } diff --git a/app/src/main/java/io/nekohasekai/sfa/ui/main/SettingsFragment.kt b/app/src/main/java/io/nekohasekai/sfa/ui/main/SettingsFragment.kt index acd5164..64fbc48 100644 --- a/app/src/main/java/io/nekohasekai/sfa/ui/main/SettingsFragment.kt +++ b/app/src/main/java/io/nekohasekai/sfa/ui/main/SettingsFragment.kt @@ -9,6 +9,7 @@ import android.view.View import android.view.ViewGroup import androidx.activity.result.contract.ActivityResultContracts import androidx.core.view.isGone +import androidx.core.view.isVisible import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import io.nekohasekai.libbox.Libbox @@ -24,6 +25,7 @@ import io.nekohasekai.sfa.ktx.text import io.nekohasekai.sfa.ui.MainActivity import io.nekohasekai.sfa.ui.debug.DebugActivity import io.nekohasekai.sfa.ui.profileoverride.ProfileOverrideActivity +import io.nekohasekai.sfa.vendor.Vendor import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext @@ -58,12 +60,18 @@ class SettingsFragment : Fragment() { reloadSettings() } } + if (!Vendor.checkUpdateAvailable()) { + binding.appSettingsCard.isVisible = false + } binding.checkUpdateEnabled.addTextChangedListener { lifecycleScope.launch(Dispatchers.IO) { val newValue = EnabledType.valueOf(it).boolValue Settings.checkUpdateEnabled = newValue } } + binding.checkUpdateButton.setOnClickListener { + Vendor.checkUpdate(activity, true) + } binding.disableMemoryLimit.addTextChangedListener { lifecycleScope.launch(Dispatchers.IO) { val newValue = EnabledType.valueOf(it).boolValue diff --git a/app/src/main/java/io/nekohasekai/sfa/vendor/VendorInterface.kt b/app/src/main/java/io/nekohasekai/sfa/vendor/VendorInterface.kt new file mode 100644 index 0000000..e8a6f07 --- /dev/null +++ b/app/src/main/java/io/nekohasekai/sfa/vendor/VendorInterface.kt @@ -0,0 +1,9 @@ +package io.nekohasekai.sfa.vendor + +import android.app.Activity + +interface VendorInterface { + fun checkUpdateAvailable(): Boolean + fun checkUpdate(activity: Activity, byUser: Boolean) + +} \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index 571922b..2df3d20 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -16,17 +16,20 @@ + android:layout_height="wrap_content" + android:layout_marginTop="16dp"> - + android:paddingStart="16dp" + android:paddingTop="16dp" + android:paddingEnd="16dp" + android:paddingBottom="8dp"> + android:hint="@string/check_update_atomic"> + + +