~gardenapple/mitch

ref: c4082deb0c7a0157103ade7b64cee8fb2ea236e7 mitch/app/src/main/java/ua/gardenapple/itchupdater/ui/BrowseFragment.kt -rw-r--r-- 34.8 KiB
c4082debgardenapple Adapt to game jam & forum themes 2 months ago
                                                                                
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
package ua.gardenapple.itchupdater.ui

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.text.Html
import android.util.Log
import android.view.*
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.webkit.*
import android.widget.ProgressBar
import android.widget.Toast
import androidx.annotation.Keep
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.widget.Toolbar
import androidx.core.app.ShareCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.preference.PreferenceManager
import com.google.android.material.textfield.TextInputEditText
import com.leinardi.android.speeddial.SpeedDialActionItem
import kotlinx.coroutines.*
import org.jsoup.Jsoup
import org.jsoup.nodes.Document
import ua.gardenapple.itchupdater.ItchWebsiteUtils
import ua.gardenapple.itchupdater.PREF_WEB_ANDROID_FILTER
import ua.gardenapple.itchupdater.R
import ua.gardenapple.itchupdater.Utils
import ua.gardenapple.itchupdater.client.ItchBrowseHandler
import ua.gardenapple.itchupdater.client.ItchWebsiteParser
import ua.gardenapple.itchupdater.client.SpecialBundleHandler
import ua.gardenapple.itchupdater.databinding.BrowseFragmentBinding
import ua.gardenapple.itchupdater.databinding.DialogWebPromptBinding
import java.io.ByteArrayInputStream
import java.io.File


class BrowseFragment : Fragment(), CoroutineScope by MainScope() {

    companion object {
        private const val LOGGING_TAG = "BrowseFragment"
        private const val WEB_VIEW_STATE_KEY: String = "WebView"
    }
    
    private var _binding: BrowseFragmentBinding? = null
    private val binding get() = _binding!!
    
    private lateinit var chromeClient: MitchWebChromeClient
    lateinit var webView: MitchWebView
        private set

    var browseHandler: ItchBrowseHandler? = null
    private var currentDoc: Document? = null
    private var currentInfo: ItchBrowseHandler.Info? = null


    override fun onAttach(context: Context) {
        super.onAttach(context)
        browseHandler = ItchBrowseHandler(context, this)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        super.onCreateView(inflater, container, savedInstanceState)

        _binding = BrowseFragmentBinding.inflate(inflater, container, false)
        webView = binding.webView
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        chromeClient = MitchWebChromeClient()

        @SuppressLint("SetJavaScriptEnabled")
        webView.settings.javaScriptEnabled = true
        webView.settings.domStorageEnabled = true
        webView.settings.setAppCacheEnabled(true)
        webView.settings.setAppCachePath(File(requireContext().filesDir, "html5-app-cache").path)
        webView.settings.databaseEnabled = true

        webView.settings.mediaPlaybackRequiresUserGesture = false
        webView.webViewClient = MitchWebViewClient()
        webView.webChromeClient = chromeClient

        webView.addJavascriptInterface(ItchJavaScriptInterface(this), "mitchCustomJS")

        webView.setDownloadListener { url, _, contentDisposition, mimeType, _ ->
            Log.d(LOGGING_TAG, "Requesting download...")
            runBlocking(Dispatchers.IO) {
                browseHandler?.onDownloadStarted(url, contentDisposition, mimeType)
            }
        }


        //Set up FAB buttons
        //(colors don't matter too much as they will be set by updateUI() anyway)
        val speedDialView = (activity as MainActivity).binding.speedDial
        speedDialView.clearActionItems()
        speedDialView.addActionItem(SpeedDialActionItem.Builder(R.id.browser_reload, R.drawable.ic_baseline_refresh_24)
            .setLabel(R.string.browser_reload)
            .create()
        )
        speedDialView.addActionItem(SpeedDialActionItem.Builder(R.id.browser_search, R.drawable.ic_baseline_search_24)
            .setLabel(R.string.browser_search)
            .create()
        )
        speedDialView.addActionItem(SpeedDialActionItem.Builder(R.id.browser_open_in_browser, R.drawable.ic_baseline_open_in_browser_24)
            .setLabel(R.string.browser_open_in_browser)
            .create()
        )
        speedDialView.addActionItem(SpeedDialActionItem.Builder(R.id.browser_share, R.drawable.ic_baseline_share_24)
            .setLabel(R.string.browser_share)
            .create()
        )
        
        speedDialView.setOnActionSelectedListener { actionItem ->
            speedDialView.close()

            when (actionItem.id) {
                R.id.browser_reload -> {
                    webView.reload()
                    return@setOnActionSelectedListener true
                }
                R.id.browser_share -> {
                    ShareCompat.IntentBuilder.from(requireActivity())
                        .setType("text/plain")
                        .setChooserTitle(R.string.browser_share)
                        .setText(webView.url)
                        .startChooser()
                    return@setOnActionSelectedListener true
                }
                R.id.browser_open_in_browser -> {
                    val intent = Intent(Intent.ACTION_VIEW)
                    intent.data = Uri.parse(webView.url)

                    val title = resources.getString(R.string.browser_open_in_browser)
                    val chooser = Intent.createChooser(intent, title)

                    startActivity(chooser)
                    return@setOnActionSelectedListener true
                }
                R.id.browser_search -> {
                    //Search dialog

                    val viewInflated: View = LayoutInflater.from(context)
                        .inflate(R.layout.dialog_search, getView() as ViewGroup?, false)

                    val input = viewInflated.findViewById<TextInputEditText>(R.id.input)

                    val alertDialog = AlertDialog.Builder(requireContext())
                        .setTitle(R.string.browser_search)
                        .setView(viewInflated)
                        .setPositiveButton(android.R.string.ok) { dialog, _ ->
                            dialog.dismiss()
                            webView.loadUrl(ItchWebsiteUtils.getSearchUrl(input.text.toString()))
                        }
                        .setNegativeButton(android.R.string.cancel) { dialog, _ ->
                            dialog.cancel()
                        }
                        .show()

                    //Show keyboard automatically
                    input.post {
                        input.isFocusableInTouchMode = true
                        input.requestFocus()

                        input.postDelayed({
                            val inputMethodManager =
                                requireContext().getSystemService(Context.INPUT_METHOD_SERVICE)
                                        as InputMethodManager
                            inputMethodManager.showSoftInput(input,
                                InputMethodManager.SHOW_IMPLICIT)
                        }, 300)
                    }

                    input.setOnEditorActionListener { _, actionId, _ ->
                        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                            alertDialog.dismiss()
                            webView.loadUrl(ItchWebsiteUtils.getSearchUrl(input.text.toString()))
                            return@setOnEditorActionListener true
                        }
                        false
                    }

                    return@setOnActionSelectedListener true
                }
                else -> {
                    return@setOnActionSelectedListener false
                }
            }
        }


        //Load page, this will also update the UI
        if(savedInstanceState?.getBundle(WEB_VIEW_STATE_KEY) != null) {
            webView.restoreState(savedInstanceState.getBundle(WEB_VIEW_STATE_KEY))
        } else {
            webView.loadUrl(ItchWebsiteUtils.getMainBrowsePage(requireContext()))
        }
    }

    /**
     * @return true if the user can't go back in the web history
     */
    fun onBackPressed(): Boolean {
        val customViewCallback = chromeClient.customViewCallback
        if (customViewCallback != null) {
            customViewCallback.onCustomViewHidden()
            return false
        }

        if(webView.canGoBack()) {
            webView.goBack()
            return false
        }
        return true
    }

    override fun onSaveInstanceState(outState: Bundle) {
        val webViewState = Bundle()
        webView.saveState(webViewState)
        outState.putBundle(WEB_VIEW_STATE_KEY, webViewState)

        super.onSaveInstanceState(outState)
    }

    override fun onPause() {
        super.onPause()

        CookieManager.getInstance().flush()
    }

    override fun onResume() {
        super.onResume()
        
        chromeClient.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()

        requireContext().stopService(Intent(context, WebViewForegroundService::class.java))

        cancel()
    }

    override fun onDetach() {
        super.onDetach()

        browseHandler = null
    }

    override fun onDestroyView() {
        super.onDestroyView()

        _binding = null
    }

    val isWebFullscreen: Boolean
        get() = chromeClient.customViewCallback != null

    /**
     * Adapts the app's UI to the theme of the current web page.
     */
    fun updateUI() {
        updateUI(currentDoc, currentInfo)
    }

    fun restoreDefaultUI() {
        updateUI(null, null)
    }

    //TODO: hide stuff on scroll?
    /**
     * Adapts the app's UI to the theme of a web page. Should only affect the UI while the browse
     * fragment is selected.
     * Must run on the UI thread!
     * @param doc the parsed DOM of the page the user is currently on. Null if the UI shouldn't adapt to any web page at all
     * @param info metadata about the page
     */
    private fun updateUI(doc: Document?, info: ItchBrowseHandler.Info?) {
        if (!this::chromeClient.isInitialized || isWebFullscreen)
            return

        val mainActivity = activity as? MainActivity ?: return

        if (!isVisible && doc != null)
            return

        //Log.d(LOGGING_TAG, "Processing UI...")

        val navBar = mainActivity.binding.bottomNavigationView
        val bottomGameBar = mainActivity.binding.bottomGameBar
        val fab = mainActivity.binding.speedDial
        val supportAppBar = mainActivity.supportActionBar!!
        val appBar = mainActivity.binding.toolbar
        val gameButton = mainActivity.binding.gameButton
        val gameButtonInfo = mainActivity.binding.gameButtonInfo

        fab.show()

        if (doc?.let { ItchWebsiteUtils.isGamePage(doc) } == true) {
            //Hide app's navbar after hiding web navbar
            val navBarHideCallback: (String) -> Unit = navBarHide@{
                if (!isVisible)
                    return@navBarHide

                navBar.visibility = View.GONE
                if (info?.purchasedInfo != null) {
                    bottomGameBar.visibility = View.VISIBLE

                    if (info.hasAndroidVersion)
                        gameButton.text = getString(R.string.game_install)
                    else
                        gameButton.text = getString(R.string.game_download)
                    gameButtonInfo.text =
                        Utils.spannedFromHtml(info.purchasedInfo.ownershipReasonHtml)

                    gameButton.setOnClickListener {
                        mainActivity.browseUrl(info.purchasedInfo.downloadPage)
                    }
                } else if (info?.isFromSpecialBundle == true) {
                    bottomGameBar.visibility = View.VISIBLE

                    gameButton.text = getString(R.string.game_bundle_claim)
                    if (info.isSpecialBundlePalestinian)
                        gameButtonInfo.text = getString(R.string.game_bundle_palestine)
                    else
                        gameButtonInfo.text = getString(R.string.game_bundle_justice)

                    gameButton.setOnClickListener {
                        lifecycleScope.launch {
                            SpecialBundleHandler.claimGame(
                                info.bundleDownloadLink!!,
                                info.game!!
                            )
                            webView.reload()
                        }
                    }
                } else if (info?.paymentInfo != null) {
                    bottomGameBar.visibility = View.VISIBLE

                    if (!info.paymentInfo.isPaymentOptional)
                        gameButton.text = getString(R.string.game_buy)
                    else if (info.hasAndroidVersion)
                        gameButton.text = getString(R.string.game_install)
                    else
                        gameButton.text = getString(R.string.game_download)

                    gameButtonInfo.text =
                        Utils.spannedFromHtml(info.paymentInfo.messageHtml)

                    gameButton.setOnClickListener {
                        val purchaseUri = Uri.parse(info.game!!.storeUrl)
                            .buildUpon()
                            .appendPath("purchase")
                        mainActivity.browseUrl(purchaseUri.toString())
                    }
                } else {
                    bottomGameBar.visibility = View.GONE
                }
            }
            if (ItchWebsiteUtils.siteHasNavbar(webView, doc)) {
                setSiteNavbarVisibility(false, navBarHideCallback)
            } else {
                setSiteNavbarVisibility(true, navBarHideCallback)
            }

            supportAppBar.title =
                Utils.spannedFromHtml("<b>${Html.escapeHtml(ItchWebsiteParser.getGameName(doc))}</b>")

            appBar.menu.clear()
            addAppBarActions(appBar, doc)
            addDefaultAppBarActions(appBar)
            supportAppBar.show()
        } else if (doc?.let { ItchWebsiteUtils.isUserPage(it) } == true) {
            val appBarTitle =
                "<b>${Html.escapeHtml(ItchWebsiteParser.getUserName(doc))}</b>"
            supportAppBar.title = Utils.spannedFromHtml(appBarTitle)

            appBar.menu.clear()
            addDefaultAppBarActions(appBar)
            supportAppBar.show()

            bottomGameBar.visibility = View.GONE
            navBar.visibility = View.GONE
        } else if (doc?.let { ItchWebsiteUtils.isUserPage(it) } == true) {
            val appBarTitle =
                "<b>${Html.escapeHtml(ItchWebsiteParser.getUserName(doc))}</b>"
            supportAppBar.title = Utils.spannedFromHtml(appBarTitle)

            appBar.menu.clear()
            addDefaultAppBarActions(appBar)
            supportAppBar.show()

            bottomGameBar.visibility = View.GONE
            navBar.visibility = View.GONE
        } else if (doc?.let { ItchWebsiteUtils.isJamOrForumPage(it) } == true) {
            val appBarTitle =
                "<b>${Html.escapeHtml(ItchWebsiteParser.getForumOrJamName(doc))}</b>"
            supportAppBar.title = Utils.spannedFromHtml(appBarTitle)

            appBar.menu.clear()
            addDefaultAppBarActions(appBar)
            supportAppBar.show()

            bottomGameBar.visibility = View.GONE
            navBar.visibility = View.GONE
        } else {
            navBar.visibility = View.VISIBLE
            bottomGameBar.visibility = View.GONE
            supportAppBar.hide()
        }

        //Colors adapt to game theme

        val defaultAccentColor = Utils.getColor(requireContext(), R.color.colorAccent)
        val defaultWhiteColor = Utils.getColor(requireContext(), R.color.colorPrimary)
        val defaultBlackColor = Utils.getColor(requireContext(), R.color.colorPrimaryDark)

        val defaultBgColor = Utils.getColor(requireContext(), R.color.colorBackground)
        val defaultFgColor = Utils.getColor(requireContext(), R.color.colorForeground)

        val gameThemeBgColor = doc?.run { ItchWebsiteUtils.getBackgroundUIColor(doc) }
        val gameThemeButtonColor = doc?.run { ItchWebsiteUtils.getAccentUIColor(doc) }
        val gameThemeButtonFgColor = doc?.run { ItchWebsiteUtils.getAccentFgUIColor(doc) }

//        Log.d(LOGGING_TAG, "game theme bg color: $gameThemeBgColor")
//        Log.d(LOGGING_TAG, "game theme button color: $gameThemeButtonColor")
//        Log.d(LOGGING_TAG, "game theme button fg color: $gameThemeButtonFgColor")

        val accentColor = gameThemeButtonColor ?: defaultAccentColor
        val accentFgColor = gameThemeButtonFgColor ?: defaultWhiteColor

        val bgColor = gameThemeBgColor ?: defaultBgColor
        val fgColor = if (gameThemeBgColor == null) defaultFgColor else defaultWhiteColor

        fab.mainFabClosedBackgroundColor = accentColor
        fab.mainFabOpenedBackgroundColor = accentColor
        fab.mainFabClosedIconColor = accentFgColor
        fab.mainFabOpenedIconColor = accentFgColor
        for (actionItem in fab.actionItems) {
            val newActionItem = SpeedDialActionItem.Builder(actionItem)
                .setFabBackgroundColor(bgColor)
                .setFabImageTintColor(fgColor)
                .setLabelBackgroundColor(bgColor)
                .setLabelColor(fgColor)
                .create()
            fab.replaceActionItem(actionItem, newActionItem)
        }
        binding.progressBar.progressDrawable.setTint(accentColor)
        appBar.setBackgroundColor(bgColor)
        appBar.setTitleTextColor(fgColor)
        appBar.overflowIcon?.setTint(fgColor)

        bottomGameBar.setBackgroundColor(bgColor)
        gameButtonInfo.setTextColor(defaultWhiteColor)
        gameButton.setTextColor(accentFgColor)
        gameButton.setBackgroundColor(accentColor)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            mainActivity.window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
            mainActivity.window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            mainActivity.window.statusBarColor = bgColor
            if (fgColor == defaultBlackColor)
                mainActivity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
            else
                mainActivity.window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_VISIBLE
        }
    }

    /**
     * Add app bar actions from itch.io toolbar, which appears on game pages and devlog pages.
     * Does not clear the current list.
     *
     * Should run on the UI thread!
     *
     * @param doc the parsed HTML document of a game store page or devlog page
     * @param appBar the app UI's top Toolbar
     */
    private fun addAppBarActions(appBar: Toolbar, doc: Document) {
        appBar.menu.clear()

        val navbarItems = doc.getElementById("user_tools")?.children() ?: return

        while (navbarItems.isNotEmpty()) {
            val item = navbarItems.last()
            val url = item.child(0).attr("href")

            if (item.getElementsByClass("related_games_btn").isNotEmpty()) {
                appBar.menu.add(Menu.NONE, 5, 5, R.string.menu_game_related)
                    .setOnMenuItemClickListener {
                        webView.loadUrl(url)
                        true
                    }

            } else if (item.getElementsByClass("rate_game_btn").isNotEmpty()) {
                appBar.menu.add(Menu.NONE, 4, 4, R.string.menu_game_rate)
                    .setOnMenuItemClickListener {
                        webView.loadUrl(url)
                        true
                    }
                    .setIcon(R.drawable.ic_baseline_rate_review_24)
                    .setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM)

            } else if (item.hasClass("devlog_link")) {
                appBar.menu.add(Menu.NONE, 3, 3, R.string.menu_game_devlog)
                    .setOnMenuItemClickListener {
                        webView.loadUrl(url)
                        true
                    }

            } else if (item.getElementsByClass("add_to_collection_btn").isNotEmpty()) {
                appBar.menu.add(Menu.NONE, 2, 2, R.string.menu_game_collection)
                    .setOnMenuItemClickListener {
                        webView.loadUrl(url)
                        true
                    }

            } else if (item.hasClass("jam_entry")) {
                //TODO: handle multiple jam entries nicely
                val menuItemName = item.child(0).text()

                appBar.menu.add(Menu.NONE, 1, 1, menuItemName)
                    .setOnMenuItemClickListener {
                        webView.loadUrl(url)
                        true
                    }
                    .setIcon(R.drawable.ic_baseline_emoji_events_24)
                    .setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM)

            } else if (item.getElementsByClass("view_more").isNotEmpty()) {
                //Cannot rely on ItchWebsiteParser, because its method requires the current URL,
                //and while loading another page, webView.url changes prematurely
                //(leading to crashes...)
                val authorName = item.getElementsByClass("mobile_label")[0].text()

                val menuItemName =
                    if (item.getElementsByClass("full_label")[0].text().contains(authorName))
                        resources.getString(R.string.menu_game_author, authorName)
                    else
                        resources.getString(R.string.menu_game_author_generic)

                appBar.menu.add(Menu.NONE, 0, 0, menuItemName).setOnMenuItemClickListener {
                    webView.loadUrl(url)
                    true
                }
            }

            navbarItems.removeAt(navbarItems.size - 1)
        }
    }

    /**
     * Adds basic app bar actions for navigating between fragments.
     * Should run on UI thread.
     *
     * @param appBar the application's top toolbar
     */
    private fun addDefaultAppBarActions(appBar: Toolbar) {
        appBar.menu.add(Menu.NONE, 10, 10, R.string.nav_installed).setOnMenuItemClickListener {
            (activity as MainActivity).setActiveFragment(MainActivity.LIBRARY_FRAGMENT_TAG, true)
            true
        }
        appBar.menu.add(Menu.NONE, 11, 11, R.string.nav_updates).setOnMenuItemClickListener {
            (activity as MainActivity).setActiveFragment(MainActivity.UPDATES_FRAGMENT_TAG, true)
            true
        }
        appBar.menu.add(Menu.NONE, 12, 12, R.string.nav_settings).setOnMenuItemClickListener {
            (activity as MainActivity).setActiveFragment(MainActivity.SETTINGS_FRAGMENT_TAG, true)
            true
        }
    }

    private fun setSiteNavbarVisibility(visible: Boolean, callback: (String) -> (Unit)) {
        val cssVisibility = if (visible) "visible" else "hidden"
        webView.post {
            webView.evaluateJavascript("""
                    {
                        let navbar = document.getElementById("user_tools")
                        if (navbar)
                            navbar.style.visibility = "$cssVisibility"
                    }
                """, callback
            )
        }
    }


    @Keep //prevent this class from being removed by compiler optimizations
    private class ItchJavaScriptInterface(val fragment: BrowseFragment) {
        @JavascriptInterface
        fun onDownloadLinkClick(uploadId: String) {
            fragment.launch(Dispatchers.IO) {
                fragment.browseHandler?.setClickedUploadId(uploadId.toInt())
            }
        }

        @JavascriptInterface
        fun onHtmlLoaded(html: String, url: String) {
            if (fragment.activity !is MainActivity)
                return

//            Log.d(LOGGING_TAG, "HTML:")
//            Utils.logLongD(LOGGING_TAG, html)

            fragment.launch(Dispatchers.Default) {
                val doc = Jsoup.parse(html)
                val info = fragment.browseHandler?.onPageVisited(doc, url)
                fragment.currentDoc = doc
                fragment.currentInfo = info
                fragment.activity?.runOnUiThread {
                    fragment.updateUI()
                }
            }
        }

        @JavascriptInterface
        fun onResize() {
            fragment.activity?.runOnUiThread {
                fragment.updateUI()
            }
        }
    }




    inner class MitchWebViewClient : WebViewClient() {
        val githubLoginPathRegex = Regex("""/?(login|sessions)(/.*)?""")

        override fun shouldOverrideUrlLoading(view: WebView, request: WebResourceRequest): Boolean {
            if (ItchWebsiteUtils.isItchWebPage(request.url)) {
                return false
            } else if (request.url.host == "github.com"
                    && request.url.path?.matches(githubLoginPathRegex) == true) {
                return false
            } else {
                val intent = Intent(Intent.ACTION_VIEW, request.url)
                if (intent.resolveActivity(requireContext().packageManager) != null) {
                    startActivity(intent)
                } else {
                    Log.i(LOGGING_TAG, "No app found that can handle ${request.url}")
                    Toast.makeText(context,
                        resources.getString(R.string.popup_handler_app_not_found, request.url),
                        Toast.LENGTH_LONG
                    ).show()
                }
                return true
            }
        }

        override fun shouldInterceptRequest(
            view: WebView,
            request: WebResourceRequest
        ): WebResourceResponse? {
            val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(view.context)
            val blockTrackers = sharedPreferences.getBoolean("preference_block_trackers", true)

            if (blockTrackers) {
                arrayOf(
                    "google-analytics.com",
                    "adservice.google.com",
                    "googlesyndication.com",
                    "doubleclick.net"
                ).forEach { trackerHostUrl ->
                    if (request.url.host?.contains(trackerHostUrl) == true)
                        return WebResourceResponse(
                            "text/plain",
                            "utf-8",
                            ByteArrayInputStream("tracker_blocked".toByteArray())
                        )
                }
            }
//            Log.d(LOGGING_TAG, request.url.host)
            return null
        }

        override fun onPageStarted(view: WebView, url: String, favicon: Bitmap?) {
            view.evaluateJavascript("""
                    document.addEventListener("DOMContentLoaded", (event) => {
                        // tell Android that the document is ready
                        mitchCustomJS.onHtmlLoaded("<html>" + document.getElementsByTagName("html")[0].innerHTML + "</html>",
                                                   window.location.href);
                                               
                        // setup download buttons
                        let downloadButtons = document.getElementsByClassName("download_btn");
                        for (var downloadButton of downloadButtons) {
                            let uploadId = downloadButton.getAttribute("data-upload_id");
                            downloadButton.addEventListener("click", (event) => {
                                mitchCustomJS.onDownloadLinkClick(uploadId);
                            });
                        }
                        
                        // remove YouTube banner
                        let ytBanner = document.querySelector(".youtube_mobile_banner_widget");
                        if (ytBanner)
                            ytBanner.style.visibility = "hidden";
                            
                        // remove game purchase banners, we implement our own
                        let elements = document.querySelectorAll(".purchase_banner, .header_buy_row .buy_row, .donate_btn");
                        for (var element of elements)
                            element.style.display = "none";
                    });
                    window.addEventListener("resize", (event) => {
                        mitchCustomJS.onResize();
                    });
                """, null
            )

            val uri = Uri.parse(url)
            if (uri.pathSegments.containsAll(listOf("games", "platform-android"))) {
                val preferenceManager = PreferenceManager.getDefaultSharedPreferences(context)
                val androidOnlyFilter = preferenceManager.getBoolean(PREF_WEB_ANDROID_FILTER, true)
                if (androidOnlyFilter) {
                    webView.evaluateJavascript("""
                    document.addEventListener("DOMContentLoaded", (event) => {
                        // Android-only filter
                        let elements = document.getElementsByClassName("game_cell");
                        for (var element of elements) {
                            if (element.getElementsByClassName("icon-android").length == 0) {
                                element.style.display = "none";
                            }
                        }
                    });
                    """, null)
                }
            }
        }
    }

    inner class MitchWebChromeClient : WebChromeClient() {
        private var customView: View? = null
        private var originalUiVisibility: Int = View.SYSTEM_UI_FLAG_VISIBLE
        var customViewCallback: CustomViewCallback? = null
            private set

        override fun onShowCustomView(view: View, callback: CustomViewCallback) {
            if (customView != null) {
                return
            }
            Log.d(LOGGING_TAG, "Show custom view")

            val foregroundServiceIntent = Intent(context, WebViewForegroundService::class.java)
            foregroundServiceIntent.putExtra(WebViewForegroundService.EXTRA_ORIGINAL_INTENT,
                requireActivity().intent)

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                requireContext().startForegroundService(foregroundServiceIntent)
            } else {
                requireContext().startService(foregroundServiceIntent)
            }

            webView.visibility = View.GONE
            
            (activity as? MainActivity)?.apply {
                binding.bottomNavigationView.visibility = View.GONE
                binding.speedDial.visibility = View.GONE
                binding.toolbar.visibility = View.GONE


                binding.fragmentContainer.addView(view)

                originalUiVisibility = binding.root.systemUiVisibility
                binding.root.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            }
            

            view.keepScreenOn = true

            customView = view
            customViewCallback = callback
        }

        override fun onHideCustomView() {
            if (customView == null) {
                return
            }
            Log.d(LOGGING_TAG, "Hide custom view")

            requireContext().stopService(Intent(context, WebViewForegroundService::class.java))

            webView.visibility = View.VISIBLE
            (activity as? MainActivity)?.apply {
                binding.fragmentContainer.removeView(customView)
                binding.root.systemUiVisibility = originalUiVisibility
            }

            customView = null
            customViewCallback = null

            updateUI()
        }

        override fun onProgressChanged(view: WebView?, newProgress: Int) {
            if (_binding == null)
                return

            val progressBar = binding.progressBar
            
            if (newProgress < 100 && progressBar.visibility == ProgressBar.GONE)
                progressBar.visibility = ProgressBar.VISIBLE

            progressBar.progress = newProgress

            if (newProgress == 100)
                progressBar.visibility = ProgressBar.GONE
        }
        
        fun onResume() {
            if (customView != null) {
                binding.root.systemUiVisibility =
                    View.SYSTEM_UI_FLAG_FULLSCREEN or
                            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or
                            View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
            }
        }

        override fun onJsAlert(
            view: WebView?,
            url: String?,
            message: String?,
            result: JsResult?
        ): Boolean {
            val dialog = AlertDialog.Builder(requireContext()).apply {
                setTitle(resources.getString(R.string.dialog_web_alert, url))
                setMessage(message)
                setPositiveButton(android.R.string.ok) { _, _ ->
                    result!!.confirm()
                }
                setCancelable(false)

                create()
            }
            dialog.show()
            return true
        }

        override fun onJsConfirm(
            view: WebView?,
            url: String?,
            message: String?,
            result: JsResult?
        ): Boolean {
            val dialog = AlertDialog.Builder(requireContext()).apply {
                setTitle(resources.getString(R.string.dialog_web_prompt, url))
                setMessage(message)
                setPositiveButton(android.R.string.ok) { _, _ ->
                    result!!.confirm()
                }
                setNegativeButton(android.R.string.cancel) { _, _ ->
                    result!!.cancel()
                }
                setOnCancelListener { 
                    result!!.cancel()
                }

                create()
            }
            dialog.show()
            return true
        }

        override fun onJsPrompt(
            view: WebView?,
            url: String?,
            message: String?,
            defaultValue: String?,
            result: JsPromptResult?
        ): Boolean {
            val binding = DialogWebPromptBinding.inflate(layoutInflater)

            val dialog = AlertDialog.Builder(requireContext()).apply {
                setTitle(resources.getString(R.string.dialog_web_prompt, url))
                binding.message.text = message
                binding.input.setText(defaultValue)
                setView(binding.root)

                setPositiveButton(android.R.string.ok) { _, _ ->
                    result!!.confirm(binding.message.text.toString())
                }
                setNegativeButton(android.R.string.cancel) { _, _ ->
                    result!!.cancel()
                }
                setOnCancelListener {
                    result!!.cancel()
                }

                create()
            }
            dialog.show()
            return true
        }
    }
}