This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 475
/
GeckoSelectionActionDelegate.kt
70 lines (58 loc) · 2.56 KB
/
GeckoSelectionActionDelegate.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package mozilla.components.browser.engine.gecko.selection
import android.app.Activity
import android.content.Context
import android.view.MenuItem
import androidx.annotation.VisibleForTesting
import mozilla.components.concept.engine.selection.SelectionActionDelegate
import org.mozilla.geckoview.BasicSelectionActionDelegate
/**
* An adapter between the GV [BasicSelectionActionDelegate] and a generic [SelectionActionDelegate].
*
* @param customDelegate handles as much of this logic as possible.
*/
open class GeckoSelectionActionDelegate(
activity: Activity,
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal val customDelegate: SelectionActionDelegate
) : BasicSelectionActionDelegate(activity) {
companion object {
/**
* @returns a [GeckoSelectionActionDelegate] if [customDelegate] is non-null and [context]
* is an instance of [Activity]. Otherwise, returns null.
*/
fun maybeCreate(context: Context, customDelegate: SelectionActionDelegate?): GeckoSelectionActionDelegate? {
return if (context is Activity && customDelegate != null) {
GeckoSelectionActionDelegate(context, customDelegate)
} else {
null
}
}
}
override fun getAllActions(): Array<String> {
return customDelegate.sortedActions(super.getAllActions() + customDelegate.getAllActions())
}
override fun isActionAvailable(id: String): Boolean {
val selectedText = mSelection?.text
val customActionIsAvailable = !selectedText.isNullOrEmpty() &&
customDelegate.isActionAvailable(id, selectedText)
return customActionIsAvailable ||
super.isActionAvailable(id)
}
override fun prepareAction(id: String, item: MenuItem) {
val title = customDelegate.getActionTitle(id)
?: return super.prepareAction(id, item)
item.title = title
}
override fun performAction(id: String, item: MenuItem): Boolean {
/* Temporary, removed once https://bugzilla.mozilla.org/show_bug.cgi?id=1694983 is fixed */
try {
val selectedText = mSelection?.text ?: return super.performAction(id, item)
return customDelegate.performAction(id, selectedText) || super.performAction(id, item)
} catch (e: SecurityException) {
return false
}
}
}