Advertisement
Ankhwatcher

Barcode Notification

Jul 16th, 2014
299
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.50 KB | None | 0 0
  1. public class NotificationUtils {
  2.  
  3.     private static final int WHITE = 0xFFFFFFFF;
  4.     private static final int BLACK = 0xFF000000;
  5.     private static final int DEFAULT_NOTIFICATION_ID = 0x1002;
  6.  
  7.     public static void notify(Context context, BarcodeModel barcodeModel) {
  8.  
  9.         NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
  10.  
  11.         NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
  12.  
  13.  
  14.         notificationBuilder.setContentTitle(context.getString(R.string.app_name));
  15.         notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
  16.         if (barcodeModel.getName() != null)
  17.             notificationBuilder.setContentText(barcodeModel.getName());
  18.         Intent intent = new Intent(Constants.LAUNCH_WEAR_APP);
  19.  
  20.         try {
  21.             Bitmap barcode  = encodeAsBitmap(barcodeModel, 600, 600);
  22.             NotificationCompat.BigPictureStyle bigStyle = new NotificationCompat.BigPictureStyle();
  23.             bigStyle.bigPicture(barcode);
  24.             notificationBuilder.setStyle(bigStyle);
  25.             intent.putExtra("barcode",barcode);
  26.         } catch (WriterException e) {
  27.             e.printStackTrace();
  28.         }
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.         PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
  36.  
  37.         notificationBuilder.setContentIntent(pendingIntent);
  38.         Notification notification = notificationBuilder.build();
  39.  
  40.         notificationManager.notify(barcodeModel.getBarcodeId(), notification);
  41.     }
  42.  
  43.     public static Bitmap encodeAsBitmap(BarcodeModel barcodeModel, int img_width, int img_height) throws WriterException {
  44.         String contentsToEncode = barcodeModel.getBarcodeValue();
  45.         if (contentsToEncode == null) {
  46.             return null;
  47.         }
  48.         Map<EncodeHintType, Object> hints = null;
  49.         String encoding = guessAppropriateEncoding(contentsToEncode);
  50.         if (encoding != null) {
  51.             hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
  52.             hints.put(EncodeHintType.CHARACTER_SET, encoding);
  53.         }
  54.         MultiFormatWriter writer = new MultiFormatWriter();
  55.         BitMatrix result;
  56.         try {
  57.             if (barcodeModel.getBarcodeFormat()!=null){
  58.             result = writer.encode(contentsToEncode, BarcodeFormat.valueOf(barcodeModel.getBarcodeFormat()), img_width, img_height, hints);}
  59.             else{
  60.                 result = writer.encode(contentsToEncode, BarcodeFormat.CODE_128, img_width, img_height, hints);
  61.             }
  62.         } catch (IllegalArgumentException iae) {
  63.             // Unsupported format
  64.             return null;
  65.         }
  66.         int width = result.getWidth();
  67.         int height = result.getHeight();
  68.         int[] pixels = new int[width * height];
  69.         for (int y = 0; y < height; y++) {
  70.             int offset = y * width;
  71.             for (int x = 0; x < width; x++) {
  72.                 pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
  73.             }
  74.         }
  75.  
  76.         Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  77.         bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
  78.         return bitmap;
  79.     }
  80.  
  81.     private static String guessAppropriateEncoding(CharSequence contents) {
  82.         // Very crude at the moment
  83.         for (int i = 0; i < contents.length(); i++) {
  84.             if (contents.charAt(i) > 0xFF) {
  85.                 return "UTF-8";
  86.             }
  87.         }
  88.         return null;
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement