blob: 54d288833d17c2d55a86a1e95681c614164b17cd [file] [log] [blame]
Jeff Hamilton79495342010-10-18 13:13:10 -05001/*
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
17package com.android.apps.tag.message;
18
Nick Kralevich081de562010-10-19 14:26:00 -070019import com.android.apps.tag.record.ImageRecord;
Nick Kralevich46793e92010-10-19 10:55:06 -070020import com.android.apps.tag.record.MimeRecord;
Jeff Hamilton79495342010-10-18 13:13:10 -050021import com.android.apps.tag.record.ParsedNdefRecord;
22import com.android.apps.tag.record.SmartPoster;
23import com.android.apps.tag.record.TextRecord;
Jason parks07b51ee2010-11-02 15:28:29 -050024import com.android.apps.tag.record.UnknownRecord;
Jeff Hamilton79495342010-10-18 13:13:10 -050025import com.android.apps.tag.record.UriRecord;
Jason parks40d9a0c2010-10-26 14:42:45 -050026import com.android.apps.tag.record.VCardRecord;
Nick Kralevichc4456512010-10-19 15:48:01 -070027
28import android.nfc.NdefMessage;
29import android.nfc.NdefRecord;
Jeff Hamilton79495342010-10-18 13:13:10 -050030
31import java.util.ArrayList;
32import java.util.List;
33
34/**
35 * Utility class for creating {@link ParsedNdefMessage}s.
36 */
37public class NdefMessageParser {
38
39 // Utility class
40 private NdefMessageParser() { }
41
42 /** Parse an NdefMessage */
43 public static ParsedNdefMessage parse(NdefMessage message) {
Jason parksa6855222010-10-28 16:15:06 -050044 return new ParsedNdefMessage(getRecords(message));
Jeff Hamilton79495342010-10-18 13:13:10 -050045 }
46
47 public static List<ParsedNdefRecord> getRecords(NdefMessage message) {
Nick Kralevich5e361072010-10-22 09:57:54 -070048 return getRecords(message.getRecords());
49 }
50
51 public static List<ParsedNdefRecord> getRecords(NdefRecord[] records) {
Jeff Hamilton79495342010-10-18 13:13:10 -050052 List<ParsedNdefRecord> elements = new ArrayList<ParsedNdefRecord>();
Nick Kralevich5e361072010-10-22 09:57:54 -070053 for (NdefRecord record : records) {
Nick Pelly11687162012-01-19 20:09:36 -080054 if (SmartPoster.isPoster(record)) {
55 elements.add(SmartPoster.parse(record));
56 } else if (UriRecord.isUri(record)) {
Jeff Hamilton79495342010-10-18 13:13:10 -050057 elements.add(UriRecord.parse(record));
58 } else if (TextRecord.isText(record)) {
59 elements.add(TextRecord.parse(record));
Nick Kralevich081de562010-10-19 14:26:00 -070060 } else if (ImageRecord.isImage(record)) {
61 elements.add(ImageRecord.parse(record));
Jason parks40d9a0c2010-10-26 14:42:45 -050062 } else if (VCardRecord.isVCard(record)) {
63 elements.add(VCardRecord.parse(record));
Nick Kralevich46793e92010-10-19 10:55:06 -070064 } else if (MimeRecord.isMime(record)) {
65 elements.add(MimeRecord.parse(record));
Jason parks07b51ee2010-11-02 15:28:29 -050066 } else {
67 elements.add(new UnknownRecord());
Jeff Hamilton79495342010-10-18 13:13:10 -050068 }
69 }
70 return elements;
71 }
72}