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

Commit 0a23b227 authored by Ivan Chiang's avatar Ivan Chiang
Browse files

[PM] Support material AlertDialog (11/N)

Use MaterialAlertDialog in InstallInstalling, InstallStaging and
InstallSuccess.

Flag: android.content.pm.use_pia_v2
Test: manual
Bug: 274120822
Change-Id: Ia2889547c12474eea636661a4edd6b7ff83b7ec3
parent 51210042
Loading
Loading
Loading
Loading
+22 −5
Original line number Diff line number Diff line
@@ -86,6 +86,17 @@ public class UiUtil {
        }
    }

    /**
     * If material design is enabled, return the MaterialAlertDialog. Otherwise, return the
     * system AlertDialog.
     */
    public static Dialog getAlertDialog(@NonNull Context context, @NonNull String title,
            @NonNull View contentView) {
        return getAlertDialog(context, title, contentView, /* positiveBtnText= */ null,
                /* negativeBtnText= */ null, /* positiveBtnListener= */ null,
                /* negativeBtnListener= */ null, /* themeResId= */ 0);
    }

    /**
     * If material design is enabled, return the MaterialAlertDialog. Otherwise, return the
     * system AlertDialog.
@@ -139,26 +150,32 @@ public class UiUtil {
     */
    public static Dialog getAlertDialog(@NonNull Context context,
            @NonNull String title, @NonNull View contentView, @Nullable String positiveBtnText,
            @NonNull String negativeBtnText,
            @Nullable String negativeBtnText,
            @Nullable DialogInterface.OnClickListener positiveBtnListener,
            @Nullable DialogInterface.OnClickListener negativeBtnListener, int themeResId) {
        if (PackageUtil.isMaterialDesignEnabled(context)) {
            MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(context, themeResId)
                    .setTitle(title)
                    .setView(contentView)
                    .setNegativeButton(negativeBtnText, negativeBtnListener);
                    .setView(contentView);
            if (positiveBtnText != null) {
                builder.setPositiveButton(positiveBtnText, positiveBtnListener);
            }

            if (negativeBtnText != null) {
                builder.setNegativeButton(negativeBtnText, negativeBtnListener);
            }
            return builder.create();
        } else {
            AlertDialog.Builder builder = new AlertDialog.Builder(context, themeResId)
                    .setTitle(title)
                    .setView(contentView)
                    .setNegativeButton(negativeBtnText, negativeBtnListener);
                    .setView(contentView);
            if (positiveBtnText != null) {
                builder.setPositiveButton(positiveBtnText, positiveBtnListener);
            }

            if (negativeBtnText != null) {
                builder.setNegativeButton(negativeBtnText, negativeBtnListener);
            }
            return builder.create();
        }
    }
+4 −14
Original line number Diff line number Diff line
@@ -19,9 +19,7 @@ package com.android.packageinstaller.v2.ui.fragments;
import static com.android.packageinstaller.v2.model.PackageUtil.ARGS_APP_SNIPPET;
import static com.android.packageinstaller.v2.model.PackageUtil.ARGS_IS_UPDATING;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
@@ -44,7 +42,7 @@ public class InstallInstallingFragment extends DialogFragment {

    private static final String LOG_TAG = InstallInstallingFragment.class.getSimpleName();
    private InstallInstalling mDialogData;
    private AlertDialog mDialog;
    private Dialog mDialog;

    public InstallInstallingFragment() {
        // Required for DialogFragment
@@ -83,23 +81,15 @@ public class InstallInstallingFragment extends DialogFragment {
            .setImageDrawable(mDialogData.getAppIcon());
        ((TextView) dialogView.requireViewById(R.id.app_label)).setText(mDialogData.getAppLabel());

        mDialog = new AlertDialog.Builder(requireContext())
            .setTitle(
                mDialogData.isAppUpdating() ? R.string.title_updating : R.string.title_installing)
            .setView(dialogView)
            .create();
        final int titleResId =
                mDialogData.isAppUpdating() ? R.string.title_updating : R.string.title_installing;
        mDialog = UiUtil.getAlertDialog(requireContext(), getString(titleResId), dialogView);

        this.setCancelable(false);

        return mDialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        mDialog.getButton(DialogInterface.BUTTON_NEGATIVE).setEnabled(false);
    }

    private void setDialogData(Bundle args) {
        AppSnippet appSnippet = args.getParcelable(ARGS_APP_SNIPPET, AppSnippet.class);
        boolean isAppUpdating = args.getBoolean(ARGS_IS_UPDATING);
+12 −19
Original line number Diff line number Diff line
@@ -16,7 +16,6 @@

package com.android.packageinstaller.v2.ui.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
@@ -37,7 +36,7 @@ public class InstallStagingFragment extends DialogFragment {

    private static final String LOG_TAG = InstallStagingFragment.class.getSimpleName();
    private ProgressBar mProgressBar;
    private AlertDialog mDialog;
    private Dialog mDialog;
    @NonNull
    private InstallActionListener mInstallActionListener;

@@ -54,29 +53,23 @@ public class InstallStagingFragment extends DialogFragment {

        View dialogView = getLayoutInflater().inflate(
                UiUtil.getInstallationLayoutResId(requireContext()), null);
        dialogView.requireViewById(R.id.progress_bar).setVisibility(View.VISIBLE);
        mProgressBar = dialogView.requireViewById(R.id.progress_bar);
        mProgressBar.setVisibility(View.VISIBLE);
        mProgressBar.setProgress(0);
        mProgressBar.setMax(100);
        mProgressBar.setIndeterminate(false);

        mDialog = new AlertDialog.Builder(requireContext())
            .setTitle(R.string.title_install_staging)
            .setView(dialogView)
            .setNegativeButton(R.string.button_cancel, (dialog, which) ->
                mInstallActionListener.onNegativeResponse(InstallStage.STAGE_STAGING))
            .setCancelable(false)
            .create();
        mDialog = UiUtil.getAlertDialog(requireContext(), getString(R.string.title_install_staging),
                dialogView, /* positiveBtnText= */ null, getString(R.string.button_cancel),
                /* positiveBtnListener= */ null,
                (dialog, which) ->
                        mInstallActionListener.onNegativeResponse(InstallStage.STAGE_STAGING));

        mDialog.setCanceledOnTouchOutside(false);
        this.setCancelable(false);
        return mDialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        mProgressBar = mDialog.requireViewById(R.id.progress_bar);
        mProgressBar.setProgress(0);
        mProgressBar.setMax(100);
        mProgressBar.setIndeterminate(false);
    }

    public void setProgress(int progress) {
        if (mProgressBar != null) {
            mProgressBar.setProgress(progress);
+19 −31
Original line number Diff line number Diff line
@@ -21,7 +21,6 @@ import static com.android.packageinstaller.v2.model.PackageUtil.ARGS_IS_UPDATING
import static com.android.packageinstaller.v2.model.PackageUtil.ARGS_RESULT_INTENT;
import static com.android.packageinstaller.v2.model.PackageUtil.ARGS_SHOULD_RETURN_RESULT;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
@@ -31,7 +30,6 @@ import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

@@ -43,6 +41,7 @@ import com.android.packageinstaller.R;
import com.android.packageinstaller.v2.model.InstallSuccess;
import com.android.packageinstaller.v2.model.PackageUtil.AppSnippet;
import com.android.packageinstaller.v2.ui.InstallActionListener;
import com.android.packageinstaller.v2.ui.UiUtil;

import java.util.List;

@@ -54,7 +53,7 @@ public class InstallSuccessFragment extends DialogFragment {

    private static final String LOG_TAG = InstallSuccessFragment.class.getSimpleName();
    private InstallSuccess mDialogData;
    private AlertDialog mDialog;
    private Dialog mDialog;
    private InstallActionListener mInstallActionListener;
    private PackageManager mPm;

@@ -101,39 +100,28 @@ public class InstallSuccessFragment extends DialogFragment {
            .setImageDrawable(mDialogData.getAppIcon());
        ((TextView) dialogView.requireViewById(R.id.app_label)).setText(mDialogData.getAppLabel());

        mDialog = new AlertDialog.Builder(requireContext())
            .setTitle(
                mDialogData.isAppUpdating() ? R.string.title_updated : R.string.title_installed)
            .setView(dialogView)
            .setNegativeButton(R.string.button_done,
                (dialog, which) -> mInstallActionListener.onNegativeResponse(
                    mDialogData.getStageCode()))
            .setPositiveButton(R.string.button_open, (dialog, which) -> {})
            .create();

        return mDialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Button launchButton = mDialog.getButton(DialogInterface.BUTTON_POSITIVE);
        boolean visible = false;
        String positiveBtnText = null;
        DialogInterface.OnClickListener positiveBtnListener = null;
        if (mDialogData.getResultIntent() != null) {
            List<ResolveInfo> list = mPm.queryIntentActivities(mDialogData.getResultIntent(), 0);
            if (list.size() > 0) {
                visible = true;
            }
        }
        if (visible) {
            launchButton.setOnClickListener(view -> {
                Log.i(LOG_TAG, "Finished installing and launching " +
                    mDialogData.getAppLabel());
                positiveBtnText = getString(R.string.button_open);
                positiveBtnListener = (dialog, which) -> {
                    Log.i(LOG_TAG, "Finished installing and launching "
                            + mDialogData.getAppLabel());
                    mInstallActionListener.openInstalledApp(mDialogData.getResultIntent());
            });
        } else {
            launchButton.setVisibility(View.GONE);
                };
            }
        }

        final int titleResId =
                mDialogData.isAppUpdating() ? R.string.title_updated : R.string.title_installed;
        mDialog = UiUtil.getAlertDialog(requireContext(), getString(titleResId), dialogView,
                positiveBtnText, getString(R.string.button_done), positiveBtnListener,
                (dialog, which) -> mInstallActionListener.onNegativeResponse(
                        mDialogData.getStageCode()));

        return mDialog;
    }

    @Override