Example usage for java.nio ByteOrder BIG_ENDIAN

List of usage examples for java.nio ByteOrder BIG_ENDIAN

Introduction

In this page you can find the example usage for java.nio ByteOrder BIG_ENDIAN.

Prototype

ByteOrder BIG_ENDIAN

To view the source code for java.nio ByteOrder BIG_ENDIAN.

Click Source Link

Document

This constant represents big endian.

Usage

From source file:au.org.ala.layers.intersect.Grid.java

/**
 * for grid cutter//from  ww w  .  j av a  2s.  co m
 * <p/>
 * writes out a list of double (same as getGrid() returns) to a file
 * <p/>
 * byteorderlsb
 * data type, FLOAT
 *
 * @param newfilename
 * @param dfiltered
 */
public void writeGrid(String newfilename, double[] dfiltered, double xmin, double ymin, double xmax,
        double ymax, double xres, double yres, int nrows, int ncols) {
    int size, i, length = dfiltered.length;
    double maxvalue = Double.MAX_VALUE * -1;
    double minvalue = Double.MAX_VALUE;

    //write data as whole file
    RandomAccessFile afile = null;
    try { //read of random access file can throw an exception
        afile = new RandomAccessFile(newfilename + ".gri", "rw");

        size = 4;
        byte[] b = new byte[size * length];
        ByteBuffer bb = ByteBuffer.wrap(b);

        if (byteorderLSB) {
            bb.order(ByteOrder.LITTLE_ENDIAN);
        } else {
            bb.order(ByteOrder.BIG_ENDIAN);
        }
        for (i = 0; i < length; i++) {
            if (Double.isNaN(dfiltered[i])) {
                bb.putFloat((float) noDataValueDefault);
            } else {
                if (minvalue > dfiltered[i]) {
                    minvalue = dfiltered[i];
                }
                if (maxvalue < dfiltered[i]) {
                    maxvalue = dfiltered[i];
                }
                bb.putFloat((float) dfiltered[i]);
            }
        }

        afile.write(b);
    } catch (Exception e) {
        logger.error("error writing grid file", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    writeHeader(newfilename, xmin, ymin, xmin + xres * ncols, ymin + yres * nrows, xres, yres, nrows, ncols,
            minvalue, maxvalue, "FLT4BYTES", String.valueOf(noDataValueDefault));
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private byte[] encodeTimelinePin(UUID uuid, int timestamp, short duration, int icon_id, String title,
        String subtitle) {// www .j  av a2s .c  o m
    final short TIMELINE_PIN_LENGTH = 46;

    icon_id |= 0x80000000;
    byte attributes_count = 2;
    byte actions_count = 0;

    int attributes_length = 10 + title.getBytes().length;
    if (subtitle != null && !subtitle.isEmpty()) {
        attributes_length += 3 + subtitle.getBytes().length;
        attributes_count += 1;
    }

    int pin_length = TIMELINE_PIN_LENGTH + attributes_length;
    ByteBuffer buf = ByteBuffer.allocate(pin_length);

    // pin - 46 bytes
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putLong(uuid.getMostSignificantBits());
    buf.putLong(uuid.getLeastSignificantBits());
    buf.putLong(0); // parent
    buf.putLong(0);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.putInt(timestamp); // 32-bit timestamp
    buf.putShort(duration);
    buf.put((byte) 0x02); // type (0x02 = pin)
    buf.putShort((short) 0x0001); // flags 0x0001 = ?
    buf.put((byte) 0x01); // layout was (0x02 = pin?), 0x01 needed for subtitle but seems to do no harm if there isn't one

    buf.putShort((short) attributes_length); // total length of all attributes and actions in bytes
    buf.put(attributes_count);
    buf.put(actions_count);

    buf.put((byte) 4); // icon
    buf.putShort((short) 4); // length of int
    buf.putInt(icon_id);
    buf.put((byte) 1); // title
    buf.putShort((short) title.getBytes().length);
    buf.put(title.getBytes());
    if (subtitle != null && !subtitle.isEmpty()) {
        buf.put((byte) 2); //subtitle
        buf.putShort((short) subtitle.getBytes().length);
        buf.put(subtitle.getBytes());
    }

    return encodeBlobdb(uuid, BLOBDB_INSERT, BLOBDB_PIN, buf.array());
}

From source file:au.org.ala.layers.intersect.Grid.java

public void writeGrid(String newfilename, float[] dfiltered, double xmin, double ymin, double xmax, double ymax,
        double xres, double yres, int nrows, int ncols) {
    int size, i, length = dfiltered.length;
    double maxvalue = Double.MAX_VALUE * -1;
    double minvalue = Double.MAX_VALUE;

    //write data as whole file
    RandomAccessFile afile = null;
    try { //read of random access file can throw an exception
        afile = new RandomAccessFile(newfilename + ".gri", "rw");

        size = 4;//from w  w  w  . j av  a 2s .com
        byte[] b = new byte[size * length];
        ByteBuffer bb = ByteBuffer.wrap(b);

        if (byteorderLSB) {
            bb.order(ByteOrder.LITTLE_ENDIAN);
        } else {
            bb.order(ByteOrder.BIG_ENDIAN);
        }
        for (i = 0; i < length; i++) {
            if (Double.isNaN(dfiltered[i])) {
                bb.putFloat((float) noDataValueDefault);
            } else {
                if (minvalue > dfiltered[i]) {
                    minvalue = dfiltered[i];
                }
                if (maxvalue < dfiltered[i]) {
                    maxvalue = dfiltered[i];
                }
                bb.putFloat((float) dfiltered[i]);
            }
        }

        afile.write(b);
    } catch (Exception e) {
        logger.error("error writing grid file", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    writeHeader(newfilename, xmin, ymin, xmin + xres * ncols, ymin + yres * nrows, xres, yres, nrows, ncols,
            minvalue, maxvalue, "FLT4BYTES", String.valueOf(noDataValueDefault));

}

From source file:org.eclipse.paho.android.service.MqttConnection.java

private void parseMqttMessageV2(String topic, MqttMessage mqttMessage) throws Exception {
    Log.v("mqtt", "parseMqttMessageV2");
    Context ctx = NanuService.getContext();
    byte origMqttMsgByte[] = mqttMessage.getPayload();
    int mqttIndex = 0;
    boolean processVTagSuccess = false;

    boolean processPTagSuccess = false;
    long mqttPacketValue = 0;

    boolean processTTagSuccess = false;
    long mqttTimestampValue = 0;

    boolean processLTagSuccess = false;
    int mqttMsgLengthValue = 0;

    boolean processMTagSuccess = false;
    String mqttMessageValue = "";
    String mqttMembersValue = "";

    boolean processGTagSuccess = false;
    long mqttGroupIDValue = 0;

    boolean processSTagSuccess = false;
    String mqttSubjectValue = "";

    boolean processCTagSuccess = false;
    int mqttMemberCountValue = 0;

    boolean processNTagSuccess = false;
    int mqttAdminCountValue = 0;

    boolean processATagSuccess = false;
    String mqttAdminsValue = "";

    String[] topicArray = topic.split("\\/");

    String sender = topicArray[2];
    if (topicArray.length == 4) {
        processGTagSuccess = true;// w  ww.j a  v  a 2s .c o  m
        try {
            mqttGroupIDValue = Long.parseLong(topicArray[3].toString().trim());
        } catch (NumberFormatException nfe) {
            processGTagSuccess = false;
            nfe.printStackTrace();
        }

        if (mqttGroupIDValue == 0) {
            try {
                mqttGroupIDValue = Long.valueOf(topicArray[3].trim());
            } catch (Exception err) {
                processGTagSuccess = false;
                err.printStackTrace();
            }
        }
    }

    String mqttMsgDateValue = "";
    for (int indexMqttCounter = 0; indexMqttCounter < origMqttMsgByte.length; indexMqttCounter++) {
        /*       Log.v(SettingsManager.TAG, "MqttService origMqttMsgByte[" + indexMqttCounter + "] = " + origMqttMsgByte[indexMqttCounter]); */
    }

    for (int indexMqttCounter = 0; indexMqttCounter < origMqttMsgByte.length; indexMqttCounter++) {
        if (indexMqttCounter == 0) {
            mqttIndex = indexMqttCounter;
            long mqttVTag = getMqttTag(origMqttMsgByte, mqttIndex);
            if (mqttVTag != -1) {
                if (mqttVTag == 86) // "V"
                {
                    processVTagSuccess = true;
                    mqttIndex = mqttIndex + 2;
                } else {
                    processVTagSuccess = false;
                    break;
                }
            }
        } else {
            if (mqttIndex == indexMqttCounter) {
                long mqttTag = getMqttTag(origMqttMsgByte, mqttIndex);
                if (mqttTag != -1) {
                    if (mqttTag == 80) /* "P" */ {
                        mqttIndex = mqttIndex + 1;
                        long mPValue = origMqttMsgByte[mqttIndex];
                        mqttPacketValue = mPValue;
                        mqttIndex = mqttIndex + 1;
                        processPTagSuccess = true;
                    } else if (mqttTag == 84) /* "T" */ {
                        mqttIndex = mqttIndex + 1;
                        byte timeStampArray[] = new byte[8];
                        for (int i = 0; i < 8; i++) {
                            timeStampArray[i] = origMqttMsgByte[mqttIndex + i];
                        }
                        mqttTimestampValue = ByteBuffer.wrap(timeStampArray).order(ByteOrder.LITTLE_ENDIAN)
                                .getLong();

                        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
                        String messageYear = sdf.format(mqttTimestampValue);
                        if (messageYear.length() != 4) {
                            mqttTimestampValue = ByteBuffer.wrap(timeStampArray).order(ByteOrder.BIG_ENDIAN)
                                    .getLong();
                        }
                        SimpleDateFormat sdfDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                        String messageDate = sdfDate.format(mqttTimestampValue);
                        processTTagSuccess = true;
                        mqttIndex = mqttIndex + 8;
                    } else if (mqttTag == 76) /* "L" */ {
                        if (processPTagSuccess) {
                            if (mqttPacketValue == -128 || (mqttPacketValue == -117)
                                    || (mqttPacketValue == -115) || (mqttPacketValue == -126)) {
                                mqttIndex = mqttIndex + 1;
                                mqttMsgLengthValue = origMqttMsgByte[mqttIndex];
                                processLTagSuccess = true;
                                mqttIndex = mqttIndex + 1;
                            } else if (mqttPacketValue == 0) {
                                mqttIndex = mqttIndex + 1;
                                byte msgLengthArray[] = new byte[4];
                                for (int i = 0; i < 4; i++) {
                                    msgLengthArray[i] = origMqttMsgByte[mqttIndex + i];
                                }
                                mqttMsgLengthValue = ByteBuffer.wrap(msgLengthArray)
                                        .order(ByteOrder.LITTLE_ENDIAN).getInt();
                                processLTagSuccess = true;
                                mqttIndex = mqttIndex + 4;
                            }
                        }
                    } else if (mqttTag == 77) /* "M" */ {
                        if (processPTagSuccess) {
                            if ((mqttPacketValue == -128) || (mqttPacketValue == -124)
                                    || (mqttPacketValue == -126) || (mqttPacketValue == -117)) {

                                if (processCTagSuccess) {
                                    mqttIndex = mqttIndex + 1;
                                    for (int i = 0; i < mqttMemberCountValue; i++) {
                                        byte groupMembersArray[] = new byte[8];
                                        for (int j = 0; j < 8; j++) {
                                            groupMembersArray[j] = origMqttMsgByte[mqttIndex + j];
                                        }
                                        long participants = ByteBuffer.wrap(groupMembersArray)
                                                .order(ByteOrder.LITTLE_ENDIAN).getLong();
                                        mqttIndex = mqttIndex + 8;
                                        if (i == (mqttMemberCountValue - 1)) {
                                            mqttMembersValue = mqttMembersValue + participants;
                                        } else {
                                            mqttMembersValue = mqttMembersValue + participants + ",";
                                        }
                                    }
                                    processMTagSuccess = true;
                                } else {
                                    break;
                                }
                            } else if (mqttPacketValue == 0) {
                                if (processLTagSuccess) {
                                    mqttIndex = mqttIndex + 1;
                                    if (mqttMsgLengthValue > 0) {
                                        byte messageArray[] = null;
                                        try {
                                            messageArray = new byte[mqttMsgLengthValue];
                                        } catch (Exception err) {
                                            err.printStackTrace();
                                            processMTagSuccess = false;
                                            break;
                                        }

                                        for (int i = 0; i < mqttMsgLengthValue; i++) {
                                            messageArray[i] = origMqttMsgByte[mqttIndex + i];
                                        }
                                        mqttMessageValue = new String(messageArray);
                                        processMTagSuccess = true;
                                        mqttIndex = mqttIndex + mqttMsgLengthValue + 1;
                                    }
                                } else {
                                    break;
                                }
                            }
                        }
                    } else if (mqttTag == 71) /* "G" */ {
                        mqttIndex = mqttIndex + 1;
                        byte groupIDArray[] = new byte[8];
                        for (int i = 0; i < 8; i++) {
                            groupIDArray[i] = origMqttMsgByte[mqttIndex + i];
                        }
                        mqttGroupIDValue = ByteBuffer.wrap(groupIDArray).order(ByteOrder.LITTLE_ENDIAN)
                                .getLong();
                        processGTagSuccess = true;
                        mqttIndex = mqttIndex + 8;
                    } else if (mqttTag == 83) /* "S" */ {
                        if (processLTagSuccess) {
                            mqttIndex = mqttIndex + 1;
                            if (mqttMsgLengthValue > 0) {
                                byte subjectArray[] = null;
                                try {
                                    subjectArray = new byte[mqttMsgLengthValue];
                                } catch (Exception err) {
                                    err.printStackTrace();
                                    processSTagSuccess = false;
                                    break;
                                }
                                for (int i = 0; i < mqttMsgLengthValue; i++) {
                                    subjectArray[i] = origMqttMsgByte[mqttIndex + i];
                                }
                                mqttSubjectValue = new String(subjectArray);
                                processSTagSuccess = true;
                                mqttIndex = mqttIndex + mqttMsgLengthValue;
                            }
                        } else {
                            break;
                        }
                    } else if (mqttTag == 67) /* "C" */ {
                        mqttIndex = mqttIndex + 1;
                        mqttMemberCountValue = origMqttMsgByte[mqttIndex];
                        processCTagSuccess = true;
                        mqttIndex = mqttIndex + 1;
                    } else if (mqttTag == 78) /* "N" */ {
                        mqttIndex = mqttIndex + 1;
                        mqttAdminCountValue = origMqttMsgByte[mqttIndex];
                        processNTagSuccess = true;
                        mqttIndex = mqttIndex + 1;
                    } else if (mqttTag == 65) /* "A" */ {
                        if (processPTagSuccess) {
                            if (mqttPacketValue == -117) {
                                if (processNTagSuccess) {
                                    mqttIndex = mqttIndex + 1;
                                    for (int i = 0; i < mqttAdminCountValue; i++) {
                                        byte groupAdminsArray[] = new byte[8];
                                        for (int j = 0; j < 8; j++) {
                                            groupAdminsArray[j] = origMqttMsgByte[mqttIndex + j];
                                        }
                                        long admins = ByteBuffer.wrap(groupAdminsArray)
                                                .order(ByteOrder.LITTLE_ENDIAN).getLong();
                                        mqttIndex = mqttIndex + 8;
                                        if (i == (mqttAdminCountValue - 1)) {
                                            mqttAdminsValue = mqttAdminsValue + admins;
                                        } else {
                                            mqttAdminsValue = mqttAdminsValue + admins + ",";
                                        }
                                    }
                                    processATagSuccess = true;
                                } else {
                                    break;
                                }
                            }
                        }
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
        }
    }

    if (!processVTagSuccess) {
        return;
    }

    PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
    boolean isScreenOn = pm.isScreenOn();
    if (isScreenOn == false) {
        WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
                | PowerManager.ON_AFTER_RELEASE, "MyLock");
        if (wl.isHeld()) {
            wl.release();
        }
        wl.acquire(10000);
        WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCpuLock");
        if (wl_cpu.isHeld()) {
            wl_cpu.release();
        }
        wl_cpu.acquire(10000);
    }

    String message = mqttMessageValue;
    Log.v("mqtt", "from: " + sender);
    Log.v("mqtt", "message: " + message);
    Intent intent = new Intent();
    intent.setClassName(ctx, "org.eclipse.paho.android.service.sample.MainActivity");
    intent.putExtra("handle", clientHandle);
    String ns = Context.NOTIFICATION_SERVICE;
    NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(ns);
    int messageNotificationId = 1;
    mNotificationManager.cancel(messageNotificationId);
    Calendar.getInstance().getTime().toString();
    long when = System.currentTimeMillis();
    String ticker = sender + " " + mqttMessageValue;
    PendingIntent pendingIntent = PendingIntent.getActivity(ctx, 3, intent, 0);
    NotificationCompat.Builder notificationCompat = new NotificationCompat.Builder(ctx);
    notificationCompat.setAutoCancel(true).setContentTitle(sender).setContentIntent(pendingIntent)
            .setContentText(mqttMessageValue).setTicker(ticker).setWhen(when)
            .setSmallIcon(R.drawable.ic_launcher);

    //  Notification notification = notificationCompat.build();
    Bitmap iconLarge = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_launcher);
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(ctx)
            .setSmallIcon(R.drawable.ic_launcher).setLargeIcon(iconLarge).setContentTitle(sender)
            .setContentText(mqttMessageValue);

    mBuilder.setContentIntent(pendingIntent);
    mBuilder.setTicker(message);
    mBuilder.setAutoCancel(true);
    mBuilder.setDefaults(
            Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE | Notification.DEFAULT_LIGHTS);
    mNotificationManager.notify(messageNotificationId, mBuilder.build());
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private byte[] encodeBlobdbNotification(int id, int timestamp, String title, String subtitle, String body,
        String sourceName, boolean hasHandle, NotificationType notificationType, String[] cannedReplies) {
    final short NOTIFICATION_PIN_LENGTH = 46;
    final short ACTION_LENGTH_MIN = 10;

    String[] parts = { title, subtitle, body };

    if (notificationType == null) {
        notificationType = NotificationType.UNKNOWN;
    }/*from  w ww.  ja  v a 2 s.  c  om*/

    int icon_id = notificationType.icon;
    byte color_id = notificationType.color;

    // Calculate length first
    byte actions_count;
    short actions_length;
    String dismiss_string;
    String open_string = "Open on phone";
    String mute_string = "Mute";
    String reply_string = "Reply";
    if (sourceName != null) {
        mute_string += " " + sourceName;
    }

    byte dismiss_action_id;
    if (hasHandle && !"ALARMCLOCKRECEIVER".equals(sourceName)) {
        actions_count = 3;
        dismiss_string = "Dismiss";
        dismiss_action_id = 0x02;
        actions_length = (short) (ACTION_LENGTH_MIN * actions_count + dismiss_string.getBytes().length
                + open_string.getBytes().length + mute_string.getBytes().length);
    } else {
        actions_count = 1;
        dismiss_string = "Dismiss all";
        dismiss_action_id = 0x03;
        actions_length = (short) (ACTION_LENGTH_MIN * actions_count + dismiss_string.getBytes().length);
    }

    int replies_length = -1;
    if (cannedReplies != null && cannedReplies.length > 0) {
        actions_count++;
        for (String reply : cannedReplies) {
            replies_length += reply.getBytes().length + 1;
        }
        actions_length += ACTION_LENGTH_MIN + reply_string.getBytes().length + replies_length + 3; // 3 = attribute id (byte) + length(short)
    }

    byte attributes_count = 2; // icon
    short attributes_length = (short) (11 + actions_length);
    if (parts != null) {
        for (String s : parts) {
            if (s == null || s.equals("")) {
                continue;
            }
            attributes_count++;
            attributes_length += (3 + s.getBytes().length);
        }
    }

    short pin_length = (short) (NOTIFICATION_PIN_LENGTH + attributes_length);

    ByteBuffer buf = ByteBuffer.allocate(pin_length);

    // pin - 46 bytes
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putLong(GB_UUID_MASK);
    buf.putLong(id);
    buf.putLong(UUID_NOTIFICATIONS.getMostSignificantBits());
    buf.putLong(UUID_NOTIFICATIONS.getLeastSignificantBits());
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.putInt(timestamp); // 32-bit timestamp
    buf.putShort((short) 0); // duration
    buf.put((byte) 0x01); // type (0x01 = notification)
    buf.putShort((short) 0x0001); // flags 0x0001 = ?
    buf.put((byte) 0x04); // layout (0x04 = notification?)
    buf.putShort(attributes_length); // total length of all attributes and actions in bytes
    buf.put(attributes_count);
    buf.put(actions_count);

    byte attribute_id = 0;
    // Encode Pascal-Style Strings
    if (parts != null) {
        for (String s : parts) {
            attribute_id++;
            if (s == null || s.equals("")) {
                continue;
            }

            int partlength = s.getBytes().length;
            if (partlength > 512)
                partlength = 512;
            buf.put(attribute_id);
            buf.putShort((short) partlength);
            buf.put(s.getBytes(), 0, partlength);
        }
    }

    buf.put((byte) 4); // icon
    buf.putShort((short) 4); // length of int
    buf.putInt(0x80000000 | icon_id);

    buf.put((byte) 28); // background_color
    buf.putShort((short) 1); // length of int
    buf.put(color_id);

    // dismiss action
    buf.put(dismiss_action_id);
    buf.put((byte) 0x02); // generic action, dismiss did not do anything
    buf.put((byte) 0x01); // number attributes
    buf.put((byte) 0x01); // attribute id (title)
    buf.putShort((short) dismiss_string.getBytes().length);
    buf.put(dismiss_string.getBytes());

    // open and mute actions
    if (hasHandle && !"ALARMCLOCKRECEIVER".equals(sourceName)) {
        buf.put((byte) 0x01);
        buf.put((byte) 0x02); // generic action
        buf.put((byte) 0x01); // number attributes
        buf.put((byte) 0x01); // attribute id (title)
        buf.putShort((short) open_string.getBytes().length);
        buf.put(open_string.getBytes());

        buf.put((byte) 0x04);
        buf.put((byte) 0x02); // generic action
        buf.put((byte) 0x01); // number attributes
        buf.put((byte) 0x01); // attribute id (title)
        buf.putShort((short) mute_string.getBytes().length);
        buf.put(mute_string.getBytes());
    }

    if (cannedReplies != null && replies_length > 0) {
        buf.put((byte) 0x05);
        buf.put((byte) 0x03); // reply action
        buf.put((byte) 0x02); // number attributes
        buf.put((byte) 0x01); // title
        buf.putShort((short) reply_string.getBytes().length);
        buf.put(reply_string.getBytes());
        buf.put((byte) 0x08); // canned replies
        buf.putShort((short) replies_length);
        for (int i = 0; i < cannedReplies.length - 1; i++) {
            buf.put(cannedReplies[i].getBytes());
            buf.put((byte) 0x00);
        }
        // last one must not be zero terminated, else we get an additional emply reply
        buf.put(cannedReplies[cannedReplies.length - 1].getBytes());
    }

    return encodeBlobdb(UUID.randomUUID(), BLOBDB_INSERT, BLOBDB_NOTIFICATION, buf.array());
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private byte[] encodeActionResponse2x(int id, byte actionId, int iconId, String caption) {
    short length = (short) (18 + caption.getBytes().length);
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + length);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(length);/*  w  w w .j a v  a 2s .  c  o m*/
    buf.putShort(ENDPOINT_EXTENSIBLENOTIFS);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.put(NOTIFICATIONACTION_RESPONSE);
    buf.putInt(id);
    buf.put(actionId);
    buf.put(NOTIFICATIONACTION_ACK);
    buf.put((byte) 2); //nr of attributes
    buf.put((byte) 6); // icon
    buf.putShort((short) 4); // length
    buf.putInt(iconId);
    buf.put((byte) 2); // title
    buf.putShort((short) caption.getBytes().length);
    buf.put(caption.getBytes());
    return buf.array();
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private byte[] encodeWeatherPin(int timestamp, String title, String subtitle, String body, String location,
        int iconId) {
    final short NOTIFICATION_PIN_LENGTH = 46;
    final short ACTION_LENGTH_MIN = 10;

    String[] parts = { title, subtitle, body, location, "test", "test" };

    // Calculate length first
    byte actions_count = 1;
    short actions_length;
    String remove_string = "Remove";
    actions_length = (short) (ACTION_LENGTH_MIN * actions_count + remove_string.getBytes().length);

    byte attributes_count = 3;
    short attributes_length = (short) (21 + actions_length);
    if (parts != null) {
        for (String s : parts) {
            if (s == null || s.equals("")) {
                continue;
            }/*  w ww .  ja  v a 2s . co m*/
            attributes_count++;
            attributes_length += (3 + s.getBytes().length);
        }
    }

    UUID uuid = UUID.fromString("61b22bc8-1e29-460d-a236-3fe409a43901");

    short pin_length = (short) (NOTIFICATION_PIN_LENGTH + attributes_length);

    ByteBuffer buf = ByteBuffer.allocate(pin_length);

    // pin (46 bytes)
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putLong(uuid.getMostSignificantBits());
    buf.putLong(uuid.getLeastSignificantBits());
    buf.putLong(uuid.getMostSignificantBits());
    buf.putLong(uuid.getLeastSignificantBits() | 0xff);
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.putInt(timestamp); // 32-bit timestamp
    buf.putShort((short) 0); // duration
    buf.put((byte) 0x02); // type (0x02 = pin)
    buf.putShort((short) 0x0001); // flags 0x0001 = ?
    buf.put((byte) 0x06); // layout (0x06 = weather)
    buf.putShort(attributes_length); // total length of all attributes and actions in bytes
    buf.put(attributes_count);
    buf.put(actions_count);

    byte attribute_id = 0;
    // Encode Pascal-Style Strings
    if (parts != null) {
        for (String s : parts) {
            attribute_id++;
            if (s == null || s.equals("")) {
                continue;
            }

            int partlength = s.getBytes().length;
            if (partlength > 512)
                partlength = 512;
            if (attribute_id == 4) {
                buf.put((byte) 11);
            } else if (attribute_id == 5) {
                buf.put((byte) 25);
            } else if (attribute_id == 6) {
                buf.put((byte) 26);
            } else {
                buf.put(attribute_id);
            }
            buf.putShort((short) partlength);
            buf.put(s.getBytes(), 0, partlength);
        }
    }

    buf.put((byte) 4); // icon
    buf.putShort((short) 4); // length of int
    buf.putInt(0x80000000 | iconId);

    buf.put((byte) 6); // icon
    buf.putShort((short) 4); // length of int
    buf.putInt(0x80000000 | iconId);

    buf.put((byte) 14); // last updated
    buf.putShort((short) 4); // length of int
    buf.putInt(timestamp);

    // remove action
    buf.put((byte) 123); // action id
    buf.put((byte) 0x09); // remove
    buf.put((byte) 0x01); // number attributes
    buf.put((byte) 0x01); // attribute id (title)
    buf.putShort((short) remove_string.getBytes().length);
    buf.put(remove_string.getBytes());

    return encodeBlobdb(uuid, BLOBDB_INSERT, BLOBDB_PIN, buf.array());
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

private byte[] encodeActionResponse(UUID uuid, int iconId, String caption) {
    short length = (short) (29 + caption.getBytes().length);
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + length);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(length);//from  ww  w.  j a v  a 2 s .c o m
    buf.putShort(ENDPOINT_NOTIFICATIONACTION);
    buf.put(NOTIFICATIONACTION_RESPONSE);
    buf.putLong(uuid.getMostSignificantBits());
    buf.putLong(uuid.getLeastSignificantBits());
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.put(NOTIFICATIONACTION_ACK);
    buf.put((byte) 2); //nr of attributes
    buf.put((byte) 6); // icon
    buf.putShort((short) 4); // length
    buf.putInt(0x80000000 | iconId);
    buf.put((byte) 2); // title
    buf.putShort((short) caption.getBytes().length);
    buf.put(caption.getBytes());
    return buf.array();
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

byte[] encodeInstallMetadata(UUID uuid, String appName, short appVersion, short sdkVersion, int flags,
        int iconId) {
    final short METADATA_LENGTH = 126;

    byte[] name_buf = new byte[96];
    System.arraycopy(appName.getBytes(), 0, name_buf, 0, appName.getBytes().length);
    ByteBuffer buf = ByteBuffer.allocate(METADATA_LENGTH);

    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putLong(uuid.getMostSignificantBits()); // watchapp uuid
    buf.putLong(uuid.getLeastSignificantBits());
    buf.order(ByteOrder.LITTLE_ENDIAN);
    buf.putInt(flags);/*from   w  ww  .j av  a2  s.com*/
    buf.putInt(iconId);
    buf.putShort(appVersion);
    buf.putShort(sdkVersion);
    buf.put((byte) 0); // app_face_bgcolor
    buf.put((byte) 0); // app_face_template_id
    buf.put(name_buf); // 96 bytes

    return encodeBlobdb(uuid, BLOBDB_INSERT, BLOBDB_APP, buf.array());
}

From source file:nodomain.freeyourgadget.gadgetbridge.service.devices.pebble.PebbleProtocol.java

byte[] encodeAppFetchAck() {
    final short LENGTH_APPFETCH = 2;
    ByteBuffer buf = ByteBuffer.allocate(LENGTH_PREFIX + LENGTH_APPFETCH);
    buf.order(ByteOrder.BIG_ENDIAN);
    buf.putShort(LENGTH_APPFETCH);/*from w  w  w. j  a  v  a  2  s .  c om*/
    buf.putShort(ENDPOINT_APPFETCH);
    buf.put((byte) 0x01);
    buf.put((byte) 0x01);

    return buf.array();
}