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; 18 19import com.android.apps.tag.message.NdefMessageParser; 20import com.android.apps.tag.message.ParsedNdefMessage; 21import com.android.apps.tag.record.ParsedNdefRecord; 22 23import android.app.Activity; 24import android.content.Intent; 25import android.nfc.NdefMessage; 26import android.nfc.NfcAdapter; 27import android.os.Bundle; 28import android.os.Parcelable; 29import android.util.Log; 30import android.view.LayoutInflater; 31import android.view.View; 32import android.view.View.OnClickListener; 33import android.widget.LinearLayout; 34import android.widget.TextView; 35 36import java.util.List; 37 38/** 39 * An {@link Activity} which handles a broadcast of a new tag that the device just discovered. 40 */ 41public class TagViewer extends Activity implements OnClickListener { 42 static final String TAG = "TagViewer"; 43 44 LinearLayout mTagContent; 45 46 @Override 47 protected void onCreate(Bundle savedInstanceState) { 48 super.onCreate(savedInstanceState); 49 50 setContentView(R.layout.tag_viewer); 51 52 mTagContent = (LinearLayout) findViewById(R.id.list); 53 54 resolveIntent(getIntent()); 55 } 56 57 void resolveIntent(Intent intent) { 58 // Parse the intent 59 String action = intent.getAction(); 60 if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) 61 || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { 62 Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); 63 NdefMessage msg = null; 64 if (rawMsgs != null && rawMsgs.length > 0) { 65 msg = (NdefMessage) rawMsgs[0]; 66 } 67 68 buildTagViews(msg); 69 } else { 70 Log.e(TAG, "Unknown intent " + intent); 71 finish(); 72 return; 73 } 74 } 75 76 void buildTagViews(NdefMessage msg) { 77 LayoutInflater inflater = LayoutInflater.from(this); 78 LinearLayout content = mTagContent; 79 80 // Clear out any old views in the content area, for example if you scan two tags in a row. 81 content.removeAllViews(); 82 83 // Build views for all of the sub records 84 if (msg == null) { 85 TextView empty = (TextView) inflater.inflate(R.layout.tag_text, content, false); 86 empty.setText(R.string.tag_empty); 87 content.addView(empty); 88 } else { 89 // Parse the first message in the list 90 //TODO figure out what to do when/if we support multiple messages per tag 91 ParsedNdefMessage parsedMsg = NdefMessageParser.parse(msg); 92 93 List<ParsedNdefRecord> records = parsedMsg.getRecords(); 94 final int size = records.size(); 95 if (size == 0) { 96 TextView empty = (TextView) inflater.inflate(R.layout.tag_text, content, false); 97 empty.setText(R.string.tag_empty); 98 content.addView(empty); 99 } else { 100 for (int i = 0; i < size; i++) { 101 ParsedNdefRecord record = records.get(i); 102 content.addView(record.getView(this, inflater, content, i)); 103 inflater.inflate(R.layout.tag_divider, content, true); 104 } 105 } 106 } 107 } 108 109 @Override 110 public void onNewIntent(Intent intent) { 111 setIntent(intent); 112 resolveIntent(intent); 113 } 114 115 @Override 116 public void onClick(View view) { 117 finish(); 118 } 119} 120