MainActivity.kt 11 KB

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