1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 package org.chromium.chrome.browser.password_manager;
5 
6 import androidx.annotation.IdRes;
7 import androidx.annotation.Nullable;
8 
9 import org.chromium.base.Callback;
10 import org.chromium.ui.modaldialog.ModalDialogManager;
11 
12 /**
13  * Class containing all data that customizes the contents displayed in the dialog.
14  */
15 public class PasswordManagerDialogContents {
16     /**
17      * Helper class for range.
18      * TODO(crbug.com/1041591): Replace it with android.util.Range once the minimum API level is 21.
19      */
20     public static class BoldRange {
21         public final int start;
22         public final int end;
BoldRange(int start, int end)23         BoldRange(int start, int end) {
24             assert (start <= end);
25             this.start = start;
26             this.end = end;
27         }
28     }
29 
30     private final String mTitle;
31     private final String mDetails;
32     private final String mPrimaryButtonText;
33     private final @Nullable String mSecondaryButtonText;
34     private final @IdRes int mIllustrationId;
35     private final Callback<Integer> mButtonClickCallback;
36 
37     private boolean mPrimaryButtonFilled;
38     private @Nullable Runnable mHelpButtonCallback;
39     private @ModalDialogManager.ModalDialogType int mDialogType;
40     private BoldRange[] mBoldRanges;
41 
42     /**
43      * Constructor for the dialog contents.
44      *
45      * @param title The title of the dialog, to be displayed below the image.
46      * @param details The details text to be displayed under the title.
47      * @param illustrationId The resource id of the image displayed above the title.
48      * @param primaryButtonText The text of the primary button.
49      * @param secondaryButtonText The text of the secondary button or null if there shouldn't be a
50      *      secondary button.
51      * @param buttonClickCallback The callback handling the click on the buttons. It takes the type
52      *      of the button as a parameter.
53      */
PasswordManagerDialogContents(String title, String details, int illustrationId, String primaryButtonText, @Nullable String secondaryButtonText, Callback<Integer> buttonClickCallback)54     public PasswordManagerDialogContents(String title, String details, int illustrationId,
55             String primaryButtonText, @Nullable String secondaryButtonText,
56             Callback<Integer> buttonClickCallback) {
57         mTitle = title;
58         mDetails = details;
59         mPrimaryButtonText = primaryButtonText;
60         mSecondaryButtonText = secondaryButtonText;
61         mIllustrationId = illustrationId;
62         mButtonClickCallback = buttonClickCallback;
63         mPrimaryButtonFilled = false;
64         mHelpButtonCallback = null;
65         mDialogType = ModalDialogManager.ModalDialogType.APP;
66         mBoldRanges = new BoldRange[] {};
67     }
68 
69     /**
70      * Sets whether or not the primary button should be displayed as filled.
71      */
setPrimaryButtonFilled(boolean primaryButtonFilled)72     public void setPrimaryButtonFilled(boolean primaryButtonFilled) {
73         mPrimaryButtonFilled = primaryButtonFilled;
74     }
75 
76     /**
77      * Sets a callback to be invoked when the help button is clicked. If left null, no help button
78      * will be displayed.
79      */
setHelpButtonCallback(Runnable helpButtonCallback)80     public void setHelpButtonCallback(Runnable helpButtonCallback) {
81         mHelpButtonCallback = helpButtonCallback;
82     }
83 
84     /**
85      * Sets type of the modal dialog to be displayed: app or tab modal.
86      */
setDialogType(@odalDialogManager.ModalDialogType int type)87     public void setDialogType(@ModalDialogManager.ModalDialogType int type) {
88         mDialogType = type;
89     }
90 
91     /**
92      * Sets the bold ranges in the dialog details.
93      * @param boldStartRanges The start positions of bold spans in dialog details, inclusive.
94      * @param boldEndRanges The end positions of bold spans in dialog details, exclusive.
95      */
setBoldRanges(int[] boldStartRanges, int[] boldEndRanges)96     public void setBoldRanges(int[] boldStartRanges, int[] boldEndRanges) {
97         assert (boldStartRanges.length == boldEndRanges.length);
98         mBoldRanges = new BoldRange[boldStartRanges.length];
99         for (int i = 0; i < boldStartRanges.length; i++) {
100             mBoldRanges[i] = new BoldRange(boldStartRanges[i], boldEndRanges[i]);
101         }
102     }
103 
104     /**
105      * Returns the title of the dialog. It is also used as content description.
106      */
getTitle()107     public String getTitle() {
108         return mTitle;
109     }
110 
111     /**
112      * Returns the details to be displayed in the dialog under the title.
113      */
getDetails()114     public String getDetails() {
115         return mDetails;
116     }
117 
118     /**
119      * Returns the text displayed in the primary button.
120      */
getPrimaryButtonText()121     public String getPrimaryButtonText() {
122         return mPrimaryButtonText;
123     }
124 
125     /**
126      * Returns the text displayed in the secondary button or null if the dialog has only one button.
127      */
128     @Nullable
getSecondaryButtonText()129     public String getSecondaryButtonText() {
130         return mSecondaryButtonText;
131     }
132 
133     /**
134      * The resource id of the image displayed above the title.
135      */
getIllustrationId()136     public @IdRes int getIllustrationId() {
137         return mIllustrationId;
138     }
139 
140     /**
141      * The callback invoked when either of the two dialog buttons is clicked.
142      * @return A {@link Callback} taking an {@link Integer} as a parameter which represents which
143      *         button was clicked.
144      */
getButtonClickCallback()145     public Callback getButtonClickCallback() {
146         return mButtonClickCallback;
147     }
148 
149     /**
150      * Whether the primary button should be displayed as filled or not.
151      */
isPrimaryButtonFilled()152     public boolean isPrimaryButtonFilled() {
153         return mPrimaryButtonFilled;
154     }
155 
156     /**
157      * Returns the callback handling the click on the help icon or null if no help icon should be
158      * displayed.
159      */
160     @Nullable
getHelpButtonCallback()161     public Runnable getHelpButtonCallback() {
162         return mHelpButtonCallback;
163     }
164 
165     /**
166      * The type of the dialog: app modal or tab modal.
167      */
getDialogType()168     public @ModalDialogManager.ModalDialogType int getDialogType() {
169         return mDialogType;
170     }
171 
172     /**
173      * Returns the bold ranges in the details text.
174      */
getBoldRanges()175     public BoldRange[] getBoldRanges() {
176         return mBoldRanges;
177     }
178 }
179