PushVibrationCommand.java :  » App » kushikatsu » jp » andeb » kushikatsu » nfc » Android Open Source

Android Open Source » App » kushikatsu 
kushikatsu » jp » andeb » kushikatsu » nfc » PushVibrationCommand.java
/*
 * Copyright 2011 Sosuke Masui <esmasui@gmail.com>,
 *                Makoto Yamazaki <makoto1975@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * $Id: PushVibrationCommand.java 143 2011-02-11 13:22:36Z makoto1975 $
 */

package jp.andeb.kushikatsu.nfc;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;

import edu.umd.cs.findbugs.annotations.CheckForNull;

import net.kazzz.felica.FeliCaException;
import net.kazzz.felica.lib.FeliCaLib.IDm;

/**
 *  Push 
 */
public final class PushVibrationCommand extends PushCommand {

    /**
     * 
     */
    public interface IPattern {
        /**
         *  {@code byte} 
         * @return
         * 
         */
        public byte patternValue();
    }

    public enum Pattern implements IPattern {
        /**
         * ()
         */
        ON((byte) 0), //

        /**
         * 0.50.5
         */
        ON_05_OFF_05((byte) 1), //

        /**
         * 11
         */
        ON_1_OFF_1((byte) 2), //

        /**
         * 21
         */
        ON_2_OFF_1((byte) 3), //

        /**
         * 31
         */
        ON_3_OFF_1((byte) 4);

        /**
         * 
         */
        private final byte patternValue_;

        /**
         * 
         *
         * @param patternValue
         * 
         */
        private Pattern(byte patternValue) {
            patternValue_ = patternValue;
        }

        @Override
        public byte patternValue() {
            return patternValue_;
        }
    };

    /**
     *  {@link PushVibrationCommand} 
     *
     * @param idm
     *  IDm{@code null} 
     * @param pattern
     *  {@link Pattern} 
     * {@link Pattern}  {@link IPattern}
     * 
     * @param count
     *  {@code 1}  {@code 3} 
     *  {@code byte} 
     * 
     * @param message
     * push {@code null} 
     * @throws FeliCaException
     */
    public PushVibrationCommand(IDm idm, IPattern pattern, int count, @CheckForNull String message)
            throws FeliCaException {
        super(idm, buildPushVibrationSegment(pattern, count, message));
    }

    private static final byte TYPE = (byte) 4;
    private static final Charset MESSAGE_CHARSET = Charset.forName("Shift_JIS");

    private static byte[][] buildPushVibrationSegment(IPattern pattern, int count,
            @CheckForNull String message) {

        final byte[] messageBytes = (message == null) ? PushCommand.EMPTY_BYTES : message
                .getBytes(MESSAGE_CHARSET);
        final int capacity = messageBytes.length + 5;// type(1byte)
                                                     // +
                                                     // paramSize(2bytes)
                                                     // +
                                                     // pattern(1byte)
                                                     // +
                                                     // count(1byte)
        final ByteBuffer buffer = ByteBuffer.allocate(capacity);

        // 

        // 
        buffer.put(TYPE);
        // 
        final int paramSize = messageBytes.length + 2; // pattern(1byte) + count(1byte)
        PushCommand.putShortAsLittleEndian(paramSize, buffer);

        // 

        // pattern
        buffer.put(pattern.patternValue());
        // count
        buffer.put((byte) count);
        // URL
        buffer.put(messageBytes);

        return new byte[][] {
            buffer.array()
        };
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.