Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 5463c83

Browse files
authored
Support for opening elements in new tabs (#3223)
1 parent d8133b0 commit 5463c83

2 files changed

Lines changed: 83 additions & 28 deletions

File tree

app/src/common/shared/org/mozilla/vrbrowser/ui/widgets/menus/ContextMenuWidget.java

Lines changed: 59 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import org.mozilla.vrbrowser.ui.widgets.WidgetManagerDelegate;
2222
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;
2323
import org.mozilla.vrbrowser.utils.StringUtils;
24-
import org.mozilla.vrbrowser.utils.UrlUtils;
2524

2625
import java.util.ArrayList;
2726
import 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
}

app/src/main/res/values/strings.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,21 @@
12571257
<!-- This string is shown in the context menu after a longpress on a link. When clicked it opens the target url in a new tab. -->
12581258
<string name="context_menu_open_new_tab_1">Open in a new tab</string>
12591259

1260+
<!-- This string is shown in the context menu after a longpress on a link. When clicked it opens the target url in a new window. -->
1261+
<string name="context_menu_open_link_new_window_1">Open link in a new window</string>
1262+
1263+
<!-- This string is shown in the context menu after a longpress on a link. When clicked it opens the target url in a new tab. -->
1264+
<string name="context_menu_open_link_new_tab_1">Open link in a new tab</string>
1265+
1266+
<!-- This string is shown in the context menu after a longpress on a link. When clicked it opens the image in the current window. -->
1267+
<string name="context_menu_view_image">View image</string>
1268+
1269+
<!-- This string is shown in the context menu after a longpress on a link. When clicked it opens the video in the current window. -->
1270+
<string name="context_menu_view_video">View video</string>
1271+
1272+
<!-- This string is shown in the context menu after a longpress on a link. When clicked it opens the audio in the current window. -->
1273+
<string name="context_menu_view_audio">View audio</string>
1274+
12601275
<!-- This string is shown in the context menu after a longpress on a link. When clicked it triggers a linked element download. -->
12611276
<string name="context_menu_download_link">Download Link</string>
12621277

@@ -1272,6 +1287,15 @@
12721287
<!-- This string is shown in the context menu after a longpress on a link. When clicked it copies the target to the clipboard -->
12731288
<string name="context_menu_copy_link">Copy link</string>
12741289

1290+
<!-- This string is shown in the context menu after a longpress on a link. When clicked it copies the image uri to the clipboard -->
1291+
<string name="context_menu_copy_image_location">Copy image location</string>
1292+
1293+
<!-- This string is shown in the context menu after a longpress on a link. When clicked it copies the video uri to the clipboard -->
1294+
<string name="context_menu_copy_video_location">Copy video location</string>
1295+
1296+
<!-- This string is shown in the context menu after a longpress on a link. When clicked it copies the audio uri to the clipboard -->
1297+
<string name="context_menu_copy_audio_location">Copy audio location</string>
1298+
12751299
<!-- This string is shown in the context menu after a longpress on a text. When clicked it cuts the selected text and can be pasted later. -->
12761300
<string name="context_menu_cut_text">Cut</string>
12771301

0 commit comments

Comments
 (0)