MainActivity.kt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. package com.vpn.fastestvpnservice
  2. import android.content.Context
  3. import android.content.Intent
  4. import android.content.pm.ActivityInfo
  5. import android.content.pm.PackageManager
  6. import android.content.res.Configuration
  7. import android.net.Uri
  8. import android.os.Build
  9. import android.os.Bundle
  10. import android.provider.Settings
  11. import android.util.Log
  12. import android.view.WindowInsets
  13. import android.view.WindowInsetsController
  14. import android.view.WindowManager
  15. import android.widget.Toast
  16. import androidx.activity.compose.setContent
  17. import androidx.activity.viewModels
  18. import androidx.compose.foundation.isSystemInDarkTheme
  19. import androidx.compose.runtime.getValue
  20. import androidx.compose.runtime.mutableStateOf
  21. import androidx.compose.runtime.remember
  22. import androidx.compose.ui.graphics.toArgb
  23. import androidx.compose.ui.platform.LocalContext
  24. import androidx.compose.ui.res.colorResource
  25. import androidx.core.app.ActivityCompat
  26. import androidx.core.view.WindowCompat
  27. import androidx.fragment.app.FragmentManager
  28. import androidx.navigation.compose.rememberNavController
  29. import com.vpn.fastestvpnservice.activities.DockActivity
  30. import com.vpn.fastestvpnservice.activities.SplashActivity2
  31. import com.vpn.fastestvpnservice.beans.isDarkTheme
  32. import com.vpn.fastestvpnservice.beans.themesList
  33. import com.vpn.fastestvpnservice.broadcasts.ConnectivityReceiver
  34. import com.vpn.fastestvpnservice.constants.AppEnum
  35. import com.vpn.fastestvpnservice.fragments.LoginFragment
  36. import com.vpn.fastestvpnservice.fragments.TvHomeFragment
  37. import com.vpn.fastestvpnservice.helpers.BasePreferenceHelper
  38. import com.vpn.fastestvpnservice.interfaces.CallBacks
  39. import com.vpn.fastestvpnservice.interfaces.OnBackPressedOnTV
  40. import com.vpn.fastestvpnservice.interfaces.UpdateServersOnProtocol
  41. import com.vpn.fastestvpnservice.interfaces.VPNConnectionCallBacks
  42. import com.vpn.fastestvpnservice.navigation.SetUpNavGraph
  43. import com.vpn.fastestvpnservice.navigation.SetUpNavGraphTV
  44. import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.Settings
  45. import com.vpn.fastestvpnservice.screens.bottomNavBarScreens.isSwitch
  46. import com.vpn.fastestvpnservice.screens.helpScreensAll.fileChooserCallback
  47. import com.vpn.fastestvpnservice.ui.theme.FastestVPNTheme
  48. import com.vpn.fastestvpnservice.utils.StaticMethods
  49. import com.vpn.fastestvpnservice.viewmodels.LoginViewModel
  50. import de.blinkt.openvpn.core.App
  51. open class MainActivity : DockActivity(), ConnectivityReceiver.ConnectivityReceiverListener {
  52. override fun onCreate(savedInstanceState: Bundle?) {
  53. super.onCreate(savedInstanceState)
  54. // if (Thread.getDefaultUncaughtExceptionHandler() !is CustomExceptionHandler) {
  55. // Thread.setDefaultUncaughtExceptionHandler(
  56. // CustomExceptionHandler(this,"112","newverson","minhaj"));
  57. // }
  58. requestedOrientation = if (App.isAndroidTvBox) {
  59. /* This for Android-TV,Box APK */
  60. ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
  61. } else {
  62. /* This for Normal APK */
  63. if (StaticMethods.isTV(this)) {
  64. ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
  65. } else {
  66. ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
  67. }
  68. }
  69. // setContentView(R.layout.activity_main)
  70. val basePreferenceHelper = BasePreferenceHelper(baseContext)
  71. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
  72. if (ActivityCompat.checkSelfPermission(
  73. this,
  74. android.Manifest.permission.POST_NOTIFICATIONS
  75. ) != PackageManager.PERMISSION_GRANTED
  76. ) {
  77. requestPermissions(arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), 1);
  78. }
  79. }
  80. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && Settings.canDrawOverlays(this)) {
  81. isSwitch.value = true
  82. basePreferenceHelper.saveLaunchState(true)
  83. }
  84. WindowCompat.setDecorFitsSystemWindows(window, false)
  85. setContent {
  86. val context = LocalContext.current
  87. // val basePreferenceHelper = BasePreferenceHelper(context)
  88. val selectedTheme = basePreferenceHelper.getTheme()
  89. val isSystemInDarkTheme = isSystemInDarkTheme()
  90. val systemTheme by remember { mutableStateOf(isSystemInDarkTheme) }
  91. if (selectedTheme == themesList[0]) {
  92. isDarkTheme.value = systemTheme
  93. } else {
  94. isDarkTheme.value = selectedTheme == themesList[2]
  95. }
  96. if (StaticMethods.isTV(baseContext)) {
  97. Log.d("test_platform_android", "TV")
  98. // val intent = Intent(context, SplashActivity::class.java)
  99. // context.startActivity(intent)
  100. // finish()
  101. FastestVPNTheme(isDarkTheme.value) {
  102. // window.statusBarColor = colorResource(id = R.color.blue_text).toArgb()
  103. val navController = rememberNavController()
  104. SetUpNavGraphTV(navHostController = navController, this)
  105. }
  106. } else {
  107. Log.d("test_platform_android", "Mobile")
  108. FastestVPNTheme(isDarkTheme.value) {
  109. window.statusBarColor = colorResource(id = R.color.blue_text).toArgb()
  110. val navController = rememberNavController()
  111. SetUpNavGraph(navHostController = navController, this)
  112. }
  113. }
  114. }
  115. // hideSystemUI()
  116. // val bundle = intent.extras
  117. // bundle.let {
  118. // it?.getString(AppEnum.SCREEN_NAME.key).let {
  119. // it?.let {
  120. // Log.d("test_nav_tv", "bundle $it")
  121. // initFragment(it)
  122. // }
  123. // }
  124. // ?: kotlin.run {
  125. // basePreferenceHelper?.getUser()?.let {
  126. // Log.d("test_nav_tv", "kotlin.run")
  127. // initFragment(AppEnum.HOME_SCREEN.key)
  128. // }
  129. // }
  130. // }
  131. FragmentManager.enableDebugLogging(true)
  132. }
  133. // override fun onBackPressed() {
  134. // super.onBackPressed()
  135. // Toast.makeText(
  136. // baseContext, "onBackPressed()", Toast.LENGTH_SHORT
  137. // ).show()
  138. // }
  139. fun hideSystemUI() {
  140. //Hides the ugly action bar at the top
  141. actionBar?.hide()
  142. //Hide the status bars
  143. WindowCompat.setDecorFitsSystemWindows(window, false)
  144. if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
  145. window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
  146. } else {
  147. window.insetsController?.apply {
  148. hide(WindowInsets.Type.statusBars())
  149. systemBarsBehavior = WindowInsetsController.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
  150. }
  151. }
  152. }
  153. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  154. super.onActivityResult(requestCode, resultCode, data)
  155. if (requestCode == 0) {
  156. // Log.d("test_file_chat", "onActivityResult: ${fileChooserCallback.toString()}")
  157. fileChooserCallback?.onReceiveValue(null)
  158. } else {
  159. fileChooserCallback?.onReceiveValue(arrayOf(Uri.parse(data?.dataString)))
  160. }
  161. fileChooserCallback = null
  162. if (requestCode == SCREEN_OVERLAY) {
  163. val basePreferenceHelper = BasePreferenceHelper(this)
  164. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && Settings.canDrawOverlays(this)) {
  165. isSwitch.value = true
  166. basePreferenceHelper.saveLaunchState(true)
  167. }
  168. }
  169. }
  170. override fun getContainerId(): Int {
  171. return R.id.mainFrameLayout
  172. }
  173. // override fun onConfigurationChanged(newConfig: Configuration) {
  174. // super.onConfigurationChanged(newConfig)
  175. // Toast.makeText(baseContext, "onConfigurationChanged", Toast.LENGTH_SHORT).show()
  176. // }
  177. // override fun getContainerId(): Int {
  178. // return 0
  179. // }
  180. override fun onDestroy() {
  181. super.onDestroy()
  182. val basePreferenceHelper = BasePreferenceHelper(baseContext)
  183. if (!StaticMethods.isTV(this)) {
  184. basePreferenceHelper.clearServerObject()
  185. }
  186. Log.d("test_conn_ser_obj", "onDestroy() called")
  187. }
  188. fun getCallingIntent(_context: Context?): Intent {
  189. return Intent(_context, MainActivity::class.java)
  190. }
  191. fun initFragment(key: String) {
  192. Log.d("test_nav_tv", "initFragment() => $key")
  193. // val fragmentNavigator: FragmentNavigator = FragmentNavigator()
  194. when (key) {
  195. AppEnum.LOGIN_SCREEN.key -> {
  196. // val i = Intent(this, SplashActivity2::class.java)
  197. // startActivity(i)
  198. fragmentNavigator.replaceFragmentWithBackStack(LoginFragment.newInstance())
  199. }
  200. AppEnum.HOME_SCREEN.key -> {
  201. // fragmentNavigator.clearAllFragments()
  202. if (App.isAndroidTvBox) {
  203. /* Android-TV,Box APK */
  204. fragmentNavigator.replaceFragmentWithBackStackTv(
  205. TvHomeFragment.newInstance(),
  206. "tv_home"
  207. )
  208. } else {
  209. /* Normal APK*/
  210. if (StaticMethods.isTV(this)) {
  211. Log.d("test_nav_tv", "isTV =>")
  212. val i = Intent(this, SplashActivity2::class.java)
  213. startActivity(i)
  214. // fragmentNavigator.replaceFragmentWithBackStackTv(TvHomeFragment.newInstance(), "tv_home")
  215. } else {
  216. fragmentNavigator.replaceFragmentWithBackStack(TvHomeFragment.newInstance())
  217. }
  218. }
  219. }
  220. // AppEnum.SIGNUP_SCREEN.key -> {
  221. // fragmentNavigator.replaceFragment(SignUpFragment.newInstance())
  222. // }
  223. AppEnum.HELP_SCREEN.key -> {
  224. // fragmentNavigator.replaceFragment(
  225. // WebViewFragment.newInstance(
  226. // AppConstant.FAQ_WEBVIEW_URL, getString(R.string.help_faq)
  227. // )
  228. // )
  229. }
  230. }
  231. }
  232. companion object {
  233. var isWGDown: Boolean = true
  234. var isAutoEnabled: Boolean = false
  235. var isSnrNotify: Boolean = false
  236. var isSelectedServersShown: Boolean = false
  237. var vpnConnectionCallBacks: VPNConnectionCallBacks? = null
  238. var isStopSubscriptionNavigation: Boolean = true
  239. var updateServersOnProtocol: UpdateServersOnProtocol? = null
  240. var callBacks: CallBacks? = null
  241. var onBackPressedOnTV: OnBackPressedOnTV? = null
  242. val SCREEN_OVERLAY = 12345
  243. }
  244. override fun onNetworkConnectionChanged(isConnected: Boolean) {
  245. }
  246. // override fun onBackPressed() {
  247. // super.onBackPressed()
  248. //
  249. // if (StaticMethods.isTV(this))
  250. // {
  251. // Log.d("test_backTV", "TV Back!!")
  252. //// this@MainActivity.finishAffinity()
  253. // }
  254. // }
  255. }
  256. // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) // commenting now...
  257. // {
  258. // ActivityCompat.requestPermissions(this,
  259. // arrayOf(
  260. // android.Manifest.permission.ACCESS_FINE_LOCATION,
  261. // android.Manifest.permission.ACCESS_COARSE_LOCATION
  262. // ),
  263. // PackageManager.PERMISSION_GRANTED
  264. // )
  265. // }
  266. // else {
  267. // ActivityCompat.requestPermissions(this,
  268. // arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION,
  269. // android.Manifest.permission.ACCESS_COARSE_LOCATION
  270. // ),
  271. // PackageManager.PERMISSION_GRANTED
  272. // )
  273. // }