installer tests

This commit is contained in:
X1nto 2020-06-29 23:20:09 +04:00
parent 6f68052077
commit d2ab184165
7 changed files with 77 additions and 9 deletions

View File

@ -56,10 +56,22 @@
</provider>
<receiver
android:name=".core.downloader.DownloadBroadcastReceiver"
android:exported="false">
<intent-filter>
<action android:name="cancel" />
</intent-filter>
</receiver>
<service android:name=".core.installer.SplitInstallerService" />
<service android:name=".core.installer.RootSplitInstallerService" />
<service android:name=".core.installer.SplitInstaller" />
<service android:name=".core.installer.AppUninstallerService" />
<service android:name=".core.installer.AppInstallerService" />
<service android:name=".core.installer.AppInstaller" />
<service android:name=".core.downloader.VancedDownloadService" />
<service android:name=".core.downloader.MicrogDownloadService" />

View File

@ -12,11 +12,13 @@ import com.downloader.OnDownloadListener
import com.downloader.OnStartOrResumeListener
import com.downloader.PRDownloader
import com.vanced.manager.R
import com.vanced.manager.core.installer.AppInstaller
import com.vanced.manager.ui.fragments.HomeFragment
import com.vanced.manager.utils.InternetTools.getFileNameFromUrl
import com.vanced.manager.utils.NotificationHelper
import com.vanced.manager.utils.NotificationHelper.cancelNotif
import com.vanced.manager.utils.NotificationHelper.createBasicNotif
import com.vanced.manager.utils.PackageHelper.installApp
import java.lang.Exception
import java.lang.IllegalStateException
import java.lang.RuntimeException
@ -61,6 +63,10 @@ class MicrogDownloadService: Service() {
override fun onDownloadComplete() {
prefs?.edit()?.putBoolean("isMicrogDownloading", false)?.apply()
cancelNotif(channel, this@MicrogDownloadService)
val intent = Intent(this@MicrogDownloadService, AppInstaller::class.java)
intent.putExtra("path", "${filesDir.path}/microg.apk")
intent.putExtra("pkg", "com.mgoogle.android.gms")
startService(intent)
}
override fun onError(error: Error) {
prefs?.edit()?.putBoolean("isMicrogDownloading", false)?.apply()

View File

@ -108,7 +108,7 @@ class VancedDownloadService: Service() {
if (variant == "root")
startService(Intent(this, RootSplitInstallerService::class.java))
else
SplitInstaller.installSplitApk(this)
startService(Intent(this, SplitInstaller::class.java))
}
override fun onBind(intent: Intent?): IBinder? {

View File

@ -0,0 +1,38 @@
package com.vanced.manager.core.installer
import android.app.PendingIntent
import android.app.Service
import android.content.Intent
import android.content.pm.PackageInstaller
import android.os.IBinder
import java.io.FileInputStream
import java.io.InputStream
class AppInstaller: Service() {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
val callbackIntent = Intent(applicationContext, AppInstallerService::class.java)
val pendingIntent = PendingIntent.getService(applicationContext, 0, callbackIntent, 0)
val packageInstaller = packageManager.packageInstaller
val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
params.setAppPackageName(intent?.getStringExtra("pkg"))
val sessionId = packageInstaller.createSession(params)
val session = packageInstaller.openSession(sessionId)
val inputStream: InputStream = FileInputStream(intent?.getStringExtra("path") as String)
val outputStream = session.openWrite("install", 0, -1)
val buffer = ByteArray(65536)
var c: Int
while (inputStream.read(buffer).also { c = it } != -1) {
outputStream.write(buffer, 0, c)
}
session.fsync(outputStream)
inputStream.close()
outputStream.close()
session.commit(pendingIntent.intentSender)
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
}

View File

@ -1,15 +1,26 @@
package com.vanced.manager.core.installer
import android.app.PendingIntent
import android.app.Service
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInstaller
import android.os.IBinder
import android.util.Log
import java.io.*
object SplitInstaller {
class SplitInstaller: Service() {
fun installSplitApk(context: Context): Int {
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
installSplitApk(this)
return START_STICKY
}
override fun onBind(intent: Intent?): IBinder? {
return null
}
private fun installSplitApk(context: Context): Int {
val apkFolderPath = context.cacheDir.path + "/"
val nameSizeMap = HashMap<String, Long>()
var totalSize: Long = 0

View File

@ -17,7 +17,7 @@ object NotificationHelper {
val notifChannel = NotificationChannel(
"69420",
context.getString(R.string.notif_channel_name),
NotificationManager.IMPORTANCE_DEFAULT
NotificationManager.IMPORTANCE_LOW
)
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(notifChannel)
@ -29,7 +29,7 @@ object NotificationHelper {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
Notification.Builder(context, channel.toString()).setChannelId("69420")
else
Notification.Builder(context).setPriority(Notification.PRIORITY_DEFAULT)
Notification.Builder(context).setPriority(Notification.PRIORITY_LOW)
val cancelDownload = Intent(context, DownloadBroadcastReceiver::class.java).apply {
action = "cancel"

View File

@ -2,6 +2,7 @@ package com.vanced.manager.utils
import android.app.Activity
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInstaller.SessionParams
import android.content.pm.PackageManager
@ -24,13 +25,13 @@ object PackageHelper {
@Throws(IOException::class)
fun installApp(
activity: Activity,
context: Context,
path: String,
pkg: String?
) {
val callbackIntent = Intent(activity.applicationContext, AppInstallerService::class.java)
val pendingIntent = PendingIntent.getService(activity.applicationContext, 0, callbackIntent, 0)
val packageInstaller = activity.packageManager.packageInstaller
val callbackIntent = Intent(context.applicationContext, AppInstallerService::class.java)
val pendingIntent = PendingIntent.getService(context.applicationContext, 0, callbackIntent, 0)
val packageInstaller = context.packageManager.packageInstaller
val params = SessionParams(SessionParams.MODE_FULL_INSTALL)
params.setAppPackageName(pkg)
val sessionId = packageInstaller.createSession(params)