@@ -87,7 +87,9 @@ public void setContextElement(ContextElement aContextElement) {
8787 mItems = new ArrayList <>();
8888 final WidgetManagerDelegate widgetManager = mWidgetManager ;
8989 if (aContextElement .linkUri != null && !aContextElement .linkUri .isEmpty ()) {
90+ // Link url
9091 mItems .add (new MenuWidget .MenuItem (aContextElement .linkUri , 0 , null ));
92+ // Open link in a new window
9193 if (mWidgetManager .canOpenNewWindow ()) {
9294 mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_open_link_new_window_1 ), 0 , () -> {
9395 if (!StringUtils .isEmpty (aContextElement .linkUri )) {
@@ -96,13 +98,15 @@ public void setContextElement(ContextElement aContextElement) {
9698 onDismiss ();
9799 }));
98100 }
101+ // Open link in a new tab
99102 mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_open_link_new_tab_1 ), 0 , () -> {
100103 if (!StringUtils .isEmpty (aContextElement .linkUri )) {
101104 widgetManager .openNewTab (aContextElement .linkUri );
102105 GleanMetricsService .Tabs .openedCounter (GleanMetricsService .Tabs .TabSource .CONTEXT_MENU );
103106 }
104107 onDismiss ();
105108 }));
109+ // Download link
106110 if (!StringUtils .isEmpty (aContextElement .linkUri )) {
107111 mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_download_link ), 0 , () -> {
108112 DownloadJob job = DownloadJob .fromLink (aContextElement );
@@ -111,66 +115,66 @@ public void setContextElement(ContextElement aContextElement) {
111115 onDismiss ();
112116 }));
113117 }
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+
114141 } else {
142+ // If there is no link, show src uri instead
115143 mItems .add (new MenuWidget .MenuItem (aContextElement .srcUri , 0 , null ));
116144 }
117- if (URLUtil .isNetworkUrl (aContextElement .srcUri )) {
118- @ StringRes int srcText ;
119- switch (aContextElement .type ) {
120- case ContextElement .TYPE_IMAGE :
121- srcText = R .string .context_menu_download_image ;
122- break ;
123- case ContextElement .TYPE_VIDEO :
124- srcText = R .string .context_menu_download_video ;
125- break ;
126- case ContextElement .TYPE_AUDIO :
127- srcText = R .string .context_menu_download_audio ;
128- break ;
129- default :
130- srcText = R .string .context_menu_download_link ;
131- 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 ;
132159 }
133- mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_open_new_tab_1 ), 0 , () -> {
134- if (!StringUtils .isEmpty (aContextElement .srcUri )) {
135- widgetManager .openNewTab (aContextElement .srcUri );
136- GleanMetricsService .Tabs .openedCounter (GleanMetricsService .Tabs .TabSource .CONTEXT_MENU );
137- }
138- onDismiss ();
139- }));
140- if (URLUtil .isHttpUrl (aContextElement .srcUri ) || URLUtil .isHttpsUrl (aContextElement .srcUri )) {
141- @ StringRes int srcText ;
142- switch (aContextElement .type ) {
143- case ContextElement .TYPE_IMAGE :
144- srcText = R .string .context_menu_download_image ;
145- break ;
146- case ContextElement .TYPE_VIDEO :
147- srcText = R .string .context_menu_download_video ;
148- break ;
149- case ContextElement .TYPE_AUDIO :
150- srcText = R .string .context_menu_download_audio ;
151- break ;
152- default :
153- srcText = R .string .context_menu_download_link ;
154- break ;
155- }
156- mItems .add (new MenuWidget .MenuItem (getContext ().getString (srcText ), 0 , () -> {
157- DownloadJob job = DownloadJob .fromSrc (aContextElement );
158- widgetManager .getFocusedWindow ().startDownload (job , false );
159- // TODO Add Download from context menu Telemetry
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 );
160164 onDismiss ();
161165 }));
162166 }
163- }
164- if (URLUtil .isNetworkUrl (aContextElement .linkUri ) || URLUtil .isNetworkUrl (aContextElement .srcUri )) {
165- mItems .add (new MenuWidget .MenuItem (getContext ().getString (R .string .context_menu_copy_link ), 0 , () -> {
167+ // Download src
168+ mItems .add (new MenuWidget .MenuItem (getContext ().getString (srcText ), 0 , () -> {
169+ DownloadJob job = DownloadJob .fromSrc (aContextElement );
170+ widgetManager .getFocusedWindow ().startDownload (job , false );
171+ // TODO Add Download from context menu Telemetry
172+ onDismiss ();
173+ }));
174+ // Copy src uri
175+ mItems .add (new MenuWidget .MenuItem (getContext ().getString (copyText ), 0 , () -> {
166176 ClipboardManager clipboard = (ClipboardManager ) getContext ().getSystemService (Context .CLIPBOARD_SERVICE );
167- Uri uri ;
168- if (aContextElement .linkUri != null ) {
169- uri = Uri .parse (aContextElement .linkUri );
170-
171- } else {
172- uri = Uri .parse (aContextElement .srcUri );
173- }
177+ Uri uri = Uri .parse (aContextElement .srcUri );
174178 if (uri != null ) {
175179 String label = aContextElement .title ;
176180 if (StringUtils .isEmpty (label )) {
@@ -197,4 +201,8 @@ public void setContextElement(ContextElement aContextElement) {
197201 mWidgetPlacement .height += 10.0f ; // Link separator
198202 }
199203
204+ private void addClipboardClip (ContextElement aContextElement ) {
205+
206+ }
207+
200208}
0 commit comments