Exemplo n.º 1
0
  @Override
  public boolean execute(String action, JSONArray data, CallbackContext callbackContext)
      throws JSONException {

    if (action.equals("extract")) {
      try {
        String id = data.getString(0);

        String[] serverPremium = {
          "s210", "s00", "s40", "s10", "s20", "s30", "s50", "s70", "s80", "s90", "s100", "s110",
          "s120", "s130", "s140", "s160", "s170", "s180", "s190", "s200", "s220", "s230", "s240",
          "s250", "s260", "s270", "s280", "s290"
        };

        URL url = new URL("http://www.nowvideo.li/mobile/video.php?id=" + id + "&download=2");
        URLConnection connection = null;
        String content = null;
        connection = url.openConnection();
        Scanner scanner = new Scanner(connection.getInputStream());
        scanner.useDelimiter("\\Z");
        content = scanner.next();

        String[] array = content.split("<source src=\"http://");
        String link = "";
        if (array.length > 2) link = array[2].split("\"")[0];
        else link = array[1].split("\"")[0];
        String originalServer = link.substring(0, link.indexOf('.'));
        String restOfLink = link.substring(link.indexOf('.') + 1, link.length());

        System.out.println(link);
        System.out.println(originalServer);
        System.out.println(restOfLink);

        String server = originalServer.substring(0, originalServer.length() - 1) + "0";

        URL urlVideo = new URL("http://" + server + "." + restOfLink);

        int responseCode = 0;
        try {
          HttpURLConnection huc = (HttpURLConnection) urlVideo.openConnection();
          huc.setRequestMethod("HEAD");
          huc.setConnectTimeout(2000);
          responseCode = huc.getResponseCode();
          if (responseCode != 200) throw new Exception();
          callbackContext.success("http://" + server + "." + restOfLink);
        } catch (Exception e) {
          for (String tryServer : serverPremium) {
            try {
              urlVideo = new URL("http://" + tryServer + "." + restOfLink);
              HttpURLConnection huc = (HttpURLConnection) urlVideo.openConnection();
              huc.setRequestMethod("HEAD");
              huc.setConnectTimeout(2000);
              responseCode = huc.getResponseCode();
              if (responseCode != 200) throw new Exception();
              callbackContext.success("http://" + tryServer + "." + restOfLink);
            } catch (Exception exc) {

            }
          }
          callbackContext.success("http://" + originalServer + "." + restOfLink);
        }

      } catch (Exception ex) {
        callbackContext.error(ex.toString());
      }

      return true;

    } else {

      return false;
    }
  }
  @Override
  public boolean execute(String action, JSONArray data, CallbackContext callbackContext)
      throws JSONException {
    reader.start();

    am.setStreamVolume(
        AudioManager.STREAM_MUSIC, am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
    this.callbackContext = callbackContext;
    timedOut = false;

    Log.w(TAG, action);
    if (mute) {
      PluginResult dataResult = new PluginResult(PluginResult.Status.OK, "NOTFOUND");
      callbackContext.sendPluginResult(dataResult);
      return true;
    }

    final ACR35Controller self = this;

    final String loadKeyCommand = "FF 82 00 00 06 %s";
    final String authCommand = "FF 86 00 00 05 01 00 %s 60 00";
    final String defaultKey = "FF FF FF FF FF FF";
    String authKeyCommand = "FF 86 00 00 05 01 00 00 60 00";

    if (action.equals("getDeviceId")) {
      cordova
          .getThreadPool()
          .execute(
              new Runnable() {
                @Override
                public void run() {
                  reader.setMute(false);

                  reader.reset(
                      new AudioJackReader.OnResetCompleteListener() {
                        @Override
                        public void onResetComplete(AudioJackReader audioJackReader) {
                          reader.getDeviceId();
                        }
                      });
                }
              });
    }

    if (action.equals("getDeviceStatus")) {
      cordova
          .getThreadPool()
          .execute(
              new Runnable() {
                @Override
                public void run() {
                  reader.setMute(false);

                  reader.reset(
                      new AudioJackReader.OnResetCompleteListener() {
                        @Override
                        public void onResetComplete(AudioJackReader audioJackReader) {
                          reader.getStatus();
                        }
                      });
                }
              });
    }

    if (action.equals("readIdFromTag")) {
      executeAPDUCommands(new byte[][] {hexToBytes("FFCA000000")});
    }
    if (action.equals("readDataFromTag")) {
      executeAPDUCommands(
          new byte[][] {
            hexToBytes(String.format(loadKeyCommand, defaultKey)),
            hexToBytes(String.format(authCommand, "04")),
            hexToBytes("FF B0 00 04 10"),
            hexToBytes("FF B0 00 05 10"),
            hexToBytes("FF B0 00 06 10"),
            hexToBytes(String.format(authCommand, "08")),
            hexToBytes("FF B0 00 08 10"),
            hexToBytes("FF B0 00 09 10"),
            hexToBytes("FF B0 00 0A 10"),
            hexToBytes(String.format(authCommand, "10")),
            hexToBytes("FF B0 00 10 10"),
            hexToBytes("FF B0 00 11 10")
          });
    }
    if (action.equals("writeDataIntoTag")) {
      try {
        String dataString = data.get(0).toString();
        byte[] dataToWrite = new byte[128];
        Arrays.fill(dataToWrite, (byte) 0);
        byte[] dataBytes = hexToBytes(dataString);
        System.arraycopy(dataBytes, 0, dataToWrite, 0, dataBytes.length);

        String dataStringToWrite = bytesToHex(dataToWrite).replaceAll("\\s", "");
        String commandString1 = "FF D6 00 04 30" + dataStringToWrite.substring(0, 95);
        String commandString2 = "FF D6 00 08 30" + dataStringToWrite.substring(96, (96 * 2) - 1);
        String commandString3 =
            "FF D6 00 10 20" + dataStringToWrite.substring(96 * 2, (96 * 2 + 64) - 1);

        Log.w(TAG, dataStringToWrite);
        executeAPDUCommands(
            new byte[][] {
              hexToBytes(String.format(loadKeyCommand, defaultKey)),
              hexToBytes(String.format(authCommand, "04")),
              hexToBytes(commandString1),
              hexToBytes(String.format(authCommand, "08")),
              hexToBytes(commandString2),
              hexToBytes(String.format(authCommand, "10")),
              hexToBytes(commandString3),
            });
      } catch (java.lang.Exception e) {
        Log.w(TAG, e);
      }
    }

    Calendar calendar =
        Calendar.getInstance(); // gets a calendar using the default time zone and locale.
    calendar.add(Calendar.SECOND, timeOut);

    timer = new Timer();
    timer.schedule(new TimeoutClass(reader, self), calendar.getTime());

    PluginResult dataResult = new PluginResult(PluginResult.Status.OK, "IGNORE");
    dataResult.setKeepCallback(true);

    callbackContext.sendPluginResult(dataResult);
    return true;
  }