146793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich/*
246793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich * Copyright (C) 2010 The Android Open Source Project
346793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich *
446793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich * Licensed under the Apache License, Version 2.0 (the "License");
546793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich * you may not use this file except in compliance with the License.
646793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich * You may obtain a copy of the License at
746793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich *
846793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich *      http://www.apache.org/licenses/LICENSE-2.0
946793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich *
1046793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich * Unless required by applicable law or agreed to in writing, software
1146793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich * distributed under the License is distributed on an "AS IS" BASIS,
1246793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1346793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich * See the License for the specific language governing permissions and
1446793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich * limitations under the License.
1546793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich */
1646793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich
1746793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevichpackage com.android.apps.tag.record;
1846793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich
198dd68417526785dd82f054bcf3f4763e19d35142Jeff Hamiltonimport com.android.apps.tag.R;
208dd68417526785dd82f054bcf3f4763e19d35142Jeff Hamiltonimport com.google.common.annotations.VisibleForTesting;
218dd68417526785dd82f054bcf3f4763e19d35142Jeff Hamiltonimport com.google.common.base.Preconditions;
228dd68417526785dd82f054bcf3f4763e19d35142Jeff Hamilton
2346793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevichimport android.app.Activity;
24a6855221fb6e5c6c19d31283a18ced44cccdf442Jason parksimport android.content.Context;
2546793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevichimport android.nfc.NdefRecord;
2646793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevichimport android.view.LayoutInflater;
2746793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevichimport android.view.View;
2846793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevichimport android.view.ViewGroup;
2946793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevichimport android.widget.TextView;
3046793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich
31f34e4d5ec99fd7883bbfcdc0f644d9d6e4df6c97Jeff Hamiltonimport java.nio.charset.Charset;
3246793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevichimport java.util.Arrays;
33a6855221fb6e5c6c19d31283a18ced44cccdf442Jason parksimport java.util.Locale;
3446793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich
3546793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich/**
3646793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich * A {@link ParsedNdefRecord} corresponding to a MIME object.
3746793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich */
3840d9a0cd0a37d184ee737b2d7138a39e4292ce3eJason parkspublic class MimeRecord extends ParsedNdefRecord {
3924147eeedccccc552ba116c74384a8ea22da9dcbNick Kralevich    private final String mType;
4024147eeedccccc552ba116c74384a8ea22da9dcbNick Kralevich    private final byte[] mContent;
4146793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich
4246793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    private MimeRecord(String mimeType, byte[] content) {
4346793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich        mType = Preconditions.checkNotNull(mimeType);
4446793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich        Preconditions.checkNotNull(content);
4546793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich        mContent = Arrays.copyOf(content, content.length);
4646793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    }
4746793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich
4846793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    @VisibleForTesting
4946793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    public String getMimeType() {
5046793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich        return mType;
5146793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    }
5246793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich
5346793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    @VisibleForTesting
5446793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    public byte[] getContent() {
5546793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich        return Arrays.copyOf(mContent, mContent.length);
5646793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    }
5746793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich
5846793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    @Override
5940d9a0cd0a37d184ee737b2d7138a39e4292ce3eJason parks    public View getView(Activity activity, LayoutInflater inflater, ViewGroup parent, int offset) {
6046793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich        TextView text = (TextView) inflater.inflate(R.layout.tag_text, parent, false);
618dd68417526785dd82f054bcf3f4763e19d35142Jeff Hamilton        text.setText(mType);
6246793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich        return text;
6346793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    }
6446793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich
65a6855221fb6e5c6c19d31283a18ced44cccdf442Jason parks    @Override
66a6855221fb6e5c6c19d31283a18ced44cccdf442Jason parks    public String getSnippet(Context context, Locale locale) {
67a6855221fb6e5c6c19d31283a18ced44cccdf442Jason parks        return mType;
68a6855221fb6e5c6c19d31283a18ced44cccdf442Jason parks    }
69a6855221fb6e5c6c19d31283a18ced44cccdf442Jason parks
7046793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    public static MimeRecord parse(NdefRecord record) {
71116871653662979554a7326991ba4fd599ac44ffNick Pelly        Preconditions.checkArgument(record.toMimeType() != null);
72116871653662979554a7326991ba4fd599ac44ffNick Pelly        return new MimeRecord(record.toMimeType(), record.getPayload());
7346793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    }
7446793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich
7546793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    public static boolean isMime(NdefRecord record) {
76116871653662979554a7326991ba4fd599ac44ffNick Pelly        return record.toMimeType() != null;
7746793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich    }
788dd68417526785dd82f054bcf3f4763e19d35142Jeff Hamilton
798dd68417526785dd82f054bcf3f4763e19d35142Jeff Hamilton    public static NdefRecord newMimeRecord(String type, byte[] data) {
80116871653662979554a7326991ba4fd599ac44ffNick Pelly        return NdefRecord.createMime(type,  data);
818dd68417526785dd82f054bcf3f4763e19d35142Jeff Hamilton    }
8246793e91497af04df8c3cf0536b33ed1d5db0da2Nick Kralevich}
83