1 /* 2 * Copyright (C) 2010 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not 5 * use this file except in compliance with the License. You may obtain a copy of 6 * the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 13 * License for the specific language governing permissions and limitations under 14 * the License. 15 */ 16 package com.example.android.nfc.record; 17 18 import android.app.Activity; 19 import android.nfc.NdefRecord; 20 import android.view.LayoutInflater; 21 import android.view.View; 22 import android.view.ViewGroup; 23 import android.widget.TextView; 24 25 import com.google.common.base.Preconditions; 26 27 import com.example.android.nfc.R; 28 29 import java.io.UnsupportedEncodingException; 30 import java.util.Arrays; 31 32 /** 33 * An NFC Text Record 34 */ 35 public class TextRecord implements ParsedNdefRecord { 36 37 /** ISO/IANA language code */ 38 private final String mLanguageCode; 39 40 private final String mText; 41 42 private TextRecord(String languageCode, String text) { 43 mLanguageCode = Preconditions.checkNotNull(languageCode); 44 mText = Preconditions.checkNotNull(text); 45 } 46 47 public View getView(Activity activity, LayoutInflater inflater, ViewGroup parent, int offset) { 48 TextView text = (TextView) inflater.inflate(R.layout.tag_text, parent, false); 49 text.setText(mText); 50 return text; 51 } 52 53 public String getText() { 54 return mText; 55 } 56 57 /** 58 * Returns the ISO/IANA language code associated with this text element. 59 */ 60 public String getLanguageCode() { 61 return mLanguageCode; 62 } 63 64 // TODO: deal with text fields which span multiple NdefRecords 65 public static TextRecord parse(NdefRecord record) { 66 Preconditions.checkArgument(record.getTnf() == NdefRecord.TNF_WELL_KNOWN); 67 Preconditions.checkArgument(Arrays.equals(record.getType(), NdefRecord.RTD_TEXT)); 68 try { 69 byte[] payload = record.getPayload(); 70 /* 71 * payload[0] contains the "Status Byte Encodings" field, per the 72 * NFC Forum "Text Record Type Definition" section 3.2.1. 73 * 74 * bit7 is the Text Encoding Field. 75 * 76 * if (Bit_7 == 0): The text is encoded in UTF-8 if (Bit_7 == 1): 77 * The text is encoded in UTF16 78 * 79 * Bit_6 is reserved for future use and must be set to zero. 80 * 81 * Bits 5 to 0 are the length of the IANA language code. 82 */ 83 String textEncoding = ((payload[0] & 0200) == 0) ? "UTF-8" : "UTF-16"; 84 int languageCodeLength = payload[0] & 0077; 85 String languageCode = new String(payload, 1, languageCodeLength, "US-ASCII"); 86 String text = 87 new String(payload, languageCodeLength + 1, 88 payload.length - languageCodeLength - 1, textEncoding); 89 return new TextRecord(languageCode, text); 90 } catch (UnsupportedEncodingException e) { 91 // should never happen unless we get a malformed tag. 92 throw new IllegalArgumentException(e); 93 } 94 } 95 96 public static boolean isText(NdefRecord record) { 97 try { 98 parse(record); 99 return true; 100 } catch (IllegalArgumentException e) { 101 return false; 102 } 103 } 104 } 105