Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class NotificationUtils {
- private static final int WHITE = 0xFFFFFFFF;
- private static final int BLACK = 0xFF000000;
- private static final int DEFAULT_NOTIFICATION_ID = 0x1002;
- public static void notify(Context context, BarcodeModel barcodeModel) {
- NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
- NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context);
- notificationBuilder.setContentTitle(context.getString(R.string.app_name));
- notificationBuilder.setSmallIcon(R.drawable.ic_launcher);
- if (barcodeModel.getName() != null)
- notificationBuilder.setContentText(barcodeModel.getName());
- Intent intent = new Intent(Constants.LAUNCH_WEAR_APP);
- try {
- Bitmap barcode = encodeAsBitmap(barcodeModel, 600, 600);
- NotificationCompat.BigPictureStyle bigStyle = new NotificationCompat.BigPictureStyle();
- bigStyle.bigPicture(barcode);
- notificationBuilder.setStyle(bigStyle);
- intent.putExtra("barcode",barcode);
- } catch (WriterException e) {
- e.printStackTrace();
- }
- PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
- notificationBuilder.setContentIntent(pendingIntent);
- Notification notification = notificationBuilder.build();
- notificationManager.notify(barcodeModel.getBarcodeId(), notification);
- }
- public static Bitmap encodeAsBitmap(BarcodeModel barcodeModel, int img_width, int img_height) throws WriterException {
- String contentsToEncode = barcodeModel.getBarcodeValue();
- if (contentsToEncode == null) {
- return null;
- }
- Map<EncodeHintType, Object> hints = null;
- String encoding = guessAppropriateEncoding(contentsToEncode);
- if (encoding != null) {
- hints = new EnumMap<EncodeHintType, Object>(EncodeHintType.class);
- hints.put(EncodeHintType.CHARACTER_SET, encoding);
- }
- MultiFormatWriter writer = new MultiFormatWriter();
- BitMatrix result;
- try {
- if (barcodeModel.getBarcodeFormat()!=null){
- result = writer.encode(contentsToEncode, BarcodeFormat.valueOf(barcodeModel.getBarcodeFormat()), img_width, img_height, hints);}
- else{
- result = writer.encode(contentsToEncode, BarcodeFormat.CODE_128, img_width, img_height, hints);
- }
- } catch (IllegalArgumentException iae) {
- // Unsupported format
- return null;
- }
- int width = result.getWidth();
- int height = result.getHeight();
- int[] pixels = new int[width * height];
- for (int y = 0; y < height; y++) {
- int offset = y * width;
- for (int x = 0; x < width; x++) {
- pixels[offset + x] = result.get(x, y) ? BLACK : WHITE;
- }
- }
- Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
- bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
- return bitmap;
- }
- private static String guessAppropriateEncoding(CharSequence contents) {
- // Very crude at the moment
- for (int i = 0; i < contents.length(); i++) {
- if (contents.charAt(i) > 0xFF) {
- return "UTF-8";
- }
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement