Home | History | Annotate | Download | only in nfc
      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 package com.example.android.nfc;
     17 
     18 import android.app.Activity;
     19 import android.content.Intent;
     20 import android.nfc.NdefMessage;
     21 import android.nfc.NdefRecord;
     22 import android.nfc.NfcAdapter;
     23 import android.os.Bundle;
     24 import android.os.Parcelable;
     25 import android.util.Log;
     26 import android.view.LayoutInflater;
     27 import android.view.WindowManager;
     28 import android.widget.LinearLayout;
     29 import android.widget.TextView;
     30 
     31 import com.example.android.nfc.record.ParsedNdefRecord;
     32 
     33 import java.util.List;
     34 
     35 /**
     36  * An {@link Activity} which handles a broadcast of a new tag that the device
     37  * just discovered.
     38  */
     39 public class TagViewer extends Activity {
     40 
     41     static final String TAG = "ViewTag";
     42 
     43     /**
     44      * This activity will finish itself in this amount of time if the user
     45      * doesn't do anything.
     46      */
     47     static final int ACTIVITY_TIMEOUT_MS = 1 * 1000;
     48 
     49     TextView mTitle;
     50 
     51     LinearLayout mTagContent;
     52 
     53     @Override
     54     protected void onCreate(Bundle savedInstanceState) {
     55         super.onCreate(savedInstanceState);
     56         setContentView(R.layout.tag_viewer);
     57         mTagContent = (LinearLayout) findViewById(R.id.list);
     58         mTitle = (TextView) findViewById(R.id.title);
     59         resolveIntent(getIntent());
     60     }
     61 
     62     void resolveIntent(Intent intent) {
     63         // Parse the intent
     64         String action = intent.getAction();
     65         if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
     66             // When a tag is discovered we send it to the service to be save. We
     67             // include a PendingIntent for the service to call back onto. This
     68             // will cause this activity to be restarted with onNewIntent(). At
     69             // that time we read it from the database and view it.
     70             Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
     71             NdefMessage[] msgs;
     72             if (rawMsgs != null) {
     73                 msgs = new NdefMessage[rawMsgs.length];
     74                 for (int i = 0; i < rawMsgs.length; i++) {
     75                     msgs[i] = (NdefMessage) rawMsgs[i];
     76                 }
     77             } else {
     78                 // Unknown tag type
     79                 byte[] empty = new byte[] {};
     80                 NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty);
     81                 NdefMessage msg = new NdefMessage(new NdefRecord[] {record});
     82                 msgs = new NdefMessage[] {msg};
     83             }
     84             // Setup the views
     85             setTitle(R.string.title_scanned_tag);
     86             buildTagViews(msgs);
     87         } else {
     88             Log.e(TAG, "Unknown intent " + intent);
     89             finish();
     90             return;
     91         }
     92     }
     93 
     94     void buildTagViews(NdefMessage[] msgs) {
     95         if (msgs == null || msgs.length == 0) {
     96             return;
     97         }
     98         LayoutInflater inflater = LayoutInflater.from(this);
     99         LinearLayout content = mTagContent;
    100         // Clear out any old views in the content area, for example if you scan
    101         // two tags in a row.
    102         content.removeAllViews();
    103         // Parse the first message in the list
    104         // Build views for all of the sub records
    105         List<ParsedNdefRecord> records = NdefMessageParser.parse(msgs[0]);
    106         final int size = records.size();
    107         for (int i = 0; i < size; i++) {
    108             ParsedNdefRecord record = records.get(i);
    109             content.addView(record.getView(this, inflater, content, i));
    110             inflater.inflate(R.layout.tag_divider, content, true);
    111         }
    112     }
    113 
    114     @Override
    115     public void onNewIntent(Intent intent) {
    116         setIntent(intent);
    117         resolveIntent(intent);
    118     }
    119 
    120     @Override
    121     public void setTitle(CharSequence title) {
    122         mTitle.setText(title);
    123     }
    124 }
    125