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

Commit a1a68048 authored by Alan Viverette's avatar Alan Viverette
Browse files

Fix alert dialog icon

Previously, failing to call setIcon() would result in a blank icon
rather than hiding the icon. Also, calling setIcon(icon) followed by
setIcon(null) could be a no-op depending on whether the alert had
already been constructed.

Change-Id: I65a96a4e89b9eac1123cbbf5d57e7e366e7b4d4e
parent 6d50c3ff
Loading
Loading
Loading
Loading
+35 −26
Original line number Original line Diff line number Diff line
@@ -102,7 +102,7 @@ public class AlertController {


    private ScrollView mScrollView;
    private ScrollView mScrollView;
    
    
    private int mIconId = -1;
    private int mIconId = 0;
    
    
    private Drawable mIcon;
    private Drawable mIcon;
    
    
@@ -337,25 +337,39 @@ public class AlertController {
    }
    }


    /**
    /**
     * Set resId to 0 if you don't want an icon.
     * Specifies the icon to display next to the alert title.
     * @param resId the resourceId of the drawable to use as the icon or 0
     *
     * if you don't want an icon.
     * @param resId the resource identifier of the drawable to use as the icon,
     *            or 0 for no icon
     */
     */
    public void setIcon(int resId) {
    public void setIcon(int resId) {
        mIcon = null;
        mIconId = resId;
        mIconId = resId;

        if (mIconView != null) {
        if (mIconView != null) {
            if (resId > 0) {
            if (resId != 0) {
                mIconView.setImageResource(mIconId);
                mIconView.setImageResource(mIconId);
            } else if (resId == 0) {
            } else {
                mIconView.setVisibility(View.GONE);
                mIconView.setVisibility(View.GONE);
            }
            }
        }
        }
    }
    }


    /**
     * Specifies the icon to display next to the alert title.
     *
     * @param icon the drawable to use as the icon or null for no icon
     */
    public void setIcon(Drawable icon) {
    public void setIcon(Drawable icon) {
        mIcon = icon;
        mIcon = icon;
        if ((mIconView != null) && (mIcon != null)) {
        mIconId = 0;

        if (mIconView != null) {
            if (icon != null) {
                mIconView.setImageDrawable(icon);
                mIconView.setImageDrawable(icon);
            } else {
                mIconView.setVisibility(View.GONE);
            }
        }
        }
    }
    }


@@ -485,28 +499,24 @@ public class AlertController {
            View titleTemplate = mWindow.findViewById(R.id.title_template);
            View titleTemplate = mWindow.findViewById(R.id.title_template);
            titleTemplate.setVisibility(View.GONE);
            titleTemplate.setVisibility(View.GONE);
        } else {
        } else {
            final boolean hasTextTitle = !TextUtils.isEmpty(mTitle);
            
            mIconView = (ImageView) mWindow.findViewById(R.id.icon);
            mIconView = (ImageView) mWindow.findViewById(R.id.icon);

            final boolean hasTextTitle = !TextUtils.isEmpty(mTitle);
            if (hasTextTitle) {
            if (hasTextTitle) {
                /* Display the title if a title is supplied, else hide it */
                // Display the title if a title is supplied, else hide it.
                mTitleView = (TextView) mWindow.findViewById(R.id.alertTitle);
                mTitleView = (TextView) mWindow.findViewById(R.id.alertTitle);

                mTitleView.setText(mTitle);
                mTitleView.setText(mTitle);


                /* Do this last so that if the user has supplied any
                // Do this last so that if the user has supplied any icons we
                 * icons we use them instead of the default ones. If the
                // use them instead of the default ones. If the user has
                 * user has specified 0 then make it disappear.
                // specified 0 then make it disappear.
                 */
                if (mIconId != 0) {
                if (mIconId > 0) {
                    mIconView.setImageResource(mIconId);
                    mIconView.setImageResource(mIconId);
                } else if (mIcon != null) {
                } else if (mIcon != null) {
                    mIconView.setImageDrawable(mIcon);
                    mIconView.setImageDrawable(mIcon);
                } else if (mIconId == 0) {
                } else {
                    
                    // Apply the padding from the icon to ensure the title is
                    /* Apply the padding from the icon to ensure the
                    // aligned correctly.
                     * title is aligned correctly.
                     */
                    mTitleView.setPadding(mIconView.getPaddingLeft(),
                    mTitleView.setPadding(mIconView.getPaddingLeft(),
                            mIconView.getPaddingTop(),
                            mIconView.getPaddingTop(),
                            mIconView.getPaddingRight(),
                            mIconView.getPaddingRight(),
@@ -514,9 +524,8 @@ public class AlertController {
                    mIconView.setVisibility(View.GONE);
                    mIconView.setVisibility(View.GONE);
                }
                }
            } else {
            } else {
                
                // Hide the title template
                // Hide the title template
                View titleTemplate = mWindow.findViewById(R.id.title_template);
                final View titleTemplate = mWindow.findViewById(R.id.title_template);
                titleTemplate.setVisibility(View.GONE);
                titleTemplate.setVisibility(View.GONE);
                mIconView.setVisibility(View.GONE);
                mIconView.setVisibility(View.GONE);
                topPanel.setVisibility(View.GONE);
                topPanel.setVisibility(View.GONE);