blob: f31666edbc5bdbda5246f4dc222eb3716522bbf1 [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;
24import android.content.Context;
25import android.content.Intent;
26import android.nfc.NdefMessage;
27import android.nfc.NdefTag;
28import android.nfc.NfcAdapter;
29import android.os.Bundle;
30import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
33import android.text.TextUtils;
34import android.util.Log;
35import android.view.ContextThemeWrapper;
36import android.view.LayoutInflater;
37import android.view.View;
38import android.view.View.OnClickListener;
39import android.view.WindowManager;
40import android.widget.Button;
41import android.widget.CheckBox;
42import android.widget.ImageView;
43import android.widget.LinearLayout;
44import android.widget.TextView;
45
46import java.util.Locale;
47
48/**
49 * An {@link Activity} which handles a broadcast of a new tag that the device just discovered.
50 */
51public class TagViewer extends Activity implements OnClickListener, Handler.Callback {
52 static final String TAG = "SaveTag";
53 static final String EXTRA_TAG_DB_ID = "db_id";
54 static final String EXTRA_MESSAGE = "msg";
55
56 /** This activity will finish itself in this amount of time if the user doesn't do anything. */
57 static final int ACTIVITY_TIMEOUT_MS = 10 * 1000;
58
59 long mTagDatabaseId;
60 ImageView mIcon;
61 TextView mTitle;
62 CheckBox mStar;
63 Button mDeleteButton;
64 Button mCancelButton;
65 NdefMessage[] mMessagesToSave = null;
66
67 @Override
68 protected void onCreate(Bundle savedInstanceState) {
69 super.onCreate(savedInstanceState);
70
71 getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
72 | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
73 | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
74 | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
75 | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON
76 | WindowManager.LayoutParams.FLAG_DIM_BEHIND
77 );
78
79 setContentView(R.layout.tag_viewer);
80
81 mTitle = (TextView) findViewById(R.id.title);
82 mIcon = (ImageView) findViewById(R.id.icon);
83 mStar = (CheckBox) findViewById(R.id.star);
84 mDeleteButton = (Button) findViewById(R.id.btn_delete);
85 mCancelButton = (Button) findViewById(R.id.btn_cancel);
86
87 mDeleteButton.setOnClickListener(this);
88 mCancelButton.setOnClickListener(this);
89 mIcon.setImageResource(R.drawable.ic_launcher_nfc);
90
91 Intent intent = getIntent();
92 NdefMessage[] msgs = null;
93 NdefTag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
94 if (tag == null) {
95 // Maybe it came from the database?
96 mTagDatabaseId = intent.getLongExtra(EXTRA_TAG_DB_ID, -1);
97 NdefMessage msg = intent.getParcelableExtra(EXTRA_MESSAGE);
98 if (msg != null) {
99 msgs = new NdefMessage[] { msg };
100 }
101
102 // Hide the text about saving the tag, it's already in the database
103 findViewById(R.id.cancel_help_text).setVisibility(View.GONE);
104 } else {
105 msgs = tag.getNdefMessages();
106 mDeleteButton.setVisibility(View.GONE);
107
108 // Set a timer on this activity since it wasn't created by the user
109 new Handler(this).sendEmptyMessageDelayed(0, ACTIVITY_TIMEOUT_MS);
110
111 // Save the messages that were just scanned
112 mMessagesToSave = msgs;
113 }
114
115 if (msgs == null || msgs.length == 0) {
116 Log.e(TAG, "No NDEF messages");
117 finish();
118 return;
119 }
120
121 Context contentContext = new ContextThemeWrapper(this, android.R.style.Theme_Light);
122 LayoutInflater inflater = LayoutInflater.from(contentContext);
123 LinearLayout list = (LinearLayout) findViewById(R.id.list);
124
125 buildTagViews(list, inflater, msgs);
126
127 if (TextUtils.isEmpty(getTitle())) {
128 // There isn't a snippet for this tag, use a default title
129 setTitle(R.string.tag_unknown);
130 }
131 }
132
133 private void buildTagViews(LinearLayout list, LayoutInflater inflater, NdefMessage[] msgs) {
134 if (msgs == null || msgs.length == 0) {
135 return;
136 }
137
138 // Build the views from the logical records in the messages
139 NdefMessage msg = msgs[0];
140
141 // Set the title to be the snippet of the message
142 ParsedNdefMessage parsedMsg = NdefMessageParser.parse(msg);
143 setTitle(parsedMsg.getSnippet(this, Locale.getDefault()));
144
145 // Build views for all of the sub records
146 for (ParsedNdefRecord record : parsedMsg.getRecords()) {
147 list.addView(record.getView(this, inflater, list));
148 inflater.inflate(R.layout.tag_divider, list, true);
149 }
150 }
151
152 @Override
153 public void setTitle(CharSequence title) {
154 mTitle.setText(title);
155 }
156
157 @Override
158 public void onClick(View view) {
159 if (view == mDeleteButton) {
160 Intent save = new Intent(this, TagService.class);
161 save.putExtra(TagService.EXTRA_DELETE_ID, mTagDatabaseId);
162 startService(save);
163 finish();
164 } else if (view == mCancelButton) {
165 mMessagesToSave = null;
166 finish();
167 }
168 }
169
170 @Override
171 public void onStop() {
172 super.onStop();
173 if (mMessagesToSave != null) {
174 saveMessages(mMessagesToSave);
175 }
176 }
177
178 void saveMessages(NdefMessage[] msgs) {
179 Intent save = new Intent(this, TagService.class);
180 save.putExtra(TagService.EXTRA_SAVE_MSGS, msgs);
181 startService(save);
182 }
183
184 @Override
185 public boolean handleMessage(Message msg) {
186 finish();
187 return true;
188 }
189}