Add 'widgets' module to intercat with Android widgets
This commit is contained in:
53
samples/android-widget-dumper.lua
Normal file
53
samples/android-widget-dumper.lua
Normal file
@@ -0,0 +1,53 @@
|
||||
-- name = "Android widgets dumper"
|
||||
|
||||
-- Place app package name with widget here
|
||||
app_pkg = "com.weather.Weather"
|
||||
--app_pkg = "com.google.android.apps.tasks"
|
||||
--app_pkg = "com.android.chrome"
|
||||
--app_pkg = "com.whatsapp"
|
||||
|
||||
-- Globals
|
||||
labels = {}
|
||||
providers = {}
|
||||
dump = ""
|
||||
wid = -1
|
||||
|
||||
function on_resume()
|
||||
local list = widgets:list(app_pkg)
|
||||
|
||||
if list == nil then
|
||||
ui:show_text("Error: No widgets")
|
||||
return
|
||||
end
|
||||
|
||||
labels = map(function(it) return it.label end, list)
|
||||
providers = map(function(it) return it.provider end, list)
|
||||
|
||||
w_content = ""
|
||||
|
||||
if wid < 0 then
|
||||
ui:show_lines(labels)
|
||||
else
|
||||
widgets:request_updates(wid)
|
||||
end
|
||||
end
|
||||
|
||||
function on_click(idx)
|
||||
if w_content == "" then
|
||||
wid = widgets:setup(providers[idx])
|
||||
widgets:request_updates(wid)
|
||||
else
|
||||
system:copy_to_clipboard(w_content)
|
||||
end
|
||||
end
|
||||
|
||||
function on_app_widget_updated(bridge)
|
||||
local provider = bridge:provider()
|
||||
local dump = bridge:dump_tree()
|
||||
local colors = bridge:dump_colors()
|
||||
w_content = provider.."\n\n"..dump.."\n\n"..serialize(colors)
|
||||
|
||||
ui:show_text("%%txt%%"..w_content)
|
||||
debug:log("dump:\n\n"..w_content)
|
||||
end
|
||||
|
||||
69
samples/android-widget-sample.lua
Normal file
69
samples/android-widget-sample.lua
Normal file
@@ -0,0 +1,69 @@
|
||||
-- name = "Android widgets sample (Gmail)"
|
||||
|
||||
local prefs = require "prefs"
|
||||
|
||||
local max_mails = 1
|
||||
local curr_tab = {}
|
||||
local w_bridge = nil
|
||||
|
||||
function on_resume()
|
||||
if not widgets:bound(prefs.wid) then
|
||||
setup_app_widget()
|
||||
end
|
||||
|
||||
widgets:request_updates(prefs.wid)
|
||||
end
|
||||
|
||||
function on_app_widget_updated(bridge)
|
||||
local tab = bridge:dump_strings().values
|
||||
|
||||
-- Removing redunant elements like widget title
|
||||
tab = skip(tab, 4)
|
||||
|
||||
-- Each mail consists of 4 text elements:
|
||||
-- 1. from, 2. time, 3. subject, 4. text
|
||||
tab = take(tab, max_mails * 4)
|
||||
|
||||
-- Concatenate "from" and "time"
|
||||
tab = concat(tab, {1, 2}, ", ")
|
||||
|
||||
curr_tab = tab
|
||||
w_bridge = bridge
|
||||
|
||||
ui:show_lines(tab)
|
||||
end
|
||||
|
||||
function on_click(idx)
|
||||
w_bridge:click(curr_tab[idx])
|
||||
end
|
||||
|
||||
function setup_app_widget()
|
||||
local id = widgets:setup("com.google.android.gm/com.google.android.gm.widget.GmailWidgetProvider")
|
||||
if (id ~= nil) then
|
||||
prefs.wid = id
|
||||
else
|
||||
ui:show_text("Can't add widget")
|
||||
return
|
||||
end
|
||||
end
|
||||
|
||||
-- Utils
|
||||
|
||||
function concat(tbl, indicesToConcatenate, delimiter)
|
||||
local resultTable = {}
|
||||
|
||||
if #indicesToConcatenate > 0 then
|
||||
local concatenatedString = {}
|
||||
for _, index in ipairs(indicesToConcatenate) do
|
||||
table.insert(concatenatedString, tbl[index])
|
||||
end
|
||||
table.insert(resultTable, table.concat(concatenatedString, delimiter))
|
||||
end
|
||||
|
||||
for i = 1, #tbl do
|
||||
if not contains(indicesToConcatenate, i) then
|
||||
table.insert(resultTable, tbl[i])
|
||||
end
|
||||
end
|
||||
return resultTable
|
||||
end
|
||||
56
samples/android-widget-sample2.lua
Normal file
56
samples/android-widget-sample2.lua
Normal file
@@ -0,0 +1,56 @@
|
||||
-- name = "Android widgets sample (The Weather Channel)"
|
||||
-- uses_app = "com.weather.Weather"
|
||||
|
||||
local prefs = require "prefs"
|
||||
|
||||
local curr_temp = ""
|
||||
local w_bridge = nil
|
||||
|
||||
function on_resume()
|
||||
if not widgets:bound(prefs.wid) then
|
||||
setup_app_widget()
|
||||
end
|
||||
|
||||
widgets:request_updates(prefs.wid)
|
||||
end
|
||||
|
||||
function on_app_widget_updated(bridge)
|
||||
local tab = bridge:dump_table()
|
||||
|
||||
current_temp = tab.relative_layout_1.relative_layout_2.text_1
|
||||
location = tab.relative_layout_1.relative_layout_2.text_2
|
||||
w_bridge = bridge
|
||||
|
||||
if location ~= nil and current_temp ~= nil then
|
||||
ui:show_text(location..": "..current_temp)
|
||||
else
|
||||
ui:show_text("Empty")
|
||||
end
|
||||
end
|
||||
|
||||
function on_app_widget_updated_alt(bridge)
|
||||
local strings = bridge:dump_strings().values
|
||||
|
||||
location = strings[2]
|
||||
current_temp = strings[1]
|
||||
w_bridge = bridge
|
||||
|
||||
if location ~= nil and current_temp ~= nil then
|
||||
ui:show_text(location..": "..current_temp)
|
||||
else
|
||||
ui:show_text("Empty")
|
||||
end
|
||||
end
|
||||
|
||||
function on_click(idx)
|
||||
w_bridge:click(current_temp)
|
||||
end
|
||||
|
||||
function setup_app_widget()
|
||||
local id = widgets:setup("com.weather.Weather/com.weather.Weather.widgets.WeatherWidgetProvider2x2")
|
||||
if (id ~= nil) then
|
||||
prefs.wid = id
|
||||
else
|
||||
ui:show_text("Can't add widget")
|
||||
end
|
||||
end
|
||||
46
samples/android-widget-sample3.lua
Normal file
46
samples/android-widget-sample3.lua
Normal file
@@ -0,0 +1,46 @@
|
||||
-- name = "Android widgets sample (Google Tasks)"
|
||||
|
||||
local prefs = require "prefs"
|
||||
local fmt = require "fmt"
|
||||
|
||||
local max_mails = 1
|
||||
local curr_tab = {}
|
||||
local w_bridge = nil
|
||||
|
||||
function on_resume()
|
||||
if not widgets:bound(prefs.wid) then
|
||||
setup_app_widget()
|
||||
end
|
||||
|
||||
widgets:request_updates(prefs.wid)
|
||||
end
|
||||
|
||||
function on_app_widget_updated(bridge)
|
||||
local tab = bridge:dump_strings().values
|
||||
|
||||
-- Bold list name
|
||||
tab[1] = fmt.bold(tab[1])
|
||||
|
||||
-- Remove "A fresh start"
|
||||
table.remove(tab, #tab-1)
|
||||
|
||||
curr_tab = tab
|
||||
w_bridge = bridge
|
||||
|
||||
ui:show_lines(tab)
|
||||
end
|
||||
|
||||
function on_click(idx)
|
||||
--w_bridge:click(curr_tab[idx])
|
||||
w_bridge:click("text_2")
|
||||
end
|
||||
|
||||
function setup_app_widget()
|
||||
local id = widgets:setup("com.google.android.apps.tasks/com.google.android.apps.tasks.features.widgetlarge.ListWidgetProvider")
|
||||
if (id ~= nil) then
|
||||
prefs.wid = id
|
||||
else
|
||||
ui:show_text("Can't add widget")
|
||||
return
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user