blob: b4e9474a0ec17666c011fe9f7a1467a0e626603d [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 Hamiltona29dc1b2010-10-28 17:44:37 -050026import android.nfc.NdefRecord;
Jeff Hamilton79495342010-10-18 13:13:10 -050027import android.nfc.NfcAdapter;
28import android.os.Bundle;
Jeff Hamiltona29dc1b2010-10-28 17:44:37 -050029import android.os.Parcelable;
Jeff Hamilton79495342010-10-18 13:13:10 -050030import android.util.Log;
Jeff Hamilton79495342010-10-18 13:13:10 -050031import android.view.LayoutInflater;
32import android.view.View;
33import android.view.View.OnClickListener;
Jeff Hamilton79495342010-10-18 13:13:10 -050034import android.widget.LinearLayout;
Jeff Hamilton59395352011-07-27 17:14:01 -050035import android.widget.TextView;
Jeff Hamilton79495342010-10-18 13:13:10 -050036
Jason parks40d9a0c2010-10-26 14:42:45 -050037import java.util.List;
Ben Komalo9330d7c2010-10-19 14:16:49 -070038
Jeff Hamilton79495342010-10-18 13:13:10 -050039/**
40 * An {@link Activity} which handles a broadcast of a new tag that the device just discovered.
41 */
Jeff Hamilton52baa662010-10-22 01:42:44 -050042public class TagViewer extends Activity implements OnClickListener {
Jeff Hamilton7d743872011-07-20 23:14:00 -050043 static final String TAG = "TagViewer";
Jeff Hamilton79495342010-10-18 13:13:10 -050044
Jeff Hamilton0215c982010-10-18 17:27:21 -050045 LinearLayout mTagContent;
Jeff Hamilton79495342010-10-18 13:13:10 -050046
47 @Override
48 protected void onCreate(Bundle savedInstanceState) {
49 super.onCreate(savedInstanceState);
50
Jeff Hamilton79495342010-10-18 13:13:10 -050051 setContentView(R.layout.tag_viewer);
52
Jeff Hamilton0215c982010-10-18 17:27:21 -050053 mTagContent = (LinearLayout) findViewById(R.id.list);
Jeff Hamilton79495342010-10-18 13:13:10 -050054
Jeff Hamilton0215c982010-10-18 17:27:21 -050055 resolveIntent(getIntent());
56 }
57
58 void resolveIntent(Intent intent) {
59 // Parse the intent
60 String action = intent.getAction();
Jeff Hamilton80e02342010-12-03 17:34:31 -060061 if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
Nick Pelly42d74142011-02-01 08:26:41 -080062 || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
Jeff Hamiltona29dc1b2010-10-28 17:44:37 -050063 Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
Jeff Hamilton59395352011-07-27 17:14:01 -050064 NdefMessage msg = null;
Jason parksf38cc722010-11-02 22:50:09 -050065 if (rawMsgs != null && rawMsgs.length > 0) {
Jeff Hamilton59395352011-07-27 17:14:01 -050066 msg = (NdefMessage) rawMsgs[0];
Jeff Hamiltona29dc1b2010-10-28 17:44:37 -050067 }
Jeff Hamilton59395352011-07-27 17:14:01 -050068
69 buildTagViews(msg);
Jeff Hamilton0215c982010-10-18 17:27:21 -050070 } else {
71 Log.e(TAG, "Unknown intent " + intent);
Jeff Hamilton79495342010-10-18 13:13:10 -050072 finish();
73 return;
74 }
Jeff Hamilton79495342010-10-18 13:13:10 -050075 }
76
Jeff Hamilton7d743872011-07-20 23:14:00 -050077 void buildTagViews(NdefMessage msg) {
Jeff Hamilton0215c982010-10-18 17:27:21 -050078 LayoutInflater inflater = LayoutInflater.from(this);
79 LinearLayout content = mTagContent;
Jeff Hamilton79495342010-10-18 13:13:10 -050080
Jeff Hamilton0215c982010-10-18 17:27:21 -050081 // Clear out any old views in the content area, for example if you scan two tags in a row.
82 content.removeAllViews();
83
Jeff Hamilton79495342010-10-18 13:13:10 -050084 // Build views for all of the sub records
Jeff Hamilton59395352011-07-27 17:14:01 -050085 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 parks40d9a0c2010-10-26 14:42:45 -050093
Jeff Hamilton59395352011-07-27 17:14:01 -050094 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 Hamilton79495342010-10-18 13:13:10 -0500107 }
108 }
109
110 @Override
Jeff Hamilton0215c982010-10-18 17:27:21 -0500111 public void onNewIntent(Intent intent) {
Jason parks40d9a0c2010-10-26 14:42:45 -0500112 setIntent(intent);
Jeff Hamilton0215c982010-10-18 17:27:21 -0500113 resolveIntent(intent);
114 }
115
116 @Override
Jeff Hamilton79495342010-10-18 13:13:10 -0500117 public void onClick(View view) {
Jeff Hamilton7d743872011-07-20 23:14:00 -0500118 finish();
Jeff Hamilton0215c982010-10-18 17:27:21 -0500119 }
Jeff Hamilton79495342010-10-18 13:13:10 -0500120}