1 // Copyright 2015-present 650 Industries. All rights reserved.
2 package host.exp.exponent.experience
3 
4 import android.content.Context
5 import androidx.viewpager.widget.ViewPager
6 import javax.inject.Inject
7 import android.os.Bundle
8 import host.exp.exponent.di.NativeModuleDepsProvider
9 import android.content.Intent
10 import androidx.fragment.app.Fragment
11 import androidx.fragment.app.FragmentActivity
12 import androidx.fragment.app.FragmentManager
13 import androidx.fragment.app.FragmentPagerAdapter
14 import host.exp.exponent.Constants
15 import host.exp.exponent.LauncherActivity
16 import host.exp.exponent.kernel.ExponentError
17 import host.exp.exponent.kernel.Kernel
18 import host.exp.expoview.databinding.ErrorActivityNewBinding
19 import java.util.*
20 
21 class ErrorActivity() : FragmentActivity() {
22   private lateinit var binding: ErrorActivityNewBinding
23 
24   private lateinit var pager: ViewPager
25 
26   private var manifestUrl: String? = null
27 
28   @Inject
29   lateinit var context: Context
30 
31   @Inject
32   lateinit var kernel: Kernel
33 
onCreatenull34   override fun onCreate(savedInstanceState: Bundle?) {
35     super.onCreate(savedInstanceState)
36     binding = ErrorActivityNewBinding.inflate(layoutInflater)
37     val view = binding.root
38     setContentView(view)
39     pager = binding.errorViewPager
40 
41     NativeModuleDepsProvider.instance.inject(ErrorActivity::class.java, this)
42 
43     ExperienceActivity.removeNotification(this)
44 
45     manifestUrl = intent.extras!!.getString(MANIFEST_URL_KEY) ?: Constants.INITIAL_URL
46     pager.adapter = ViewPagerAdapter(supportFragmentManager)
47   }
48 
onResumenull49   override fun onResume() {
50     super.onResume()
51     visibleActivity = this
52   }
53 
onPausenull54   override fun onPause() {
55     super.onPause()
56     if (visibleActivity === this) {
57       visibleActivity = null
58     }
59   }
60 
onBackPressednull61   override fun onBackPressed() {
62     if (pager.currentItem == 0) {
63       kernel.killActivityStack(this)
64     } else {
65       pager.currentItem = pager.currentItem - 1
66     }
67   }
68 
onClickHomenull69   fun onClickHome() {
70     clearErrorList()
71 
72     startActivity(Intent(this, LauncherActivity::class.java))
73 
74     // Mark as not visible so that any new errors go to a new activity.
75     if (visibleActivity === this) {
76       visibleActivity = null
77     }
78     kernel.killActivityStack(this)
79   }
80 
onClickReloadnull81   fun onClickReload() {
82     if (manifestUrl != null) {
83       clearErrorList()
84 
85       // Mark as not visible so that any new errors go to a new activity.
86       if (visibleActivity === this) {
87         visibleActivity = null
88       }
89       kernel.killActivityStack(this)
90       kernel.reloadVisibleExperience(manifestUrl!!)
91     } else {
92       // Mark as not visible so that any new errors go to a new activity.
93       if (visibleActivity === this) {
94         visibleActivity = null
95       }
96       finish()
97     }
98   }
99 
onClickViewErrorLognull100   fun onClickViewErrorLog() {
101     if (pager.currentItem == 0) {
102       pager.currentItem = 1
103     }
104   }
105 
106   private inner class ViewPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
getItemnull107     override fun getItem(pos: Int): Fragment {
108       val args = intent.extras
109       args!!.putString("manifestUrl", manifestUrl)
110       return when (pos) {
111         1 -> {
112           errorConsoleFragment = ErrorConsoleFragment().apply {
113             arguments = args
114           }
115           errorConsoleFragment!!
116         }
117         else -> {
118           ErrorFragment().apply { arguments = args }
119         }
120       }
121     }
122 
getCountnull123     override fun getCount(): Int {
124       return 2
125     }
126   }
127 
128   companion object {
129     const val IS_HOME_KEY = "isHome"
130     const val MANIFEST_URL_KEY = "manifestUrl"
131     const val USER_ERROR_MESSAGE_KEY = "userErrorMessage"
132     const val DEVELOPER_ERROR_MESSAGE_KEY = "developerErrorMessage"
133     const val DEBUG_MODE_KEY = "isDebugModeEnabled"
134     const val ERROR_HEADER_KEY = "errorHeader"
135 
136     @JvmStatic var visibleActivity: ErrorActivity? = null
137       private set
138 
139     @JvmStatic val errorList = LinkedList<ExponentError>()
140 
141     private var errorConsoleFragment: ErrorConsoleFragment? = null
142 
addErrornull143     fun addError(error: ExponentError) {
144       synchronized(errorList) { errorList.addFirst(error) }
145 
146       // notify ErrorConsoleFragment of the update so that it can refresh its ListView
147       if (visibleActivity != null && errorConsoleFragment != null) {
148         visibleActivity!!.runOnUiThread {
149           errorConsoleFragment!!.adapter.notifyDataSetChanged()
150         }
151       }
152     }
153 
clearErrorListnull154     fun clearErrorList() {
155       synchronized(errorList) { errorList.clear() }
156     }
157   }
158 }
159