V2RayProxyOnlyService.kt 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. package com.v2ray.ang.service
  2. import android.app.Service
  3. import android.content.Context
  4. import android.content.Intent
  5. import android.os.Build
  6. import android.os.IBinder
  7. import androidx.annotation.RequiresApi
  8. import com.v2ray.ang.handler.SettingsManager
  9. import com.v2ray.ang.handler.V2RayServiceManager
  10. import com.v2ray.ang.util.MyContextWrapper
  11. import java.lang.ref.SoftReference
  12. class V2RayProxyOnlyService : Service(), ServiceControl {
  13. /**
  14. * Initializes the service.
  15. */
  16. override fun onCreate() {
  17. super.onCreate()
  18. V2RayServiceManager.serviceControl = SoftReference(this)
  19. }
  20. /**
  21. * Handles the start command for the service.
  22. * @param intent The intent.
  23. * @param flags The flags.
  24. * @param startId The start ID.
  25. * @return The start mode.
  26. */
  27. override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  28. V2RayServiceManager.startCoreLoop()
  29. return START_STICKY
  30. }
  31. /**
  32. * Destroys the service.
  33. */
  34. override fun onDestroy() {
  35. super.onDestroy()
  36. V2RayServiceManager.stopCoreLoop()
  37. }
  38. /**
  39. * Gets the service instance.
  40. * @return The service instance.
  41. */
  42. override fun getService(): Service {
  43. return this
  44. }
  45. /**
  46. * Starts the service.
  47. */
  48. override fun startService() {
  49. // do nothing
  50. }
  51. /**
  52. * Stops the service.
  53. */
  54. override fun stopService() {
  55. stopSelf()
  56. }
  57. /**
  58. * Protects the VPN socket.
  59. * @param socket The socket to protect.
  60. * @return True if the socket is protected, false otherwise.
  61. */
  62. override fun vpnProtect(socket: Int): Boolean {
  63. return true
  64. }
  65. /**
  66. * Binds the service.
  67. * @param intent The intent.
  68. * @return The binder.
  69. */
  70. override fun onBind(intent: Intent?): IBinder? {
  71. return null
  72. }
  73. /**
  74. * Attaches the base context to the service.
  75. * @param newBase The new base context.
  76. */
  77. @RequiresApi(Build.VERSION_CODES.N)
  78. override fun attachBaseContext(newBase: Context?) {
  79. val context = newBase?.let {
  80. MyContextWrapper.wrap(newBase, SettingsManager.getLocale())
  81. }
  82. super.attachBaseContext(context)
  83. }
  84. }