Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 20c4f7fb authored by Sumedh Sen's avatar Sumedh Sen
Browse files

[pia v2] Show a user confirmation dialog only when update-ownership isn't changed

Bug: 319320680
Test: atest CtsPackageInstallTestCases:UpdateOwnershipEnforcementTest
Change-Id: I3e2053fa9911ac98892bd5bc5202c0dcb9100543
parent f8f7410e
Loading
Loading
Loading
Loading
+35 −9
Original line number Diff line number Diff line
@@ -420,25 +420,48 @@ class InstallRepository(private val context: Context) {
     *      * If AppOP is granted and user action is required to proceed with install
     *      * If AppOp grant is to be requested from the user
     */
    fun requestUserConfirmation(): InstallStage {
    fun requestUserConfirmation(): InstallStage? {
        return if (isTrustedSource) {
            if (localLogv) {
                Log.i(LOG_TAG, "Install allowed")
            }
            // Returns InstallUserActionRequired stage if install details could be successfully
            // computed, else it returns InstallAborted.
            generateConfirmationSnippet()
            maybeDeferUserConfirmation()
        } else {
            val unknownSourceStage = handleUnknownSources(appOpRequestInfo)
            if (unknownSourceStage.stageCode == InstallStage.STAGE_READY) {
                // Source app already has appOp granted.
                generateConfirmationSnippet()
                maybeDeferUserConfirmation()
            } else {
                unknownSourceStage
            }
        }
    }

    /**
     *  If the update-owner for the incoming app is being changed, defer confirming with the
     *  user and directly proceed with the install. The system will request another
     *  user confirmation shortly.
     */
    private fun maybeDeferUserConfirmation(): InstallStage? {
        // Returns InstallUserActionRequired stage if install details could be successfully
        // computed, else it returns InstallAborted.
        val confirmationSnippet: InstallStage = generateConfirmationSnippet()

        val existingUpdateOwner: CharSequence? = getExistingUpdateOwner(newPackageInfo!!)
        return if (sessionId == SessionInfo.INVALID_ID &&
            !TextUtils.isEmpty(existingUpdateOwner) &&
            !TextUtils.equals(existingUpdateOwner, callingPackage)
        ) {
            // Since update ownership is being changed, the system will request another
            // user confirmation shortly. Thus, we don't need to ask the user to confirm
            // installation here.
            initiateInstall()
            null
        } else {
            confirmationSnippet
        }
    }

    private fun generateConfirmationSnippet(): InstallStage {
        val packageSource: Any?
        val pendingUserActionReason: Int
@@ -639,11 +662,14 @@ class InstallRepository(private val context: Context) {
    }

    private fun getExistingUpdateOwnerLabel(pkgInfo: PackageInfo): CharSequence? {
        return getApplicationLabel(getExistingUpdateOwner(pkgInfo))
    }

    private fun getExistingUpdateOwner(pkgInfo: PackageInfo): String? {
        return try {
            val packageName = pkgInfo.packageName
            val sourceInfo = packageManager.getInstallSourceInfo(packageName)
            val existingUpdateOwner = sourceInfo.updateOwnerPackageName
            getApplicationLabel(existingUpdateOwner)
            sourceInfo.updateOwnerPackageName
        } catch (e: PackageManager.NameNotFoundException) {
            null
        }
@@ -894,8 +920,8 @@ class InstallRepository(private val context: Context) {
     * When the identity of the install source could not be determined, user can skip checking the
     * source and directly proceed with the install.
     */
    fun forcedSkipSourceCheck(): InstallStage {
        return generateConfirmationSnippet()
    fun forcedSkipSourceCheck(): InstallStage? {
        return maybeDeferUserConfirmation()
    }

    val stagingProgress: LiveData<Int>