blob: 70c758e57191c5db0188cecf2ead5c73e2bbd9d5 [file] [log] [blame]
Jeff Hamilton79495342010-10-18 13:13:10 -05001/*
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;
Jeff Hamilton79495342010-10-18 13:13:10 -050024import android.content.Intent;
25import android.nfc.NdefMessage;
Jeff Hamilton79495342010-10-18 13:13:10 -050026import android.nfc.NfcAdapter;
27import android.os.Bundle;
Jeff Hamiltona29dc1b2010-10-28 17:44:37 -050028import android.os.Parcelable;
Jeff Hamilton79495342010-10-18 13:13:10 -050029import android.util.Log;
Jeff Hamilton79495342010-10-18 13:13:10 -050030import android.view.LayoutInflater;
31import android.view.View;
32import android.view.View.OnClickListener;
Jeff Hamilton79495342010-10-18 13:13:10 -050033import android.widget.LinearLayout;
Jeff Hamilton59395352011-07-27 17:14:01 -050034import android.widget.TextView;
Jeff Hamilton79495342010-10-18 13:13:10 -050035
Jason parks40d9a0c2010-10-26 14:42:45 -050036import java.util.List;
Ben Komalo9330d7c2010-10-19 14:16:49 -070037
Jeff Hamilton79495342010-10-18 13:13:10 -050038/**
39 * An {@link Activity} which handles a broadcast of a new tag that the device just discovered.
40 */
Jeff Hamilton52baa662010-10-22 01:42:44 -050041public class TagViewer extends Activity implements OnClickListener {
Jeff Hamilton7d743872011-07-20 23:14:00 -050042 static final String TAG = "TagViewer";
Jeff Hamilton79495342010-10-18 13:13:10 -050043
Jeff Hamilton0215c982010-10-18 17:27:21 -050044 LinearLayout mTagContent;
Jeff Hamilton79495342010-10-18 13:13:10 -050045
46 @Override
47 protected void onCreate(Bundle savedInstanceState) {
48 super.onCreate(savedInstanceState);
49
Jeff Hamilton79495342010-10-18 13:13:10 -050050 setContentView(R.layout.tag_viewer);
51
Jeff Hamilton0215c982010-10-18 17:27:21 -050052 mTagContent = (LinearLayout) findViewById(R.id.list);
Jeff Hamilton79495342010-10-18 13:13:10 -050053
Jeff Hamilton0215c982010-10-18 17:27:21 -050054 resolveIntent(getIntent());
55 }
56
57 void resolveIntent(Intent intent) {
58 // Parse the intent
59 String action = intent.getAction();
Jeff Hamilton80e02342010-12-03 17:34:31 -060060 if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
Nick Pelly42d74142011-02-01 08:26:41 -080061 || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Jeff Hamiltona29dc1b2010-10-28 17:44:37 -050062 Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
Jeff Hamilton59395352011-07-27 17:14:01 -050063 NdefMessage msg = null;
Jason parksf38cc722010-11-02 22:50:09 -050064 if (rawMsgs != null && rawMsgs.length > 0) {
Jeff Hamilton59395352011-07-27 17:14:01 -050065 msg = (NdefMessage) rawMsgs[0];
Jeff Hamiltona29dc1b2010-10-28 17:44:37 -050066 }
Jeff Hamilton59395352011-07-27 17:14:01 -050067
68 buildTagViews(msg);
Jeff Hamilton0215c982010-10-18 17:27:21 -050069 } else {
70 Log.e(TAG, "Unknown intent " + intent);
Jeff Hamilton79495342010-10-18 13:13:10 -050071 finish();
72 return;
73 }
Jeff Hamilton79495342010-10-18 13:13:10 -050074 }
75
zhenqing.x.qiu63cda472017-08-30 15:45:07 +090076 private void addView(LayoutInflater inflater, LinearLayout content, View v) {
77 if (v == null) {
78 TextView empty = (TextView) inflater.inflate(R.layout.tag_text, content, false);
79 empty.setText(R.string.tag_empty);
80 v = empty;
81 }
82 content.addView(v);
83 }
84
Jeff Hamilton7d743872011-07-20 23:14:00 -050085 void buildTagViews(NdefMessage msg) {
Jeff Hamilton0215c982010-10-18 17:27:21 -050086 LayoutInflater inflater = LayoutInflater.from(this);
87 LinearLayout content = mTagContent;
Jeff Hamilton79495342010-10-18 13:13:10 -050088
Jeff Hamilton0215c982010-10-18 17:27:21 -050089 // Clear out any old views in the content area, for example if you scan two tags in a row.
90 content.removeAllViews();
91
Jeff Hamilton79495342010-10-18 13:13:10 -050092 // Build views for all of the sub records
Jeff Hamilton59395352011-07-27 17:14:01 -050093 if (msg == null) {
zhenqing.x.qiu63cda472017-08-30 15:45:07 +090094 addView(inflater, content, null);
Jeff Hamilton59395352011-07-27 17:14:01 -050095 } else {
96 // Parse the first message in the list
97 //TODO figure out what to do when/if we support multiple messages per tag
98 ParsedNdefMessage parsedMsg = NdefMessageParser.parse(msg);
Jason parks40d9a0c2010-10-26 14:42:45 -050099
Jeff Hamilton59395352011-07-27 17:14:01 -0500100 List<ParsedNdefRecord> records = parsedMsg.getRecords();
101 final int size = records.size();
102 if (size == 0) {
zhenqing.x.qiu63cda472017-08-30 15:45:07 +0900103 addView(inflater, content, null);
Jeff Hamilton59395352011-07-27 17:14:01 -0500104 } else {
105 for (int i = 0; i < size; i++) {
106 ParsedNdefRecord record = records.get(i);
zhenqing.x.qiu63cda472017-08-30 15:45:07 +0900107 addView(inflater, content, record.getView(this, inflater, content, i));
Jeff Hamilton59395352011-07-27 17:14:01 -0500108 inflater.inflate(R.layout.tag_divider, content, true);
109 }
110 }
Jeff Hamilton79495342010-10-18 13:13:10 -0500111 }
112 }
113
114 @Override
Jeff Hamilton0215c982010-10-18 17:27:21 -0500115 public void onNewIntent(Intent intent) {
Jason parks40d9a0c2010-10-26 14:42:45 -0500116 setIntent(intent);
Jeff Hamilton0215c982010-10-18 17:27:21 -0500117 resolveIntent(intent);
118 }
119
120 @Override
Jeff Hamilton79495342010-10-18 13:13:10 -0500121 public void onClick(View view) {
Jeff Hamilton7d743872011-07-20 23:14:00 -0500122 finish();
Jeff Hamilton0215c982010-10-18 17:27:21 -0500123 }
Jeff Hamilton79495342010-10-18 13:13:10 -0500124}