replaced all deprecated methods with kotlin coroutines

This commit is contained in:
X1nto 2020-08-07 20:51:45 +04:00
parent d8f4246abd
commit f29af1607e
7 changed files with 46 additions and 30 deletions

View File

@ -33,7 +33,6 @@ class MicrogDownloadService: Service() {
private fun downloadMicrog() {
runBlocking {
launch {
getExternalFilesDir("apk")?.delete()
val url = getObjectFromJson(
"${PreferenceManager.getDefaultSharedPreferences(this@MicrogDownloadService)
.getString("install_url", baseUrl)}/microg.json", "url"

View File

@ -20,6 +20,7 @@ import com.vanced.manager.utils.InternetTools.getFileNameFromUrl
import com.vanced.manager.utils.InternetTools.getObjectFromJson
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.File
class VancedDownloadService: Service() {
@ -41,7 +42,7 @@ class VancedDownloadService: Service() {
val context = this
runBlocking {
launch {
getExternalFilesDir("apks")?.delete()
File(getExternalFilesDir("apk")?.path as String).deleteRecursively()
val defPrefs = PreferenceManager.getDefaultSharedPreferences(context)
val installUrl = defPrefs.getString("install_url", baseUrl)
val vancedVer = getObjectFromJson("$installUrl/vanced.json", "version")

View File

@ -3,11 +3,13 @@ package com.vanced.manager.core.installer
import android.app.Service
import android.content.Intent
import android.content.pm.PackageInstaller
import android.os.Handler
import android.os.IBinder
import android.util.Log
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.vanced.manager.ui.fragments.HomeFragment
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
class AppUninstallerService: Service() {
@ -26,16 +28,22 @@ class AppUninstallerService: Service() {
}
}
PackageInstaller.STATUS_SUCCESS -> {
Handler().postDelayed({
localBroadcastManager.sendBroadcast(Intent(HomeFragment.REFRESH_HOME))
Log.d("VMpm", "Successfully uninstalled $pkgName")
}, 500)
runBlocking {
launch {
delay(500)
localBroadcastManager.sendBroadcast(Intent(HomeFragment.REFRESH_HOME))
Log.d("VMpm", "Successfully uninstalled $pkgName")
}
}
}
PackageInstaller.STATUS_FAILURE -> {
Handler().postDelayed({
localBroadcastManager.sendBroadcast(Intent(HomeFragment.REFRESH_HOME))
Log.d("VMpm", "Failed to uninstall $pkgName")
}, 500)
runBlocking {
launch {
delay(500)
localBroadcastManager.sendBroadcast(Intent(HomeFragment.REFRESH_HOME))
Log.d("VMpm", "Failed to uninstall $pkgName")
}
}
}
}
stopSelf()

View File

@ -2,7 +2,6 @@ package com.vanced.manager.core.installer
import android.app.Service
import android.content.Intent
import android.os.AsyncTask
import android.os.Build
import android.os.IBinder
import android.util.Log
@ -13,6 +12,8 @@ import com.topjohnwu.superuser.Shell
import com.vanced.manager.ui.fragments.HomeFragment
import com.vanced.manager.utils.AppUtils.sendFailure
import com.vanced.manager.utils.FileInfo
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
@ -25,11 +26,13 @@ class RootSplitInstallerService: Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
Shell.getShell {
AsyncTask.execute {
val apkFilesPath = getExternalFilesDir("apks")?.path
val fileInfoList = apkFilesPath?.let { it1 -> getFileInfoList(it1) }
if (fileInfoList != null) {
installSplitApkFiles(fileInfoList)
runBlocking {
launch {
val apkFilesPath = getExternalFilesDir("apks")?.path
val fileInfoList = apkFilesPath?.let { it1 -> getFileInfoList(it1) }
if (fileInfoList != null) {
installSplitApkFiles(fileInfoList)
}
}
}
}

View File

@ -58,10 +58,7 @@ class HomeFragment : Fragment(), View.OnClickListener {
includeVancedLayout.vancedUninstallbtn.setOnClickListener(this@HomeFragment)
includeMicrogLayout.microgInstallbtn.setOnClickListener(this@HomeFragment)
includeMicrogLayout.microgUninstallbtn.setOnClickListener(this@HomeFragment)
}
binding.includeChangelogsLayout.changelogButton.setOnClickListener {
cardExpandCollapse()
includeChangelogsLayout.changelogButton.setOnClickListener(this@HomeFragment)
}
binding.includeVancedLayout.vancedCard.setOnLongClickListener {
@ -148,6 +145,7 @@ class HomeFragment : Fragment(), View.OnClickListener {
writeToVariantPref("nonroot", R.anim.slide_in_left, R.anim.slide_out_right)
Toast.makeText(requireActivity(), activity?.getString(R.string.root_not_granted), Toast.LENGTH_SHORT).show()
}
R.id.changelog_button -> cardExpandCollapse()
}
}

View File

@ -7,6 +7,7 @@ import android.widget.Toast
import androidx.preference.*
import com.google.firebase.messaging.FirebaseMessaging
import com.vanced.manager.R
import java.io.File
class SettingsFragment : PreferenceFragmentCompat() {
@ -93,9 +94,10 @@ class SettingsFragment : PreferenceFragmentCompat() {
findPreference<Preference>("clear_files")?.setOnPreferenceClickListener {
with(requireActivity()) {
getExternalFilesDir("apk")?.delete()
getExternalFilesDir("apks")?.delete()
Toast.makeText(this, getString(R.string.cleared_files), Toast.LENGTH_SHORT)
listOf("apk", "apks").forEach { dir ->
File(getExternalFilesDir(dir)?.path as String).deleteRecursively()
}
Toast.makeText(this, getString(R.string.cleared_files), Toast.LENGTH_SHORT).show()
}
true
}

View File

@ -5,12 +5,14 @@ import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInstaller
import android.os.Handler
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.vanced.manager.R
import com.vanced.manager.core.downloader.*
import com.vanced.manager.core.installer.*
import com.vanced.manager.ui.fragments.HomeFragment
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
object AppUtils {
@ -34,11 +36,14 @@ object AppUtils {
fun sendFailure(status: Int, context: Context) {
//Delay error broadcast until activity (and fragment) get back to the screen
Handler().postDelayed({
val intent = Intent(HomeFragment.INSTALL_FAILED)
intent.putExtra("errorMsg", getErrorMessage(status, context))
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
}, 500)
runBlocking {
launch {
delay(500)
val intent = Intent(HomeFragment.INSTALL_FAILED)
intent.putExtra("errorMsg", getErrorMessage(status, context))
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
}
}
}
private fun getErrorMessage(status: Int, context: Context): String {