diff options
Diffstat (limited to 'app/src/fil/libre/repwifiapp/helpers/Utils.java')
-rw-r--r-- | app/src/fil/libre/repwifiapp/helpers/Utils.java | 231 |
1 files changed, 192 insertions, 39 deletions
diff --git a/app/src/fil/libre/repwifiapp/helpers/Utils.java b/app/src/fil/libre/repwifiapp/helpers/Utils.java index 8cd90bf..ad10c1a 100644 --- a/app/src/fil/libre/repwifiapp/helpers/Utils.java +++ b/app/src/fil/libre/repwifiapp/helpers/Utils.java @@ -63,63 +63,50 @@ public class Utils { Log.d(APP_NAME, msg); } - public static boolean writeFile(String filePath, String text, boolean overwrite) { - - FileWriter writer = null; - boolean retval = false; + public static boolean writeFileLines(String filePath, String[] lines, boolean overwrite) { - try { + if (lines == null) { + return false; + } - writer = new FileWriter(filePath, (!overwrite)); - writer.write(text); + if (lines.length == 0) { + return true; + } - retval = true; + StringBuilder sb = new StringBuilder(); - } catch (Exception e) { - _lastException = e; - retval = false; - } finally { + for (String l : lines) { - if (writer != null) { - try { - writer.close(); - } catch (IOException e) { - logError("error while closing filewriter", e); - } + if (l == null) { + return false; } + sb.append(l + "\n"); } - return retval; + return writeFile(filePath, sb.toString(), overwrite); } - public static boolean writeFileLines(String filePath, String[] lines, boolean overwrite) { + public static boolean writeFile(String filePath, String content, boolean overwrite) { - if (lines == null) { + if (content == null) { return false; } FileWriter writer = null; - boolean retval = false; try { writer = new FileWriter(filePath, (!overwrite)); + writer.write(content); - if (lines.length == 0) { - writer.write(""); - } - - for (String l : lines) { - writer.write(l + "\n"); - } - - retval = true; + return true; } catch (Exception e) { _lastException = e; - retval = false; + return false; + } finally { if (writer != null) { @@ -132,8 +119,6 @@ public class Utils { } - return retval; - } public static String[] readFileLines(String filePath) { @@ -152,7 +137,6 @@ public class Utils { BufferedReader bufr = null; List<String> lines = new ArrayList<String>(); - String[] ret = null; try { @@ -165,11 +149,13 @@ public class Utils { } String[] ar = new String[lines.size()]; - ret = lines.toArray(ar); + + return lines.toArray(ar); } catch (Exception e) { logError("Error while reading file " + filePath, e); - ret = null; + return null; + } finally { try { if (bufr != null) { @@ -187,10 +173,61 @@ public class Utils { } } - return ret; - } + public static String readFile(String filePath){ + + if (filePath == null) { + return null; + } + + File f = new File(filePath); + if (!f.exists()) { + logError("File doesn't exist: " + filePath); + return null; + } + + FileReader fr = null; + BufferedReader bufr = null; + + StringBuilder sb = new StringBuilder(); + + try { + + fr = new FileReader(filePath); + bufr = new BufferedReader(fr); + String line = ""; + + while ((line = bufr.readLine()) != null) { + sb.append(line); + sb.append("\n"); + } + + return sb.toString(); + + } catch (Exception e) { + logError("Error while reading file " + filePath, e); + return null; + + } finally { + try { + if (bufr != null) { + bufr.close(); + } + } catch (IOException ex) { + logError("error while closing filereader", ex); + } + try { + if (fr != null) { + fr.close(); + } + } catch (IOException exc) { + logError("error while closing filereader", exc); + } + } + + } + public static long daysToMilliseconds(int days) { return (days * MILLIS_IN_DAY); } @@ -199,4 +236,120 @@ public class Utils { return (milliseconds / MILLIS_IN_DAY); } + public static boolean dumpLogcatToFile(String filePath) { + + if (filePath == null) { + return false; + } + + try { + + String cmd1 = "logcat -d | grep " + APP_NAME + ">" + filePath; + String cmd2 = "logcat -d | grep " + Commons.getContext().getPackageName() + ">>" + + filePath; + String SEP_LOG = "\n\n---------- [REPWIFI_LOG_SEPARATOR] ----------\n\n"; + + RootCommand c1 = new RootCommand(cmd1); + RootCommand c2 = new RootCommand(cmd2); + + if (c1.execute() != 0) { + return false; + } + + if (!writeFile(filePath, SEP_LOG, false)) { + return false; + } + + if (c2.execute() != 0) { + return false; + } + + return true; + + } catch (Exception e) { + logError("Exception during log dump.", e); + return false; + } + + } + + public static String netmaskIntToString(int mask) { + + if (mask < 8 || mask > 32) { + return null; + } + + StringBuilder sb = new StringBuilder(32); + StringBuilder sb2 = new StringBuilder(15); + + for (int i = 0; i < mask; i++) { + sb.append("1"); + } + + for (int i = 0; i < 32 - mask; i++) { + sb.append("0"); + } + + for (int i = 0; i < 3; i++) { + String bitString = sb.substring((i * 8), (i * 8) + 8); + int ibyte = Integer.parseInt(bitString, 2); + sb2.append(ibyte); + sb2.append("."); + } + String bitString = sb.substring(24, 32); + int ibyte = Integer.parseInt(bitString, 2); + sb2.append(ibyte); + + return sb2.toString(); + + } + + public static int netmaskStringToInt(String mask) { + + if (mask == null) { + return -1; + } + + String[] octs = mask.split("\\."); + if (octs.length != 4) { + return -1; + } + + int intmask = 0; + boolean prevIsZero = false; + for (String o : octs) { + + int intval = 0; + + try { + intval = Integer.parseInt(o, 10); + } catch (NumberFormatException e) { + return -1; + } + + String b = Integer.toBinaryString(intval); + if (b.length() != 8 && b.contains("1")) { + //invalid mask! has ones after a zero + return -1; + } + for (int i = 0; i < b.length(); i++) { + if (b.charAt(i) == '0') { + prevIsZero = true; + + } else if (prevIsZero) { + // invalid mask + return -1; + + } else { + intmask += 1; + } + + } + + } + + return intmask; + + } + } |