Advertisement
Guest User

Untitled

a guest
Jun 15th, 2019
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 0 0
  1. getUserConsent()
  2. }
  3.  
  4. @SuppressLint("HardwareIds")
  5. private fun getUserConsent() {
  6. try {
  7. if (!isConsentYet) {
  8. val msg = "Your privacy policy"
  9.  
  10. @SuppressLint("InflateParams")
  11. val view = LayoutInflater.from(this@MainActivity)
  12. .inflate(R.layout.dialog_consent, null, false)
  13.  
  14. val d = AlertDialog.Builder(this)
  15. .setView(view)
  16. .setCancelable(false)
  17. .setTitle("User Consent")
  18. .setIcon(R.mipmap.ic_launcher)
  19. .create()
  20. view.txtAlertMsg.text = Html.fromHtml(msg)
  21. view.btnAccept.setOnClickListener {
  22.  
  23. ConsentInformation.getInstance(this@MainActivity).addTestDevice(
  24. Settings.Secure.getString(
  25. contentResolver,
  26. Settings.Secure.ANDROID_ID
  27. )
  28. )
  29.  
  30. ConsentInformation.getInstance(this@MainActivity).consentStatus =
  31. if (view.chkGDPR.isChecked)
  32. ConsentStatus.PERSONALIZED else ConsentStatus.NON_PERSONALIZED
  33. isConsentYet = true
  34. d.dismiss()
  35. loadAds()
  36. }
  37. d.show()
  38. setTextViewHTML(view.txtAlertMsg, msg)
  39. } else {
  40. loadAds()
  41. }
  42. } catch (e: Exception) {
  43. e.printStackTrace()
  44. }
  45.  
  46. }
  47.  
  48. protected fun setTextViewHTML(text: TextView, html: String) {
  49. val sequence = Html.fromHtml(html)
  50. val strBuilder = SpannableStringBuilder(sequence)
  51. val urls = strBuilder.getSpans(0, sequence.length,
  52. URLSpan::class.java)
  53. for (span in urls) {
  54. makeLinkClickable(strBuilder, span)
  55. }
  56. text.text = strBuilder
  57. text.movementMethod = LinkMovementMethod.getInstance()
  58. }
  59.  
  60. protected fun makeLinkClickable(strBuilder:
  61. SpannableStringBuilder, span: URLSpan) {
  62. val start = strBuilder.getSpanStart(span)
  63. val end = strBuilder.getSpanEnd(span)
  64. val flags = strBuilder.getSpanFlags(span)
  65. val clickable = object : ClickableSpan() {
  66. override fun onClick(view: View) {
  67. val i = Intent(Intent.ACTION_VIEW)
  68. i.data = Uri.parse("Privacy URL")
  69. startActivity(i)
  70. }
  71. }
  72. strBuilder.setSpan(clickable, start, end, flags)
  73. strBuilder.removeSpan(span)
  74. }
  75.  
  76. private fun loadAds() {
  77. mAdView = findViewById(R.id.adView)
  78. MobileAds.initialize(this, getString(R.string.app_id))
  79. val adRequest = AdRequest.Builder().build()
  80. val adView = AdView(this)
  81. adView.adSize = AdSize.BANNER
  82. adView.adUnitId = getString(R.string.ad_bottom)
  83. mAdView.loadAd(adRequest)
  84.  
  85. mAdView.adListener = object : AdListener() {
  86. override fun onAdLoaded() {
  87. // Code to be executed when an ad finishes loading.
  88. Log.w("AdListener", "onAdLoaded")
  89. }
  90.  
  91. override fun onAdFailedToLoad(errorCode: Int) {
  92. // Code to be executed when an ad request fails.
  93. Log.w("AdListener",
  94. "onAdFailedToLoad".plus(errorCode))
  95. }
  96.  
  97. override fun onAdOpened() {
  98. // Code to be executed when an ad opens an overlay
  99. that
  100. // covers the screen.
  101. Log.w("AdListener", "onAdOpened")
  102. }
  103.  
  104. override fun onAdClicked() {
  105. // Code to be executed when the user clicks on an ad.
  106. Log.w("AdListener", "onAdClicked")
  107. }
  108.  
  109. override fun onAdLeftApplication() {
  110. // Code to be executed when the user has left the app.
  111. Log.w("AdListener", "onAdLeftApplication")
  112. }
  113.  
  114. override fun onAdClosed() {
  115. // Code to be executed when the user is about to
  116. return
  117. // to the app after tapping on an ad.
  118. Log.w("AdListener", "onAdClosed")
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement