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.NdefRecord;
27import android.nfc.NfcAdapter;
28import android.os.Bundle;
29import android.os.Parcelable;
30import android.util.Log;
31import android.view.LayoutInflater;
32import android.view.View;
33import android.view.View.OnClickListener;
34import android.widget.LinearLayout;
35import android.widget.TextView;
36
37import java.util.List;
38
39/**
40 * An {@link Activity} which handles a broadcast of a new tag that the device just discovered.
41 */
42public class TagViewer extends Activity implements OnClickListener {
43    static final String TAG = "TagViewer";
44
45    LinearLayout mTagContent;
46
47    @Override
48    protected void onCreate(Bundle savedInstanceState) {
49        super.onCreate(savedInstanceState);
50
51        setContentView(R.layout.tag_viewer);
52
53        mTagContent = (LinearLayout) findViewById(R.id.list);
54
55        resolveIntent(getIntent());
56    }
57
58    void resolveIntent(Intent intent) {
59        // Parse the intent
60        String action = intent.getAction();
61        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)
62                || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) {
63            Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
64            NdefMessage msg = null;
65            if (rawMsgs != null && rawMsgs.length > 0) {
66                msg = (NdefMessage) rawMsgs[0];
67            }
68
69            buildTagViews(msg);
70        } else {
71            Log.e(TAG, "Unknown intent " + intent);
72            finish();
73            return;
74        }
75    }
76
77    void buildTagViews(NdefMessage msg) {
78        LayoutInflater inflater = LayoutInflater.from(this);
79        LinearLayout content = mTagContent;
80
81        // Clear out any old views in the content area, for example if you scan two tags in a row.
82        content.removeAllViews();
83
84        // Build views for all of the sub records
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);
93
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            }
107        }
108    }
109
110    @Override
111    public void onNewIntent(Intent intent) {
112        setIntent(intent);
113        resolveIntent(intent);
114    }
115
116    @Override
117    public void onClick(View view) {
118        finish();
119    }
120}
121