mirror of
https://github.com/YTVanced/VancedManager
synced 2024-11-10 12:55:06 +00:00
hopefully fixed all issues
This commit is contained in:
parent
acff4385aa
commit
61dea49c22
7 changed files with 208 additions and 163 deletions
|
@ -1,6 +1,9 @@
|
|||
package com.vanced.manager.core
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageInstaller
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
|
@ -8,14 +11,17 @@ import androidx.preference.PreferenceManager
|
|||
import com.dezlum.codelabs.getjson.GetJson
|
||||
import com.vanced.manager.R
|
||||
import com.vanced.manager.core.base.BaseActivity
|
||||
import com.vanced.manager.core.installer.SplitInstallerService
|
||||
import zlc.season.rxdownload4.file
|
||||
import java.io.File
|
||||
import java.io.*
|
||||
|
||||
// This activity will NOT be used in manifest
|
||||
// since MainActivity will extend it
|
||||
@SuppressLint("Registered")
|
||||
open class Main: BaseActivity() {
|
||||
|
||||
private lateinit var packageInstaller: PackageInstaller
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
|
||||
|
@ -76,4 +82,106 @@ open class Main: BaseActivity() {
|
|||
prefs.edit().putBoolean("statement", true).apply()
|
||||
}
|
||||
|
||||
fun installSplitApk(): Int {
|
||||
val apkFolderPath = cacheDir.path
|
||||
val nameSizeMap = HashMap<String, Long>()
|
||||
var totalSize: Long = 0
|
||||
var sessionId = 0
|
||||
val folder = File(apkFolderPath)
|
||||
val listOfFiles = folder.listFiles()
|
||||
try {
|
||||
for (listOfFile in listOfFiles!!) {
|
||||
if (listOfFile.isFile) {
|
||||
Log.d("AppLog", "installApk: " + listOfFile.name)
|
||||
nameSizeMap[listOfFile.name] = listOfFile.length()
|
||||
totalSize += listOfFile.length()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return -1
|
||||
}
|
||||
val installParams = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
|
||||
installParams.setSize(totalSize)
|
||||
try {
|
||||
sessionId = packageInstaller.createSession(installParams)
|
||||
Log.d("AppLog","Success: created install session [$sessionId]")
|
||||
for ((key, value) in nameSizeMap) {
|
||||
doWriteSession(sessionId, apkFolderPath + key, value, key)
|
||||
}
|
||||
doCommitSession(sessionId)
|
||||
Log.d("AppLog","Success")
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return sessionId
|
||||
}
|
||||
|
||||
private fun doWriteSession(sessionId: Int, inPath: String?, sizeBytes: Long, splitName: String): Int {
|
||||
var inPathToUse = inPath
|
||||
var sizeBytesToUse = sizeBytes
|
||||
if ("-" == inPathToUse) {
|
||||
inPathToUse = null
|
||||
} else if (inPathToUse != null) {
|
||||
val file = File(inPathToUse)
|
||||
if (file.isFile)
|
||||
sizeBytesToUse = file.length()
|
||||
}
|
||||
var session: PackageInstaller.Session? = null
|
||||
var inputStream: InputStream? = null
|
||||
var out: OutputStream? = null
|
||||
try {
|
||||
session = packageInstaller.openSession(sessionId)
|
||||
if (inPathToUse != null) {
|
||||
inputStream = FileInputStream(inPathToUse)
|
||||
}
|
||||
out = session.openWrite(splitName, 0, sizeBytesToUse)
|
||||
var total = 0
|
||||
val buffer = ByteArray(65536)
|
||||
var c: Int
|
||||
while (true) {
|
||||
c = inputStream!!.read(buffer)
|
||||
if (c == -1)
|
||||
break
|
||||
total += c
|
||||
out.write(buffer, 0, c)
|
||||
}
|
||||
session.fsync(out)
|
||||
Log.d("AppLog", "Success: streamed $total bytes")
|
||||
return PackageInstaller.STATUS_SUCCESS
|
||||
} catch (e: IOException) {
|
||||
Log.e("AppLog", "Error: failed to write; " + e.message)
|
||||
return PackageInstaller.STATUS_FAILURE
|
||||
} finally {
|
||||
try {
|
||||
out?.close()
|
||||
inputStream?.close()
|
||||
session?.close()
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun doCommitSession(sessionId: Int) {
|
||||
var session: PackageInstaller.Session? = null
|
||||
try {
|
||||
try {
|
||||
session = packageInstaller.openSession(sessionId)
|
||||
val callbackIntent = Intent(applicationContext, SplitInstallerService::class.java)
|
||||
val pendingIntent = PendingIntent.getService(applicationContext, 0, callbackIntent, 0)
|
||||
session.commit(pendingIntent.intentSender)
|
||||
session.close()
|
||||
Log.d("AppLog", "install request sent")
|
||||
Log.d("AppLog", "doCommitSession: " + packageInstaller.mySessions)
|
||||
Log.d("AppLog", "doCommitSession: after session commit ")
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
} finally {
|
||||
session!!.close()
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
package com.vanced.manager.core.base
|
||||
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
import android.view.View
|
||||
|
@ -10,7 +9,7 @@ import androidx.browser.customtabs.CustomTabsIntent
|
|||
import androidx.core.content.ContextCompat
|
||||
import androidx.fragment.app.Fragment
|
||||
import androidx.navigation.findNavController
|
||||
import com.vanced.manager.core.installer.SplitInstallActivity
|
||||
import com.vanced.manager.ui.MainActivity
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers
|
||||
import io.reactivex.disposables.Disposable
|
||||
import io.reactivex.rxkotlin.subscribeBy
|
||||
|
@ -74,9 +73,11 @@ open class BaseFragment : Fragment() {
|
|||
if (apk == "lang") {
|
||||
if (apkVar != "en")
|
||||
downloadEn(loadBar)
|
||||
else
|
||||
launchInstaller()
|
||||
}
|
||||
else launchInstaller()
|
||||
|
||||
else
|
||||
launchInstaller()
|
||||
} else {
|
||||
view?.findNavController()?.navigate(navigate)
|
||||
}
|
||||
|
@ -90,7 +91,6 @@ open class BaseFragment : Fragment() {
|
|||
|
||||
private fun downloadEn(loadBar: ProgressBar) {
|
||||
val url = "https://x1nto.github.io/VancedFiles/Splits/Language/split_config.en.apk"
|
||||
|
||||
val task = activity?.cacheDir?.path?.let {
|
||||
Task(
|
||||
url = url,
|
||||
|
@ -122,9 +122,8 @@ open class BaseFragment : Fragment() {
|
|||
}
|
||||
|
||||
private fun launchInstaller() {
|
||||
val intent = Intent(requireContext(), SplitInstallActivity::class.java)
|
||||
activity?.startActivityForResult(intent, 666)
|
||||
val activity = (activity as MainActivity?)!!
|
||||
activity.installSplitApk()
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.vanced.manager.core.base;
|
||||
|
||||
import androidx.fragment.app.Fragment;
|
||||
|
||||
import com.vanced.manager.ui.MainActivity;
|
||||
|
||||
public class DummyJava extends Fragment {
|
||||
|
||||
}
|
|
@ -1,122 +0,0 @@
|
|||
package com.vanced.manager.core.installer
|
||||
|
||||
import android.app.Activity
|
||||
import android.app.PendingIntent
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageInstaller
|
||||
import android.os.Bundle
|
||||
import android.os.PersistableBundle
|
||||
import android.util.Log
|
||||
import java.io.*
|
||||
|
||||
class SplitInstallActivity: Activity() {
|
||||
|
||||
private lateinit var packageInstaller: PackageInstaller
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
|
||||
super.onCreate(savedInstanceState, persistentState)
|
||||
installSplitApk()
|
||||
}
|
||||
|
||||
private fun installSplitApk(): Int {
|
||||
val apkFolderPath = cacheDir.path
|
||||
val nameSizeMap = HashMap<String, Long>()
|
||||
var totalSize: Long = 0
|
||||
var sessionId = 0
|
||||
val folder = File(apkFolderPath)
|
||||
val listOfFiles = folder.listFiles()
|
||||
try {
|
||||
for (listOfFile in listOfFiles!!) {
|
||||
if (listOfFile.isFile) {
|
||||
Log.d("AppLog", "installApk: " + listOfFile.name)
|
||||
nameSizeMap[listOfFile.name] = listOfFile.length()
|
||||
totalSize += listOfFile.length()
|
||||
}
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return -1
|
||||
}
|
||||
val installParams = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
|
||||
installParams.setSize(totalSize)
|
||||
try {
|
||||
sessionId = packageInstaller.createSession(installParams)
|
||||
Log.d("AppLog","Success: created install session [$sessionId]")
|
||||
for ((key, value) in nameSizeMap) {
|
||||
doWriteSession(sessionId, apkFolderPath + key, value, key)
|
||||
}
|
||||
doCommitSession(sessionId)
|
||||
Log.d("AppLog","Success")
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
return sessionId
|
||||
}
|
||||
|
||||
private fun doWriteSession(sessionId: Int, inPath: String?, sizeBytes: Long, splitName: String): Int {
|
||||
var inPathToUse = inPath
|
||||
var sizeBytesToUse = sizeBytes
|
||||
if ("-" == inPathToUse) {
|
||||
inPathToUse = null
|
||||
} else if (inPathToUse != null) {
|
||||
val file = File(inPathToUse)
|
||||
if (file.isFile)
|
||||
sizeBytesToUse = file.length()
|
||||
}
|
||||
var session: PackageInstaller.Session? = null
|
||||
var inputStream: InputStream? = null
|
||||
var out: OutputStream? = null
|
||||
try {
|
||||
session = packageInstaller.openSession(sessionId)
|
||||
if (inPathToUse != null) {
|
||||
inputStream = FileInputStream(inPathToUse)
|
||||
}
|
||||
out = session.openWrite(splitName, 0, sizeBytesToUse)
|
||||
var total = 0
|
||||
val buffer = ByteArray(65536)
|
||||
var c: Int
|
||||
while (true) {
|
||||
c = inputStream!!.read(buffer)
|
||||
if (c == -1)
|
||||
break
|
||||
total += c
|
||||
out.write(buffer, 0, c)
|
||||
}
|
||||
session.fsync(out)
|
||||
Log.d("AppLog", "Success: streamed $total bytes")
|
||||
return PackageInstaller.STATUS_SUCCESS
|
||||
} catch (e: IOException) {
|
||||
Log.e("AppLog", "Error: failed to write; " + e.message)
|
||||
return PackageInstaller.STATUS_FAILURE
|
||||
} finally {
|
||||
try {
|
||||
out?.close()
|
||||
inputStream?.close()
|
||||
session?.close()
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun doCommitSession(sessionId: Int) {
|
||||
var session: PackageInstaller.Session? = null
|
||||
try {
|
||||
try {
|
||||
session = packageInstaller.openSession(sessionId)
|
||||
val callbackIntent = Intent(applicationContext, SplitInstallerService::class.java)
|
||||
val pendingIntent = PendingIntent.getService(applicationContext, 0, callbackIntent, 0)
|
||||
session.commit(pendingIntent.intentSender)
|
||||
session.close()
|
||||
Log.d("AppLog", "install request sent")
|
||||
Log.d("AppLog", "doCommitSession: " + packageInstaller.mySessions)
|
||||
Log.d("AppLog", "doCommitSession: after session commit ")
|
||||
} catch (e: IOException) {
|
||||
e.printStackTrace()
|
||||
}
|
||||
|
||||
} finally {
|
||||
session!!.close()
|
||||
}
|
||||
}
|
||||
}
|
|
@ -42,6 +42,11 @@ class MainActivity : Main() {
|
|||
|
||||
}
|
||||
|
||||
override fun onResume() {
|
||||
super.onResume()
|
||||
recreate()
|
||||
}
|
||||
|
||||
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||
val navHost = findNavController(R.id.bottom_nav_host)
|
||||
val prefs = PreferenceManager.getDefaultSharedPreferences(this)
|
||||
|
@ -93,7 +98,6 @@ class MainActivity : Main() {
|
|||
|
||||
}
|
||||
|
||||
|
||||
private fun setDisplayHomeAsUpEnabled(isNeeded: Boolean) {
|
||||
val toolbar: Toolbar = findViewById(R.id.home_toolbar)
|
||||
when {
|
||||
|
|
|
@ -91,11 +91,11 @@ class HomeFragment : Home() {
|
|||
when {
|
||||
microgRemoteVer > microgVer -> {
|
||||
microginstallbtn?.text = activity?.getString(R.string.update)
|
||||
microginstallbtn?.icon = ResourcesCompat.getDrawable(resources, R.drawable.ic_cloud_upload_black_24dp, null)
|
||||
microginstallbtn?.icon = activity?.getDrawable(R.drawable.ic_cloud_upload_black_24dp)
|
||||
}
|
||||
microgRemoteVer == microgVer -> {
|
||||
microginstallbtn?.text = activity?.getString(R.string.button_installed)
|
||||
microginstallbtn?.icon = ResourcesCompat.getDrawable(resources, R.drawable.outline_cloud_done_24, null)
|
||||
microginstallbtn?.icon = activity?.getDrawable(R.drawable.outline_cloud_done_24)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -104,12 +104,12 @@ class HomeFragment : Home() {
|
|||
pm.getPackageInfo("com.vanced.android.youtube", 0).versionName
|
||||
when {
|
||||
vancedRemoteVer > vancedVer -> {
|
||||
vancedinstallbtn?.text = getString(R.string.update)
|
||||
vancedinstallbtn?.icon = ResourcesCompat.getDrawable(resources, R.drawable.ic_cloud_upload_black_24dp, null)
|
||||
vancedinstallbtn?.text = activity?.getString(R.string.update)
|
||||
vancedinstallbtn?.icon = activity?.getDrawable(R.drawable.ic_cloud_upload_black_24dp)
|
||||
}
|
||||
vancedRemoteVer == vancedVer -> {
|
||||
vancedinstallbtn?.text = getString(R.string.update)
|
||||
vancedinstallbtn?.icon = ResourcesCompat.getDrawable(resources, R.drawable.outline_cloud_done_24, null)
|
||||
vancedinstallbtn?.text = activity?.getString(R.string.button_installed)
|
||||
vancedinstallbtn?.icon = activity?.getDrawable(R.drawable.outline_cloud_done_24)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,35 +1,82 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<RelativeLayout
|
||||
<com.google.android.material.card.MaterialCardView
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:background="?colorSurface"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
style="@style/MaterialCard"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_gravity="center"
|
||||
app:contentPaddingTop="0dp"
|
||||
app:contentPaddingBottom="0dp"
|
||||
app:contentPaddingRight="6dp"
|
||||
app:contentPaddingLeft="6dp"
|
||||
tools:context=".ui.fragments.UpdateCheckFragment">
|
||||
|
||||
<com.google.android.material.appbar.AppBarLayout
|
||||
android:id="@+id/vanced_install_appbar"
|
||||
android:layout_width="match_parent"
|
||||
<LinearLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:elevation="1dp"
|
||||
android:background="?colorSurface"
|
||||
android:layout_alignParentTop="true">
|
||||
android:orientation="vertical">
|
||||
|
||||
<com.google.android.material.appbar.MaterialToolbar
|
||||
android:id="@+id/vanced_install_toolbar"
|
||||
<TextView
|
||||
style="@style/CardTitle"
|
||||
android:id="@+id/update_center_text"
|
||||
android:layout_gravity="center"
|
||||
android:text="@string/update_center"
|
||||
android:textAlignment="center"
|
||||
android:fontFamily="@font/exo_semibold" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/update_center_checking"
|
||||
style="@style/AppVer.Bold"
|
||||
android:layout_marginTop="4dp"
|
||||
android:layout_gravity="center"
|
||||
android:text="Checking For Updates..."
|
||||
android:textAlignment="center"
|
||||
android:textSize="16sp" />
|
||||
|
||||
<ProgressBar
|
||||
android:id="@+id/update_center_progressbar"
|
||||
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="?attr/actionBarSize"
|
||||
app:titleTextColor="?attr/toolbarIconColor"
|
||||
android:background="@android:color/transparent" />
|
||||
android:layout_height="wrap_content"
|
||||
android:visibility="gone"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
</com.google.android.material.appbar.AppBarLayout>
|
||||
<LinearLayout
|
||||
android:id="@+id/update_button_container"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/vanced_install_frame"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_below="@id/vanced_install_appbar"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:layout_marginTop="8dp"/>
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/update_center_dismiss"
|
||||
style="@style/Widget.MaterialComponents.Button.OutlinedButton"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="4dp"
|
||||
android:text="@string/close"
|
||||
android:textAllCaps="false"/>
|
||||
|
||||
<FrameLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content">
|
||||
|
||||
</RelativeLayout>
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/update_center_update"
|
||||
style="@style/ButtonStyle"
|
||||
android:layout_marginStart="4dp"
|
||||
android:text="@string/update" />
|
||||
|
||||
<com.google.android.material.button.MaterialButton
|
||||
android:id="@+id/update_center_recheck"
|
||||
style="@style/ButtonStyle"
|
||||
android:layout_marginStart="4dp"
|
||||
android:text="@string/re_check" />
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</LinearLayout>
|
||||
|
||||
</com.google.android.material.card.MaterialCardView>
|
Loading…
Reference in a new issue