Home | History | Annotate | Download | only in record
      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 
     17 package com.android.apps.tag.record;
     18 
     19 import com.android.apps.tag.R;
     20 import com.google.common.annotations.VisibleForTesting;
     21 import com.google.common.base.Preconditions;
     22 
     23 import android.app.Activity;
     24 import android.content.Context;
     25 import android.nfc.NdefRecord;
     26 import android.view.LayoutInflater;
     27 import android.view.View;
     28 import android.view.ViewGroup;
     29 import android.widget.TextView;
     30 
     31 import java.nio.charset.Charset;
     32 import java.util.Arrays;
     33 import java.util.Locale;
     34 
     35 /**
     36  * A {@link ParsedNdefRecord} corresponding to a MIME object.
     37  */
     38 public 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.toMimeType() != null);
     72         return new MimeRecord(record.toMimeType(), record.getPayload());
     73     }
     74 
     75     public static boolean isMime(NdefRecord record) {
     76         return record.toMimeType() != null;
     77     }
     78 
     79     public static NdefRecord newMimeRecord(String type, byte[] data) {
     80         return NdefRecord.createMime(type,  data);
     81     }
     82 }
     83