content: package dev.amirzr.flutter_v2ray_client.v2ray.services;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  import android.util.Log;
  
  import androidx.annotation.Nullable;
  
  import dev.amirzr.flutter_v2ray_client.v2ray.core.V2rayCoreManager;
  import dev.amirzr.flutter_v2ray_client.v2ray.interfaces.V2rayServicesListener;
  import dev.amirzr.flutter_v2ray_client.v2ray.utils.AppConfigs;
  import dev.amirzr.flutter_v2ray_client.v2ray.utils.V2rayConfig;
  
  
  public class V2rayProxyOnlyService extends Service implements V2rayServicesListener {
  
  
      @Override
      public void onCreate() {
          super.onCreate();
          V2rayCoreManager.getInstance().setUpListener(this);
      }
  
      @Override
      public int onStartCommand(Intent intent, int flags, int startId) {
          AppConfigs.V2RAY_SERVICE_COMMANDS startCommand = (AppConfigs.V2RAY_SERVICE_COMMANDS) intent.getSerializableExtra("COMMAND");
          if (startCommand.equals(AppConfigs.V2RAY_SERVICE_COMMANDS.START_SERVICE)) {
              V2rayConfig v2rayConfig = (V2rayConfig) intent.getSerializableExtra("V2RAY_CONFIG");
              if (v2rayConfig == null) {
                  this.onDestroy();
              }
              if (V2rayCoreManager.getInstance().isV2rayCoreRunning()) {
                  V2rayCoreManager.getInstance().stopCore();
              }
              assert v2rayConfig != null;
              if (V2rayCoreManager.getInstance().startCore(v2rayConfig)) {
                  Log.e(V2rayProxyOnlyService.class.getSimpleName(), "onStartCommand success => v2ray core started.");
              } else {
                  this.onDestroy();
              }
          } else if (startCommand.equals(AppConfigs.V2RAY_SERVICE_COMMANDS.STOP_SERVICE)) {
              V2rayCoreManager.getInstance().stopCore();
              AppConfigs.V2RAY_CONFIG = null;
          } else if (startCommand.equals(AppConfigs.V2RAY_SERVICE_COMMANDS.MEASURE_DELAY)) {
              new Thread(() -> {
                  Intent sendB = new Intent("CONNECTED_V2RAY_SERVER_DELAY");
                  sendB.putExtra("DELAY", String.valueOf(V2rayCoreManager.getInstance().getConnectedV2rayServerDelay()));
                  sendBroadcast(sendB);
              }, "MEASURE_CONNECTED_V2RAY_SERVER_DELAY").start();
          } else {
              this.onDestroy();
          }
          return START_STICKY;
      }
  
  
      @Override
      public void onDestroy() {
          super.onDestroy();
      }
  
      @Nullable
      @Override
      public IBinder onBind(Intent intent) {
          return null;
      }
  
      @Override
      public boolean onProtect(int socket) {
          return true;
      }
  
      @Override
      public Service getService() {
          return this;
      }
  
      @Override
      public void startService() {
          //ignore
      }
  
      @Override
      public void stopService() {
          try {
              stopSelf();
          } catch (Exception e) {
              //ignore
          }
      }
  }
  