Ejemplo n.º 1
0
 public static void main(final String[] args) {
   try {
     final String encrypted = Util.encrypt("fred flintstone");
     final String decrypted = Util.decrypt(encrypted);
     DBG.m(encrypted + " / " + decrypted);
   } catch (Exception x) {
     DBG.m(x);
   }
 }
Ejemplo n.º 2
0
  /**
   * @param s
   * @return
   * @throws GeneralSecurityException
   * @throws UnsupportedEncodingException
   */
  public static String decrypt(final String s)
      throws GeneralSecurityException, UnsupportedEncodingException {
    String decrypted = null;

    try {
      if (s != null) {
        final SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
        final SecretKey secretKey =
            secretKeyFactory.generateSecret(new PBEKeySpec(secret.toCharArray()));
        final Cipher cipher = Cipher.getInstance("PBEWithMD5AndDES");
        cipher.init(Cipher.DECRYPT_MODE, secretKey, new PBEParameterSpec(SALT, 20));
        final byte[] stringBytes = s.getBytes("UTF-8");
        final byte[] decodedBytes = Base64.decode(stringBytes, Base64.DEFAULT);
        final byte[] decryptedBytes = cipher.doFinal(decodedBytes);
        decrypted = new String(decryptedBytes, "UTF-8");
      }
    } catch (GeneralSecurityException x) {
      throw x;
    } catch (UnsupportedEncodingException x) {
      throw x;
    } catch (Exception x) {
      DBG.m(x);
    }

    return decrypted;
  }
Ejemplo n.º 3
0
 /**
  * http://developer.android.com/training/monitoring-device-state/connectivity-monitoring.html
  * http://developer.android.com/training/basics/network-ops/managing.html
  *
  * @param context
  * @return
  */
 public static boolean isNetworkAvailable(final Context context, final boolean mobile) {
   boolean available = false;
   try {
     final ConnectivityManager connectivitymanager =
         (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     final NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
     if ((networkinfo != null) && networkinfo.isConnectedOrConnecting()) {
       switch (networkinfo.getType()) {
         case ConnectivityManager.TYPE_WIFI:
         case ConnectivityManager.TYPE_WIMAX:
         case ConnectivityManager.TYPE_ETHERNET:
           available = true;
           break;
         case ConnectivityManager.TYPE_MOBILE:
           if (mobile) {
             available = true;
           }
           break;
       }
     }
   } catch (Throwable x) {
     DBG.m(x);
   }
   return available;
 }
Ejemplo n.º 4
0
 /** @param closeable */
 public static void close(final java.io.Closeable closeable) {
   try {
     if (closeable != null) {
       closeable.close();
     }
   } catch (java.io.IOException x) {
     DBG.m(x);
   }
 }
Ejemplo n.º 5
0
 public static void strict() {
   StrictMode.setThreadPolicy(
       new StrictMode.ThreadPolicy.Builder()
           .detectAll()
           .penaltyLog()
           .penaltyFlashScreen()
           .build());
   StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll().penaltyLog().build());
   DBG.w("STRICT MODE ON");
 }
Ejemplo n.º 6
0
 /*
  * http://android-developers.blogspot.com/2013/02/using-cryptography-to-store-credentials.html
  */
 static {
   try {
     final SecureRandom secureRandom =
         new SecureRandom(); // Do not seed secureRandom ! Automatically seeded from system
     // entropy.
     secret = new BigInteger(130, secureRandom).toString(32);
   } catch (Exception x) {
     DBG.m(x);
   }
 }
Ejemplo n.º 7
0
 /**
  * @param context
  * @return
  */
 public static String version(final Context context) {
   String version = null;
   try {
     final PackageInfo packageinfo =
         context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
     version = packageinfo.versionName;
   } catch (Exception x) {
     DBG.m(x);
   }
   return version;
 }
Ejemplo n.º 8
0
 /**
  * @param context
  * @return
  */
 public static String getApplicationName(final Context context) {
   String name = null;
   try {
     final PackageManager packagemanager = context.getPackageManager();
     final PackageInfo packageinfo = packagemanager.getPackageInfo(context.getPackageName(), 0);
     name =
         packageinfo.applicationInfo.loadLabel(packagemanager).toString()
             + "("
             + packageinfo.packageName
             + ")";
   } catch (Exception x) {
     DBG.m(x);
   }
   return name;
 }
Ejemplo n.º 9
0
 /**
  * @param context
  * @param classname
  * @return
  */
 public static boolean isServiceRunning(final Context context, final String classname) {
   boolean running = false;
   try {
     final ActivityManager activitymanager =
         (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
     final List<ActivityManager.RunningServiceInfo> runningserviceinfos =
         activitymanager.getRunningServices(Integer.MAX_VALUE);
     for (final ActivityManager.RunningServiceInfo runningserviceinfo : runningserviceinfos) {
       final String serviceclassname = runningserviceinfo.service.getClassName();
       if (serviceclassname == null) {
         continue;
       }
       if (serviceclassname.equals(classname)) {
         running = true;
         break;
       }
     }
   } catch (Exception x) {
     DBG.m(x);
   }
   return running;
 }
Ejemplo n.º 10
0
  /** @return */
  public static String getIPv4Address() {
    String ipv4address = null;

    try {
      final List<NetworkInterface> networkinterfaces =
          Collections.list(NetworkInterface.getNetworkInterfaces());
      for (final NetworkInterface networkinterface : networkinterfaces) {
        final List<InetAddress> addresses = Collections.list(networkinterface.getInetAddresses());
        for (final InetAddress address : addresses) {
          if ((address == null) || address.isLoopbackAddress()) {
            continue;
          }
          if (address instanceof Inet4Address) {
            ipv4address = address.getHostAddress().toString();
            break;
          }
        }
      }
    } catch (Exception x) {
      DBG.m(x);
    }

    return ipv4address;
  }
Ejemplo n.º 11
0
  /** @param context */
  public static void showDiagnosticInfo(final Context context) {
    try {
      DBG.m("  Build.VERSION.SDK_INT: " + Build.VERSION.SDK_INT);
      DBG.m("  Build.VERSION.CODENAME: " + Build.VERSION.CODENAME);
      DBG.m("  Build.DEVICE: " + android.os.Build.DEVICE);
      DBG.m("  Build.MODEL: " + android.os.Build.MODEL);
      DBG.m("  Build.PRODUCT: " + android.os.Build.PRODUCT);

      final DisplayMetrics displaymetrics = new DisplayMetrics();
      final WindowManager windowmanager =
          (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
      windowmanager.getDefaultDisplay().getMetrics(displaymetrics);
      DBG.m("  DisplayMetrics.density: " + displaymetrics.density);
      DBG.m("  DisplayMetrics.densityDpi: " + displaymetrics.densityDpi);
      DBG.m("  DisplayMetrics.heightPixels: " + displaymetrics.heightPixels);
      DBG.m("  DisplayMetrics.widthPixels: " + displaymetrics.widthPixels);
      DBG.m("  DisplayMetrics.scaledDensity: " + displaymetrics.scaledDensity);
      DBG.m("  DisplayMetrics.xdpi: " + displaymetrics.xdpi);
      DBG.m("  DisplayMetrics.ydpi: " + displaymetrics.ydpi);
    } catch (Exception x) {
      DBG.m(x);
    }
  }