Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of 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, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.apps.tag.message; |
| 18 | |
Nick Kralevich | 081de56 | 2010-10-19 14:26:00 -0700 | [diff] [blame] | 19 | import com.android.apps.tag.record.ImageRecord; |
Nick Kralevich | 46793e9 | 2010-10-19 10:55:06 -0700 | [diff] [blame] | 20 | import com.android.apps.tag.record.MimeRecord; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 21 | import com.android.apps.tag.record.ParsedNdefRecord; |
| 22 | import com.android.apps.tag.record.SmartPoster; |
| 23 | import com.android.apps.tag.record.TextRecord; |
Jason parks | 07b51ee | 2010-11-02 15:28:29 -0500 | [diff] [blame] | 24 | import com.android.apps.tag.record.UnknownRecord; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 25 | import com.android.apps.tag.record.UriRecord; |
Jason parks | 40d9a0c | 2010-10-26 14:42:45 -0500 | [diff] [blame] | 26 | import com.android.apps.tag.record.VCardRecord; |
Nick Kralevich | c445651 | 2010-10-19 15:48:01 -0700 | [diff] [blame] | 27 | |
| 28 | import android.nfc.NdefMessage; |
| 29 | import android.nfc.NdefRecord; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 30 | |
| 31 | import java.util.ArrayList; |
| 32 | import java.util.List; |
| 33 | |
| 34 | /** |
| 35 | * Utility class for creating {@link ParsedNdefMessage}s. |
| 36 | */ |
| 37 | public class NdefMessageParser { |
| 38 | |
| 39 | // Utility class |
| 40 | private NdefMessageParser() { } |
| 41 | |
| 42 | /** Parse an NdefMessage */ |
| 43 | public static ParsedNdefMessage parse(NdefMessage message) { |
Jason parks | a685522 | 2010-10-28 16:15:06 -0500 | [diff] [blame] | 44 | return new ParsedNdefMessage(getRecords(message)); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | public static List<ParsedNdefRecord> getRecords(NdefMessage message) { |
Nick Kralevich | 5e36107 | 2010-10-22 09:57:54 -0700 | [diff] [blame] | 48 | return getRecords(message.getRecords()); |
| 49 | } |
| 50 | |
| 51 | public static List<ParsedNdefRecord> getRecords(NdefRecord[] records) { |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 52 | List<ParsedNdefRecord> elements = new ArrayList<ParsedNdefRecord>(); |
Nick Kralevich | 5e36107 | 2010-10-22 09:57:54 -0700 | [diff] [blame] | 53 | for (NdefRecord record : records) { |
Nick Pelly | 1168716 | 2012-01-19 20:09:36 -0800 | [diff] [blame] | 54 | if (SmartPoster.isPoster(record)) { |
| 55 | elements.add(SmartPoster.parse(record)); |
| 56 | } else if (UriRecord.isUri(record)) { |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 57 | elements.add(UriRecord.parse(record)); |
| 58 | } else if (TextRecord.isText(record)) { |
| 59 | elements.add(TextRecord.parse(record)); |
Nick Kralevich | 081de56 | 2010-10-19 14:26:00 -0700 | [diff] [blame] | 60 | } else if (ImageRecord.isImage(record)) { |
| 61 | elements.add(ImageRecord.parse(record)); |
Jason parks | 40d9a0c | 2010-10-26 14:42:45 -0500 | [diff] [blame] | 62 | } else if (VCardRecord.isVCard(record)) { |
| 63 | elements.add(VCardRecord.parse(record)); |
Nick Kralevich | 46793e9 | 2010-10-19 10:55:06 -0700 | [diff] [blame] | 64 | } else if (MimeRecord.isMime(record)) { |
| 65 | elements.add(MimeRecord.parse(record)); |
Jason parks | 07b51ee | 2010-11-02 15:28:29 -0500 | [diff] [blame] | 66 | } else { |
| 67 | elements.add(new UnknownRecord()); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 68 | } |
| 69 | } |
| 70 | return elements; |
| 71 | } |
| 72 | } |