1414import android .util .AttributeSet ;
1515import android .util .TypedValue ;
1616import android .view .GestureDetector ;
17- import android .view .LayoutInflater ;
1817import android .view .MotionEvent ;
1918import android .view .View ;
20- import android .view .ViewGroup ;
2119import android .view .animation .Animation ;
2220import android .view .animation .AnimationUtils ;
2321import android .view .inputmethod .EditorInfo ;
2422import android .widget .FrameLayout ;
25- import android .widget .ImageButton ;
2623import android .widget .ImageView ;
2724import android .widget .RelativeLayout ;
2825
26+ import androidx .annotation .NonNull ;
27+ import androidx .annotation .StringRes ;
28+
29+ import org .mozilla .geckoview .GeckoSessionSettings ;
2930import org .mozilla .vrbrowser .R ;
3031import org .mozilla .vrbrowser .audio .AudioEngine ;
3132import org .mozilla .vrbrowser .browser .BookmarksStore ;
3233import org .mozilla .vrbrowser .browser .SessionStore ;
34+ import org .mozilla .vrbrowser .browser .SettingsStore ;
3335import org .mozilla .vrbrowser .search .SearchEngineWrapper ;
3436import org .mozilla .vrbrowser .telemetry .TelemetryWrapper ;
37+ import org .mozilla .vrbrowser .ui .widgets .WidgetPlacement ;
3538import org .mozilla .vrbrowser .utils .StringUtils ;
3639import org .mozilla .vrbrowser .utils .UIThreadExecutor ;
3740import org .mozilla .vrbrowser .utils .UrlUtils ;
4144import java .net .URL ;
4245import java .net .URLDecoder ;
4346
44- import androidx .annotation .StringRes ;
4547import kotlin .Unit ;
4648import mozilla .components .browser .domains .autocomplete .DomainAutocompleteResult ;
4749import mozilla .components .browser .domains .autocomplete .ShippedDomainsProvider ;
5052
5153public class NavigationURLBar extends FrameLayout {
5254 private InlineAutocompleteEditText mURL ;
53- private ImageButton mMicrophoneButton ;
55+ private UIButton mMicrophoneButton ;
56+ private UIButton mUAModeButton ;
5457 private ImageView mInsecureIcon ;
5558 private ImageView mLoadingView ;
5659 private Animation mLoadingAnimation ;
@@ -62,10 +65,11 @@ public class NavigationURLBar extends FrameLayout {
6265 private int mURLWebsiteColor ;
6366 private NavigationURLBarDelegate mDelegate ;
6467 private ShippedDomainsProvider mAutocompleteProvider ;
65- private ImageButton mBookmarkButton ;
68+ private UIButton mBookmarkButton ;
6669 private AudioEngine mAudio ;
6770 private boolean mIsBookmarkMode ;
6871 private boolean mBookmarkEnabled = true ;
72+ private boolean mIsContextButtonsEnabled = true ;
6973 private UIThreadExecutor mUIThreadExecutor = new UIThreadExecutor ();
7074
7175 private Unit domainAutocompleteFilter (String text ) {
@@ -99,7 +103,6 @@ private void initialize(Context aContext) {
99103 mAudio = AudioEngine .fromContext (aContext );
100104
101105 // Inflate this data binding layout
102- LayoutInflater inflater = LayoutInflater .from (aContext );
103106 inflate (aContext , R .layout .navigation_url , this );
104107
105108 // Use Domain autocomplete provider from components
@@ -119,6 +122,7 @@ private void initialize(Context aContext) {
119122
120123 mURL .setOnFocusChangeListener ((view , focused ) -> {
121124 showVoiceSearch (!focused || (mURL .getText ().length () == 0 ));
125+ showContextButtons (!focused && mIsContextButtonsEnabled );
122126
123127 mURL .setSelection (mURL .getText ().length (), 0 );
124128 });
@@ -142,6 +146,12 @@ private void initialize(Context aContext) {
142146 mMicrophoneButton = findViewById (R .id .microphoneButton );
143147 mMicrophoneButton .setTag (R .string .view_id_tag , R .id .microphoneButton );
144148 mMicrophoneButton .setOnClickListener (mMicrophoneListener );
149+
150+ mUAModeButton = findViewById (R .id .uaModeButton );
151+ mUAModeButton .setTag (R .string .view_id_tag , R .id .uaModeButton );
152+ mUAModeButton .setOnClickListener (mUAModeListener );
153+ setUAMode (SettingsStore .getInstance (aContext ).getUaMode ());
154+
145155 mURLLeftContainer = findViewById (R .id .urlLeftContainer );
146156 mInsecureIcon = findViewById (R .id .insecureIcon );
147157 mLoadingView = findViewById (R .id .loadingView );
@@ -178,7 +188,6 @@ public void onResume() {
178188 if (mIsLoading ) {
179189 mLoadingView .startAnimation (mLoadingAnimation );
180190 }
181-
182191 }
183192
184193 public void setDelegate (NavigationURLBarDelegate delegate ) {
@@ -192,27 +201,19 @@ public void setIsBookmarkMode(boolean isBookmarkMode) {
192201 mIsBookmarkMode = isBookmarkMode ;
193202 if (isBookmarkMode ) {
194203 mMicrophoneButton .setVisibility (GONE );
204+ mUAModeButton .setVisibility (GONE );
195205 mBookmarkButton .setVisibility (GONE );
206+
196207 } else {
197208 mMicrophoneButton .setVisibility (VISIBLE );
209+ mUAModeButton .setVisibility (VISIBLE );
198210 if (mBookmarkEnabled ) {
199211 mBookmarkButton .setVisibility (VISIBLE );
200212 }
201213 }
202214 syncViews ();
203215 }
204216
205- private void setBookmarkEnabled (boolean aEnabled ) {
206- if (mBookmarkEnabled != aEnabled ) {
207- mBookmarkEnabled = aEnabled ;
208- mBookmarkButton .setVisibility (aEnabled ? View .VISIBLE : View .GONE );
209- ViewGroup .LayoutParams params = mMicrophoneButton .getLayoutParams ();
210- params .width = (int ) getResources ().getDimension (aEnabled ? R .dimen .url_bar_item_width : R .dimen .url_bar_last_item_width );
211- mMicrophoneButton .setLayoutParams (params );
212- mMicrophoneButton .setBackgroundResource (aEnabled ? R .drawable .url_button : R .drawable .url_button_end );
213- }
214- }
215-
216217 private void handleBookmarkClick () {
217218 if (mAudio != null ) {
218219 mAudio .playSound (AudioEngine .Sound .CLICK );
@@ -293,12 +294,17 @@ else if (aURL.startsWith("data:") && SessionStore.get().isCurrentSessionPrivate(
293294 mURL .setText (aURL );
294295 }
295296 }
296- setBookmarkEnabled (aURL .length () > 0 && !aURL .startsWith ("about://" ));
297+ mIsContextButtonsEnabled = aURL .length () > 0 && !aURL .startsWith ("about://" );
298+ showContextButtons (mIsContextButtonsEnabled );
297299 }
298300
299301 mURL .addTextChangedListener (mURLTextWatcher );
300302 }
301303
304+ private boolean isEmptyUrl (@ NonNull String aURL ) {
305+ return aURL .length () == 0 || aURL .startsWith ("about://" );
306+ }
307+
302308 public String getText () {
303309 return mURL .getText ().toString ();
304310 }
@@ -331,32 +337,48 @@ public void setIsLoading(boolean aIsLoading) {
331337 }
332338 }
333339
340+ public void setUAMode (int uaMode ) {
341+ if (uaMode == GeckoSessionSettings .USER_AGENT_MODE_DESKTOP ) {
342+ mUAModeButton .setImageResource (R .drawable .ic_icon_ua_desktop );
343+
344+ } else {
345+ mUAModeButton .setImageResource (R .drawable .ic_icon_ua_default );
346+ }
347+ }
348+
349+ private void showContextButtons (boolean aEnabled ) {
350+ if (mBookmarkEnabled != aEnabled ) {
351+ mBookmarkEnabled = aEnabled ;
352+ }
353+
354+ if (aEnabled ) {
355+ mMicrophoneButton .setBackgroundResource (R .drawable .url_button );
356+ mMicrophoneButton .getLayoutParams ().width = (int )getContext ().getResources ().getDimension (R .dimen .url_bar_item_width );
357+ mBookmarkButton .setVisibility (VISIBLE );
358+ mUAModeButton .setVisibility (VISIBLE );
359+
360+ } else {
361+ mMicrophoneButton .setBackgroundResource (R .drawable .url_button_end );
362+ mMicrophoneButton .getLayoutParams ().width = (int )getContext ().getResources ().getDimension (R .dimen .url_bar_last_item_width );
363+ mBookmarkButton .setVisibility (GONE );
364+ mUAModeButton .setVisibility (GONE );
365+ }
366+ }
367+
334368 public void showVoiceSearch (boolean enabled ) {
335369 if (enabled ) {
336- if (mBookmarkEnabled ) {
337- mMicrophoneButton .setBackgroundResource (R .drawable .url_button );
338- mMicrophoneButton .getLayoutParams ().width = (int )getContext ().getResources ().getDimension (R .dimen .url_bar_item_width );
339- }
370+ mURL .setPadding (mURL .getPaddingStart (), mURL .getPaddingTop (), WidgetPlacement .convertDpToPixel (getContext (), 100 ), mURL .getPaddingBottom ());
371+
340372 mMicrophoneButton .setImageResource (R .drawable .ic_icon_microphone );
373+ mMicrophoneButton .setTooltip (getResources ().getString (R .string .voice_search_tooltip ));
341374 mMicrophoneButton .setOnClickListener (mMicrophoneListener );
342375
343- if (mIsBookmarkMode ) {
344- mMicrophoneButton .setVisibility (GONE );
345- } else if (mBookmarkEnabled ) {
346- mBookmarkButton .setVisibility (VISIBLE );
347- }
348-
349376 } else if (mURL .hasFocus ()){
377+ mURL .setPadding (mURL .getPaddingStart (), mURL .getPaddingTop (), WidgetPlacement .convertDpToPixel (getContext (), 40 ), mURL .getPaddingBottom ());
378+
350379 mMicrophoneButton .setImageResource (R .drawable .ic_icon_clear );
351- mMicrophoneButton .setBackgroundResource (R .drawable .url_button_end );
352- mMicrophoneButton .getLayoutParams ().width = (int )getContext ().getResources ().getDimension (R .dimen .url_bar_last_item_width );
380+ mMicrophoneButton .setTooltip (getResources ().getString (R .string .clear_tooltip ));
353381 mMicrophoneButton .setOnClickListener (mClearListener );
354-
355- if (mIsBookmarkMode ) {
356- mMicrophoneButton .setVisibility (VISIBLE );
357- }
358-
359- mBookmarkButton .setVisibility (GONE );
360382 }
361383 }
362384
@@ -449,6 +471,26 @@ public void setClickable(boolean clickable) {
449471 TelemetryWrapper .voiceInputEvent ();
450472 };
451473
474+ private OnClickListener mUAModeListener = view -> {
475+ if (mAudio != null ) {
476+ mAudio .playSound (AudioEngine .Sound .CLICK );
477+ }
478+
479+ view .requestFocusFromTouch ();
480+
481+ int uaMode = SessionStore .get ().getUaMode ();
482+ if (uaMode == GeckoSessionSettings .USER_AGENT_MODE_VR ) {
483+ setUAMode (GeckoSessionSettings .USER_AGENT_MODE_DESKTOP );
484+ SessionStore .get ().setUaMode (GeckoSessionSettings .USER_AGENT_MODE_DESKTOP );
485+
486+ }else {
487+ setUAMode (GeckoSessionSettings .USER_AGENT_MODE_VR );
488+ SessionStore .get ().setUaMode (GeckoSessionSettings .USER_AGENT_MODE_VR );
489+ }
490+
491+ TelemetryWrapper .voiceInputEvent ();
492+ };
493+
452494 private OnClickListener mClearListener = view -> {
453495 if (mAudio != null ) {
454496 mAudio .playSound (AudioEngine .Sound .CLICK );
@@ -465,12 +507,9 @@ public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2)
465507
466508 @ Override
467509 public void onTextChanged (CharSequence charSequence , int i , int i1 , int i2 ) {
468- if (mURL .getText ().length () > 0 ) {
469- showVoiceSearch (false );
470-
471- } else {
472- showVoiceSearch (true );
473- }
510+ String aURL = mURL .getText ().toString ();
511+ showVoiceSearch (isEmptyUrl (aURL ));
512+ showContextButtons (isEmptyUrl (aURL ) && mIsContextButtonsEnabled );
474513 }
475514
476515 @ Override
0 commit comments