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