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

Commit ec09e15b authored by Ivan Chiang's avatar Ivan Chiang
Browse files

[PM] Add app snippet on the uninstallation error dialogs

Flag: android.content.pm.use_pia_v2
Test: manual
Bug: 274120822
Change-Id: I4780d48ae768d397b95727d4fd1cc36fa68f3973
parent c08efc96
Loading
Loading
Loading
Loading
+30 −25
Original line number Diff line number Diff line
@@ -54,7 +54,6 @@ import com.android.packageinstaller.v2.model.PackageUtil.getMaxTargetSdkVersionF
import com.android.packageinstaller.v2.model.PackageUtil.getPackageNameForUid
import com.android.packageinstaller.v2.model.PackageUtil.isPermissionGranted
import com.android.packageinstaller.v2.model.PackageUtil.isProfileOfOrSame
import com.android.packageinstaller.v2.model.UninstallAborted.Companion.ABORT_REASON_UNINSTALL_DONE
import android.content.pm.Flags as PmFlags
import android.multiuser.Flags as MultiuserFlags

@@ -134,10 +133,34 @@ class UninstallRepository(private val context: Context) {
            return UninstallAborted(UninstallAborted.ABORT_REASON_APP_UNAVAILABLE)
        }

        callback = intent.getParcelableExtra(
            PackageInstaller.EXTRA_CALLBACK, PackageManager.UninstallCompleteCallback::class.java
        )

        try {
            targetAppInfo = packageManager.getApplicationInfo(
                targetPackageName!!,
                ApplicationInfoFlags.of(
                    PackageManager.MATCH_ANY_USER.toLong() or
                            PackageManager.MATCH_ARCHIVED_PACKAGES
                )
            )
        } catch (e: PackageManager.NameNotFoundException) {
            Log.e(LOG_TAG, "Unable to get packageName")
        }

        if (targetAppInfo == null) {
            Log.e(LOG_TAG, "Invalid packageName: $targetPackageName")
            return UninstallAborted(UninstallAborted.ABORT_REASON_APP_UNAVAILABLE)
        }

        uninstallFromAllUsers = intent.getBooleanExtra(Intent.EXTRA_UNINSTALL_ALL_USERS, false)
        if (uninstallFromAllUsers && !userManager!!.isAdminUser) {
            Log.e(LOG_TAG, "Only admin user can request uninstall for all users")
            return UninstallAborted(UninstallAborted.ABORT_REASON_USER_NOT_ALLOWED)
            return UninstallAborted(
                UninstallAborted.ABORT_REASON_USER_NOT_ALLOWED,
                getAppSnippet(context, targetAppInfo!!)
            )
        }

        uninstalledUser = intent.getParcelableExtra(Intent.EXTRA_USER, UserHandle::class.java)
@@ -153,30 +176,12 @@ class UninstallRepository(private val context: Context) {
                        "User " + Process.myUserHandle() + " can't request uninstall " +
                                "for user " + uninstalledUser
                    )
                    return UninstallAborted(UninstallAborted.ABORT_REASON_USER_NOT_ALLOWED)
                }
            }
        }

        callback = intent.getParcelableExtra(
            PackageInstaller.EXTRA_CALLBACK, PackageManager.UninstallCompleteCallback::class.java
        )

        try {
            targetAppInfo = packageManager.getApplicationInfo(
                targetPackageName!!,
                ApplicationInfoFlags.of(
                    PackageManager.MATCH_ANY_USER.toLong() or
                            PackageManager.MATCH_ARCHIVED_PACKAGES
                    return UninstallAborted(
                        UninstallAborted.ABORT_REASON_USER_NOT_ALLOWED,
                        getAppSnippet(context, targetAppInfo!!)
                    )
            )
        } catch (e: PackageManager.NameNotFoundException) {
            Log.e(LOG_TAG, "Unable to get packageName")
                }

        if (targetAppInfo == null) {
            Log.e(LOG_TAG, "Invalid packageName: $targetPackageName")
            return UninstallAborted(UninstallAborted.ABORT_REASON_APP_UNAVAILABLE)
            }
        }

        // The class name may have been specified (e.g. when deleting an app from all apps)
@@ -533,7 +538,7 @@ class UninstallRepository(private val context: Context) {
            callback!!.onUninstallComplete(targetPackageName!!, legacyStatus, message)

            // Since the caller already received the results, just finish the app at this point
            uninstallResult.value = UninstallAborted(ABORT_REASON_UNINSTALL_DONE)
            uninstallResult.value = UninstallAborted(UninstallAborted.ABORT_REASON_UNINSTALL_DONE)
            return
        }
        val returnResult = intent.getBooleanExtra(Intent.EXTRA_RETURN_RESULT, false)
+10 −2
Original line number Diff line number Diff line
@@ -84,12 +84,20 @@ data class UninstallFailed(
    }
}

data class UninstallAborted(val abortReason: Int) : UninstallStage(STAGE_ABORTED) {

data class UninstallAborted(
    val abortReason: Int,
    val appSnippet: PackageUtil.AppSnippet? = null
) : UninstallStage(STAGE_ABORTED) {
    var dialogTitleResource = 0
    var dialogTextResource = 0
    val activityResultCode = Activity.RESULT_FIRST_USER

    val appIcon: Drawable?
        get() = appSnippet?.icon

    val appLabel: String?
        get() = appSnippet?.let { appSnippet.label as String? }

    init {
        when (abortReason) {
            ABORT_REASON_APP_UNAVAILABLE -> {
+8 −1
Original line number Diff line number Diff line
@@ -129,10 +129,17 @@ public class UninstallationFragment extends DialogFragment {
    }

    private void updateUninstallAbortedUI(Dialog dialog, UninstallAborted uninstallStage) {
        mAppSnippet.setVisibility(View.GONE);
        mKeepDataLayout.setVisibility(View.GONE);
        mCustomMessageTextView.setVisibility(View.VISIBLE);

        if (uninstallStage.getAppIcon() != null) {
            mAppSnippet.setVisibility(View.VISIBLE);
            mAppIcon.setImageDrawable(uninstallStage.getAppIcon());
            mAppLabelTextView.setText(uninstallStage.getAppLabel());
        } else {
            mAppSnippet.setVisibility(View.GONE);
        }

        // Set the title and the message
        dialog.setTitle(uninstallStage.getDialogTitleResource());
        mCustomMessageTextView.setText(uninstallStage.getDialogTextResource());