content: package dev.amirzr.flutter_v2ray_client.v2ray.utils;
  
  import android.content.Context;
  import android.util.Log;
  
  import dev.amirzr.flutter_v2ray_client.v2ray.core.V2rayCoreManager;
  
  import org.json.JSONArray;
  import org.json.JSONObject;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.util.ArrayList;
  
  public class Utilities {
  
      public static void CopyFiles(InputStream src, File dst) throws IOException {
          try (OutputStream out = new FileOutputStream(dst)) {
              byte[] buf = new byte[1024];
              int len;
              while ((len = src.read(buf)) > 0) {
                  out.write(buf, 0, len);
              }
          }
      }
  
      public static String getUserAssetsPath(Context context) {
          File extDir = context.getExternalFilesDir("assets");
          if (extDir == null) {
              return "";
          }
          if (!extDir.exists()) {
              return context.getDir("assets", 0).getAbsolutePath();
          } else {
              return extDir.getAbsolutePath();
          }
      }
  
      public static void copyAssets(final Context context) {
          String extFolder = getUserAssetsPath(context);
          try {
              String geo = "geosite.dat,geoip.dat";
              for (String assets_obj : context.getAssets().list("")) {
                  if (geo.contains(assets_obj)) {
                      CopyFiles(context.getAssets().open(assets_obj), new File(extFolder, assets_obj));
                  }
              }
          } catch (Exception e) {
              Log.e("Utilities", "copyAssets failed=>", e);
          }
      }
  
  
      public static String convertIntToTwoDigit(int value) {
          if (value < 10) return "0" + value;
          else return value + "";
      }
  
  
      public static V2rayConfig parseV2rayJsonFile(final String remark, String config, final ArrayList<String> blockedApplication, final ArrayList<String> bypass_subnets) {
          final V2rayConfig v2rayConfig = new V2rayConfig();
          v2rayConfig.REMARK = remark;
          v2rayConfig.BLOCKED_APPS = blockedApplication;
          v2rayConfig.BYPASS_SUBNETS = bypass_subnets;
          v2rayConfig.APPLICATION_ICON = AppConfigs.APPLICATION_ICON;
          v2rayConfig.APPLICATION_NAME = AppConfigs.APPLICATION_NAME;
          v2rayConfig.NOTIFICATION_DISCONNECT_BUTTON_NAME = AppConfigs.NOTIFICATION_DISCONNECT_BUTTON_NAME;
          try {
              JSONObject config_json = new JSONObject(config);
              try {
                  JSONArray inbounds = config_json.getJSONArray("inbounds");
                  for (int i = 0; i < inbounds.length(); i++) {
                      try {
                          if (inbounds.getJSONObject(i).getString("protocol").equals("socks")) {
                              v2rayConfig.LOCAL_SOCKS5_PORT = inbounds.getJSONObject(i).getInt("port");
                          }
                      } catch (Exception e) {
                          //ignore
                      }
                      try {
                          if (inbounds.getJSONObject(i).getString("protocol").equals("http")) {
                              v2rayConfig.LOCAL_HTTP_PORT = inbounds.getJSONObject(i).getInt("port");
                          }
                      } catch (Exception e) {
                          //ignore
                      }
                  }
              } catch (Exception e) {
                  Log.w(V2rayCoreManager.class.getSimpleName(), "startCore warn => can`t find inbound port of socks5 or http.");
                  return null;
              }
              try {
                  v2rayConfig.CONNECTED_V2RAY_SERVER_ADDRESS = config_json.getJSONArray("outbounds")
                          .getJSONObject(0).getJSONObject("settings")
                          .getJSONArray("vnext").getJSONObject(0)
                          .getString("address");
                  v2rayConfig.CONNECTED_V2RAY_SERVER_PORT = config_json.getJSONArray("outbounds")
                          .getJSONObject(0).getJSONObject("settings")
                          .getJSONArray("vnext").getJSONObject(0)
                          .getString("port");
              } catch (Exception e) {
                  v2rayConfig.CONNECTED_V2RAY_SERVER_ADDRESS = config_json.getJSONArray("outbounds")
                          .getJSONObject(0).getJSONObject("settings")
                          .getJSONArray("servers").getJSONObject(0)
                          .getString("address");
                  v2rayConfig.CONNECTED_V2RAY_SERVER_PORT = config_json.getJSONArray("outbounds")
                          .getJSONObject(0).getJSONObject("settings")
                          .getJSONArray("servers").getJSONObject(0)
                          .getString("port");
              }
              try {
                  if (config_json.has("policy")) {
                      config_json.remove("policy");
                  }
                  if (config_json.has("stats")) {
                      config_json.remove("stats");
                  }
              } catch (Exception ignore_error) {
                  //ignore
              }
              if (AppConfigs.ENABLE_TRAFFIC_AND_SPEED_STATICS) {
                  try {
                      JSONObject policy = new JSONObject();
                      JSONObject levels = new JSONObject();
                      levels.put("8", new JSONObject()
                              .put("connIdle", 300)
                              .put("downlinkOnly", 1)
                              .put("handshake", 4)
                              .put("uplinkOnly", 1));
                      JSONObject system = new JSONObject()
                              .put("statsOutboundUplink", true)
                              .put("statsOutboundDownlink", true);
                      policy.put("levels", levels);
                      policy.put("system", system);
                      config_json.put("policy", policy);
                      config_json.put("stats", new JSONObject());
                      config = config_json.toString();
                      v2rayConfig.ENABLE_TRAFFIC_STATICS = true;
                  } catch (Exception e) {
                      //ignore
                  }
              }
          } catch (Exception e) {
              Log.e(Utilities.class.getName(), "parseV2rayJsonFile failed => ", e);
              //ignore
              return null;
          }
          v2rayConfig.V2RAY_FULL_JSON_CONFIG = config;
          return v2rayConfig;
      }
  
  
  }
  