wildantechnoart77

Untitled

May 18th, 2018 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 45.46 KB | None | 0 0
  1. package com.deguarantee.biayaoperasi
  2.  
  3. import android.annotation.SuppressLint
  4. import android.app.ProgressDialog
  5. import android.content.ActivityNotFoundException
  6. import android.content.Intent
  7. import android.net.Uri
  8. import android.os.AsyncTask
  9. import android.os.Bundle
  10. import android.util.Log
  11. import android.view.LayoutInflater
  12. import android.view.Menu
  13. import android.view.MenuItem
  14. import android.view.View
  15. import android.widget.*
  16. import androidx.appcompat.app.ActionBarDrawerToggle
  17. import androidx.appcompat.app.AlertDialog
  18. import androidx.appcompat.app.AppCompatActivity
  19. import androidx.core.view.GravityCompat
  20. import androidx.recyclerview.widget.GridLayoutManager
  21. import com.deguarantee.biayaoperasi.adapter.AdapterDaily
  22. import com.deguarantee.biayaoperasi.network.NetworkClient
  23. import com.deguarantee.biayaoperasi.pojo.DailyItemsResponse
  24. import com.deguarantee.biayaoperasi.pojo.BundlesResponse
  25. import com.deguarantee.biayaoperasi.pojo.UpcomingItemsResponse
  26. import com.facebook.ads.*
  27. import com.google.ads.consent.*
  28. import com.google.android.gms.ads.*
  29. import com.google.android.gms.ads.AdError
  30. import com.google.android.gms.ads.AdListener
  31. import com.google.android.gms.ads.interstitial.InterstitialAd
  32. import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback
  33. import com.google.android.gms.ads.nativead.NativeAd
  34. import com.google.android.gms.ads.nativead.NativeAdOptions
  35. import com.google.android.gms.ads.nativead.NativeAdView
  36. import com.google.android.material.dialog.MaterialAlertDialogBuilder
  37. import com.google.android.material.navigation.NavigationView
  38. import com.google.android.material.snackbar.Snackbar
  39. import com.google.firebase.firestore.FirebaseFirestore
  40. import com.unity3d.ads.IUnityAdsListener
  41. import com.unity3d.ads.UnityAds
  42. import kotlinx.android.synthetic.main.activity_splash_screen.*
  43. import kotlinx.android.synthetic.main.app_bar_main.*
  44. import kotlinx.android.synthetic.main.dialog_view.view.*
  45. import org.jetbrains.annotations.NotNull
  46. import retrofit2.Call
  47. import retrofit2.Callback
  48. import retrofit2.Response
  49. import java.net.MalformedURLException
  50. import java.net.URL
  51. import java.util.*
  52.  
  53. class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
  54.     private var mInterstitialAd: InterstitialAd? = null
  55.     private var consentForm: ConsentForm? = null
  56.     private val network = NetworkClient.INSTANCE
  57.     private val itemList = arrayListOf<Any>()
  58.     private lateinit var adapter: AdapterDaily
  59.     private lateinit var mDialogBuilder: MaterialAlertDialogBuilder
  60.     private lateinit var mDialog: AlertDialog
  61.     private lateinit var adView: NativeAdView
  62.     private var mainDialog: AlertDialog? = null
  63.     private var mainDialogFan: AlertDialog? = null
  64.     private lateinit var dialogView: View
  65.     private lateinit var adViewFanDialog: LinearLayout
  66.     private lateinit var nativeAdId: String
  67.     private lateinit var publisherIds: String
  68.     private lateinit var dialogViewFan: View
  69.     private lateinit var interstitialFb: com.facebook.ads.InterstitialAd
  70.     private lateinit var fanNativeDialog: com.facebook.ads.NativeAd
  71.     private lateinit var adViewFan: LinearLayout
  72.     private lateinit var adViewNative: NativeAdView
  73.  
  74.     private lateinit var nativeLayout: NativeAdLayout
  75.     private lateinit var nativeLayoutFan: NativeAdLayout
  76.  
  77.     private var whatToShow = SHOW_DAILY
  78.     private var showFan: Boolean = false
  79.  
  80.     // The AdLoader used to load ads.
  81.     private lateinit var adLoader: AdLoader
  82.  
  83.     // List of native ads that have been successfully loaded.
  84.     private val mNativeAds: ArrayList<NativeAd> = arrayListOf()
  85.  
  86.     // private lateinit var interstitialFb: com.facebook.ads.InterstitialAd
  87.  
  88.     companion object {
  89.         private const val NUMBER_OF_ADS = 0
  90.         private const val TAG = "MainActivity"
  91.         private const val SHOW_DAILY = 1
  92.         private const val SHOW_UPCOMING = 2
  93.         private const val SHOW_Bundles = 3
  94.     }
  95.  
  96.     @SuppressLint("InflateParams")
  97.     override fun onCreate(savedInstanceState: Bundle?) {
  98.         super.onCreate(savedInstanceState)
  99.         setContentView(R.layout.activity_main)
  100.         setSupportActionBar(toolbar)
  101.  
  102.         unityAdInit()
  103.         AudienceNetworkAds.initialize(this)
  104.         initFbAds()
  105.         fanNativeBanner()
  106.  
  107.         val db = FirebaseFirestore.getInstance()
  108.         db.collection("admob")
  109.             .document("adList")
  110.             .get()
  111.             .addOnSuccessListener {
  112.                 nativeAdId = it.getString("native").toString()
  113.                 publisherIds = it.getString("publisherIds").toString()
  114.  
  115.                 setupRecyclerView()
  116.                 setupAds()
  117.                 setupNavigation()
  118.                 setupClickListener()
  119.                 getDailyList()
  120.                 requestConsent()
  121.                 loadNativeAds()
  122.                 initNativeAds()
  123.                 loadDialogNativeAds()
  124.  
  125.                 redirectApp()
  126.                 getSkinStatus()
  127.  
  128.                 initNativeAds()
  129.                 loadDialogNativeAds()
  130.                 fanNativeDialog()
  131.  
  132.                 val btnRate: Button = dialogView.findViewById(R.id.btn_rate)
  133.                 val btnCancel: Button = dialogView.findViewById(R.id.btn_cancel)
  134.                 val btnExit: Button = dialogView.findViewById(R.id.btn_exit)
  135.  
  136.                 val btnRateFan: Button = dialogViewFan.findViewById(R.id.btn_rate_fan)
  137.                 val btnCancelFan: Button = dialogViewFan.findViewById(R.id.btn_cancel_fan)
  138.                 val btnExitFan: Button = dialogViewFan.findViewById(R.id.btn_exit_fan)
  139.  
  140.                 btnRate.setOnClickListener {
  141.                     try {
  142.                         startActivity(
  143.                             Intent(
  144.                                 Intent.ACTION_VIEW,
  145.                                 Uri.parse("market://details?id=${packageName}&hl=in")
  146.                             )
  147.                         )
  148.                     } catch (ex: ActivityNotFoundException) {
  149.                         startActivity(
  150.                             Intent(
  151.                                 Intent.ACTION_VIEW,
  152.                                 Uri.parse("https://play.google.com/store/apps/details?id=${packageName}")
  153.                             )
  154.                         )
  155.                     }
  156.                 }
  157.                 btnExit.setOnClickListener { finishAffinity() }
  158.                 btnCancel.setOnClickListener { mainDialog?.dismiss() }
  159.  
  160.                 btnRateFan.setOnClickListener {
  161.                     try {
  162.                         startActivity(
  163.                             Intent(
  164.                                 Intent.ACTION_VIEW,
  165.                                 Uri.parse("market://details?id=${packageName}&hl=in")
  166.                             )
  167.                         )
  168.                     } catch (ex: ActivityNotFoundException) {
  169.                         startActivity(
  170.                             Intent(
  171.                                 Intent.ACTION_VIEW,
  172.                                 Uri.parse("https://play.google.com/store/apps/details?id=${packageName}")
  173.                             )
  174.                         )
  175.                     }
  176.                 }
  177.                 btnExitFan.setOnClickListener { finishAffinity() }
  178.                 btnCancelFan.setOnClickListener { mainDialogFan?.dismiss() }
  179.             }.addOnFailureListener {
  180.                 Toast.makeText(
  181.                     this,
  182.                     "Terjadi kesalahan, silakan coba lagi",
  183.                     Toast.LENGTH_SHORT
  184.                 ).show()
  185.             }
  186.     }
  187.  
  188.     private fun fanNativeDialog() {
  189.         mainDialogFan = AlertDialog.Builder(this@MainActivity, R.style.CustomAlertDialog).create()
  190.         dialogViewFan = layoutInflater.inflate(R.layout.dialog_ads_fan, null)
  191.         mainDialogFan?.setView(dialogViewFan)
  192.         mainDialogFan?.setTitle("You sure you want to leave?")
  193.         nativeLayoutFan = dialogViewFan.findViewById(R.id.native_ad_container)
  194.  
  195.         fanNativeDialog = NativeAd(this, getString(R.string.native_fan_id))
  196.  
  197.         val adListener = object : NativeAdListener {
  198.             override fun onError(p0: Ad?, p1: com.facebook.ads.AdError?) {}
  199.  
  200.             override fun onAdLoaded(ad: Ad?) {
  201.                 // Race condition, load() called again before last ad was displayed
  202.                 if (fanNativeDialog == null || fanNativeDialog !== ad) {
  203.                     return
  204.                 }
  205.                 // Inflate Native Banner Ad into Container
  206.                 inflateAdDialog(fanNativeDialog)
  207.             }
  208.  
  209.             override fun onAdClicked(p0: Ad?) {}
  210.  
  211.             override fun onLoggingImpression(p0: Ad?) {}
  212.  
  213.             override fun onMediaDownloaded(p0: Ad?) {}
  214.  
  215.         }
  216.  
  217.         val loadAdConfig = fanNativeDialog.buildLoadAdConfig()
  218.             .withAdListener(adListener)
  219.             .build()
  220.  
  221.         // load the ad
  222.         fanNativeDialog.loadAd(loadAdConfig)
  223.     }
  224.  
  225.     @SuppressLint("InflateParams")
  226.     private fun inflateAdDialog(nativeAd: com.facebook.ads.NativeAd) {
  227.         nativeAd.unregisterView()
  228.  
  229.         val inflater = LayoutInflater.from(this)
  230.         // Inflate the Ad view.  The layout referenced should be the one you created in the last step.
  231.         adViewFanDialog =
  232.             inflater.inflate(R.layout.native_ad_layout, nativeLayoutFan, false) as LinearLayout
  233.         nativeLayoutFan.addView(adViewFanDialog)
  234.  
  235.         // Add the AdOptionsView
  236.         val adChoicesContainer: LinearLayout =
  237.             adViewFanDialog.findViewById(R.id.ad_choices_container)
  238.         val adOptionsView = AdOptionsView(this, nativeAd, nativeLayoutFan)
  239.         adChoicesContainer.removeAllViews()
  240.         adChoicesContainer.addView(adOptionsView, 0)
  241.  
  242.         // Create native UI using the ad metadata.
  243.         val nativeAdIcon: com.facebook.ads.MediaView =
  244.             adViewFanDialog.findViewById(R.id.native_ad_icon)
  245.         val nativeAdTitle: TextView = adViewFanDialog.findViewById(R.id.native_ad_title)
  246.         val nativeAdMedia: com.facebook.ads.MediaView =
  247.             adViewFanDialog.findViewById(R.id.native_ad_media)
  248.         val nativeAdSocialContext: TextView =
  249.             adViewFanDialog.findViewById(R.id.native_ad_social_context)
  250.         val nativeAdBody: TextView = adViewFanDialog.findViewById(R.id.native_ad_body)
  251.         val sponsoredLabel: TextView = adViewFanDialog.findViewById(R.id.native_ad_sponsored_label)
  252.         val nativeAdCallToAction: Button =
  253.             adViewFanDialog.findViewById(R.id.native_ad_call_to_action)
  254.  
  255.         // Set the Text.
  256.         nativeAdTitle.text = nativeAd.advertiserName
  257.         nativeAdBody.text = nativeAd.adBodyText
  258.         nativeAdSocialContext.text = nativeAd.adSocialContext
  259.         nativeAdCallToAction.visibility =
  260.             if (nativeAd.hasCallToAction()) View.VISIBLE else View.INVISIBLE
  261.         nativeAdCallToAction.text = nativeAd.adCallToAction
  262.         sponsoredLabel.text = nativeAd.sponsoredTranslation
  263.  
  264.         // Create a list of clickable views
  265.         val clickableViews: ArrayList<View> = ArrayList()
  266.         clickableViews.add(nativeAdTitle)
  267.         clickableViews.add(nativeAdCallToAction)
  268.  
  269.         // Register the Title and CTA button to listen for clicks.
  270.         nativeAd.registerViewForInteraction(
  271.             adViewFanDialog,
  272.             nativeAdMedia,
  273.             nativeAdIcon,
  274.             clickableViews
  275.         )
  276.         mainDialogFan?.setCancelable(false)
  277.     }
  278.  
  279.     private fun fanNativeBanner() {
  280.         val nativeBannerAd: NativeBannerAd? =
  281.             NativeBannerAd(this, getString(R.string.native_fan_id))
  282.  
  283.         mDialogBuilder = MaterialAlertDialogBuilder(this)
  284.         val v = layoutInflater.inflate(R.layout.dialog_view, null)
  285.         mDialogBuilder.setView(v)
  286.         val et = v.findViewById(R.id.name) as EditText
  287.         mDialogBuilder.setTitle("Battle Royale Name")
  288.  
  289.         v.btn_accept.setOnClickListener {
  290.             mDialog.dismiss()
  291.             name = et.text.toString()
  292.             if (name.isEmpty()) {
  293.                 notifyNoName()
  294.             } else
  295.                 progress()
  296.         }
  297.  
  298.         v.btn_cancel.setOnClickListener {
  299.             mDialog.dismiss()
  300.         }
  301.  
  302.         mDialog = mDialogBuilder.create()
  303.         mDialog.setCancelable(false)
  304.  
  305.         nativeLayout = v.findViewById(R.id.native_fan_ad)
  306.  
  307.         val adListener = object : NativeAdListener {
  308.             override fun onError(p0: Ad?, p1: com.facebook.ads.AdError?) {
  309.                 Toast.makeText(applicationContext, p1?.errorMessage.toString(), Toast.LENGTH_LONG)
  310.                     .show()
  311.             }
  312.  
  313.             override fun onAdLoaded(ad: Ad?) {
  314.                 // Race condition, load() called again before last ad was displayed
  315.                 if (nativeBannerAd == null || nativeBannerAd != ad) {
  316.                     return
  317.                 }
  318.                 // Inflate Native Banner Ad into Container
  319.                 inflateAd(nativeBannerAd)
  320.             }
  321.  
  322.             override fun onAdClicked(p0: Ad?) {}
  323.  
  324.             override fun onLoggingImpression(p0: Ad?) {}
  325.  
  326.             override fun onMediaDownloaded(p0: Ad?) {}
  327.  
  328.         }
  329.  
  330.         val loadAdConfig = nativeBannerAd?.buildLoadAdConfig()
  331.             ?.withAdListener(adListener)
  332.             ?.build()
  333.  
  334.         // load the ad
  335.         nativeBannerAd?.loadAd(loadAdConfig)
  336.     }
  337.  
  338.     private fun inflateAd(nativeBannerAd: NativeBannerAd) {
  339.         // Unregister last ad
  340.         nativeBannerAd.unregisterView()
  341.  
  342.         // Add the Ad view into the ad container.
  343.         val inflater = LayoutInflater.from(applicationContext)
  344.         // Inflate the Ad view.  The layout referenced should be the one you created in the last step.
  345.         adViewFan =
  346.             inflater.inflate(R.layout.native_banner_ad_layout, nativeLayout, false) as LinearLayout
  347.         nativeLayout.addView(adViewFan)
  348.  
  349.         // Add the AdChoices icon
  350.         val adChoicesContainer: RelativeLayout = adViewFan.findViewById(R.id.ad_choices_container)
  351.         val adOptionsView = AdOptionsView(this, nativeBannerAd, nativeLayout)
  352.         adChoicesContainer.removeAllViews()
  353.         adChoicesContainer.addView(adOptionsView, 0)
  354.  
  355.         // Create native UI using the ad metadata.
  356.         val nativeAdTitle = adViewFan.findViewById<TextView>(R.id.native_ad_title)
  357.         val nativeAdSocialContext = adViewFan.findViewById<TextView>(R.id.native_ad_social_context)
  358.         val sponsoredLabel = adViewFan.findViewById<TextView>(R.id.native_ad_sponsored_label)
  359.         val nativeAdIconView: com.facebook.ads.MediaView? =
  360.             adViewFan.findViewById(R.id.native_icon_view)
  361.         val nativeAdCallToAction = adViewFan.findViewById<Button>(R.id.native_ad_call_to_action)
  362.  
  363.         // Set the Text.
  364.         nativeAdCallToAction?.text = nativeBannerAd.adCallToAction
  365.         nativeAdCallToAction?.visibility =
  366.             if (nativeBannerAd.hasCallToAction()) View.VISIBLE else View.INVISIBLE
  367.         nativeAdTitle?.text = nativeBannerAd.advertiserName
  368.         nativeAdSocialContext?.text = nativeBannerAd.adSocialContext
  369.         sponsoredLabel?.text = nativeBannerAd.sponsoredTranslation
  370.  
  371.         // Register the Title and CTA button to listen for clicks.
  372.         val clickableViews: MutableList<View> = ArrayList()
  373.         nativeAdTitle?.let { clickableViews.add(it) }
  374.         nativeAdCallToAction?.let { clickableViews.add(it) }
  375.         nativeBannerAd.registerViewForInteraction(adViewFan, nativeAdIconView, clickableViews)
  376.     }
  377.  
  378.     private fun unityAdInit() {
  379.         UnityAds.initialize(this, getString(R.string.unity_ad_id))
  380.         UnityAds.load(getString(R.string.unity_ad_id))
  381.     }
  382.  
  383.     private fun initFbAds() {
  384.         interstitialFb =
  385.             InterstitialAd(this, getString(R.string.fb_inters_placement_id))
  386.         val listener = object : InterstitialAdListener {
  387.             override fun onInterstitialDisplayed(p0: Ad?) {
  388.             }
  389.  
  390.             override fun onAdClicked(p0: Ad?) {
  391.             }
  392.  
  393.             override fun onInterstitialDismissed(p0: Ad?) {
  394.             }
  395.  
  396.             override fun onError(p0: Ad?, p1: com.facebook.ads.AdError?) {
  397.             }
  398.  
  399.             override fun onAdLoaded(p0: Ad?) {
  400.             }
  401.  
  402.             override fun onLoggingImpression(p0: Ad?) {
  403.             }
  404.         }
  405.         interstitialFb.loadAd(
  406.             interstitialFb.buildLoadAdConfig()
  407.                 .withAdListener(listener)
  408.                 .build()
  409.         )
  410.     }
  411.  
  412.     private fun getSkinStatus() {
  413.         val db = FirebaseFirestore.getInstance()
  414.         db.collection("getSkin")
  415.             .document("button")
  416.             .addSnapshotListener { documentSnapshot, _ ->
  417.                 val getStatus = documentSnapshot?.getBoolean("enable")
  418.                 if (getStatus == true) {
  419.                     btGetSkin.text = getString(R.string.get_skin)
  420.                 } else {
  421.                     //enablefrom firebase
  422.                     //   btGetSkin.text = getString(R.string.get_skin)
  423.                     btGetSkin.text = getString(R.string.rateApp)
  424.                 }
  425.             }
  426.     }
  427.  
  428.     private fun redirectApp() {
  429.         val db = FirebaseFirestore.getInstance()
  430.         db.collection("redirectApp")
  431.             .document("redirect")
  432.             .addSnapshotListener { documentSnapshot, _ ->
  433.                 val getStatus = documentSnapshot?.getBoolean("status")
  434.                 val getLink = documentSnapshot?.getString("link")
  435.                 if (getStatus == true) {
  436.                     val intent = Intent(Intent.ACTION_VIEW)
  437.                     intent.data = Uri.parse(getLink)
  438.                     startActivity(intent)
  439.                 }
  440.             }
  441.     }
  442.  
  443.     @SuppressLint("InflateParams")
  444.     private fun initNativeAds() {
  445.         mainDialog = AlertDialog.Builder(this@MainActivity, R.style.CustomAlertDialog).create()
  446.         dialogView = layoutInflater.inflate(R.layout.dialog_ads, null)
  447.         mainDialog?.setView(dialogView)
  448.         mainDialog?.setTitle("You sure you want to leave?")
  449.         adViewNative = dialogView.findViewById(R.id.native_ad_view) as NativeAdView
  450.         adViewNative.mediaView = adViewNative.findViewById(R.id.media_view)
  451.         adViewNative.visibility = View.GONE
  452.  
  453.         // Register the view used for each individual asset.
  454.         adViewNative.headlineView = adViewNative.findViewById(R.id.primary)
  455.         adViewNative.callToActionView = adViewNative.findViewById(R.id.cta)
  456.         adViewNative.iconView = adViewNative.findViewById(R.id.icon)
  457.         adViewNative.starRatingView = adViewNative.findViewById(R.id.rating_bar)
  458.         adViewNative.advertiserView = adViewNative.findViewById(R.id.secondary)
  459.         mainDialog?.setCancelable(false)
  460.     }
  461.  
  462.     private fun loadDialogNativeAds() {
  463.         val videoOptions = VideoOptions.Builder()
  464.             .setStartMuted(false)
  465.             .build()
  466.         val adOptions = NativeAdOptions.Builder()
  467.             .setVideoOptions(videoOptions)
  468.             .build()
  469.         val adLoader = AdLoader.Builder(this, nativeAdId)
  470.             .forNativeAd { adView: com.google.android.gms.ads.nativead.NativeAd ->
  471.                 // Some assets are guaranteed to be in every UnifiedNativeAd.
  472.                 (adViewNative.headlineView as TextView).text = adView.headline
  473.                 (adViewNative.callToActionView as Button).text = adView.callToAction
  474.  
  475.                 // These assets aren't guaranteed to be in every UnifiedNativeAd, so it's important to
  476.                 // check before trying to display them.
  477.                 val icon = adView.icon
  478.                 if (icon == null) {
  479.                     adViewNative.iconView?.visibility = View.INVISIBLE
  480.                 } else {
  481.                     (adViewNative.iconView as ImageView).setImageDrawable(icon.drawable)
  482.                     adViewNative.iconView?.visibility = View.VISIBLE
  483.                 }
  484.                 if (adView.starRating == null) {
  485.                     adViewNative.starRatingView?.visibility = View.INVISIBLE
  486.                 } else {
  487.                     (adViewNative.starRatingView as RatingBar).rating =
  488.                         adView.starRating?.toString()?.toFloat()!!
  489.                     adViewNative.starRatingView?.visibility = View.VISIBLE
  490.                 }
  491.                 if (adView.advertiser == null) {
  492.                     adViewNative.advertiserView?.visibility = View.INVISIBLE
  493.                 } else {
  494.                     (adViewNative.advertiserView as TextView).text = adView.advertiser
  495.                     adViewNative.advertiserView?.visibility = View.VISIBLE
  496.                 }
  497.  
  498.                 // Assign native ad object to the native view.
  499.                 adViewNative.setNativeAd(adView)
  500.             }.withAdListener(object : AdListener() {
  501.                 override fun onAdLoaded() {
  502.                     super.onAdLoaded()
  503.                     adViewNative.visibility = View.VISIBLE
  504.                 }
  505.  
  506.                 override fun onAdFailedToLoad(p0: LoadAdError?) {
  507.                     super.onAdFailedToLoad(p0)
  508.                     showFan = true
  509.                 }
  510.             }).withNativeAdOptions(adOptions)
  511.             .build()
  512.         adLoader.loadAd(AdRequest.Builder().build())
  513.     }
  514.  
  515.     private fun insertAdsInMenuItems() {
  516.         if (mNativeAds.isEmpty()) {
  517.             return
  518.         }
  519.  
  520.         val offset = itemList.size / mNativeAds.size + 1
  521.         var index = 0
  522.         for (ad in mNativeAds) {
  523.             itemList.add(index, ad)
  524.             index += offset
  525.         }
  526.     }
  527.  
  528.     private fun loadNativeAds() {
  529.         val builder = AdLoader.Builder(this, nativeAdId)
  530.         adLoader = builder.forNativeAd { unifiedNativeAd ->
  531.             // A native ad loaded successfully, check if the ad loader has finished loading
  532.             // and if so, insert the ads into the list.
  533.             mNativeAds.add(unifiedNativeAd)
  534.             if (!adLoader.isLoading) {
  535.                 insertAdsInMenuItems()
  536.             }
  537.         }.withAdListener(
  538.             object : AdListener() {
  539.                 override fun onAdFailedToLoad(@NotNull p0: LoadAdError) {
  540.                     // A native ad failed to load, check if the ad loader has finished loading
  541.                     // and if so, insert the ads into the list.
  542.                     Log.e(
  543.                         "MainActivity",
  544.                         "The previous native ad failed to load. Attempting to" + " load another."
  545.                     )
  546.                     if (!adLoader.isLoading) {
  547.                         insertAdsInMenuItems()
  548.                     }
  549.                 }
  550.             }).build()
  551.  
  552.         // Load the Native Express ad. ini ganti
  553.         adLoader.loadAds(AdRequest.Builder().build(), NUMBER_OF_ADS)
  554.     }
  555.  
  556.     override fun onBackPressed() {
  557.         val drawerLayout: androidx.drawerlayout.widget.DrawerLayout =
  558.             findViewById(R.id.drawer_layout)
  559.         if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
  560.             drawerLayout.closeDrawer(GravityCompat.START)
  561.         } else {
  562.             //app.showAds()
  563.             if (showFan) {
  564.                 mainDialogFan?.show()
  565.             } else {
  566.                 mainDialog?.show()
  567.             }
  568.         }
  569.     }
  570.  
  571.     override fun onCreateOptionsMenu(menu: Menu?): Boolean {
  572.         menuInflater.inflate(R.menu.dashboard, menu)
  573.         return true
  574.     }
  575.  
  576.     override fun onNavigationItemSelected(item: MenuItem): Boolean {
  577.         when (item.itemId) {
  578.             R.id.nav_featured -> {
  579.                 item.isChecked = true
  580.                 if (whatToShow != SHOW_DAILY) {
  581.                     whatToShow = SHOW_DAILY
  582.                     itemList.clear()
  583.                     mNativeAds.clear()
  584.                     loadNativeAds()
  585.                     getDailyList()
  586.                     if (mInterstitialAd != null) {
  587.                         mInterstitialAd?.show(this@MainActivity)
  588.                     } else {
  589.                         UnityAds.show(
  590.                             this@MainActivity,
  591.                             getString(R.string.inters_placement_id)
  592.                         )
  593.                     }
  594.                 }
  595.             }
  596.             R.id.nav_upcoming -> {
  597.                 item.isChecked = true
  598.                 if (whatToShow != SHOW_UPCOMING) {
  599.                     whatToShow = SHOW_UPCOMING
  600.                     itemList.clear()
  601.                     mNativeAds.clear()
  602.                     loadNativeAds()
  603.                     getUpcomingList()
  604.                     if (mInterstitialAd != null) {
  605.                         mInterstitialAd?.show(this@MainActivity)
  606.                     } else {
  607.                         UnityAds.show(
  608.                             this@MainActivity,
  609.                             getString(R.string.inters_placement_id)
  610.                         )
  611.                     }
  612.                 }
  613.             }
  614.             R.id.nav_bundles -> {
  615.                 item.isChecked = true
  616.                 if (whatToShow != SHOW_Bundles) {
  617.                     whatToShow = SHOW_Bundles
  618.                     itemList.clear()
  619.                     mNativeAds.clear()
  620.                     loadNativeAds()
  621.                     getBundlesList()
  622.                     if (mInterstitialAd != null) {
  623.                         mInterstitialAd?.show(this@MainActivity)
  624.                     } else {
  625.                         UnityAds.show(
  626.                             this@MainActivity,
  627.                             getString(R.string.inters_placement_id)
  628.                         )
  629.                     }
  630.                 }
  631.             }
  632.             R.id.nav_rate -> {
  633.                 val appPackageName = packageName // getPackageName() from Context or Activity object
  634.                 try {
  635.                     startActivity(
  636.                         Intent(
  637.                             Intent.ACTION_VIEW,
  638.                             Uri.parse("market://details?id=$appPackageName")
  639.                         )
  640.                     )
  641.                 } catch (anfe: android.content.ActivityNotFoundException) {
  642.                     startActivity(
  643.                         Intent(
  644.                             Intent.ACTION_VIEW,
  645.                             Uri.parse("https://play.google.com/store/apps/details?id=$appPackageName")
  646.                         )
  647.                     )
  648.                 }
  649.  
  650.             }
  651.         }
  652.         val drawerLayout: androidx.drawerlayout.widget.DrawerLayout =
  653.             findViewById(R.id.drawer_layout)
  654.         drawerLayout.closeDrawer(GravityCompat.START)
  655.         return true
  656.     }
  657.  
  658.     private fun setupRecyclerView() {
  659.         val gridLayoutManager = GridLayoutManager(this, 2)
  660.         adapter = AdapterDaily(this, itemList)
  661.         recyclerView.layoutManager = gridLayoutManager
  662.         recyclerView.adapter = adapter
  663.     }
  664.  
  665.     private fun setupNavigation() {
  666.         val drawerLayout: androidx.drawerlayout.widget.DrawerLayout =
  667.             findViewById(R.id.drawer_layout)
  668.         val navView: NavigationView = findViewById(R.id.nav_view)
  669.         val toggle = ActionBarDrawerToggle(
  670.             this,
  671.             drawerLayout,
  672.             toolbar,
  673.             R.string.nav_open,
  674.             R.string.nav_close
  675.         )
  676.         drawerLayout.addDrawerListener(toggle)
  677.         toggle.syncState()
  678.         navView.setNavigationItemSelectedListener(this)
  679.     }
  680.  
  681.     private fun setupAds() {
  682.         val db = FirebaseFirestore.getInstance()
  683.         db.collection("admob")
  684.             .document("adList")
  685.             .get()
  686.             .addOnSuccessListener {
  687.                 val getId = it.getString("interstitial").toString()
  688.  
  689.                 val adRequest = AdRequest.Builder().build()
  690.  
  691.                 InterstitialAd.load(
  692.                     this@MainActivity,
  693.                     getId,
  694.                     adRequest,
  695.                     object : InterstitialAdLoadCallback() {
  696.                         override fun onAdFailedToLoad(adError: LoadAdError) {
  697.                             mInterstitialAd = null
  698.                         }
  699.  
  700.                         override fun onAdLoaded(interstitialAd: InterstitialAd) {
  701.                             mInterstitialAd = interstitialAd
  702.                         }
  703.                     })
  704.             }.addOnFailureListener {
  705.                 Toast.makeText(
  706.                     this,
  707.                     "Terjadi kesalahan, silakan coba lagi",
  708.                     Toast.LENGTH_SHORT
  709.                 ).show()
  710.             }
  711.     }
  712.  
  713.     private fun setupClickListener() {
  714.         btGetSkin.setOnClickListener {
  715.             //            if (mInterstitialAd!!.isLoaded) {
  716. //                mInterstitialAd!!.show()
  717. //            }
  718.             // progress()
  719.             if (btGetSkin.text == getString(R.string.rateApp)) {
  720.                 try {
  721.                     val playstoreuri1: Uri = Uri.parse("market://details?id=$packageName")
  722.                     val playstoreIntent1 = Intent(Intent.ACTION_VIEW, playstoreuri1)
  723.                     startActivity(playstoreIntent1)
  724.                 } catch (exp: Exception) {
  725.                     val playstoreuri2: Uri =
  726.                         Uri.parse("http://play.google.com/store/apps/details?id=$packageName")
  727.                     val playstoreIntent2 = Intent(Intent.ACTION_VIEW, playstoreuri2)
  728.                     startActivity(playstoreIntent2)
  729.                 }
  730.             } else {
  731.                 askName()
  732.             }
  733.         }
  734.     }
  735.  
  736.     private fun requestConsent() {
  737.         val consentInformation = ConsentInformation.getInstance(this@MainActivity)
  738.         val publisherIds = arrayOf(publisherIds)
  739.         consentInformation.requestConsentInfoUpdate(
  740.             publisherIds,
  741.             object : ConsentInfoUpdateListener {
  742.                 override fun onConsentInfoUpdated(consentStatus: ConsentStatus) {
  743.                     // User's consent status successfully updated.
  744.                     Log.d(TAG, "onConsentInfoUpdated")
  745.                     when (consentStatus) {
  746.                         ConsentStatus.PERSONALIZED -> {
  747.                             Log.d(TAG, "PERSONALIZED")
  748.                             ConsentInformation.getInstance(this@MainActivity).consentStatus =
  749.                                 ConsentStatus.PERSONALIZED
  750.                         }
  751.  
  752.                         ConsentStatus.NON_PERSONALIZED -> {
  753.                             Log.d(TAG, "NON_PERSONALIZED")
  754.                             ConsentInformation.getInstance(this@MainActivity).consentStatus =
  755.                                 ConsentStatus.PERSONALIZED
  756.                         }
  757.  
  758.                         ConsentStatus.UNKNOWN -> {
  759.                             Log.d(TAG, "UNKNOWN")
  760.                             if (ConsentInformation.getInstance(this@MainActivity).isRequestLocationInEeaOrUnknown) {
  761.                                 var privacyUrl: URL? = null
  762.                                 try {
  763.                                     // TODO: Replace with your app's privacy policy URL.
  764.                                     privacyUrl =
  765.                                         URL("https://akbarkur.blogspot.com/2019/07/privacy-policy-finder-apps.html")
  766.                                 } catch (e: MalformedURLException) {
  767.                                     e.printStackTrace()
  768.                                     // Handle error.
  769.                                 }
  770.  
  771.                                 consentForm = ConsentForm.Builder(this@MainActivity, privacyUrl)
  772.                                     .withListener(object : ConsentFormListener() {
  773.                                         override fun onConsentFormLoaded() {
  774.                                             // Consent consentForm loaded successfully.
  775.                                             Log.d(TAG, "onConsentFormLoaded")
  776.                                             if (consentForm != null) {
  777.                                                 Log.d(TAG, "show ok")
  778.                                                 consentForm!!.show()
  779.                                             }
  780.                                         }
  781.  
  782.                                         override fun onConsentFormOpened() {
  783.                                             // Consent consentForm was displayed.
  784.                                             Log.d(TAG, "onConsentFormOpened")
  785.                                         }
  786.  
  787.                                         override fun onConsentFormClosed(
  788.                                             consentStatus: ConsentStatus?,
  789.                                             userPrefersAdFree: Boolean?
  790.                                         ) {
  791.                                             // Consent consentForm was closed.
  792.                                             Log.d(TAG, "onConsentFormClosed")
  793.                                         }
  794.  
  795.                                         override fun onConsentFormError(errorDescription: String?) {
  796.                                             // Consent consentForm error.
  797.                                             Log.d(TAG, "onConsentFormError")
  798.                                             Log.d(TAG, errorDescription.toString())
  799.                                         }
  800.                                     })
  801.                                     .withPersonalizedAdsOption()
  802.                                     .withNonPersonalizedAdsOption()
  803.                                     .build()
  804.  
  805.                                 consentForm!!.load()
  806.  
  807.                             } else {
  808.                                 Log.d(TAG, "PERSONALIZED else")
  809.                                 ConsentInformation.getInstance(this@MainActivity).consentStatus =
  810.                                     ConsentStatus.PERSONALIZED
  811.                             }
  812.                         }
  813.  
  814.                         else -> {
  815.                         }
  816.                     }
  817.                 }
  818.  
  819.                 override fun onFailedToUpdateConsentInfo(errorDescription: String) {
  820.                     // User's consent status failed to update.
  821.                     Log.d(TAG, "onFailedToUpdateConsentInfo")
  822.                     Log.d(TAG, errorDescription)
  823.                 }
  824.             })
  825.     }
  826.  
  827.     private fun getDailyList() {
  828.         adapter.setData(arrayListOf())
  829.  
  830.         progressbar.visibility = View.VISIBLE
  831.         network.getDailyList(BuildConfig.AUTHORIZATION)
  832.             .enqueue(object : Callback<DailyItemsResponse> {
  833.                 override fun onFailure(call: Call<DailyItemsResponse>, t: Throwable) {
  834.                     progressbar.visibility = View.GONE
  835.                     Snackbar.make(recyclerView, "No internet connection", Snackbar.LENGTH_SHORT)
  836.                         .setAction("Try Again") { getDailyList() }
  837.                         .show()
  838.                 }
  839.  
  840.                 override fun onResponse(
  841.                     call: Call<DailyItemsResponse>,
  842.                     response: Response<DailyItemsResponse>
  843.                 ) {
  844.                     progressbar.visibility = View.GONE
  845.                     if (response.isSuccessful) {
  846.                         response.body()?.data?.let { adapter.setData(it) }
  847.                     }
  848.                 }
  849.             })
  850.     }
  851.  
  852.     private fun getUpcomingList() {
  853.         adapter.setData(arrayListOf())
  854.  
  855.         progressbar.visibility = View.VISIBLE
  856.         network.getUpcomming(BuildConfig.AUTHORIZATION)
  857.             .enqueue(object : Callback<UpcomingItemsResponse> {
  858.                 override fun onFailure(call: Call<UpcomingItemsResponse>, t: Throwable) {
  859.                     progressbar.visibility = View.GONE
  860.                     Snackbar.make(recyclerView, "No internet connection", Snackbar.LENGTH_SHORT)
  861.                         .setAction("Try Again") { getUpcomingList() }
  862.                         .show()
  863.                 }
  864.  
  865.                 override fun onResponse(
  866.                     call: Call<UpcomingItemsResponse>,
  867.                     response: Response<UpcomingItemsResponse>
  868.                 ) {
  869.                     progressbar.visibility = View.GONE
  870.                     if (response.isSuccessful) {
  871.                         adapter.setData(response.body()!!.data)
  872.                     }
  873.                 }
  874.             })
  875.     }
  876.  
  877.     private fun getBundlesList() {
  878.         adapter.setData(arrayListOf())
  879.  
  880.         progressbar.visibility = View.VISIBLE
  881.         network.getBundles(BuildConfig.AUTHORIZATION)
  882.             .enqueue(object : Callback<BundlesResponse> {
  883.                 override fun onFailure(call: Call<BundlesResponse>, t: Throwable) {
  884.                     progressbar.visibility = View.GONE
  885.                     Snackbar.make(recyclerView, "No internet connection", Snackbar.LENGTH_SHORT)
  886.                         .setAction("Try Again") { getBundlesList() }
  887.                         .show()
  888.                 }
  889.  
  890.                 override fun onResponse(
  891.                     call: Call<BundlesResponse>,
  892.                     response: Response<BundlesResponse>
  893.                 ) {
  894.                     progressbar.visibility = View.GONE
  895.                     if (response.isSuccessful) {
  896.                         adapter.setData(response.body()!!.data)
  897.                     }
  898.                 }
  899.             })
  900.     }
  901.  
  902.     internal var name = ""
  903.  
  904.     @SuppressLint("InflateParams")
  905.     private fun askName() {
  906.         mDialog.show()
  907.     }
  908.  
  909.     private fun notifyNoName() {
  910.         mDialogBuilder = MaterialAlertDialogBuilder(this@MainActivity, R.style.MyDialogTheme)
  911.  
  912.         mDialogBuilder.setMessage("please insert Valid Battle Royale Username")
  913.         mDialogBuilder.setTitle("Invalid User")
  914.         mDialogBuilder.setCancelable(true)
  915.         mDialogBuilder.setNeutralButton(
  916.             "Cancel"
  917.         ) { dialog, _ -> dialog.dismiss() }
  918.         mDialogBuilder.show()
  919.     }
  920.  
  921.     private var progressDialog: ProgressDialog? = null
  922.  
  923.     private fun progress() {
  924.         progressDialog = ProgressDialog(this@MainActivity, R.style.MyDialogTheme)
  925.         progressDialog?.max = 100
  926.         progressDialog?.setMessage("Getting Skins")
  927.         progressDialog?.setTitle("Processing...")
  928.         progressDialog?.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL)
  929.         progressDialog?.setCancelable(false)
  930.         progressDialog?.show()
  931.         val aboutTo = progressDialog!!.max - 50
  932.         val middle = progressDialog!!.max - progressDialog!!.max / 2
  933.         val maxAboutToMsg = resources.getStringArray(R.array.progressEnd).size
  934.         val maxMiddleToMsg = resources.getStringArray(R.array.progressMiddle).size
  935.         val maxBeginToMsg = resources.getStringArray(R.array.progressBegin).size
  936.  
  937.         DownloadTask(
  938.             progressDialog!!.max,
  939.             aboutTo,
  940.             middle,
  941.             maxAboutToMsg,
  942.             maxMiddleToMsg,
  943.             maxBeginToMsg
  944.         ).execute()
  945.     }
  946.  
  947.     //    companion object {
  948. //        private const val TAG = "MainActivity ----- : "
  949. //    }
  950.     internal var random = Random()
  951.  
  952.     @SuppressLint("StaticFieldLeak")
  953.     private inner class DownloadTask(
  954.         var max: Int,
  955.         var aboutTo: Int,
  956.         var middle: Int,
  957.         var maxAboutToMsg: Int,
  958.         var maxMiddleToMsg: Int,
  959.         var maxBeginToMsg: Int
  960.     ) :
  961.         AsyncTask<String, Int, Void>() {
  962.         override fun doInBackground(vararg params: String?): Void? {
  963.             while (count <= max) {
  964.                 try {
  965.                     val sl = random.nextInt(800 - 600) + 200
  966.                     //                    int sl = random.nextInt(20 - 10) + 10;
  967.                     Thread.sleep(sl.toLong())
  968.                     //                    Log.i("sleep", "sleeping for " + sl + " milliseconds");
  969.                     progressDialog!!.incrementProgressBy(1)
  970.                     publishProgress(progressDialog!!.progress)
  971.                 } catch (e: InterruptedException) {
  972.                     e.printStackTrace()
  973.                 }
  974.  
  975.                 count++
  976.             }
  977.             return null
  978.         }
  979.  
  980.         var count = 0
  981.  
  982.         override fun onPostExecute(result: Void?) {
  983.             super.onPostExecute(result)
  984.             progressDialog?.dismiss()
  985.             almostDone()
  986.         }
  987.  
  988.         override fun onProgressUpdate(vararg values: Int?) {
  989.             super.onProgressUpdate(*values)
  990.             when {
  991.                 progressDialog?.progress == progressDialog?.max -> {
  992.                     // progressDialog?.dismiss()
  993.                     // almostDone()
  994.                     //progressDialog.dismiss();
  995.                 }
  996.                 progressDialog?.progress!! >= aboutTo -> progressDialog?.setMessage(
  997.                     resources.getStringArray(
  998.                         R.array.progressEnd
  999.                     )[random.nextInt(maxAboutToMsg)]
  1000.                 )
  1001.                 progressDialog?.progress!! >= middle -> progressDialog?.setMessage(
  1002.                     resources.getStringArray(
  1003.                         R.array.progressMiddle
  1004.                     )[random.nextInt(maxMiddleToMsg)]
  1005.                 )
  1006.                 else -> progressDialog?.setMessage(
  1007.                     resources.getStringArray(R.array.progressBegin)[random.nextInt(
  1008.                         maxBeginToMsg
  1009.                     )]
  1010.                 )
  1011.             }
  1012.  
  1013.         }
  1014.     }
  1015.  
  1016.     fun almostDone() {
  1017.         if (mInterstitialAd != null) {
  1018.             mInterstitialAd?.show(this@MainActivity)
  1019.             mInterstitialAd?.fullScreenContentCallback =
  1020.                 object : FullScreenContentCallback() {
  1021.                     override fun onAdDismissedFullScreenContent() {
  1022.                         warn(
  1023.                             getString(R.string.successfulDownloadAttemptTitle),
  1024.                             getString(R.string.successfulDownloadAttemptMessege)
  1025.                         )
  1026.                     }
  1027.  
  1028.                     override fun onAdFailedToShowFullScreenContent(adError: AdError?) {
  1029.  
  1030.                     }
  1031.  
  1032.                     override fun onAdShowedFullScreenContent() {
  1033.                         mInterstitialAd = null
  1034.                         warn(
  1035.                             getString(R.string.successfulDownloadAttemptMessege),
  1036.                             getString(R.string.successfulDownloadAttemptTitle)
  1037.                         )
  1038.                     }
  1039.                 }
  1040.         } else {
  1041.             if (interstitialFb.isAdLoaded) {
  1042.                 interstitialFb.show()
  1043.                 warn(
  1044.                     getString(R.string.successfulDownloadAttemptMessege),
  1045.                     getString(R.string.successfulDownloadAttemptTitle)
  1046.                 )
  1047.             } else {
  1048.                 UnityAds.show(
  1049.                     this@MainActivity,
  1050.                     getString(R.string.inters_placement_id)
  1051.                 )
  1052.                 UnityAds.initialize(this, getString(R.string.unity_ad_id),
  1053.                     object : IUnityAdsListener {
  1054.  
  1055.                         override fun onUnityAdsStart(p0: String?) {
  1056.                         }
  1057.  
  1058.                         override fun onUnityAdsFinish(
  1059.                             p0: String?,
  1060.                             p1: UnityAds.FinishState?
  1061.                         ) {
  1062.                             warn(
  1063.                                 getString(R.string.successfulDownloadAttemptMessege),
  1064.                                 getString(R.string.successfulDownloadAttemptTitle)
  1065.                             )
  1066.                         }
  1067.  
  1068.                         override fun onUnityAdsError(
  1069.                             p0: UnityAds.UnityAdsError?,
  1070.                             p1: String?
  1071.                         ) {
  1072.                             warn(
  1073.                                 getString(R.string.successfulDownloadAttemptMessege),
  1074.                                 getString(R.string.successfulDownloadAttemptTitle)
  1075.                             )
  1076.                         }
  1077.  
  1078.                         override fun onUnityAdsReady(p0: String?) {
  1079.  
  1080.                         }
  1081.  
  1082.                     })
  1083.             }
  1084.         }
  1085.         btGetSkin.text = getString(R.string.rateApp)
  1086.     }
  1087.  
  1088.     @SuppressLint("InflateParams")
  1089.     private fun pleaseReview() {
  1090.         mDialogBuilder = MaterialAlertDialogBuilder(this@MainActivity, R.style.MyDialogTheme)
  1091.         mDialogBuilder.setView(layoutInflater.inflate(R.layout.rate_view, null))
  1092.  
  1093.         mDialogBuilder.setTitle(getString(R.string.askToRateTitle))
  1094.         mDialogBuilder.setMessage(getString(R.string.askToRateMessage))
  1095.         mDialogBuilder.setCancelable(false)
  1096.         //Setting OK Button
  1097.         mDialogBuilder.setPositiveButton("RATE NOW") { dialog, _ ->
  1098.             try {
  1099.                 startActivity(
  1100.                     Intent(
  1101.                         Intent.ACTION_VIEW,
  1102.                         Uri.parse("market://details?id=" + applicationContext.packageName)
  1103.                     )
  1104.                 )
  1105.             } catch (e: android.content.ActivityNotFoundException) {
  1106.                 startActivity(
  1107.                     Intent(
  1108.                         Intent.ACTION_VIEW,
  1109.                         Uri.parse("http://play.google.com/store/apps/details?id=" + applicationContext.packageName)
  1110.                     )
  1111.                 )
  1112.             }
  1113.  
  1114.             dialog.dismiss()
  1115.         }
  1116.         mDialogBuilder.setNegativeButton(
  1117.             "Later"
  1118.         ) { dialog, _ -> dialog.dismiss() }
  1119.         mDialogBuilder.show()
  1120.     }
  1121.  
  1122.     fun warn(s: String, s2: String) {
  1123.         mDialogBuilder = MaterialAlertDialogBuilder(this@MainActivity, R.style.MyDialogTheme)
  1124.         mDialogBuilder.setTitle(s)
  1125.         mDialogBuilder.setMessage(s2)
  1126.         mDialogBuilder.setCancelable(false)
  1127.         //Setting OK Button
  1128.         mDialogBuilder.setPositiveButton("OK") { dialog, _ ->
  1129.             pleaseReview()
  1130.             dialog.dismiss()
  1131.         }
  1132.         mDialogBuilder.show()
  1133.     }
  1134. }
Add Comment
Please, Sign In to add comment