Skip to content
This repository was archived by the owner on Mar 15, 2022. It is now read-only.

Commit 9076732

Browse files
authored
Merge pull request #363 from YTVanced/dev
2.2.1
2 parents 9575447 + 3e15ca0 commit 9076732

File tree

13 files changed

+121
-91
lines changed

13 files changed

+121
-91
lines changed

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ android {
1515
applicationId = "com.vanced.manager"
1616
minSdkVersion(21)
1717
targetSdkVersion(30)
18-
versionCode = 220
19-
versionName = "2.2.0 (RootedFirebase)"
18+
versionCode = 221
19+
versionName = "2.2.1 (RootedFirebase)"
2020

2121
vectorDrawables.useSupportLibrary = true
2222

app/src/main/java/com/vanced/manager/adapter/AppListAdapter.kt

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ class AppListAdapter(
7676

7777
override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
7878
holder.bind(position)
79-
79+
val dataModel = if (isRoot) rootDataModels[position] else dataModels[position]
8080
holder.appCard.setOnClickListener {
8181
tooltip.close()
8282
AppInfoDialog.newInstance(
8383
appName = apps[position],
84-
appIcon = dataModels[position]?.appIcon,
85-
changelog = dataModels[position]?.changelog?.value
84+
appIcon = dataModel?.appIcon,
85+
changelog = dataModel?.changelog?.value
8686
).show(context.supportFragmentManager, "info")
8787
}
8888
}
@@ -92,15 +92,21 @@ class AppListAdapter(
9292
init {
9393

9494
if (prefs.getBoolean("enable_vanced", true)) {
95-
dataModels.add(viewModel.vancedModel.value)
96-
rootDataModels.add(viewModel.vancedRootModel.value)
95+
if (isRoot) {
96+
rootDataModels.add(viewModel.vancedRootModel.value)
97+
} else {
98+
dataModels.add(viewModel.vancedModel.value)
99+
}
97100
apps.add(context.getString(R.string.vanced))
98101
itemCount++
99102
}
100103

101104
if (prefs.getBoolean("enable_music", true)) {
102-
dataModels.add(viewModel.musicModel.value)
103-
rootDataModels.add(viewModel.musicRootModel.value)
105+
if (isRoot) {
106+
rootDataModels.add(viewModel.musicRootModel.value)
107+
} else {
108+
dataModels.add(viewModel.musicModel.value)
109+
}
104110
apps.add(context.getString(R.string.music))
105111
itemCount++
106112
}

app/src/main/java/com/vanced/manager/core/downloader/VancedDownloader.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.vanced.manager.core.downloader
22

33
import android.content.Context
44
import android.content.SharedPreferences
5+
import android.util.Log
56
import androidx.preference.PreferenceManager.getDefaultSharedPreferences
67
import com.google.firebase.analytics.FirebaseAnalytics
78
import com.google.firebase.analytics.ktx.logEvent
@@ -15,6 +16,7 @@ import com.vanced.manager.utils.PackageHelper.downloadStockCheck
1516
import com.vanced.manager.utils.PackageHelper.installVanced
1617
import com.vanced.manager.utils.PackageHelper.installVancedRoot
1718
import java.io.File
19+
import java.lang.Exception
1820

1921
object VancedDownloader {
2022

@@ -58,7 +60,13 @@ object VancedDownloader {
5860
count = 0
5961

6062
vancedVersionCode = vanced.value?.int("versionCode") ?: 0
61-
downloadSplits(context)
63+
try {
64+
downloadSplits(context)
65+
} catch (e: Exception) {
66+
Log.d("VMDownloader", e.stackTraceToString())
67+
downloadProgress.value?.downloadingFile?.postValue(context.getString(R.string.error_downloading, "Vanced"))
68+
}
69+
6270
}
6371

6472
private fun downloadSplits(context: Context, type: String = "theme") {

app/src/main/java/com/vanced/manager/model/DataModel.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import kotlinx.coroutines.launch
1616
open class DataModel(
1717
private val jsonObject: LiveData<JsonObject?>,
1818
private val context: Context,
19-
open val appPkg: String,
20-
open val appName: String,
21-
open val appIcon: Drawable?,
19+
val appPkg: String,
20+
val appName: String,
21+
val appIcon: Drawable?,
2222
) {
2323

2424
private val versionCode = MutableLiveData<Int>()
@@ -32,11 +32,9 @@ open class DataModel(
3232

3333
private fun fetch() = CoroutineScope(Dispatchers.IO).launch {
3434
val jobj = jsonObject.value
35-
isAppInstalled.postValue(isPackageInstalled(appPkg, context.packageManager))
35+
isAppInstalled.postValue(isAppInstalled(appPkg))
3636
versionCode.postValue(jobj?.int("versionCode") ?: 0)
37-
versionName.postValue(jobj?.string("version")?.removeSuffix("-vanced") ?: context.getString(
38-
R.string.unavailable
39-
))
37+
versionName.postValue(jobj?.string("version")?.removeSuffix("-vanced") ?: context.getString(R.string.unavailable))
4038
changelog.postValue(jobj?.string("changelog") ?: context.getString(R.string.unavailable))
4139
}
4240

@@ -50,8 +48,8 @@ open class DataModel(
5048
}
5149
this?.let {
5250
isAppInstalled.observe(it) {
53-
installedVersionCode.postValue(getPkgVersionCode(appPkg))
54-
installedVersionName.postValue(getPkgVersionName(appPkg))
51+
installedVersionCode.value = getPkgVersionCode(appPkg)
52+
installedVersionName.value = getPkgVersionName(appPkg)
5553
}
5654
}
5755
this?.let {
@@ -64,27 +62,29 @@ open class DataModel(
6462
}
6563
}
6664

67-
open fun getPkgVersionName(pkg: String): String {
65+
open fun isAppInstalled(pkg: String): Boolean = isPackageInstalled(pkg, context.packageManager)
66+
67+
private fun getPkgVersionName(pkg: String): String {
6868
val pm = context.packageManager
6969
return if (isAppInstalled.value == true) {
70-
pm.getPackageInfo(pkg, 0).versionName.removeSuffix("-vanced")
70+
pm?.getPackageInfo(pkg, 0)?.versionName?.removeSuffix("-vanced") ?: context.getString(R.string.unavailable)
7171
} else {
7272
context.getString(R.string.unavailable)
7373
}
7474
}
7575

7676
@Suppress("DEPRECATION")
7777
private fun getPkgVersionCode(pkg: String): Int {
78+
val pm = context.packageManager
7879
return if (isAppInstalled.value == true) {
7980
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P)
80-
context.packageManager.getPackageInfo(pkg, 0).longVersionCode.and(0xFFFFFFFF)
81-
.toInt()
81+
pm?.getPackageInfo(pkg, 0)?.longVersionCode?.and(0xFFFFFFFF)?.toInt() ?: 0
8282
else
83-
context.packageManager.getPackageInfo(pkg, 0).versionCode
83+
pm?.getPackageInfo(pkg, 0)?.versionCode ?: 0
8484
} else 0
8585
}
8686

87-
open fun compareInt(int1: Int?, int2: Int?): String {
87+
private fun compareInt(int1: Int?, int2: Int?): String {
8888
if (int2 != null && int1 != null) {
8989
return when {
9090
int1 == 0 -> context.getString(R.string.install)

app/src/main/java/com/vanced/manager/model/ProgressModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ open class ProgressModel {
2323
}
2424

2525
init {
26-
installing.value = false
26+
installing.postValue(false)
2727
reset()
2828
}
2929

app/src/main/java/com/vanced/manager/model/RootDataModel.kt

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,30 @@ import android.content.Context
44
import android.graphics.drawable.Drawable
55
import androidx.lifecycle.LiveData
66
import com.beust.klaxon.JsonObject
7-
import com.vanced.manager.R
87
import com.vanced.manager.utils.PackageHelper
98

10-
open class RootDataModel(
9+
class RootDataModel(
1110
jsonObject: LiveData<JsonObject?>,
12-
private val context: Context,
13-
override val appPkg: String,
14-
override val appName: String,
15-
override val appIcon: Drawable?,
16-
private val scriptName: String
11+
context: Context,
12+
appPkg: String,
13+
appName: String,
14+
appIcon: Drawable?,
15+
//BUG THIS!
16+
//kotlin thinks that this value is null if we use
17+
//private val scriptName: String
18+
//Although it's impossible for it to be null.
19+
//Ironic, isn't it?
20+
private val scriptName: String?
1721
): DataModel(
1822
jsonObject, context, appPkg, appName, appIcon
1923
) {
2024

21-
override fun getPkgVersionName(pkg: String): String {
22-
return if (PackageHelper.scriptExists(scriptName)) {
23-
super.getPkgVersionName(pkg)
25+
override fun isAppInstalled(pkg: String): Boolean {
26+
//Adapt to nullable shit
27+
return if (scriptName?.let { PackageHelper.scriptExists(it) } == true) {
28+
super.isAppInstalled(appPkg)
2429
} else {
25-
context.getString(R.string.unavailable)
26-
}
27-
}
28-
29-
override fun compareInt(int1: Int?, int2: Int?): String {
30-
return if (PackageHelper.scriptExists(scriptName)) {
31-
super.compareInt(int1, int2)
32-
} else {
33-
context.getString(R.string.install)
30+
false
3431
}
3532
}
3633

app/src/main/java/com/vanced/manager/ui/viewmodels/HomeViewModel.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import kotlinx.coroutines.launch
4343
open class HomeViewModel(private val activity: FragmentActivity): ViewModel() {
4444

4545
private val prefs = getDefaultSharedPreferences(activity)
46+
private val variant get() = prefs.getString("vanced_variant", "nonroot")
4647

4748
val vancedModel = MutableLiveData<DataModel>()
4849
val vancedRootModel = MutableLiveData<RootDataModel>()
@@ -90,7 +91,6 @@ open class HomeViewModel(private val activity: FragmentActivity): ViewModel() {
9091
}
9192

9293
fun openInstallDialog(view: View, app: String) {
93-
val variant = prefs.getString("vanced_variant", "nonroot")
9494
if (variant == "nonroot" && app != activity.getString(R.string.microg) && !microgModel.value?.isAppInstalled?.value!!) {
9595
microgToast.show()
9696
return
@@ -147,21 +147,22 @@ open class HomeViewModel(private val activity: FragmentActivity): ViewModel() {
147147
}
148148

149149
fun uninstallPackage(pkg: String) {
150-
if (prefs.getString("vanced_variant", "nonroot") == "root" && uninstallRootApk(pkg)) {
150+
if (variant == "root" && uninstallRootApk(pkg)) {
151151
viewModelScope.launch { loadJson(activity) }
152152
} else {
153153
uninstallApk(pkg, activity)
154154
}
155155
}
156156

157157
init {
158-
if (prefs.getString("vanced_variant", "nonroot") == "root") {
158+
if (variant == "root") {
159159
vancedRootModel.value = RootDataModel(vanced, activity, vancedRootPkg, activity.getString(R.string.vanced), AppCompatResources.getDrawable(activity, R.drawable.ic_vanced), "vanced")
160160
musicRootModel.value = RootDataModel(music, activity, musicRootPkg, activity.getString(R.string.music), AppCompatResources.getDrawable(activity, R.drawable.ic_music), "music")
161+
} else {
162+
vancedModel.value = DataModel(vanced, activity, vancedPkg, activity.getString(R.string.vanced), AppCompatResources.getDrawable(activity, R.drawable.ic_vanced))
163+
musicModel.value = DataModel(music, activity, musicPkg, activity.getString(R.string.music), AppCompatResources.getDrawable(activity, R.drawable.ic_music))
164+
microgModel.value = DataModel(microg, activity, microgPkg, activity.getString(R.string.microg), AppCompatResources.getDrawable(activity, R.drawable.ic_microg))
161165
}
162-
vancedModel.value = DataModel(vanced, activity, vancedPkg, activity.getString(R.string.vanced), AppCompatResources.getDrawable(activity, R.drawable.ic_vanced))
163-
musicModel.value = DataModel(music, activity, musicPkg, activity.getString(R.string.music), AppCompatResources.getDrawable(activity, R.drawable.ic_music))
164-
microgModel.value = DataModel(microg, activity, microgPkg, activity.getString(R.string.microg), AppCompatResources.getDrawable(activity, R.drawable.ic_microg))
165166
managerModel.value = DataModel(manager, activity, managerPkg, activity.getString(R.string.app_name), AppCompatResources.getDrawable(activity, R.mipmap.ic_launcher))
166167
}
167168
}

app/src/main/java/com/vanced/manager/utils/AppUtils.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ object AppUtils: CoroutineScope by CoroutineScope(Dispatchers.IO) {
5959
}
6060
}
6161

62+
fun sendFailure(error: String, context: Context): Job {
63+
return launch {
64+
delay(700)
65+
val intent = Intent(HomeFragment.INSTALL_FAILED)
66+
intent.putExtra("errorMsg", getErrorMessage(error, context))
67+
intent.putExtra("fullErrorMsg", error)
68+
LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
69+
}
70+
}
71+
6272
@Throws(IOException::class)
6373
fun generateChecksum(data: ByteArray): String {
6474
try {

app/src/main/java/com/vanced/manager/utils/DownloadHelper.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ object DownloadHelper : CoroutineScope by CoroutineScope(Dispatchers.IO) {
117117

118118
fun downloadManager(context: Context) {
119119
val url = "https://github.com/YTVanced/VancedManager/releases/latest/download/manager.apk"
120-
download(url,"https://github.com/YTVanced/VancedManager", "manager", "manager.apk", context, onDownloadComplete = {
120+
download(url,"https://github.com/YTVanced/VancedManager/", "manager", "manager.apk", context, onDownloadComplete = {
121121
val apk = File("${context.getExternalFilesDir("manager")?.path}/manager.apk")
122122
val uri =
123123
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)

app/src/main/java/com/vanced/manager/utils/PackageHelper.kt

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -161,23 +161,28 @@ object PackageHelper {
161161
}
162162

163163
fun install(path: String, context: Context) {
164-
val callbackIntent = Intent(context, AppInstallerService::class.java)
165-
val pendingIntent = PendingIntent.getService(context, 0, callbackIntent, 0)
166-
val packageInstaller = context.packageManager.packageInstaller
167-
val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
168-
val sessionId = packageInstaller.createSession(params)
169-
val session = packageInstaller.openSession(sessionId)
170-
val inputStream: InputStream = FileInputStream(path)
171-
val outputStream = session.openWrite("install", 0, -1)
172-
val buffer = ByteArray(65536)
173-
var c: Int
174-
while (inputStream.read(buffer).also { c = it } != -1) {
175-
outputStream.write(buffer, 0, c)
176-
}
177-
session.fsync(outputStream)
178-
inputStream.close()
179-
outputStream.close()
180-
session.commit(pendingIntent.intentSender)
164+
try {
165+
val callbackIntent = Intent(context, AppInstallerService::class.java)
166+
val pendingIntent = PendingIntent.getService(context, 0, callbackIntent, 0)
167+
val packageInstaller = context.packageManager.packageInstaller
168+
val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
169+
val sessionId = packageInstaller.createSession(params)
170+
val session = packageInstaller.openSession(sessionId)
171+
val inputStream: InputStream = FileInputStream(path)
172+
val outputStream = session.openWrite("install", 0, -1)
173+
val buffer = ByteArray(65536)
174+
var c: Int
175+
while (inputStream.read(buffer).also { c = it } != -1) {
176+
outputStream.write(buffer, 0, c)
177+
}
178+
session.fsync(outputStream)
179+
inputStream.close()
180+
outputStream.close()
181+
session.commit(pendingIntent.intentSender)
182+
} catch (e: IOException) {
183+
Log.d(INSTALLER_TAG, e.stackTraceToString())
184+
}
185+
181186
}
182187

183188
private fun installRootMusic(files: ArrayList<FileInfo>, context: Context): Boolean {
@@ -271,7 +276,7 @@ object PackageHelper {
271276
}
272277
doCommitSession(sessionId, context)
273278
Log.d(INSTALLER_TAG,"Success")
274-
} catch (e: IOException) {
279+
} catch (e: Exception) {
275280
e.printStackTrace()
276281
}
277282
return sessionId
@@ -326,19 +331,16 @@ object PackageHelper {
326331
private fun doCommitSession(sessionId: Int, context: Context) {
327332
var session: PackageInstaller.Session? = null
328333
try {
329-
try {
330-
session = context.packageManager.packageInstaller.openSession(sessionId)
331-
val callbackIntent = Intent(context, AppInstallerService::class.java)
332-
val pendingIntent = PendingIntent.getService(context, 0, callbackIntent, 0)
333-
session.commit(pendingIntent.intentSender)
334-
session.close()
335-
Log.d(INSTALLER_TAG, "install request sent")
336-
Log.d(INSTALLER_TAG, "doCommitSession: " + context.packageManager.packageInstaller.mySessions)
337-
Log.d(INSTALLER_TAG, "doCommitSession: after session commit ")
338-
} catch (e: IOException) {
339-
e.printStackTrace()
340-
}
341-
334+
session = context.packageManager.packageInstaller.openSession(sessionId)
335+
val callbackIntent = Intent(context, AppInstallerService::class.java)
336+
val pendingIntent = PendingIntent.getService(context, 0, callbackIntent, 0)
337+
session.commit(pendingIntent.intentSender)
338+
session.close()
339+
Log.d(INSTALLER_TAG, "install request sent")
340+
Log.d(INSTALLER_TAG, "doCommitSession: " + context.packageManager.packageInstaller.mySessions)
341+
Log.d(INSTALLER_TAG, "doCommitSession: after session commit ")
342+
} catch (e: IOException) {
343+
e.printStackTrace()
342344
} finally {
343345
session?.close()
344346
}

0 commit comments

Comments
 (0)