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
17package com.android.apps.tag.message;
18
19import com.android.apps.tag.record.ImageRecord;
20import com.android.apps.tag.record.MimeRecord;
21import com.android.apps.tag.record.ParsedNdefRecord;
22import com.android.apps.tag.record.SmartPoster;
23import com.android.apps.tag.record.TextRecord;
24import com.android.apps.tag.record.UnknownRecord;
25import com.android.apps.tag.record.UriRecord;
26import com.android.apps.tag.record.VCardRecord;
27
28import android.nfc.NdefMessage;
29import android.nfc.NdefRecord;
30
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) {
44        return new ParsedNdefMessage(getRecords(message));
45    }
46
47    public static List<ParsedNdefRecord> getRecords(NdefMessage message) {
48        return getRecords(message.getRecords());
49    }
50
51    public static List<ParsedNdefRecord> getRecords(NdefRecord[] records) {
52        List<ParsedNdefRecord> elements = new ArrayList<ParsedNdefRecord>();
53        for (NdefRecord record : records) {
54            if (SmartPoster.isPoster(record)) {
55                elements.add(SmartPoster.parse(record));
56            } else if (UriRecord.isUri(record)) {
57                elements.add(UriRecord.parse(record));
58            } else if (TextRecord.isText(record)) {
59                elements.add(TextRecord.parse(record));
60            } else if (ImageRecord.isImage(record)) {
61                elements.add(ImageRecord.parse(record));
62            } else if (VCardRecord.isVCard(record)) {
63                elements.add(VCardRecord.parse(record));
64            } else if (MimeRecord.isMime(record)) {
65                elements.add(MimeRecord.parse(record));
66            } else {
67                elements.add(new UnknownRecord());
68            }
69        }
70        return elements;
71    }
72}
73