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

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

[PM] Support material AlertDialog (2/N)

Use MaterialAlertDialog in AnonymousSource dialog and ExternalSources
dialog.

Flag: android.content.pm.use_pia_v2
Test: manual
Bug: 274120822

Change-Id: I67b5e0f286ab8a3120b2c27953f1399833087486
parent 7f8afc0f
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -45,8 +45,8 @@
    </style>

    <style name="Theme.MaterialAlertDialog.Variant" parent="@style/Theme.MaterialAlertDialog.System">
        <item name="android:buttonBarPositiveButtonStyle">@style/Widget.PackageInstaller.Material.Button</item>
        <item name="android:buttonBarNegativeButtonStyle">@style/Widget.PackageInstaller.Material.Button</item>
        <item name="buttonBarPositiveButtonStyle">@style/Widget.PackageInstaller.Material.Button</item>
        <item name="buttonBarNegativeButtonStyle">@style/Widget.PackageInstaller.Material.Button</item>
    </style>

    <style name="Theme.UninstallerActivity"
+23 −2
Original line number Diff line number Diff line
@@ -49,6 +49,14 @@ public class UiUtil {
        }
    }

    /** If material design is enabled, return the material theme resource id of the text button.
     * Otherwise, return {@code 0} to use the default theme.
     */
    public static int getTextButtonThemeResId(@NonNull Context context) {
        return PackageUtil.isMaterialDesignEnabled(context)
                ? R.style.Theme_MaterialAlertDialog_Variant : 0;
    }

    /**
     * Gets the positive button in the {@code dialog}. Returns null if the specified
     * button does not exist or the dialog has not yet been fully created.
@@ -84,15 +92,28 @@ public class UiUtil {
            @NonNull View contentView, int positiveBtnTextResId, int negativeBtnTextResId,
            @Nullable DialogInterface.OnClickListener positiveBtnListener,
            @Nullable DialogInterface.OnClickListener negativeBtnListener) {
        return getAlertDialog(context, title, contentView, positiveBtnTextResId,
                negativeBtnTextResId, positiveBtnListener, negativeBtnListener,
                /* themeResId= */ 0);
    }

    /**
     * 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, int positiveBtnTextResId,
            int negativeBtnTextResId, @Nullable DialogInterface.OnClickListener positiveBtnListener,
            @Nullable DialogInterface.OnClickListener negativeBtnListener, int themeResId) {
        if (PackageUtil.isMaterialDesignEnabled(context)) {
            return new MaterialAlertDialogBuilder(context)
            return new MaterialAlertDialogBuilder(context, themeResId)
                    .setTitle(title)
                    .setView(contentView)
                    .setPositiveButton(positiveBtnTextResId, positiveBtnListener)
                    .setNegativeButton(negativeBtnTextResId, negativeBtnListener)
                    .create();
        } else {
            return new AlertDialog.Builder(context)
            return new AlertDialog.Builder(context, themeResId)
                    .setTitle(title)
                    .setView(contentView)
                    .setPositiveButton(positiveBtnTextResId, positiveBtnListener)
+25 −24
Original line number Diff line number Diff line
@@ -16,13 +16,13 @@

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

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
@@ -31,8 +31,8 @@ import androidx.fragment.app.DialogFragment;
import com.android.packageinstaller.R;
import com.android.packageinstaller.v2.model.InstallStage;
import com.android.packageinstaller.v2.model.InstallUserActionRequired;
import com.android.packageinstaller.v2.model.PackageUtil;
import com.android.packageinstaller.v2.ui.InstallActionListener;
import com.android.packageinstaller.v2.ui.UiUtil;

/**
 * Dialog to show when the source of apk can not be identified.
@@ -43,7 +43,7 @@ public class AnonymousSourceFragment extends DialogFragment {
    @NonNull
    private InstallActionListener mInstallActionListener;
    @NonNull
    private AlertDialog mDialog;
    private Dialog mDialog;

    @Override
    public void onAttach(@NonNull Context context) {
@@ -56,28 +56,20 @@ public class AnonymousSourceFragment extends DialogFragment {
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Log.i(LOG_TAG, "Creating " + LOG_TAG);

        View dialogView = getLayoutInflater().inflate(R.layout.install_fragment_layout, null);
        View dialogView = getLayoutInflater().inflate(
                UiUtil.getInstallationLayoutResId(requireContext()), null);
        TextView customMessage = dialogView.requireViewById(R.id.custom_message);
        customMessage.setText(R.string.message_anonymous_source_warning);
        customMessage.setVisibility(View.VISIBLE);

        int themeResId = 0;
        // The base theme inherits a deviceDefault theme. Applying a material style on the base
        // theme to support the material design.
        if (PackageUtil.isMaterialDesignEnabled(requireContext())) {
            Log.d(LOG_TAG, "Apply material design");
            themeResId = R.style.Theme_MaterialAlertDialog_Variant;
        }

        mDialog = new AlertDialog.Builder(requireContext(), themeResId)
                .setTitle(R.string.title_anonymous_source_warning)
                .setView(dialogView)
                .setPositiveButton(R.string.button_continue,
        mDialog = UiUtil.getAlertDialog(requireContext(),
                getString(R.string.title_anonymous_source_warning), dialogView,
                R.string.button_continue, R.string.button_cancel,
                ((dialog, which) -> mInstallActionListener.onPositiveResponse(
                                InstallUserActionRequired.USER_ACTION_REASON_ANONYMOUS_SOURCE)))
                .setNegativeButton(R.string.button_cancel,
                        InstallUserActionRequired.USER_ACTION_REASON_ANONYMOUS_SOURCE)),
                ((dialog, which) -> mInstallActionListener.onNegativeResponse(
                                InstallStage.STAGE_USER_ACTION_REQUIRED))).create();
                        InstallStage.STAGE_USER_ACTION_REQUIRED)),
                UiUtil.getTextButtonThemeResId(requireContext()));
        return mDialog;
    }

@@ -90,7 +82,10 @@ public class AnonymousSourceFragment extends DialogFragment {
    @Override
    public void onStart() {
        super.onStart();
        mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true);
        Button button = UiUtil.getAlertDialogPositiveButton(mDialog);
        if (button != null) {
            button.setFilterTouchesWhenObscured(true);
        }
    }

    @Override
@@ -98,12 +93,18 @@ public class AnonymousSourceFragment extends DialogFragment {
        super.onPause();
        // This prevents tapjacking since an overlay activity started in front of Pia will
        // cause Pia to be paused.
        mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
        Button button = UiUtil.getAlertDialogPositiveButton(mDialog);
        if (button != null) {
            button.setEnabled(false);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
        Button button = UiUtil.getAlertDialogPositiveButton(mDialog);
        if (button != null) {
            button.setEnabled(true);
        }
    }
}
+25 −25
Original line number Diff line number Diff line
@@ -20,13 +20,13 @@ import static com.android.packageinstaller.v2.model.PackageUtil.ARGS_ACTION_REAS
import static com.android.packageinstaller.v2.model.PackageUtil.ARGS_APP_SNIPPET;
import static com.android.packageinstaller.v2.model.PackageUtil.ARGS_SOURCE_PKG;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

@@ -36,9 +36,9 @@ import androidx.fragment.app.DialogFragment;

import com.android.packageinstaller.R;
import com.android.packageinstaller.v2.model.InstallUserActionRequired;
import com.android.packageinstaller.v2.model.PackageUtil;
import com.android.packageinstaller.v2.model.PackageUtil.AppSnippet;
import com.android.packageinstaller.v2.ui.InstallActionListener;
import com.android.packageinstaller.v2.ui.UiUtil;

/**
 * Dialog to show when the installing app is an unknown source and needs AppOp grant to install
@@ -52,7 +52,7 @@ public class ExternalSourcesBlockedFragment extends DialogFragment {
    @NonNull
    private InstallActionListener mInstallActionListener;
    @NonNull
    private AlertDialog mDialog;
    private Dialog mDialog;

    public ExternalSourcesBlockedFragment() {
        // Required for DialogFragment
@@ -88,7 +88,8 @@ public class ExternalSourcesBlockedFragment extends DialogFragment {
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
        setDialogData(requireArguments());

        View dialogView = getLayoutInflater().inflate(R.layout.install_fragment_layout, null);
        View dialogView = getLayoutInflater().inflate(
                UiUtil.getInstallationLayoutResId(requireContext()), null);

        dialogView.requireViewById(R.id.app_snippet).setVisibility(View.VISIBLE);
        ((ImageView) dialogView.requireViewById(R.id.app_icon))
@@ -101,24 +102,14 @@ public class ExternalSourcesBlockedFragment extends DialogFragment {

        Log.i(LOG_TAG, "Creating " + LOG_TAG + "\n" + mDialogData);

        int themeResId = 0;
        // The base theme inherits a deviceDefault theme. Applying a material style on the base
        // theme to support the material design.
        if (PackageUtil.isMaterialDesignEnabled(requireContext())) {
            Log.d(LOG_TAG, "Apply material design");
            themeResId = R.style.Theme_MaterialAlertDialog_Variant;
        }

        mDialog = new AlertDialog.Builder(requireContext(), themeResId)
                .setTitle(R.string.title_unknown_source_blocked)
                .setView(dialogView)
                .setPositiveButton(R.string.external_sources_settings,
        mDialog = UiUtil.getAlertDialog(requireContext(),
                getString(R.string.title_unknown_source_blocked), dialogView,
                R.string.external_sources_settings, R.string.cancel,
                (dialog, which) -> mInstallActionListener.sendUnknownAppsIntent(
                            mDialogData.getUnknownSourcePackageName()))
                .setNegativeButton(R.string.cancel,
                        mDialogData.getUnknownSourcePackageName()),
                (dialog, which) -> mInstallActionListener.onNegativeResponse(
                                mDialogData.getStageCode()))
                .create();
                        mDialogData.getStageCode()),
                UiUtil.getTextButtonThemeResId(requireContext()));
        return mDialog;
    }

@@ -131,7 +122,10 @@ public class ExternalSourcesBlockedFragment extends DialogFragment {
    @Override
    public void onStart() {
        super.onStart();
        mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setFilterTouchesWhenObscured(true);
        Button button = UiUtil.getAlertDialogPositiveButton(mDialog);
        if (button != null) {
            button.setFilterTouchesWhenObscured(true);
        }
    }

    @Override
@@ -139,13 +133,19 @@ public class ExternalSourcesBlockedFragment extends DialogFragment {
        super.onPause();
        // This prevents tapjacking since an overlay activity started in front of Pia will
        // cause Pia to be paused.
        mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
        Button button = UiUtil.getAlertDialogPositiveButton(mDialog);
        if (button != null) {
            button.setEnabled(false);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        mDialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
        Button button = UiUtil.getAlertDialogPositiveButton(mDialog);
        if (button != null) {
            button.setEnabled(true);
        }
    }

    private void setDialogData(Bundle args) {