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 | |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 19 | import com.android.apps.tag.TagList.TagQuery; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 20 | import com.android.apps.tag.message.NdefMessageParser; |
| 21 | import com.android.apps.tag.message.ParsedNdefMessage; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 22 | import com.android.apps.tag.provider.TagContract.NdefMessages; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 23 | import com.android.apps.tag.record.ParsedNdefRecord; |
| 24 | |
| 25 | import android.app.Activity; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 26 | import android.content.Intent; |
Ben Komalo | 9330d7c | 2010-10-19 14:16:49 -0700 | [diff] [blame^] | 27 | import android.content.res.AssetFileDescriptor; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 28 | import android.database.Cursor; |
Ben Komalo | 9330d7c | 2010-10-19 14:16:49 -0700 | [diff] [blame^] | 29 | import android.media.AudioSystem; |
| 30 | import android.media.MediaPlayer; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 31 | import android.net.Uri; |
| 32 | import android.nfc.FormatException; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 33 | import android.nfc.NdefMessage; |
| 34 | import android.nfc.NdefTag; |
| 35 | import android.nfc.NfcAdapter; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 36 | import android.os.AsyncTask; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 37 | import android.os.Bundle; |
| 38 | import android.os.Handler; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 39 | import android.os.Message; |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 40 | import android.text.format.DateUtils; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 41 | import android.util.Log; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 42 | import android.view.LayoutInflater; |
| 43 | import android.view.View; |
| 44 | import android.view.View.OnClickListener; |
| 45 | import android.view.WindowManager; |
| 46 | import android.widget.Button; |
| 47 | import android.widget.CheckBox; |
| 48 | import android.widget.ImageView; |
| 49 | import android.widget.LinearLayout; |
| 50 | import android.widget.TextView; |
| 51 | |
Ben Komalo | 9330d7c | 2010-10-19 14:16:49 -0700 | [diff] [blame^] | 52 | import java.io.IOException; |
| 53 | |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 54 | /** |
| 55 | * An {@link Activity} which handles a broadcast of a new tag that the device just discovered. |
| 56 | */ |
| 57 | public class TagViewer extends Activity implements OnClickListener, Handler.Callback { |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 58 | static final String TAG = "SaveTag"; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 59 | static final String EXTRA_TAG_DB_ID = "db_id"; |
| 60 | static final String EXTRA_MESSAGE = "msg"; |
| 61 | |
| 62 | /** This activity will finish itself in this amount of time if the user doesn't do anything. */ |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 63 | static final int ACTIVITY_TIMEOUT_MS = 5 * 1000; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 64 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 65 | Uri mTagUri; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 66 | ImageView mIcon; |
| 67 | TextView mTitle; |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 68 | TextView mDate; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 69 | CheckBox mStar; |
| 70 | Button mDeleteButton; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 71 | Button mDoneButton; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 72 | NdefMessage[] mMessagesToSave = null; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 73 | LinearLayout mTagContent; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 74 | |
| 75 | @Override |
| 76 | protected void onCreate(Bundle savedInstanceState) { |
| 77 | super.onCreate(savedInstanceState); |
| 78 | |
| 79 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
| 80 | | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD |
| 81 | | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
| 82 | | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON |
| 83 | | WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON |
| 84 | | WindowManager.LayoutParams.FLAG_DIM_BEHIND |
| 85 | ); |
| 86 | |
| 87 | setContentView(R.layout.tag_viewer); |
| 88 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 89 | mTagContent = (LinearLayout) findViewById(R.id.list); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 90 | mTitle = (TextView) findViewById(R.id.title); |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 91 | mDate = (TextView) findViewById(R.id.date); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 92 | mIcon = (ImageView) findViewById(R.id.icon); |
| 93 | mStar = (CheckBox) findViewById(R.id.star); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 94 | mDeleteButton = (Button) findViewById(R.id.button_delete); |
| 95 | mDoneButton = (Button) findViewById(R.id.button_done); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 96 | |
| 97 | mDeleteButton.setOnClickListener(this); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 98 | mDoneButton.setOnClickListener(this); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 99 | mIcon.setImageResource(R.drawable.ic_launcher_nfc); |
| 100 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 101 | resolveIntent(getIntent()); |
| 102 | } |
| 103 | |
| 104 | void resolveIntent(Intent intent) { |
| 105 | // Parse the intent |
| 106 | String action = intent.getAction(); |
| 107 | if (NfcAdapter.ACTION_NDEF_TAG_DISCOVERED.equals(action)) { |
| 108 | // Get the messages from the tag |
| 109 | //TODO check if the tag is writable and offer to write it? |
| 110 | NdefTag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); |
| 111 | NdefMessage[] msgs = tag.getNdefMessages(); |
| 112 | if (msgs == null || msgs.length == 0) { |
| 113 | Log.e(TAG, "No NDEF messages"); |
| 114 | finish(); |
| 115 | return; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 116 | } |
| 117 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 118 | // Setup the views |
| 119 | setTitle(R.string.title_scanned_tag); |
| 120 | mStar.setVisibility(View.GONE); |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 121 | mDate.setVisibility(View.GONE); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 122 | |
Ben Komalo | 9330d7c | 2010-10-19 14:16:49 -0700 | [diff] [blame^] | 123 | // Play notification. |
| 124 | try { |
| 125 | MediaPlayer player = new MediaPlayer(); |
| 126 | AssetFileDescriptor file = getResources().openRawResourceFd( |
| 127 | R.raw.discovered_tag_notification); |
| 128 | player.setDataSource( |
| 129 | file.getFileDescriptor(), |
| 130 | file.getStartOffset(), |
| 131 | file.getLength()); |
| 132 | file.close(); |
| 133 | player.setAudioStreamType(AudioSystem.STREAM_SYSTEM); |
| 134 | player.prepare(); |
| 135 | player.start(); |
| 136 | } catch (IOException ex) { |
| 137 | Log.w(TAG, "Sound creation failed for tag discovery"); |
| 138 | } |
| 139 | |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 140 | // Set a timer on this activity since it wasn't created by the user |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 141 | // new Handler(this).sendEmptyMessageDelayed(0, ACTIVITY_TIMEOUT_MS); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 142 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 143 | // Mark messages that were just scanned for saving |
| 144 | mMessagesToSave = msgs; |
| 145 | |
| 146 | // Build the views for the tag |
| 147 | buildTagViews(msgs); |
| 148 | } else if (Intent.ACTION_VIEW.equals(action)) { |
| 149 | // Setup the views |
| 150 | setTitle(R.string.title_existing_tag); |
| 151 | mStar.setVisibility(View.VISIBLE); |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 152 | mDate.setVisibility(View.VISIBLE); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 153 | |
| 154 | // Read the tag from the database asynchronously |
| 155 | mTagUri = intent.getData(); |
| 156 | new LoadTagTask().execute(mTagUri); |
| 157 | } else { |
| 158 | Log.e(TAG, "Unknown intent " + intent); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 159 | finish(); |
| 160 | return; |
| 161 | } |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 162 | } |
| 163 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 164 | void buildTagViews(NdefMessage[] msgs) { |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 165 | if (msgs == null || msgs.length == 0) { |
| 166 | return; |
| 167 | } |
| 168 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 169 | LayoutInflater inflater = LayoutInflater.from(this); |
| 170 | LinearLayout content = mTagContent; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 171 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 172 | // Clear out any old views in the content area, for example if you scan two tags in a row. |
| 173 | content.removeAllViews(); |
| 174 | |
| 175 | // Parse the first message in the list |
| 176 | //TODO figure out what to do when/if we support multiple messages per tag |
| 177 | ParsedNdefMessage parsedMsg = NdefMessageParser.parse(msgs[0]); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 178 | |
| 179 | // Build views for all of the sub records |
| 180 | for (ParsedNdefRecord record : parsedMsg.getRecords()) { |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 181 | content.addView(record.getView(this, inflater, content)); |
| 182 | inflater.inflate(R.layout.tag_divider, content, true); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
| 186 | @Override |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 187 | public void onNewIntent(Intent intent) { |
| 188 | // If we get a new scan while looking at a tag just save off the old tag... |
| 189 | if (mMessagesToSave != null) { |
| 190 | saveMessages(mMessagesToSave); |
| 191 | mMessagesToSave = null; |
| 192 | } |
| 193 | |
| 194 | // ... and show the new one. |
| 195 | resolveIntent(intent); |
| 196 | } |
| 197 | |
| 198 | @Override |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 199 | public void setTitle(CharSequence title) { |
| 200 | mTitle.setText(title); |
| 201 | } |
| 202 | |
| 203 | @Override |
| 204 | public void onClick(View view) { |
| 205 | if (view == mDeleteButton) { |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 206 | if (mTagUri == null) { |
| 207 | // The tag hasn't been saved yet, so indicate it shouldn't be saved |
| 208 | mMessagesToSave = null; |
| 209 | finish(); |
| 210 | } else { |
| 211 | // The tag came from the database, start a service to delete it |
| 212 | Intent delete = new Intent(this, TagService.class); |
| 213 | delete.putExtra(TagService.EXTRA_DELETE_URI, mTagUri); |
| 214 | startService(delete); |
| 215 | finish(); |
| 216 | } |
| 217 | } else if (view == mDoneButton) { |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 218 | finish(); |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | @Override |
| 223 | public void onStop() { |
| 224 | super.onStop(); |
| 225 | if (mMessagesToSave != null) { |
| 226 | saveMessages(mMessagesToSave); |
| 227 | } |
| 228 | } |
| 229 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 230 | /** |
| 231 | * Starts a service to asynchronously save the messages to the content provider. |
| 232 | */ |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 233 | void saveMessages(NdefMessage[] msgs) { |
| 234 | Intent save = new Intent(this, TagService.class); |
| 235 | save.putExtra(TagService.EXTRA_SAVE_MSGS, msgs); |
| 236 | startService(save); |
| 237 | } |
| 238 | |
| 239 | @Override |
| 240 | public boolean handleMessage(Message msg) { |
| 241 | finish(); |
| 242 | return true; |
| 243 | } |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 244 | |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 245 | interface ViewTagQuery { |
| 246 | final static String[] PROJECTION = new String[] { |
| 247 | NdefMessages.BYTES, // 0 |
| 248 | NdefMessages.STARRED, // 1 |
| 249 | NdefMessages.DATE, // 2 |
| 250 | }; |
| 251 | |
| 252 | static final int COLUMN_BYTES = 0; |
| 253 | static final int COLUMN_STARRED = 1; |
| 254 | static final int COLUMN_DATE = 2; |
| 255 | } |
| 256 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 257 | /** |
| 258 | * Loads a tag from the database, parses it, and builds the views |
| 259 | */ |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 260 | final class LoadTagTask extends AsyncTask<Uri, Void, Cursor> { |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 261 | @Override |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 262 | public Cursor doInBackground(Uri... args) { |
| 263 | Cursor cursor = getContentResolver().query(args[0], ViewTagQuery.PROJECTION, |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 264 | null, null, null); |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 265 | |
| 266 | // Ensure the cursor loads its window |
| 267 | if (cursor != null) cursor.getCount(); |
| 268 | return cursor; |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public void onPostExecute(Cursor cursor) { |
| 273 | NdefMessage msg = null; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 274 | try { |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 275 | if (cursor != null && cursor.moveToFirst()) { |
| 276 | msg = new NdefMessage(cursor.getBlob(ViewTagQuery.COLUMN_BYTES)); |
| 277 | if (msg != null) { |
| 278 | mDate.setText(DateUtils.getRelativeTimeSpanString(TagViewer.this, |
| 279 | cursor.getLong(ViewTagQuery.COLUMN_DATE))); |
| 280 | mStar.setChecked(cursor.getInt(ViewTagQuery.COLUMN_STARRED) != 0); |
| 281 | buildTagViews(new NdefMessage[] { msg }); |
| 282 | } |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 283 | } |
| 284 | } catch (FormatException e) { |
| 285 | Log.e(TAG, "invalid tag format", e); |
| 286 | } finally { |
| 287 | if (cursor != null) cursor.close(); |
| 288 | } |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 289 | } |
| 290 | } |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 291 | } |