content: package eu.faircode.netguard;
  
  /*
      This file is part of NetGuard.
  
      NetGuard is free software: you can redistribute it and/or modify
      it under the terms of the GNU General Public License as published by
      the Free Software Foundation, either version 3 of the License, or
      (at your option) any later version.
  
      NetGuard is distributed in the hope that it will be useful,
      but WITHOUT ANY WARRANTY; without even the implied warranty of
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      GNU General Public License for more details.
  
      You should have received a copy of the GNU General Public License
      along with NetGuard.  If not, see <http://www.gnu.org/licenses/>.
  
      Copyright 2015-2024 by Marcel Bokhorst (M66B)
  */
  
  import android.content.Context;
  import android.content.Intent;
  import android.net.ConnectivityManager;
  import android.net.LinkProperties;
  import android.net.Network;
  import android.os.Build;
  import android.os.Bundle;
  import android.os.PowerManager;
  
  import androidx.core.net.ConnectivityManagerCompat;
  import android.util.Log;
  
  import java.net.InetAddress;
  import java.util.ArrayList;
  import java.util.List;
  import java.util.Set;
  
  public class Util {
      private static final String TAG = "NetGuard.Util";
  
      private static native String jni_getprop(String name);
  
      static {
          System.loadLibrary("netguard");
      }
  
      public static boolean isMeteredNetwork(Context context) {
          ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
          return (cm != null && ConnectivityManagerCompat.isActiveNetworkMetered(cm));
      }
  
      public static List<String> getDefaultDNS(Context context) {
          List<String> listDns = new ArrayList<>();
  
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
              ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
              Network an = cm.getActiveNetwork();
              if (an != null) {
                  LinkProperties lp = cm.getLinkProperties(an);
                  if (lp != null) {
                      List<InetAddress> dns = lp.getDnsServers();
                      if (dns != null)
                          for (InetAddress d : dns) {
                              Log.i(TAG, "DNS from LP: " + d.getHostAddress());
                              listDns.add(d.getHostAddress().split("%")[0]);
                          }
                  }
              }
          } else {
              String dns1 = jni_getprop("net.dns1");
              String dns2 = jni_getprop("net.dns2");
              if (dns1 != null)
                  listDns.add(dns1.split("%")[0]);
              if (dns2 != null)
                  listDns.add(dns2.split("%")[0]);
          }
  
          return listDns;
      }
  
      public static boolean isInteractive(Context context) {
          PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
          return (pm != null && pm.isInteractive());
      }
  
      public static void logExtras(Intent intent) {
          if (intent != null)
              logBundle(intent.getExtras());
      }
  
      public static void logBundle(Bundle data) {
          if (data != null) {
              Set<String> keys = data.keySet();
              StringBuilder stringBuilder = new StringBuilder();
              for (String key : keys) {
                  Object value = data.get(key);
                  stringBuilder.append(key)
                          .append("=")
                          .append(value)
                          .append(value == null ? "" : " (" + value.getClass().getSimpleName() + ")")
                          .append("\r\n");
              }
              Log.d(TAG, stringBuilder.toString());
          }
      }
  }
  