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.record;
18
19import com.android.apps.tag.R;
20import com.google.common.annotations.VisibleForTesting;
21import com.google.common.base.Preconditions;
22
23import android.app.Activity;
24import android.content.Context;
25import android.nfc.NdefRecord;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.ViewGroup;
29import android.widget.TextView;
30
31import java.nio.charset.Charset;
32import java.util.Arrays;
33import java.util.Locale;
34
35/**
36 * A {@link ParsedNdefRecord} corresponding to a MIME object.
37 */
38public class MimeRecord extends ParsedNdefRecord {
39    private final String mType;
40    private final byte[] mContent;
41
42    private MimeRecord(String mimeType, byte[] content) {
43        mType = Preconditions.checkNotNull(mimeType);
44        Preconditions.checkNotNull(content);
45        mContent = Arrays.copyOf(content, content.length);
46    }
47
48    @VisibleForTesting
49    public String getMimeType() {
50        return mType;
51    }
52
53    @VisibleForTesting
54    public byte[] getContent() {
55        return Arrays.copyOf(mContent, mContent.length);
56    }
57
58    @Override
59    public View getView(Activity activity, LayoutInflater inflater, ViewGroup parent, int offset) {
60        TextView text = (TextView) inflater.inflate(R.layout.tag_text, parent, false);
61        text.setText(mType);
62        return text;
63    }
64
65    @Override
66    public String getSnippet(Context context, Locale locale) {
67        return mType;
68    }
69
70    public static MimeRecord parse(NdefRecord record) {
71        Preconditions.checkArgument(record.getTnf() == NdefRecord.TNF_MIME_MEDIA);
72        String type = new String(record.getType(), Charset.forName("US-ASCII"));
73        byte[] payload = record.getPayload();
74        return new MimeRecord(type, payload);
75    }
76
77    public static boolean isMime(NdefRecord record) {
78        try {
79            parse(record);
80            return true;
81        } catch (IllegalArgumentException e) {
82            return false;
83        }
84    }
85
86    public static NdefRecord newMimeRecord(String type, byte[] data) {
87        Preconditions.checkNotNull(type);
88        Preconditions.checkNotNull(data);
89
90        byte[] typeBytes = type.getBytes(Charset.forName("US-ASCII"));
91
92        return new NdefRecord(NdefRecord.TNF_MIME_MEDIA, typeBytes, new byte[0], data);
93    }
94}
95