-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
67 lines (59 loc) · 1.91 KB
/
main.lua
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
local microphone = require "plugin.microphonePower"
local json = require("json")
local function isValueInTable( haystack, needle )
assert( type(haystack) == "table", "isValueInTable() : First parameter must be a table." )
assert( needle ~= nil, "isValueInTable() : Second parameter must be not be nil." )
for key, value in pairs( haystack ) do
if ( value == needle ) then
return true
end
end
return false
end
local function canUseMicrophone()
local grantedPermissions = system.getInfo("grantedAppPermissions")
if ( grantedPermissions ) then
if ( not isValueInTable( grantedPermissions, "Microphone" ) ) then
return false
end
end
return true
end
local function onMicrophoneAccessGiven()
timer.performWithDelay( 100, function()
microphone.getPower()
end, -1 )
end
-- Called when the user has granted or denied the requested permissions.
local function permissionsListener( event )
-- Print out granted/denied permissions.
print( "permissionsListener( " .. json.prettify( event or {} ) .. " )" )
if canUseMicrophone() then
print("permissionsListener : microphone access given")
-- microphone access is given
-- now get power of microphone
onMicrophoneAccessGiven()
else
-- ask for permissions again to access microphone
print("permissionsListener : microphone access not given")
end
end
if canUseMicrophone() then
-- microphone access is given
-- now get power of microphone
onMicrophoneAccessGiven()
else
-- Make the actual request from the user.
native.showPopup( "requestAppPermission", {
appPermission = "Microphone",
urgency = "Critical",
rationaleTitle = "Microphone Permissions Required!",
rationaleDescription = "This app wants to access your microphone.",
listener = permissionsListener,
} )
end
local function callBackListener( event )
print( "event in microphonePower callback : ", json.encode(event) )
end
-- init microphonePower plugin
microphone.init(callBackListener)