Revert "remove useless ThreadPoolExecutor in ServiceInstaller.kt" as it is needed for consistent and manageable ServiceConnection
This commit is contained in:
@@ -43,11 +43,11 @@ import java.util.concurrent.LinkedBlockingQueue
|
|||||||
import java.util.concurrent.ThreadPoolExecutor
|
import java.util.concurrent.ThreadPoolExecutor
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
import java.util.concurrent.atomic.AtomicBoolean
|
||||||
import java.util.concurrent.atomic.AtomicInteger
|
|
||||||
|
|
||||||
class ServiceInstaller(context: Context) : InstallerBase(context) {
|
class ServiceInstaller(context: Context) : InstallerBase(context) {
|
||||||
|
|
||||||
private lateinit var serviceConnection: ServiceConnection
|
private lateinit var serviceConnection: ServiceConnection
|
||||||
|
private val executor = ThreadPoolExecutor(0, 1, 30L, TimeUnit.SECONDS, LinkedBlockingQueue())
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val ACTION_INSTALL_REPLACE_EXISTING = 2
|
const val ACTION_INSTALL_REPLACE_EXISTING = 2
|
||||||
@@ -97,13 +97,19 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun uninstall(packageName: String) {
|
override fun uninstall(packageName: String) {
|
||||||
val attachedToServiceTimeout = AtomicBoolean(false)
|
executor.execute {
|
||||||
|
val readyWithAction = AtomicBoolean(false)
|
||||||
AuroraApplication.enqueuedInstalls.add(packageName)
|
Handler(Looper.getMainLooper()).post {
|
||||||
|
val serviceConnection = object : ServiceConnection {
|
||||||
serviceConnection = object : ServiceConnection {
|
|
||||||
override fun onServiceConnected(name: ComponentName, binder: IBinder) {
|
override fun onServiceConnected(name: ComponentName, binder: IBinder) {
|
||||||
attachedToServiceTimeout.set(true)
|
if (isAlreadyQueued(packageName)) {
|
||||||
|
if (::serviceConnection.isInitialized) {
|
||||||
|
context.unbindService(serviceConnection)
|
||||||
|
}
|
||||||
|
readyWithAction.set(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
AuroraApplication.enqueuedInstalls.add(packageName)
|
||||||
val service = IPrivilegedService.Stub.asInterface(binder)
|
val service = IPrivilegedService.Stub.asInterface(binder)
|
||||||
|
|
||||||
if (service.hasPrivilegedPermissions()) {
|
if (service.hasPrivilegedPermissions()) {
|
||||||
@@ -111,9 +117,7 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
|
|
||||||
val callback = object : IPrivilegedCallback.Stub() {
|
val callback = object : IPrivilegedCallback.Stub() {
|
||||||
|
|
||||||
override fun handleResult(packageName: String, returnCode: Int) {
|
override fun handleResult(packageName: String, returnCode: Int) {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleResultX(
|
override fun handleResultX(
|
||||||
packageName: String,
|
packageName: String,
|
||||||
@@ -122,6 +126,7 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
) {
|
) {
|
||||||
removeFromInstallQueue(packageName)
|
removeFromInstallQueue(packageName)
|
||||||
handleCallbackUninstall(packageName, returnCode, extra)
|
handleCallbackUninstall(packageName, returnCode, extra)
|
||||||
|
readyWithAction.set(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -135,6 +140,7 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
} catch (e: RemoteException) {
|
} catch (e: RemoteException) {
|
||||||
Log.e("Failed to connect Aurora Services")
|
Log.e("Failed to connect Aurora Services")
|
||||||
removeFromInstallQueue(packageName)
|
removeFromInstallQueue(packageName)
|
||||||
|
readyWithAction.set(true)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
removeFromInstallQueue(packageName)
|
removeFromInstallQueue(packageName)
|
||||||
@@ -143,12 +149,14 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
context.getString(R.string.installer_status_failure),
|
context.getString(R.string.installer_status_failure),
|
||||||
context.getString(R.string.installer_service_misconfigured)
|
context.getString(R.string.installer_service_misconfigured)
|
||||||
)
|
)
|
||||||
|
readyWithAction.set(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onServiceDisconnected(name: ComponentName) {
|
override fun onServiceDisconnected(name: ComponentName) {
|
||||||
removeFromInstallQueue(packageName)
|
removeFromInstallQueue(packageName)
|
||||||
Log.e("Disconnected from Aurora Services")
|
Log.e("Disconnected from Aurora Services")
|
||||||
|
readyWithAction.set(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -160,23 +168,28 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
serviceConnection,
|
serviceConnection,
|
||||||
Context.BIND_AUTO_CREATE
|
Context.BIND_AUTO_CREATE
|
||||||
)
|
)
|
||||||
|
|
||||||
Handler(Looper.getMainLooper()).postDelayed({
|
|
||||||
if (!attachedToServiceTimeout.get()) {
|
|
||||||
removeFromInstallQueue(packageName)
|
|
||||||
}
|
}
|
||||||
}, 25 * 1000)
|
while (!readyWithAction.get()) {
|
||||||
|
Thread.sleep(1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
|
||||||
private fun xInstall(packageName: String, uriList: List<Uri>, fileList: List<String>) {
|
private fun xInstall(packageName: String, uriList: List<Uri>, fileList: List<String>) {
|
||||||
val attachedToServiceTimeout = AtomicBoolean(false)
|
executor.execute {
|
||||||
|
val readyWithAction = AtomicBoolean(false)
|
||||||
AuroraApplication.enqueuedInstalls.add(packageName)
|
Handler(Looper.getMainLooper()).post {
|
||||||
|
|
||||||
serviceConnection = object : ServiceConnection {
|
serviceConnection = object : ServiceConnection {
|
||||||
override fun onServiceConnected(name: ComponentName, binder: IBinder) {
|
override fun onServiceConnected(name: ComponentName, binder: IBinder) {
|
||||||
attachedToServiceTimeout.set(true)
|
if (isAlreadyQueued(packageName)) {
|
||||||
|
if (::serviceConnection.isInitialized) {
|
||||||
|
context.unbindService(serviceConnection)
|
||||||
|
}
|
||||||
|
readyWithAction.set(true)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
AuroraApplication.enqueuedInstalls.add(packageName)
|
||||||
val service = IPrivilegedService.Stub.asInterface(binder)
|
val service = IPrivilegedService.Stub.asInterface(binder)
|
||||||
|
|
||||||
if (service.hasPrivilegedPermissions()) {
|
if (service.hasPrivilegedPermissions()) {
|
||||||
@@ -184,9 +197,7 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
|
|
||||||
val callback = object : IPrivilegedCallback.Stub() {
|
val callback = object : IPrivilegedCallback.Stub() {
|
||||||
|
|
||||||
override fun handleResult(packageName: String, returnCode: Int) {
|
override fun handleResult(packageName: String, returnCode: Int) {}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun handleResultX(
|
override fun handleResultX(
|
||||||
packageName: String,
|
packageName: String,
|
||||||
@@ -195,6 +206,7 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
) {
|
) {
|
||||||
removeFromInstallQueue(packageName)
|
removeFromInstallQueue(packageName)
|
||||||
handleCallback(packageName, returnCode, extra)
|
handleCallback(packageName, returnCode, extra)
|
||||||
|
readyWithAction.set(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -212,6 +224,7 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
} catch (e: RemoteException) {
|
} catch (e: RemoteException) {
|
||||||
removeFromInstallQueue(packageName)
|
removeFromInstallQueue(packageName)
|
||||||
postError(packageName, e.localizedMessage, e.stackTraceToString())
|
postError(packageName, e.localizedMessage, e.stackTraceToString())
|
||||||
|
readyWithAction.set(true)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
throw Exception("New method not implemented")
|
throw Exception("New method not implemented")
|
||||||
@@ -229,6 +242,7 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
} catch (e: RemoteException) {
|
} catch (e: RemoteException) {
|
||||||
removeFromInstallQueue(packageName)
|
removeFromInstallQueue(packageName)
|
||||||
postError(packageName, e.localizedMessage, e.stackTraceToString())
|
postError(packageName, e.localizedMessage, e.stackTraceToString())
|
||||||
|
readyWithAction.set(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -238,11 +252,13 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
context.getString(R.string.installer_status_failure),
|
context.getString(R.string.installer_status_failure),
|
||||||
context.getString(R.string.installer_service_misconfigured)
|
context.getString(R.string.installer_service_misconfigured)
|
||||||
)
|
)
|
||||||
|
readyWithAction.set(true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onServiceDisconnected(name: ComponentName) {
|
override fun onServiceDisconnected(name: ComponentName) {
|
||||||
removeFromInstallQueue(packageName)
|
removeFromInstallQueue(packageName)
|
||||||
|
readyWithAction.set(true)
|
||||||
Log.e("Disconnected from Aurora Services")
|
Log.e("Disconnected from Aurora Services")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -255,16 +271,18 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
serviceConnection,
|
serviceConnection,
|
||||||
Context.BIND_AUTO_CREATE
|
Context.BIND_AUTO_CREATE
|
||||||
)
|
)
|
||||||
Handler(Looper.getMainLooper()).postDelayed({
|
|
||||||
if (!attachedToServiceTimeout.get()) {
|
|
||||||
removeFromInstallQueue(packageName)
|
|
||||||
}
|
}
|
||||||
}, 25 * 1000)
|
while (!readyWithAction.get()) {
|
||||||
|
Thread.sleep(1000)
|
||||||
|
}
|
||||||
|
Log.i("Services Callback : install wait done")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleCallbackUninstall(packageName: String, returnCode: Int, extra: String?) {
|
private fun handleCallbackUninstall(packageName: String, returnCode: Int, extra: String?) {
|
||||||
Log.i("Services Callback : $packageName $returnCode $extra")
|
Log.i("Services Callback : $packageName $returnCode $extra")
|
||||||
|
|
||||||
|
try {
|
||||||
when (returnCode) {
|
when (returnCode) {
|
||||||
PackageInstaller.STATUS_SUCCESS -> {
|
PackageInstaller.STATUS_SUCCESS -> {
|
||||||
EventBus.getDefault().post(
|
EventBus.getDefault().post(
|
||||||
@@ -286,11 +304,15 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
if (::serviceConnection.isInitialized) {
|
if (::serviceConnection.isInitialized) {
|
||||||
context.unbindService(serviceConnection)
|
context.unbindService(serviceConnection)
|
||||||
}
|
}
|
||||||
|
} catch (th: Throwable) {
|
||||||
|
th.printStackTrace()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleCallback(packageName: String, returnCode: Int, extra: String?) {
|
private fun handleCallback(packageName: String, returnCode: Int, extra: String?) {
|
||||||
Log.i("Services Callback : $packageName $returnCode $extra")
|
Log.i("Services Callback : $packageName $returnCode $extra")
|
||||||
|
|
||||||
|
try {
|
||||||
when (returnCode) {
|
when (returnCode) {
|
||||||
PackageInstaller.STATUS_SUCCESS -> {
|
PackageInstaller.STATUS_SUCCESS -> {
|
||||||
EventBus.getDefault().post(
|
EventBus.getDefault().post(
|
||||||
@@ -312,13 +334,20 @@ class ServiceInstaller(context: Context) : InstallerBase(context) {
|
|||||||
if (::serviceConnection.isInitialized) {
|
if (::serviceConnection.isInitialized) {
|
||||||
context.unbindService(serviceConnection)
|
context.unbindService(serviceConnection)
|
||||||
}
|
}
|
||||||
|
} catch (th: Throwable) {
|
||||||
|
th.printStackTrace()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun postError(packageName: String, error: String?, extra: String?) {
|
override fun postError(packageName: String, error: String?, extra: String?) {
|
||||||
|
try {
|
||||||
super.postError(packageName, error, extra)
|
super.postError(packageName, error, extra)
|
||||||
if (::serviceConnection.isInitialized) {
|
if (::serviceConnection.isInitialized) {
|
||||||
context.unbindService(serviceConnection)
|
context.unbindService(serviceConnection)
|
||||||
}
|
}
|
||||||
|
} catch (th: Throwable) {
|
||||||
|
th.printStackTrace()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getUri(file: File): Uri {
|
override fun getUri(file: File): Uri {
|
||||||
|
|||||||
Reference in New Issue
Block a user