Skip to content

Commit

Permalink
Add "copy ref to clipboard" context menu item (#2598)
Browse files Browse the repository at this point in the history
  • Loading branch information
tuomas2 committed Aug 31, 2023
1 parent 2975bda commit 5a2668d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 21 deletions.
42 changes: 30 additions & 12 deletions app/src/main/java/net/bible/android/view/activity/page/BibleView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ package net.bible.android.view.activity.page
import android.annotation.SuppressLint
import android.app.Activity
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
Expand Down Expand Up @@ -75,7 +73,6 @@ import net.bible.android.control.bookmark.StudyPadOrderEvent
import net.bible.android.control.bookmark.StudyPadTextEntryDeleted
import net.bible.android.control.download.DownloadControl
import net.bible.android.control.event.ABEventBus
import net.bible.android.control.event.ToastEvent
import net.bible.android.control.event.window.CurrentWindowChangedEvent
import net.bible.android.control.event.window.NumberOfWindowsChangedEvent
import net.bible.android.control.event.window.ScrollSecondaryWindowEvent
Expand Down Expand Up @@ -1108,21 +1105,42 @@ class BibleView(val mainBibleActivity: MainBibleActivity,

internal inner class LinkLongPressContextMenuInfo(private val targetLink: String) : BibleViewContextMenuInfo {
override fun onContextItemSelected(item: MenuItem): Boolean {
val windowMode = when (item.itemId) {
R.id.open_link_in_special_window -> WindowMode.WINDOW_MODE_SPECIAL
R.id.open_link_in_new_window -> WindowMode.WINDOW_MODE_NEW
R.id.open_link_in_this_window -> WindowMode.WINDOW_MODE_THIS
else -> WindowMode.WINDOW_MODE_UNDEFINED
val uri = Uri.parse(targetLink)
if(item.itemId == R.id.copy_link_to_clipboard) {
val osisRef = uri.getQueryParameter("osis")?: return false
val doc = uri.getQueryParameter("doc")
val ordinal = uri.getQueryParameter("ordinal")?.toInt()
val v11n = uri.getQueryParameter("v11n")
val abUrl = CommonUtils.makeAndBibleUrl(
osisRef,
doc,
v11n,
ordinal
)
CommonUtils.copyToClipboard(
ClipData.newPlainText(abUrl, abUrl),
R.string.reference_copied_to_clipboard
)
} else {
val windowMode = when (item.itemId) {
R.id.open_link_in_special_window -> WindowMode.WINDOW_MODE_SPECIAL
R.id.open_link_in_new_window -> WindowMode.WINDOW_MODE_NEW
R.id.open_link_in_this_window -> WindowMode.WINDOW_MODE_THIS
else -> WindowMode.WINDOW_MODE_UNDEFINED
}
linkControl.windowMode = windowMode
openLink(uri)
linkControl.windowMode = WindowMode.WINDOW_MODE_UNDEFINED
contextMenuInfo = null
}
linkControl.windowMode = windowMode
openLink(Uri.parse(targetLink))
linkControl.windowMode = WindowMode.WINDOW_MODE_UNDEFINED
contextMenuInfo = null
return true
}

override fun onCreateContextMenu(menu: ContextMenu, v: View, menuInflater: MenuInflater) {
menuInflater.inflate(R.menu.link_context_menu, menu)

val parsed: Uri = Uri.parse(targetLink)
menu.findItem(R.id.copy_link_to_clipboard).isVisible = parsed.scheme == UriConstants.SCHEME_REFERENCE
val openLinksInSpecialWindowByDefault = CommonUtils.settings.getBoolean("open_links_in_special_window_pref", true)
val item =
if(openLinksInSpecialWindowByDefault)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -847,11 +847,15 @@ class SplitBibleArea(private val mainBibleActivity: MainBibleActivity): FrameLay
)
R.id.copyReference -> CommandPreference(
launch = { _, _, _ ->
val doc = window.pageManager.currentPage.currentDocument
val doc = window.pageManager.currentPage.currentDocument?: return@CommandPreference
val key = window.pageManager.currentPage.singleKey?: return@CommandPreference
val ordinal = window.pageManager.currentPage.anchorOrdinal?.start

val url = CommonUtils.makeAndBibleUrl(doc, key, ordinal)?: return@CommandPreference
val url = CommonUtils.makeAndBibleUrl(
keyStr = key.osisRef,
docInitials = doc.initials,
ordinal = ordinal
)
CommonUtils.copyToClipboard(
ClipData.newPlainText(key.name, url),
R.string.reference_copied_to_clipboard
Expand Down
19 changes: 12 additions & 7 deletions app/src/main/java/net/bible/service/common/CommonUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1557,14 +1557,19 @@ object CommonUtils : CommonUtilsBase() {
BackupControl.AbDbFileType.UNKNOWN
}

fun makeAndBibleUrl(doc: Book?, key: Key, ordinal: Int?): String? {
var url = "https://andbible.org/bible/${key.osisRef}"
fun makeAndBibleUrl(
keyStr: String,
docInitials: String? = null,
v11n: String? = null,
ordinal: Int? = null
): String {
var url = "https://andbible.org/bible/$keyStr"
val queryParameters = mutableListOf<String>()
if(doc != null) {
queryParameters.add("document=${doc.initials}")
if(doc is SwordBook) {
queryParameters.add("v11n=${doc.versification.name}")
}
if(docInitials != null) {
queryParameters.add("document=$docInitials")
}
if(v11n != null) {
queryParameters.add("v11n=$v11n")
}
if(ordinal != null) {
queryParameters.add("ordinal=${ordinal}")
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/menu/link_context_menu.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@
<item android:id="@+id/open_link_in_special_window"
android:title="@string/open_link_in_special_window"
/>
<item android:id="@+id/copy_link_to_clipboard"
android:title="@string/copyReference"
android:visible="false"
/>
</menu>

0 comments on commit 5a2668d

Please sign in to comment.