Skip to content

Commit

Permalink
android: add support for 3rd party dictionary apps
Browse files Browse the repository at this point in the history
  • Loading branch information
pazos committed Jul 4, 2019
1 parent a335648 commit 08247fb
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
55 changes: 55 additions & 0 deletions frontend/apps/reader/modules/readerdictionary.lua
Expand Up @@ -298,6 +298,56 @@ If you'd like to change the order in which dictionaries are queried (and their r
}
}
}
if Device:canExternalDictLookup() then
local function genExternalDictItems()
local items_table = {}
for i, v in ipairs(Device:getExternalDictLookupList()) do
local setting = v[1]
local dict_name = v[2]
local is_enabled = v[3]
table.insert(items_table, {
text = dict_name,
checked_func = function()
return setting == G_reader_settings:readSetting("external_dict_lookup_method")
end,
enabled_func = function()
return is_enabled == true
end,
callback = function()
G_reader_settings:saveSetting("external_dict_lookup_method", v[1])
end,
})
end
return items_table
end
table.insert(menu_items.dictionary_settings.sub_item_table, 1, {
text = _("Use external dictionary"),
checked_func = function()
return G_reader_settings:isTrue("external_dict_lookup")
end,
callback = function()
G_reader_settings:flipNilOrFalse("external_dict_lookup")
end,
})
table.insert(menu_items.dictionary_settings.sub_item_table, 2, {
text_func = function()
local display_name = _("none")
local ext_id = G_reader_settings:readSetting("external_dict_lookup_method")
for i, v in ipairs(Device:getExternalDictLookupList()) do
if v[1] == ext_id then
display_name = v[2]
break
end
end
return T(_("Dictionary: %1"), display_name)
end,
enabled_func = function()
return G_reader_settings:isTrue("external_dict_lookup")
end,
sub_item_table = genExternalDictItems(),
separator = true,
})
end
end

function ReaderDictionary:onLookupWord(word, box, highlight, link)
Expand Down Expand Up @@ -710,6 +760,11 @@ function ReaderDictionary:stardictLookup(word, dict_names, fuzzy_search, box, li
})
end

if Device:canExternalDictLookup() and G_reader_settings:isTrue("external_dict_lookup") then
Device:doExternalDictLookup(word, G_reader_settings:readSetting("external_dict_lookup_method"))
return
end

if fuzzy_search then
self:showLookupInfo(word)
end
Expand Down
31 changes: 31 additions & 0 deletions frontend/device/android/device.lua
Expand Up @@ -40,6 +40,21 @@ local function getCodename()
return codename
end

local EXTERNAL_DICTS_AVAILABILITY_CHECKED = false
local EXTERNAL_DICTS = require("device/android/dictionaries")
local function getExternalDicts()
if not EXTERNAL_DICTS_AVAILABILITY_CHECKED then
EXTERNAL_DICTS_AVAILABILITY_CHECKED = true
for i, v in ipairs(EXTERNAL_DICTS) do
local package = v[4]
if android.isPackageEnabled(package) then
v[3] = true
end
end
end
return EXTERNAL_DICTS
end

local Device = Generic:new{
isAndroid = yes,
model = android.prop.product,
Expand All @@ -57,6 +72,21 @@ local Device = Generic:new{
if not link or type(link) ~= "string" then return end
return android.openLink(link) == 0
end,
canExternalDictLookup = yes,
getExternalDictLookupList = getExternalDicts,
doExternalDictLookup = function (self, text, method)
local params = nil
for i, v in ipairs(getExternalDicts()) do
if v[1] == method then
package = v[4]
action = v[5]
break
end
end
android.dictLookup(text, package, action)
end,


--[[
Disable jit on some modules on android to make koreader on Android more stable.
Expand Down Expand Up @@ -86,6 +116,7 @@ function Device:init()
or ev.code == C.APP_CMD_WINDOW_REDRAW_NEEDED then
this.device.screen:_updateWindow()
elseif ev.code == C.APP_CMD_RESUME then
EXTERNAL_DICTS_AVAILABILITY_CHECKED = false
local new_file = android.getIntent()
if new_file ~= nil and lfs.attributes(new_file, "mode") == "file" then
-- we cannot blit to a window here since we have no focus yet.
Expand Down
15 changes: 15 additions & 0 deletions frontend/device/android/dictionaries.lua
@@ -0,0 +1,15 @@
return { --[[ supported android dictionary applications.
Most of them should support Intent.ACTION_SEND, Intent.ACTION_SEARCH or
Intent.ACTION_PROCESS_TEXT. Some applications implement their custom intents. ]]--

{ "Aard2", "Aard2", false, "itkach.aard2", "aard2" },
{ "Alpus", "Alpus", false, "com.ngcomputing.fora.android", "search" },
{ "ColorDict", "ColorDict", false, "com.socialnmobile.colordict", "colordict" },
{ "Fora", "Fora Dict", false, "com.ngc.fora", "search" },
{ "GoldenFree", "GoldenDict Free", false, "mobi.goldendict.android.free", "send" },
{ "GoldenPro", "GoldenDict Pro", false, "mobi.goldendict.android.pro", "send" },
{ "Kiwix", "Kiwix", false, "org.kiwix.kiwixmobile", "text" },
{ "Mdict", "Mdict", false, "cn.mdict", "send" },
{ "QuickDic", "QuickDic", false, "de.reimardoeffinger.quickdic", "quickdic" },
}
1 change: 1 addition & 0 deletions frontend/device/generic/device.lua
Expand Up @@ -71,6 +71,7 @@ local Device = {

canOpenLink = no,
openLink = no,
canExternalDictLookup = no,
}

function Device:new(o)
Expand Down

0 comments on commit 08247fb

Please sign in to comment.