This repository has been archived by the owner on Mar 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
127 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package com.vanced.manager.model | ||
|
||
import android.content.Context | ||
import android.graphics.drawable.Drawable | ||
import androidx.preference.PreferenceManager.getDefaultSharedPreferences | ||
import com.vanced.manager.utils.InternetTools.getJsonInt | ||
import com.vanced.manager.utils.InternetTools.getJsonString | ||
import com.vanced.manager.utils.PackageHelper.isPackageInstalled | ||
|
||
open class DataModel( | ||
private val jsonName: String, | ||
private val context: Context | ||
) { | ||
|
||
private val variant = getDefaultSharedPreferences(context).getString("vanced_variant") | ||
|
||
private val appPkg = | ||
when (jsonName) { | ||
"vanced" -> if (variant == "root") "com.google.android.youtube" else "com.vanced.android.youtube" | ||
"microg" -> "com.mgoogle.android.gms" | ||
"music" -> "com.vanced.android.youtube.music" | ||
} | ||
|
||
open fun isAppInstalled(): Boolean = isPackageInstalled(appPkg, context.packageManager) | ||
|
||
open fun getVersionName(): String = getJsonString("$jsonName.json", "version") | ||
|
||
open fun getVersionCode(): Int = getJsonInt("$jsonName.json", "versionCode") | ||
|
||
open fun getInstalledVersionName(): String = getPkgVersion(isAppInstalled(), pkgPkg) | ||
|
||
open fun getInstalledVersionCode(): Int = getJsonInt("$jsonName.json", "versionCode") | ||
|
||
open fun getButtonTxt(): String = compareInt(getInstalledVersionCode(), getVersionCode()) | ||
|
||
open fun getButtonIcon(): Drawable = compareIntDrawable(getInstalledVersionCode(), getVersionCode()) | ||
|
||
private fun getPkgVersion(toCheck: Boolean, pkg: String): String { | ||
return if (toCheck) { | ||
pm.getPackageInfo(pkg, 0).versionName | ||
} else { | ||
context.getString(R.string.unavailable) | ||
} | ||
} | ||
|
||
@Suppress("DEPRECATION") | ||
private fun getPkgVerCode(toCheck: Boolean, pkg: String): Int { | ||
return if (toCheck) { | ||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) | ||
pm.getPackageInfo(pkg, 0).longVersionCode.and(0xFFFFFFFF).toInt() | ||
else | ||
pm.getPackageInfo(pkg, 0).versionCode | ||
} else 0 | ||
} | ||
|
||
private fun compareInt(int1: Int, int2: Int): String { | ||
return when { | ||
int1 == 0 -> context.getString(R.string.install) | ||
int2 > int1 -> context.getString(R.string.update) | ||
int2 == int1 || int1 > int2 -> context.getString(R.string.button_reinstall) | ||
else -> context.getString(R.string.install) | ||
} | ||
|
||
} | ||
|
||
private fun compareIntDrawable(int1: Int, int2: Int): Drawable? { | ||
return when { | ||
int1 == 0 -> context.getDrawable(R.drawable.ic_download) | ||
int2 > int1 -> context.getDrawable(R.drawable.ic_update) | ||
int2 == int1 -> context.getDrawable(R.drawable.ic_done) | ||
else -> context.getDrawable(R.drawable.ic_download) | ||
} | ||
} | ||
|
||
} |
2 changes: 2 additions & 0 deletions
2
app/src/main/java/com/vanced/manager/ui/fragments/MainFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
package com.vanced.manager.ui.fragments | ||
|
||
import androidx.fragment.app.Fragment | ||
|
||
class HomeFragment : Fragment() { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.