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.R;
20import com.android.apps.tag.record.ParsedNdefRecord;
21import com.android.apps.tag.record.SmartPoster;
22import com.android.apps.tag.record.UriRecord;
23import com.google.common.collect.ImmutableList;
24
25import android.content.Context;
26
27import java.util.List;
28import java.util.Locale;
29
30/**
31 * A parsed version of an {@link android.nfc.NdefMessage}
32 */
33public class ParsedNdefMessage {
34
35    private List<ParsedNdefRecord> mRecords;
36
37    public ParsedNdefMessage(List<ParsedNdefRecord> records) {
38        mRecords = ImmutableList.copyOf(records);
39    }
40
41    /**
42     * Returns the list of parsed records on this message.
43     */
44    public List<ParsedNdefRecord> getRecords() {
45        return mRecords;
46    }
47
48    /**
49     * Returns the snippet information associated with the NdefMessage
50     * most appropriate for the given {@code locale}.
51     */
52    public String getSnippet(Context context, Locale locale) {
53        if (mRecords.isEmpty()) {
54            return context.getString(R.string.tag_empty);
55        }
56
57        ParsedNdefRecord record = mRecords.get(0);
58
59        if (record instanceof UriRecord) {
60            // Be a good citizen of the Smart Poster spec.
61
62            int smartPosterRecord = -1;
63
64            final int size = mRecords.size();
65            for (int i = 1 ; i < size ; i++) {
66
67                ParsedNdefRecord r = mRecords.get(i);
68
69                if (r instanceof SmartPoster) {
70                    smartPosterRecord = i;
71                    break;
72                }
73            }
74
75            if (smartPosterRecord != -1) {
76                record = mRecords.get(smartPosterRecord);
77            }
78        }
79
80        return record.getSnippet(context, locale);
81    }
82}
83