I have an app with Device Owner permissions and i'm trying to install another app silently. I'm using PackageInstaller API for this and an apk file which is in my assets folder. its running well but returning an intent with STATUS_FAILURE and message INSTALL_FAILED_INTERNAL_ERROR: Permission Denied.
I followed this answer to write my AppInstaller class:
class AppInstaller {
companion object {
const val ACTION_INSTALL_COMPLETE = "my.package.name.INSTALL_COMPLETE"
}
@Throws(IOException::class)
fun installPackage(context: Context, fileName: String, packageName: String): Boolean {
val input = context.assets.open(fileName)
val packageInstaller = context.packageManager.packageInstaller
val params = PackageInstaller.SessionParams(PackageInstaller.SessionParams.MODE_FULL_INSTALL)
params.setAppPackageName(packageName)
// set params
val sessionId = packageInstaller.createSession(params)
val session = packageInstaller.openSession(sessionId)
val out = session.openWrite("DPC", 0, -1)
val buffer = ByteArray(65536)
var c = input.read(buffer)
while (c != -1) {
out.write(buffer, 0, c)
c = input.read(buffer)
}
session.fsync(out)
input.close()
out.close()
session.commit(createIntentSender(context, sessionId))
return true
}
private fun createIntentSender(context: Context, sessionId: Int): IntentSender {
val pendingIntent = PendingIntent.getBroadcast(context, sessionId, Intent(ACTION_INSTALL_COMPLETE), 0)
return pendingIntent.intentSender
}
}
I tried with and without these permissions:
<uses-permission android:name="android.permission.INSTALL_PACKAGES" tools:ignore="ProtectedPermissions"/>
<uses-permission android:name="android.permission.DELETE_PACKAGES" tools:ignore="ProtectedPermissions"/>
<uses-permission android:name="android.permission.REQUEST_DELETE_PACKAGES"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
the result of this code is true
dpm.isDeviceOwnerApp("my.package.name")
by this aosp code and as describe in android docs:
Committing may require user intervention to complete the installation, unless the caller falls into one of the following categories, in which case the installation will complete automatically.
the device owner
the affiliated profile owner
the device owner delegated app with DevicePolicyManager.DELEGATION_PACKAGE_INSTALLATION
Sessions can install brand new apps, upgrade existing apps, or add new splits into an existing app.
so i should have permissions.
Any ideas why i get this error? thanks.