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; 17 18 import android.nfc.NdefMessage; 19 import android.nfc.NdefRecord; 20 21 import com.example.android.nfc.record.ParsedNdefRecord; 22 import com.example.android.nfc.record.SmartPoster; 23 import com.example.android.nfc.record.TextRecord; 24 import com.example.android.nfc.record.UriRecord; 25 26 import java.util.ArrayList; 27 import java.util.List; 28 29 /** 30 * Utility class for creating {@link ParsedNdefMessage}s. 31 */ 32 public class NdefMessageParser { 33 34 // Utility class NdefMessageParser()35 private NdefMessageParser() { 36 37 } 38 39 /** Parse an NdefMessage */ parse(NdefMessage message)40 public static List<ParsedNdefRecord> parse(NdefMessage message) { 41 return getRecords(message.getRecords()); 42 } 43 getRecords(NdefRecord[] records)44 public static List<ParsedNdefRecord> getRecords(NdefRecord[] records) { 45 List<ParsedNdefRecord> elements = new ArrayList<ParsedNdefRecord>(); 46 for (NdefRecord record : records) { 47 if (UriRecord.isUri(record)) { 48 elements.add(UriRecord.parse(record)); 49 } else if (TextRecord.isText(record)) { 50 elements.add(TextRecord.parse(record)); 51 } else if (SmartPoster.isPoster(record)) { 52 elements.add(SmartPoster.parse(record)); 53 } 54 } 55 return elements; 56 } 57 } 58