diff --git a/frontend/apps/reader/modules/readerdictionary.lua b/frontend/apps/reader/modules/readerdictionary.lua index 203cc499d..599c07f19 100644 --- a/frontend/apps/reader/modules/readerdictionary.lua +++ b/frontend/apps/reader/modules/readerdictionary.lua @@ -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) @@ -710,6 +760,18 @@ 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"), function() + if self.highlight then + local clear_id = self.highlight:getClearId() + UIManager:scheduleIn(0.5, function() + self.highlight:clear(clear_id) + end) + end + end) + return + end + if fuzzy_search then self:showLookupInfo(word) end diff --git a/frontend/device/android/device.lua b/frontend/device/android/device.lua index 007db8381..231ecb301 100644 --- a/frontend/device/android/device.lua +++ b/frontend/device/android/device.lua @@ -41,6 +41,23 @@ local function getCodename() return codename end +local EXTERNAL_DICTS_AVAILABILITY_CHECKED = false +local EXTERNAL_DICTS = require("device/android/dictionaries") +local external_dict_when_back_callback = nil + +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, @@ -58,6 +75,22 @@ 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, callback) + external_dict_when_back_callback = callback + local package, action = 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. @@ -87,6 +120,12 @@ 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 + if external_dict_when_back_callback then + external_dict_when_back_callback() + external_dict_when_back_callback = nil + end + 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. diff --git a/frontend/device/android/dictionaries.lua b/frontend/device/android/dictionaries.lua new file mode 100644 index 000000000..eacc83434 --- /dev/null +++ b/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" }, +} diff --git a/frontend/device/generic/device.lua b/frontend/device/generic/device.lua index 39d521fa1..5aa53fdcd 100644 --- a/frontend/device/generic/device.lua +++ b/frontend/device/generic/device.lua @@ -71,6 +71,7 @@ local Device = { canOpenLink = no, openLink = no, + canExternalDictLookup = no, } function Device:new(o)