1 // Copyright 2020 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 
5 package org.chromium.weblayer;
6 
7 import android.os.Bundle;
8 import android.util.AndroidRuntimeException;
9 import android.view.View.OnClickListener;
10 import android.view.View.OnLongClickListener;
11 
12 import androidx.annotation.ColorRes;
13 import androidx.annotation.NonNull;
14 
15 import org.chromium.weblayer_private.interfaces.UrlBarOptionsKeys;
16 
17 /**
18  * Class containing options to tweak the URL bar.
19  */
20 public final class UrlBarOptions {
builder()21     public static Builder builder() {
22         return new Builder();
23     }
24 
25     private Bundle mOptions;
26     private OnClickListener mTextClickListener;
27     private OnLongClickListener mTextLongClickListener;
28 
29     /**
30      * A Builder class to help create UrlBarOptions.
31      */
32     public static final class Builder {
33         private Bundle mOptions;
34         private OnClickListener mTextClickListener;
35         private OnLongClickListener mTextLongClickListener;
36 
Builder()37         private Builder() {
38             mOptions = new Bundle();
39         }
40 
getBundle()41         Bundle getBundle() {
42             return mOptions;
43         }
44 
getTextClickListener()45         OnClickListener getTextClickListener() {
46             return mTextClickListener;
47         }
48 
getTextLongClickListener()49         OnLongClickListener getTextLongClickListener() {
50             return mTextLongClickListener;
51         }
52 
53         /**
54          * Sets the text size of the URL bar.
55          *
56          * @param textSize The desired size of the URL bar text in scalable pixels.
57          * The default is 14.0F and the minimum allowed size is 5.0F.
58          */
59         @NonNull
setTextSizeSP(float textSize)60         public Builder setTextSizeSP(float textSize) {
61             mOptions.putFloat(UrlBarOptionsKeys.URL_TEXT_SIZE, textSize);
62             return this;
63         }
64 
65         /**
66          * Specifies whether the URL text in the URL bar should also show Page Info UI on click.
67          * By default, only the security status icon does so.
68          */
69         @NonNull
showPageInfoWhenTextIsClicked()70         public Builder showPageInfoWhenTextIsClicked() {
71             mOptions.putBoolean(UrlBarOptionsKeys.SHOW_PAGE_INFO_WHEN_URL_TEXT_CLICKED, true);
72             return this;
73         }
74 
75         /**
76          * Specifies whether the publisher URL is shown.
77          */
78         @NonNull
showPublisherUrl()79         public Builder showPublisherUrl() {
80             mOptions.putBoolean(UrlBarOptionsKeys.SHOW_PUBLISHER_URL, true);
81             return this;
82         }
83 
84         /**
85          * Sets the color of the URL bar text.
86          *
87          * @param textColor The color for the Url bar text.
88          */
89         @NonNull
setTextColor(@olorRes int textColor)90         public Builder setTextColor(@ColorRes int textColor) {
91             mOptions.putInt(UrlBarOptionsKeys.URL_TEXT_COLOR, textColor);
92             return this;
93         }
94 
95         /**
96          * Sets the color of the URL bar security status icon.
97          *
98          * @param iconColor The color for the Url bar icon.
99          */
100         @NonNull
setIconColor(@olorRes int iconColor)101         public Builder setIconColor(@ColorRes int iconColor) {
102             mOptions.putInt(UrlBarOptionsKeys.URL_ICON_COLOR, iconColor);
103             return this;
104         }
105 
106         @NonNull
setTextClickListener(@onNull OnClickListener clickListener)107         public Builder setTextClickListener(@NonNull OnClickListener clickListener) {
108             mTextClickListener = clickListener;
109             return this;
110         }
111 
112         @NonNull
setTextLongClickListener(@onNull OnLongClickListener longClickListener)113         public Builder setTextLongClickListener(@NonNull OnLongClickListener longClickListener) {
114             mTextLongClickListener = longClickListener;
115             return this;
116         }
117 
118         /**
119          * Builds a UrlBarOptions object.
120          */
121         @NonNull
build()122         public UrlBarOptions build() {
123             boolean showPageInfoWhenUrlTextClicked = mOptions.getBoolean(
124                     UrlBarOptionsKeys.SHOW_PAGE_INFO_WHEN_URL_TEXT_CLICKED, /*default= */ false);
125             if (mTextClickListener != null && showPageInfoWhenUrlTextClicked) {
126                 throw new AndroidRuntimeException("Text click listener cannot be set when "
127                         + "SHOW_PAGE_INFO_WHEN_URL_TEXT_CLICKED is true.");
128             }
129             return new UrlBarOptions(this);
130         }
131     }
132 
UrlBarOptions(Builder builder)133     private UrlBarOptions(Builder builder) {
134         mOptions = builder.getBundle();
135         mTextClickListener = builder.getTextClickListener();
136         mTextLongClickListener = builder.getTextLongClickListener();
137     }
138 
139     /**
140      * Gets the URL bar options as a Bundle.
141      */
getBundle()142     Bundle getBundle() {
143         return mOptions;
144     }
145 
146     /**
147      * Gets the text size of the URL bar text in scalable pixels.
148      */
getTextSizeSP()149     public float getTextSizeSP() {
150         return mOptions.getFloat(UrlBarOptionsKeys.URL_TEXT_SIZE);
151     }
152 
153     /**
154      * Gets the ClickListener.
155      */
getTextClickListener()156     OnClickListener getTextClickListener() {
157         return mTextClickListener;
158     }
159 
160     /**
161      * Gets the LongClickListener.
162      */
getTextLongClickListener()163     OnLongClickListener getTextLongClickListener() {
164         return mTextLongClickListener;
165     }
166 }
167