Ejemplo n.º 1
0
  public static String[] getProLicenseUnchecked() {
    // Get license file name
    String storageDir = Environment.getExternalStorageDirectory().getAbsolutePath();
    File licenseFile = new File(storageDir + File.separator + LICENSE_FILE_NAME);
    if (!licenseFile.exists())
      licenseFile =
          new File(storageDir + File.separator + ".xprivacy" + File.separator + LICENSE_FILE_NAME);

    // Get imported license file name
    String importedLicense =
        getUserDataDirectory(Process.myUid()) + File.separator + LICENSE_FILE_NAME;

    // Import license file
    if (licenseFile.exists()) {
      try {
        File out = new File(importedLicense);
        Util.log(null, Log.WARN, "Licensing: importing " + out.getAbsolutePath());
        InputStream is = new FileInputStream(licenseFile.getAbsolutePath());
        OutputStream os = new FileOutputStream(out.getAbsolutePath());
        byte[] buffer = new byte[1024];
        int read;
        while ((read = is.read(buffer)) != -1) os.write(buffer, 0, read);
        is.close();
        os.flush();
        os.close();

        // Protect license file
        setPermissions(out.getAbsolutePath(), 0700, Process.myUid(), Process.myUid());

        licenseFile.delete();
      } catch (Throwable ex) {
        Util.bug(null, ex);
      }
    }

    // Check license file
    licenseFile = new File(importedLicense);
    if (licenseFile.exists()) {
      // Read license
      try {
        IniFile iniFile = new IniFile(licenseFile);
        String name = iniFile.get("name", "");
        String email = iniFile.get("email", "");
        String signature = iniFile.get("signature", "");
        return new String[] {name, email, signature};
      } catch (Throwable ex) {
        bug(null, ex);
        return null;
      }
    } else Util.log(null, Log.INFO, "Licensing: no license file");
    return null;
  }
Ejemplo n.º 2
0
  public boolean loadInitialValue() {
    Vector v = null;
    CellPosition nt = null;
    int pos;
    double value;
    String data = null;

    clear();

    String iniPath = ini.GetFileName();
    pos = iniPath.lastIndexOf(File.separatorChar);
    if (pos >= 0) iniPath = iniPath.substring(0, pos + 1);
    else iniPath = "";

    if ((v = ini.get(modelName, "width")) != null) {
      cols = Integer.parseInt((String) v.elementAt(0));
      v = ini.get(modelName, "height");
      rows = Integer.parseInt((String) v.elementAt(0));
      dim = new CellPosition(2);
      dim.setValue(0, rows);
      dim.setValue(1, cols);
    } else {
      if ((v = ini.get(modelName, "dim")) != null) {
        dim = new CellPosition((String) v.elementAt(0));
        rows = dim.getValue(0);
        cols = dim.getValue(1);
      }
    }
    if (dim == null) {
      System.err.println("cannot find dimension of cell model " + modelName);
      return false;
    }

    // System.out.println(modelName+" dimension = "+dim.toString());
    nt = new CellPosition(dim.size());

    value = Double.NaN;
    if ((v = ini.get(modelName, "initialvalue")) != null) {
      value = Double.parseDouble((String) v.elementAt(0));
      this.setNumberWidth(Double.toString(value).length());
    }
    currentState = new CellState(dim, value);

    if ((v = ini.get(modelName, "initialrow")) != null) {
      for (int j = 0; j < v.size(); j++) {
        data = (String) v.elementAt(j);
        pos = data.indexOf(' ');
        int row = Integer.parseInt(data.substring(0, pos));
        if (row >= rows) {
          System.err.println(
              "The number of row for initialRow is out of range. It's "
                  + row
                  + " and must be in [ 0, "
                  + (rows - 1)
                  + "]");
          return false;
        }
        nt.setValue(0, row);
        StringTokenizer tokens = new StringTokenizer(data.substring(pos + 1));
        if (tokens.countTokens() < cols) {
          System.err.println(
              "Insuficient data for initialRow. Last row with "
                  + tokens.countTokens()
                  + " elements. Note: May be a middle row definition with less elements.");
          return false;
        }
        for (int col = 0; col < cols; col++) {
          try {
            value = Double.parseDouble(tokens.nextToken());
          } catch (Exception ex) {
            value = Double.NaN;
          }
          nt.setValue(1, col);
          currentState.setValue(nt, value);
          this.setNumberWidth(Double.toString(value).length());
        }
      }
    }

    if ((v = ini.get(modelName, "initialRowValue")) != null) {
      for (int j = 0; j < v.size(); j++) {
        data = (String) v.elementAt(j);
        pos = data.indexOf(' ');
        int row = Integer.parseInt(data.substring(0, pos));
        if (row >= rows) {
          System.err.println(
              "The number of row for initialRow is out of range. It's "
                  + row
                  + " and must be in [ 0, "
                  + (rows - 1)
                  + "]");
          return false;
        }
        data = data.substring(pos + 1).trim();
        if (data.length() == 0) {
          System.err.println(
              "Invalid initial row value for initialRowValue (must be a pair rowNumber rowValues)!");
          return false;
        }

        nt.setValue(0, row);
        if (data.length() != cols) {
          System.err.println(
              "The size of the rows for the initial values of the CoupledCell must be equal to the width value !");
          return false;
        }
        for (int col = 0; col < cols; col++) {
          char ch = data.charAt(col);
          value = (ch >= '0' && ch <= '9') ? ch - '0' : Double.NaN;
          nt.setValue(1, col);
          currentState.setValue(nt, value);
        }
      }
    }

    if ((v = ini.get(modelName, "initialCellsValue")) != null) {
      String filename = (String) v.elementAt(0);
      if (filename.length() > 0) {
        try {
          BufferedReader reader = new BufferedReader(new FileReader(iniPath + filename));

          while ((data = reader.readLine()) != null) {
            if (data.trim().length() > 0) {
              if ((pos = data.indexOf('=')) > 0) {
                nt.setValue(data.substring(0, pos));
                value = Double.NaN;
                try {
                  value = Double.parseDouble(data.substring(pos + 1).trim());
                  this.setNumberWidth(Double.toString(value).length());
                } catch (NumberFormatException e) {
                }

                currentState.setValue(nt, value);
              }
            }
          }
          reader.close();

        } catch (FileNotFoundException e) {
          System.err.println(
              "Can't open the file '" + filename + "' defined by the initialCellsValue clause");
          return false;
        } catch (IOException e) {
          System.err.println(e.getMessage());
          return false;
        }
      }
    }

    if ((v = ini.get(modelName, "initialMapValue")) != null) {
      String filename = (String) v.elementAt(0);
      if (filename.length() > 0) {
        try {
          BufferedReader reader = new BufferedReader(new FileReader(iniPath + filename));
          CellPosition.indexToPos(dim, 0, nt);
          boolean overflow = false;
          while ((data = reader.readLine()) != null && !overflow) {
            data = data.trim();
            if (data.length() > 0) {
              try {
                value = Double.parseDouble(data);
              } catch (NumberFormatException e) {
                value = Double.NaN;
              }
              currentState.setValue(nt, value);
              overflow = nt.next(dim);
            }
          }
          reader.close();

        } catch (FileNotFoundException e) {
          System.err.println(
              "Can't open the file '" + filename + "' defined by the initialCellsValue clause");
          return false;
        } catch (IOException e) {
          System.err.println(e.getMessage());
          return false;
        }
      }
    }

    currentTime = new VTime(0, 0, 0, 0);

    timeList.addElement(currentTime);
    stateList.addElement(currentState);

    return true;
  }