2121import org .mozilla .vrbrowser .ui .widgets .WidgetManagerDelegate ;
2222import org .mozilla .vrbrowser .ui .widgets .WidgetPlacement ;
2323import org .mozilla .vrbrowser .utils .StringUtils ;
24- import org .mozilla .vrbrowser .utils .UrlUtils ;
2524
2625import java .util .ArrayList ;
2726import java .util .function .Predicate ;
@@ -88,22 +87,26 @@ public void setContextElement(ContextElement aContextElement) {
8887 mItems = new ArrayList <>();
8988 final WidgetManagerDelegate widgetManager = mWidgetManager ;
9089 if (aContextElement .linkUri != null && !aContextElement .linkUri .isEmpty ()) {
90+ // Link url
9191 mItems .add (new MenuWidget .MenuItem (aContextElement .linkUri , 0 , null ));
92+ // Open link in a new window
9293 if (mWidgetManager .canOpenNewWindow ()) {
93- mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_open_new_window_1 ), 0 , () -> {
94+ mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_open_link_new_window_1 ), 0 , () -> {
9495 if (!StringUtils .isEmpty (aContextElement .linkUri )) {
9596 widgetManager .openNewWindow (aContextElement .linkUri );
9697 }
9798 onDismiss ();
9899 }));
99100 }
100- mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_open_new_tab_1 ), 0 , () -> {
101+ // Open link in a new tab
102+ mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_open_link_new_tab_1 ), 0 , () -> {
101103 if (!StringUtils .isEmpty (aContextElement .linkUri )) {
102104 widgetManager .openNewTab (aContextElement .linkUri );
103105 GleanMetricsService .Tabs .openedCounter (GleanMetricsService .Tabs .TabSource .CONTEXT_MENU );
104106 }
105107 onDismiss ();
106108 }));
109+ // Download link
107110 if (!StringUtils .isEmpty (aContextElement .linkUri )) {
108111 mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_download_link ), 0 , () -> {
109112 DownloadJob job = DownloadJob .fromLink (aContextElement );
@@ -112,42 +115,66 @@ public void setContextElement(ContextElement aContextElement) {
112115 onDismiss ();
113116 }));
114117 }
118+ // Copy link uri
119+ mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_copy_link ), 0 , () -> {
120+ ClipboardManager clipboard = (ClipboardManager ) getContext ().getSystemService (Context .CLIPBOARD_SERVICE );
121+ Uri uri = Uri .parse (aContextElement .linkUri );
122+ if (uri != null ) {
123+ String label = aContextElement .title ;
124+ if (StringUtils .isEmpty (label )) {
125+ label = aContextElement .altText ;
126+ }
127+ if (StringUtils .isEmpty (label )) {
128+ label = aContextElement .altText ;
129+ }
130+ if (StringUtils .isEmpty (label )) {
131+ label = uri .toString ();
132+ }
133+ ClipData clip = ClipData .newRawUri (label , uri );
134+ if (clipboard != null ) {
135+ clipboard .setPrimaryClip (clip );
136+ }
137+ }
138+ onDismiss ();
139+ }));
140+
115141 } else {
142+ // If there is no link, show src uri instead
116143 mItems .add (new MenuWidget .MenuItem (aContextElement .srcUri , 0 , null ));
117144 }
118- if (URLUtil .isNetworkUrl (aContextElement .srcUri )) {
119- @ StringRes int srcText ;
120- switch (aContextElement .type ) {
121- case ContextElement .TYPE_IMAGE :
122- srcText = R .string .context_menu_download_image ;
123- break ;
124- case ContextElement .TYPE_VIDEO :
125- srcText = R .string .context_menu_download_video ;
126- break ;
127- case ContextElement .TYPE_AUDIO :
128- srcText = R .string .context_menu_download_audio ;
129- break ;
130- default :
131- srcText = R .string .context_menu_download_link ;
132- break ;
145+
146+ if (URLUtil .isNetworkUrl (aContextElement .srcUri ) && aContextElement .type != ContextElement .TYPE_NONE ) {
147+ @ StringRes int copyText = R .string .context_menu_copy_image_location ;
148+ @ StringRes int srcText = R .string .context_menu_download_image ;
149+ @ StringRes int viewText = R .string .context_menu_view_image ;
150+ if (aContextElement .type == ContextElement .TYPE_VIDEO ) {
151+ srcText = R .string .context_menu_download_video ;
152+ copyText = R .string .context_menu_copy_video_location ;
153+ viewText = R .string .context_menu_view_video ;
154+
155+ } else if (aContextElement .type == ContextElement .TYPE_AUDIO ) {
156+ srcText = R .string .context_menu_download_audio ;
157+ copyText = R .string .context_menu_copy_audio_location ;
158+ viewText = R .string .context_menu_view_audio ;
159+ }
160+ // View src
161+ if (aContextElement .baseUri != null && !aContextElement .baseUri .equals (aContextElement .srcUri )) {
162+ mItems .add (new MenuWidget .MenuItem (getContext ().getString (viewText ), 0 , () -> {
163+ widgetManager .getFocusedWindow ().getSession ().loadUri (aContextElement .srcUri );
164+ onDismiss ();
165+ }));
133166 }
167+ // Download src
134168 mItems .add (new MenuWidget .MenuItem (getContext ().getString (srcText ), 0 , () -> {
135169 DownloadJob job = DownloadJob .fromSrc (aContextElement );
136170 widgetManager .getFocusedWindow ().startDownload (job , false );
137171 // TODO Add Download from context menu Telemetry
138172 onDismiss ();
139173 }));
140- }
141- if (URLUtil .isNetworkUrl (aContextElement .linkUri ) || URLUtil .isNetworkUrl (aContextElement .srcUri )) {
142- mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_copy_link ), 0 , () -> {
174+ // Copy src uri
175+ mItems .add (new MenuWidget .MenuItem (getContext ().getString (copyText ), 0 , () -> {
143176 ClipboardManager clipboard = (ClipboardManager ) getContext ().getSystemService (Context .CLIPBOARD_SERVICE );
144- Uri uri ;
145- if (aContextElement .linkUri != null ) {
146- uri = Uri .parse (aContextElement .linkUri );
147-
148- } else {
149- uri = Uri .parse (aContextElement .srcUri );
150- }
177+ Uri uri = Uri .parse (aContextElement .srcUri );
151178 if (uri != null ) {
152179 String label = aContextElement .title ;
153180 if (StringUtils .isEmpty (label )) {
@@ -174,4 +201,8 @@ public void setContextElement(ContextElement aContextElement) {
174201 mWidgetPlacement .height += 10.0f ; // Link separator
175202 }
176203
204+ private void addClipboardClip (ContextElement aContextElement ) {
205+
206+ }
207+
177208}
0 commit comments