Guest User

Untitled

a guest
Jan 24th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.84 KB | None | 0 0
  1.  
  2. -- Abstract: Facebook Audience Network
  3. -- Version: 1.0
  4. -- Sample code is MIT licensed; see https://www.coronalabs.com/links/code/license
  5. ---------------------------------------------------------------------------------------
  6.  
  7. display.setStatusBar( display.HiddenStatusBar )
  8.  
  9. ------------------------------
  10. -- RENDER THE SAMPLE CODE UI
  11. ------------------------------
  12. local sampleUI = require( "sampleUI.sampleUI" )
  13. sampleUI:newUI( { theme="darkgrey", title="Facebook Audience Network", showBuildNum=true } )
  14.  
  15. ------------------------------
  16. -- CONFIGURE STAGE
  17. ------------------------------
  18. display.getCurrentStage():insert( sampleUI.backGroup )
  19. local mainGroup = display.newGroup()
  20. display.getCurrentStage():insert( sampleUI.frontGroup )
  21.  
  22. ----------------------
  23. -- BEGIN SAMPLE CODE
  24. ----------------------
  25.  
  26. -- Preset Facebook placement IDs (replace these with your own)
  27. -- See here for instructions: https://developers.facebook.com/docs/audience-network/getting-started
  28. local bannerPlacementID = "YOUR_BANNER_PLACEMENT_ID"
  29. local interstitialPlacementID = "730847337121994_1120793191460738"
  30.  
  31. -- Multiple test devices can be included in a table array { "DEVICE_1_HASH_ID", "DEVICE_2_HASH_ID" }
  32. -- See here for instructions: https://docs.coronalabs.com/plugin/fbAudienceNetwork/init.html
  33. local deviceHashArray = { "12345678901234567890" }
  34.  
  35.  
  36. -- Require libraries/plugins
  37. local widget = require( "widget" )
  38. local fbAudienceNetwork = require( "plugin.fbAudienceNetwork" )
  39.  
  40. -- Set app font
  41. local appFont = sampleUI.appFont
  42.  
  43. -- Table of data for menu buttons
  44. local menuButtons = {
  45. loadBanner = { label="Load Banner Ad", y=120 },
  46. showBanner = { label="Show Banner Ad", y=170 },
  47. hideBanner = { label="Hide Banner Ad", y=220 },
  48. loadInterstitial = { label="Load Interstitial Ad", y=285 },
  49. showInterstitial = { label="Show Interstitial Ad", y=335 }
  50. }
  51.  
  52. -- Set local variables
  53. local setupComplete = false
  54.  
  55. -- Create objects to visually prompt action
  56. local bannerPrompt = display.newPolygon( mainGroup, 62, menuButtons["loadBanner"].y, { 0,-12, 12,0, 0,12 } )
  57. bannerPrompt:setFillColor( 0.8 )
  58. bannerPrompt.alpha = 0
  59. local interstitialPrompt = display.newPolygon( mainGroup, 62, menuButtons["loadInterstitial"].y, { 0,-12, 12,0, 0,12 } )
  60. interstitialPrompt:setFillColor( 0.8 )
  61. interstitialPrompt.alpha = 0
  62.  
  63. -- Create spinner widget for indicating ad status
  64. widget.setTheme( "widget_theme_android_holo_light" )
  65. local spinner = widget.newSpinner( { x=display.contentCenterX, y=410, deltaAngle=10, incrementEvery=10 } )
  66. mainGroup:insert( spinner )
  67. spinner.alpha = 0
  68.  
  69.  
  70. -- Function to manage spinner appearance/animation
  71. local function manageSpinner( action )
  72. if ( action == "show" ) then
  73. spinner:start()
  74. transition.cancel( "spinner" )
  75. transition.to( spinner, { alpha=1, tag="spinner", time=((1-spinner.alpha)*320), transition=easing.outQuad } )
  76. elseif ( action == "hide" ) then
  77. transition.cancel( "spinner" )
  78. transition.to( spinner, { alpha=0, tag="spinner", time=((1-(1-spinner.alpha))*320), transition=easing.outQuad,
  79. onComplete=function() spinner:stop(); end } )
  80. end
  81. end
  82.  
  83.  
  84. -- Function to update button visibility/state
  85. local function updateUI( params )
  86.  
  87. -- Disable inactive buttons
  88. for i = 1,#params["disable"] do
  89. local ref = params["disable"][i]
  90. local button = menuButtons[ref]["object"]
  91. button:setEnabled( false )
  92. button.alpha = 0.3
  93. end
  94.  
  95. -- Move/transition banner ad prompt
  96. if ( params.bannerPromptTo ) then
  97. transition.to( bannerPrompt, { y=menuButtons[params.bannerPromptTo].y, alpha=1, time=400, transition=easing.outQuad } )
  98. end
  99.  
  100. -- Move/transition interstitial ad prompt
  101. if ( params.interstitialPromptTo ) then
  102. transition.to( interstitialPrompt, { y=menuButtons[params.interstitialPromptTo].y, alpha=1, time=400, transition=easing.outQuad } )
  103. end
  104.  
  105. -- Enable new active buttons
  106. timer.performWithDelay( 400,
  107. function()
  108. for i = 1,#params["enable"] do
  109. local ref = params["enable"][i]
  110. local button = menuButtons[ref]["object"]
  111. button:setEnabled( true )
  112. button.alpha = 1
  113. end
  114. end
  115. )
  116. end
  117.  
  118.  
  119. -- Ad listener function
  120. local function adListener( event )
  121.  
  122. --Exit function if user hasn't set up testing parameters
  123. -- if ( setupComplete == false ) then
  124. -- if ( event.phase == "init" ) then
  125. -- fbAudienceNetwork.load( "banner", { placementId = bannerPlacementID })
  126. -- local alert = native.showAlert( "Important", 'For testing the Facebook Audience Network, you must follow Facebook protocol by gathering this device’s hash ID. With this device connected, find the "Test mode device hash" within the device console log, enter it within "main.lua" on line 33, and then build/run this sample again.', { "OK" } )
  127. -- end
  128.  
  129. -- return
  130. -- end
  131. print("\n\n\n\n", "came call here now");
  132.  
  133.  
  134. -- Successful initialization of the Facebook Audience Network
  135. if ( event.phase == "init" ) then
  136. print( "\n\nFacebook Audience Network event: initialization successful" )
  137. -- Enable both buttons to load ads
  138. updateUI( { enable={ "loadBanner","loadInterstitial" }, disable={ "showBanner","hideBanner","showInterstitial" }, bannerPromptTo="loadBanner", interstitialPromptTo="loadInterstitial" } )
  139.  
  140. -- An ad loaded successfully
  141. elseif ( event.phase == "loaded" ) then
  142. print( "\n\nFacebook Audience Network event: " .. tostring(event.type) .. " ad loaded successfully" )
  143. -- Enable show button
  144. if ( event.type == "banner" ) then
  145. updateUI( { enable={ "showBanner" }, disable={ "loadBanner","hideBanner" }, bannerPromptTo="showBanner" } )
  146. elseif ( event.type == "interstitial" ) then
  147. updateUI( { enable={ "showInterstitial" }, disable={ "loadInterstitial" }, interstitialPromptTo="showInterstitial" } )
  148. end
  149. manageSpinner( "hide" )
  150.  
  151. -- An interstitial ad was closed by the user
  152. elseif ( event.phase == "closed" ) then
  153. print( "\n\nFacebook Audience Network event: " .. tostring(event.type) .. " ad closed by user" )
  154. -- Enable button to load another interstitial; disable button to show interstitial
  155. updateUI( { enable={ "loadInterstitial" }, disable={ "showInterstitial" }, interstitialPromptTo="loadInterstitial" } )
  156.  
  157. -- The ad was clicked/tapped
  158. elseif ( event.phase == "clicked" ) then
  159. print( "\n\nFacebook Audience Network event: " .. tostring(event.type) .. " ad clicked/tapped by user" )
  160.  
  161. -- The ad failed to load
  162. elseif ( event.phase == "failed" ) then
  163. print( "\n\nFacebook Audience Network event: " .. tostring(event.type) .. " ad failed to load" )
  164. -- Reset to load button
  165. if ( event.type == "banner" ) then
  166. updateUI( { enable={ "loadBanner" }, disable={ "showBanner","hideBanner" }, bannerPromptTo="loadBanner" } )
  167. elseif ( event.type == "interstitial" ) then
  168. updateUI( { enable={ "loadInterstitial" }, disable={ "showInterstitial" }, interstitialPromptTo="loadInterstitial" } )
  169. end
  170. manageSpinner( "hide" )
  171. end
  172. end
  173.  
  174.  
  175. -- Function to prompt/alert user for setup
  176. local function checkSetup()
  177.  
  178. if ( system.getInfo( "environment" ) ~= "device" ) then return end
  179.  
  180. --if ( tostring(bannerPlacementID) == "YOUR_BANNER_PLACEMENT_ID" or tostring(interstitialPlacementID) == "YOUR_INTERSTITIAL_PLACEMENT_ID" ) then
  181. if (tostring(interstitialPlacementID) == "YOUR_INTERSTITIAL_PLACEMENT_ID" ) then
  182. local alert = native.showAlert( "Important", 'Confirm that you have specified your Facebook placement IDs within "main.lua" on lines 28-29. Proceed to the Facebook guide for instructions.', { "OK", "guide" },
  183. function( event )
  184. if ( event.action == "clicked" and event.index == 2 ) then
  185. system.openURL( "https://developers.facebook.com/docs/audience-network/getting-started" )
  186. end
  187. end )
  188. elseif ( tostring(deviceHashArray[1]) == "YOUR_DEVICE_1_HASH_ID" ) then
  189. fbAudienceNetwork.init( adListener )
  190. else
  191. setupComplete = true
  192. end
  193. end
  194.  
  195.  
  196. -- Button handler function
  197. local function onButtonRelease( event )
  198.  
  199. if ( event.target.id == "loadBanner" ) then
  200. fbAudienceNetwork.load( "banner", { placementId = bannerPlacementID, bannerSize = "BANNER_HEIGHT_50" })
  201. manageSpinner( "show" )
  202.  
  203. elseif ( event.target.id == "showBanner" ) then
  204. if ( fbAudienceNetwork.isLoaded( bannerPlacementID ) == true ) then
  205. updateUI( { enable={ "hideBanner" }, disable={ "loadBanner","showBanner" }, bannerPromptTo="hideBanner" } )
  206. fbAudienceNetwork.show( "banner", { placementId = bannerPlacementID, y="bottom" } )
  207. end
  208.  
  209. elseif ( event.target.id == "hideBanner" ) then
  210. updateUI( { enable={ "loadBanner" }, disable={ "showBanner","hideBanner" }, bannerPromptTo="loadBanner" } )
  211. fbAudienceNetwork.hide( bannerPlacementID )
  212.  
  213. elseif ( event.target.id == "loadInterstitial" ) then
  214. fbAudienceNetwork.load( "interstitial", { placementId = interstitialPlacementID } )
  215. manageSpinner( "show" )
  216.  
  217. elseif ( event.target.id == "showInterstitial" ) then
  218. if ( fbAudienceNetwork.isLoaded( interstitialPlacementID ) == true ) then
  219. fbAudienceNetwork.show( "interstitial", { placementId = interstitialPlacementID } )
  220. end
  221. end
  222. return true
  223. end
  224.  
  225.  
  226.  
  227. -- Loop through table to display buttons
  228. for k,v in pairs( menuButtons ) do
  229.  
  230. local button = widget.newButton(
  231. {
  232. label = v.label,
  233. id = k,
  234. shape = "rectangle",
  235. width = 188,
  236. height = 32,
  237. font = appFont,
  238. fontSize = 16,
  239. fillColor = { default={ 0.12,0.32,0.52,1 }, over={ 0.132,0.352,0.572,1 } },
  240. labelColor = { default={ 1,1,1,1 }, over={ 1,1,1,1 } },
  241. onRelease = onButtonRelease
  242. })
  243. button.x = display.contentCenterX + 10
  244. button.y = v.y
  245. button:setEnabled( false )
  246. button.alpha = 0.3
  247. menuButtons[k]["object"] = button
  248. mainGroup:insert( button )
  249. end
  250.  
  251.  
  252. -- Initially alert user to set up device for testing
  253. --checkSetup()
  254.  
  255. -- Initialize the Facebook Audience Network
  256. --fbAudienceNetwork.load( "interstitial", { placementId = interstitialPlacementID, testDevices={"xxxx"} } );
  257. fbAudienceNetwork.init( adListener, { testDevices={"xxxssssx"} } );
Add Comment
Please, Sign In to add comment