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

Commit 2e84dd7e authored by Sumedh Sen's avatar Sumedh Sen
Browse files

[piav2] Hide the open button when launcher activity is disabled

If the newly installed app cannot be opened, do not show the launch
button to prevent confusing the users.

Bug: 316619479
Test: atest CtsPackageInstallTestCases:IntentTest
Change-Id: Ie135fe480b5b28208133eecc0df2a9839ef93714
parent 02f5de97
Loading
Loading
Loading
Loading
+11 −1
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import android.content.pm.PackageInstaller
import android.content.pm.PackageInstaller.SessionInfo
import android.content.pm.PackageInstaller.SessionParams
import android.content.pm.PackageManager
import android.content.pm.PackageManager.COMPONENT_ENABLED_STATE_DISABLED
import android.net.Uri
import android.os.ParcelFileDescriptor
import android.os.Process
@@ -830,7 +831,8 @@ class InstallRepository(private val context: Context) {
            val resultIntent = if (shouldReturnResult) {
                Intent().putExtra(Intent.EXTRA_INSTALL_RESULT, PackageManager.INSTALL_SUCCEEDED)
            } else {
                packageManager.getLaunchIntentForPackage(newPackageInfo!!.packageName)
                val intent = packageManager.getLaunchIntentForPackage(newPackageInfo!!.packageName)
                if (isLauncherActivityEnabled(intent)) intent else null
            }
            _installResult.setValue(InstallSuccess(appSnippet, shouldReturnResult, resultIntent))
        } else {
@@ -838,6 +840,14 @@ class InstallRepository(private val context: Context) {
        }
    }

    private fun isLauncherActivityEnabled(intent: Intent?): Boolean {
        if (intent == null || intent.component == null) {
            return false
        }
        return (intent.component?.let { packageManager.getComponentEnabledSetting(it) }
            != COMPONENT_ENABLED_STATE_DISABLED)
    }

    /**
     * Cleanup the staged session. Also signal the packageinstaller that an install session is to
     * be aborted
+17 −10
Original line number Diff line number Diff line
@@ -23,13 +23,13 @@ import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import com.android.packageinstaller.R;
import com.android.packageinstaller.v2.model.InstallStage;
import com.android.packageinstaller.v2.model.InstallSuccess;
import com.android.packageinstaller.v2.ui.InstallActionListener;
import java.util.List;
@@ -40,6 +40,7 @@ import java.util.List;
 */
public class InstallSuccessFragment extends DialogFragment {

    private static final String LOG_TAG = InstallSuccessFragment.class.getSimpleName();
    private final InstallSuccess mDialogData;
    private AlertDialog mDialog;
    private InstallActionListener mInstallActionListener;
@@ -60,12 +61,15 @@ public class InstallSuccessFragment extends DialogFragment {
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        View dialogView = getLayoutInflater().inflate(R.layout.install_content_view, null);
        mDialog = new AlertDialog.Builder(requireContext()).setTitle(mDialogData.getAppLabel())
            .setIcon(mDialogData.getAppIcon()).setView(dialogView).setNegativeButton(R.string.done,
        mDialog = new AlertDialog.Builder(requireContext())
            .setTitle(mDialogData.getAppLabel())
            .setIcon(mDialogData.getAppIcon())
            .setView(dialogView)
            .setNegativeButton(R.string.done,
                (dialog, which) -> mInstallActionListener.onNegativeResponse(
                    InstallStage.STAGE_SUCCESS))
            .setPositiveButton(R.string.launch, (dialog, which) -> {
            }).create();
                    mDialogData.getStageCode()))
            .setPositiveButton(R.string.launch, (dialog, which) -> {})
            .create();

        dialogView.requireViewById(R.id.install_success).setVisibility(View.VISIBLE);

@@ -76,25 +80,28 @@ public class InstallSuccessFragment extends DialogFragment {
    public void onStart() {
        super.onStart();
        Button launchButton = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        boolean enabled = false;
        boolean visible = false;
        if (mDialogData.getResultIntent() != null) {
            List<ResolveInfo> list = mPm.queryIntentActivities(mDialogData.getResultIntent(), 0);
            if (list.size() > 0) {
                enabled = true;
                visible = true;
            }
        }
        if (enabled) {
        if (visible) {
            launchButton.setOnClickListener(view -> {
                Log.i(LOG_TAG, "Finished installing and launching " +
                    mDialogData.getAppLabel());
                mInstallActionListener.openInstalledApp(mDialogData.getResultIntent());
            });
        } else {
            launchButton.setEnabled(false);
            launchButton.setVisibility(View.GONE);
        }
    }

    @Override
    public void onCancel(@NonNull DialogInterface dialog) {
        super.onCancel(dialog);
        Log.i(LOG_TAG, "Finished installing " + mDialogData.getAppLabel());
        mInstallActionListener.onNegativeResponse(mDialogData.getStageCode());
    }
}