Beispiel #1
0
 public static ParsedResult parseResult(Result theResult) {
   for (ResultParser parser : PARSERS) {
     ParsedResult result = parser.parse(theResult);
     if (result != null) {
       return result;
     }
   }
   return new TextParsedResult(theResult.getText(), null);
 }
 private static void doTest(
     String contents,
     String description,
     String summary,
     String location,
     String startString,
     String endString,
     String organizer,
     String[] attendees,
     double latitude,
     double longitude) {
   Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
   ParsedResult result = ResultParser.parseResult(fakeResult);
   assertSame(ParsedResultType.CALENDAR, result.getType());
   CalendarParsedResult calResult = (CalendarParsedResult) result;
   assertEquals(description, calResult.getDescription());
   assertEquals(summary, calResult.getSummary());
   assertEquals(location, calResult.getLocation());
   assertEquals(startString, DATE_TIME_FORMAT.format(calResult.getStart()));
   assertEquals(
       endString, calResult.getEnd() == null ? null : DATE_TIME_FORMAT.format(calResult.getEnd()));
   assertEquals(organizer, calResult.getOrganizer());
   assertArrayEquals(attendees, calResult.getAttendees());
   assertEqualOrNaN(latitude, calResult.getLatitude());
   assertEqualOrNaN(longitude, calResult.getLongitude());
 }
 private static boolean a(String paramString, int paramInt) {
   int i = paramInt + 1;
   int j = paramString.indexOf('/', i);
   if (j < 0) {
     j = paramString.length();
   }
   return ResultParser.a(paramString, i, j - i);
 }
 private static void doTest(String contents, String normalized, BarcodeFormat format) {
   Result fakeResult = new Result(contents, null, null, format);
   ParsedResult result = ResultParser.parseResult(fakeResult);
   assertSame(ParsedResultType.PRODUCT, result.getType());
   ProductParsedResult productResult = (ProductParsedResult) result;
   assertEquals(contents, productResult.getProductID());
   assertEquals(normalized, productResult.getNormalizedProductID());
 }
 private static void doTest(
     String contents, double latitude, double longitude, double altitude, String query) {
   Result fakeResult = new Result(contents, null, null, BarcodeFormat.QR_CODE);
   ParsedResult result = ResultParser.parseResult(fakeResult);
   assertSame(ParsedResultType.GEO, result.getType());
   GeoParsedResult geoResult = (GeoParsedResult) result;
   assertEquals(latitude, geoResult.getLatitude());
   assertEquals(longitude, geoResult.getLongitude());
   assertEquals(altitude, geoResult.getAltitude());
 }
Beispiel #6
0
 String fillInCustomSearchURL(String text) {
   if (customProductSearch == null) {
     return text; // ?
   }
   String url = customProductSearch.replace("%s", text);
   if (rawResult != null) {
     url = url.replace("%f", rawResult.getBarcodeFormat().toString());
     if (url.contains("%t")) {
       ParsedResult parsedResultAgain = ResultParser.parseResult(rawResult);
       url = url.replace("%t", parsedResultAgain.getType().toString());
     }
   }
   return url;
 }
Beispiel #7
0
 // Handles send intents from the Contacts app, retrieving a contact as a VCARD.
 private void encodeFromStreamExtra(Intent intent) throws WriterException {
   format = BarcodeFormat.QR_CODE;
   Bundle bundle = intent.getExtras();
   if (bundle == null) {
     throw new WriterException("No extras");
   }
   Uri uri = bundle.getParcelable(Intent.EXTRA_STREAM);
   if (uri == null) {
     throw new WriterException("No EXTRA_STREAM");
   }
   byte[] vcard;
   String vcardString;
   InputStream stream = null;
   try {
     stream = activity.getContentResolver().openInputStream(uri);
     if (stream == null) {
       throw new WriterException("Can't open stream for " + uri);
     }
     ByteArrayOutputStream baos = new ByteArrayOutputStream();
     byte[] buffer = new byte[2048];
     int bytesRead;
     while ((bytesRead = stream.read(buffer)) > 0) {
       baos.write(buffer, 0, bytesRead);
     }
     vcard = baos.toByteArray();
     vcardString = new String(vcard, 0, vcard.length, "UTF-8");
   } catch (IOException ioe) {
     throw new WriterException(ioe);
   } finally {
     if (stream != null) {
       try {
         stream.close();
       } catch (IOException e) {
         // continue
       }
     }
   }
   Log.d(TAG, "Encoding share intent content:");
   Log.d(TAG, vcardString);
   Result result = new Result(vcardString, vcard, null, BarcodeFormat.QR_CODE);
   ParsedResult parsedResult = ResultParser.parseResult(result);
   if (!(parsedResult instanceof AddressBookParsedResult)) {
     throw new WriterException("Result was not an address");
   }
   encodeQRCodeContents((AddressBookParsedResult) parsedResult);
   if (contents == null || contents.isEmpty()) {
     throw new WriterException("No content to encode");
   }
 }
  /**
   * A valid barcode has been found, so give an indication of success and show the results.
   *
   * @param rawResult The contents of the barcode.
   * @param scaleFactor amount by which thumbnail was scaled
   * @param barcode A greyscale bitmap of the camera data which was decoded.
   */
  @SuppressWarnings("unchecked")
  public void handleDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
    inactivityTimer.onActivity();

    ParsedResult result = ResultParser.parseResult(rawResult);

    // Hand back whatever action they requested - this can be changed to Intents.Scan.ACTION when
    // the deprecated intent is retired.
    Intent intent = new Intent(getIntent().getAction());
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    intent.putExtra(Intents.Scan.RESULT, result.getDisplayResult());
    intent.putExtra(Intents.Scan.RESULT_TYPE, result.getType().ordinal());
    intent.putExtra(Intents.Scan.RESULT_FORMAT, rawResult.getBarcodeFormat().toString());
    byte[] rawBytes = bm2bytes(barcode);
    // byte[] rawBytes = rawResult.getRawBytes();
    if (rawBytes != null && rawBytes.length > 0) {
      intent.putExtra(Intents.Scan.RESULT_BYTES, rawBytes);
    }
    Map<ResultMetadataType, ?> metadata = rawResult.getResultMetadata();
    if (metadata != null) {
      if (metadata.containsKey(ResultMetadataType.UPC_EAN_EXTENSION)) {
        intent.putExtra(
            Intents.Scan.RESULT_UPC_EAN_EXTENSION,
            metadata.get(ResultMetadataType.UPC_EAN_EXTENSION).toString());
      }
      Integer orientation = (Integer) metadata.get(ResultMetadataType.ORIENTATION);
      if (orientation != null) {
        intent.putExtra(Intents.Scan.RESULT_ORIENTATION, orientation.intValue());
      }
      String ecLevel = (String) metadata.get(ResultMetadataType.ERROR_CORRECTION_LEVEL);
      if (ecLevel != null) {
        intent.putExtra(Intents.Scan.RESULT_ERROR_CORRECTION_LEVEL, ecLevel);
      }
      Iterable<byte[]> byteSegments =
          (Iterable<byte[]>) metadata.get(ResultMetadataType.BYTE_SEGMENTS);
      if (byteSegments != null) {
        int i = 0;
        for (byte[] byteSegment : byteSegments) {
          intent.putExtra(Intents.Scan.RESULT_BYTE_SEGMENTS_PREFIX + i, byteSegment);
          i++;
        }
      }
    }

    Message message = Message.obtain(handler, R.id.return_scan_result, intent);
    handler.sendMessage(message);
  }
Beispiel #9
0
 final String fillInCustomSearchURL(String text) {
   if (customProductSearch == null) {
     return text; // ?
   }
   try {
     text = URLEncoder.encode(text, "UTF-8");
   } catch (UnsupportedEncodingException e) {
     // can't happen; UTF-8 is always supported. Continue, I guess, without encoding
   }
   String url = customProductSearch;
   if (rawResult != null) {
     // Replace %f but only if it doesn't seem to be a hex escape sequence. This remains
     // problematic but avoids the more surprising problem of breaking escapes
     url = url.replaceFirst("%f(?![0-9a-f])", rawResult.getBarcodeFormat().toString());
     if (url.contains("%t")) {
       ParsedResult parsedResultAgain = ResultParser.parseResult(rawResult);
       url = url.replace("%t", parsedResultAgain.getType().toString());
     }
   }
   // Replace %s last as it might contain itself %f or %t
   return url.replace("%s", text);
 }
 private static ParsedResult parseResult(Result rawResult) {
   return ResultParser.parseResult(rawResult);
 }