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 | |
| 19 | import com.android.apps.tag.message.NdefMessageParser; |
| 20 | import com.android.apps.tag.message.ParsedNdefMessage; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 21 | import com.android.apps.tag.provider.TagContract.NdefMessages; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 22 | import com.android.apps.tag.record.ParsedNdefRecord; |
| 23 | |
| 24 | import android.app.Activity; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 25 | import android.content.Intent; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 26 | import android.database.Cursor; |
| 27 | import android.net.Uri; |
| 28 | import android.nfc.FormatException; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 29 | import android.nfc.NdefMessage; |
| 30 | import android.nfc.NdefTag; |
| 31 | import android.nfc.NfcAdapter; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 32 | import android.os.AsyncTask; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 33 | import android.os.Bundle; |
| 34 | import android.os.Handler; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 35 | import android.os.Message; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 36 | import android.util.Log; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 37 | import android.view.LayoutInflater; |
| 38 | import android.view.View; |
| 39 | import android.view.View.OnClickListener; |
| 40 | import android.view.WindowManager; |
| 41 | import android.widget.Button; |
| 42 | import android.widget.CheckBox; |
| 43 | import android.widget.ImageView; |
| 44 | import android.widget.LinearLayout; |
| 45 | import android.widget.TextView; |
| 46 | |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 47 | /** |
| 48 | * An {@link Activity} which handles a broadcast of a new tag that the device just discovered. |
| 49 | */ |
| 50 | public class TagViewer extends Activity implements OnClickListener, Handler.Callback { |
| 51 | static final String TAG = "SaveTag"; |
| 52 | static final String EXTRA_TAG_DB_ID = "db_id"; |
| 53 | static final String EXTRA_MESSAGE = "msg"; |
| 54 | |
| 55 | /** 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^] | 56 | static final int ACTIVITY_TIMEOUT_MS = 5 * 1000; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 57 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 58 | Uri mTagUri; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 59 | ImageView mIcon; |
| 60 | TextView mTitle; |
| 61 | CheckBox mStar; |
| 62 | Button mDeleteButton; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 63 | Button mDoneButton; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 64 | NdefMessage[] mMessagesToSave = null; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 65 | LinearLayout mTagContent; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 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 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 81 | mTagContent = (LinearLayout) findViewById(R.id.list); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 82 | mTitle = (TextView) findViewById(R.id.title); |
| 83 | mIcon = (ImageView) findViewById(R.id.icon); |
| 84 | mStar = (CheckBox) findViewById(R.id.star); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 85 | mDeleteButton = (Button) findViewById(R.id.button_delete); |
| 86 | mDoneButton = (Button) findViewById(R.id.button_done); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 87 | |
| 88 | mDeleteButton.setOnClickListener(this); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 89 | mDoneButton.setOnClickListener(this); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 90 | mIcon.setImageResource(R.drawable.ic_launcher_nfc); |
| 91 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 92 | resolveIntent(getIntent()); |
| 93 | } |
| 94 | |
| 95 | void resolveIntent(Intent intent) { |
| 96 | // Parse the intent |
| 97 | String action = intent.getAction(); |
| 98 | if (NfcAdapter.ACTION_NDEF_TAG_DISCOVERED.equals(action)) { |
| 99 | // Get the messages from the tag |
| 100 | //TODO check if the tag is writable and offer to write it? |
| 101 | NdefTag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); |
| 102 | NdefMessage[] msgs = tag.getNdefMessages(); |
| 103 | if (msgs == null || msgs.length == 0) { |
| 104 | Log.e(TAG, "No NDEF messages"); |
| 105 | finish(); |
| 106 | return; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 109 | // Setup the views |
| 110 | setTitle(R.string.title_scanned_tag); |
| 111 | mStar.setVisibility(View.GONE); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 112 | |
| 113 | // 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^] | 114 | // new Handler(this).sendEmptyMessageDelayed(0, ACTIVITY_TIMEOUT_MS); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 115 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 116 | // Mark messages that were just scanned for saving |
| 117 | mMessagesToSave = msgs; |
| 118 | |
| 119 | // Build the views for the tag |
| 120 | buildTagViews(msgs); |
| 121 | } else if (Intent.ACTION_VIEW.equals(action)) { |
| 122 | // Setup the views |
| 123 | setTitle(R.string.title_existing_tag); |
| 124 | mStar.setVisibility(View.VISIBLE); |
| 125 | |
| 126 | // Read the tag from the database asynchronously |
| 127 | mTagUri = intent.getData(); |
| 128 | new LoadTagTask().execute(mTagUri); |
| 129 | } else { |
| 130 | Log.e(TAG, "Unknown intent " + intent); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 131 | finish(); |
| 132 | return; |
| 133 | } |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 134 | } |
| 135 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 136 | void buildTagViews(NdefMessage[] msgs) { |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 137 | if (msgs == null || msgs.length == 0) { |
| 138 | return; |
| 139 | } |
| 140 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 141 | LayoutInflater inflater = LayoutInflater.from(this); |
| 142 | LinearLayout content = mTagContent; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 143 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 144 | // Clear out any old views in the content area, for example if you scan two tags in a row. |
| 145 | content.removeAllViews(); |
| 146 | |
| 147 | // Parse the first message in the list |
| 148 | //TODO figure out what to do when/if we support multiple messages per tag |
| 149 | ParsedNdefMessage parsedMsg = NdefMessageParser.parse(msgs[0]); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 150 | |
| 151 | // Build views for all of the sub records |
| 152 | for (ParsedNdefRecord record : parsedMsg.getRecords()) { |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 153 | content.addView(record.getView(this, inflater, content)); |
| 154 | inflater.inflate(R.layout.tag_divider, content, true); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | @Override |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 159 | public void onNewIntent(Intent intent) { |
| 160 | // If we get a new scan while looking at a tag just save off the old tag... |
| 161 | if (mMessagesToSave != null) { |
| 162 | saveMessages(mMessagesToSave); |
| 163 | mMessagesToSave = null; |
| 164 | } |
| 165 | |
| 166 | // ... and show the new one. |
| 167 | resolveIntent(intent); |
| 168 | } |
| 169 | |
| 170 | @Override |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 171 | public void setTitle(CharSequence title) { |
| 172 | mTitle.setText(title); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void onClick(View view) { |
| 177 | if (view == mDeleteButton) { |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 178 | if (mTagUri == null) { |
| 179 | // The tag hasn't been saved yet, so indicate it shouldn't be saved |
| 180 | mMessagesToSave = null; |
| 181 | finish(); |
| 182 | } else { |
| 183 | // The tag came from the database, start a service to delete it |
| 184 | Intent delete = new Intent(this, TagService.class); |
| 185 | delete.putExtra(TagService.EXTRA_DELETE_URI, mTagUri); |
| 186 | startService(delete); |
| 187 | finish(); |
| 188 | } |
| 189 | } else if (view == mDoneButton) { |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 190 | finish(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public void onStop() { |
| 196 | super.onStop(); |
| 197 | if (mMessagesToSave != null) { |
| 198 | saveMessages(mMessagesToSave); |
| 199 | } |
| 200 | } |
| 201 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 202 | /** |
| 203 | * Starts a service to asynchronously save the messages to the content provider. |
| 204 | */ |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 205 | void saveMessages(NdefMessage[] msgs) { |
| 206 | Intent save = new Intent(this, TagService.class); |
| 207 | save.putExtra(TagService.EXTRA_SAVE_MSGS, msgs); |
| 208 | startService(save); |
| 209 | } |
| 210 | |
| 211 | @Override |
| 212 | public boolean handleMessage(Message msg) { |
| 213 | finish(); |
| 214 | return true; |
| 215 | } |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame^] | 216 | |
| 217 | /** |
| 218 | * Loads a tag from the database, parses it, and builds the views |
| 219 | */ |
| 220 | final class LoadTagTask extends AsyncTask<Uri, Void, NdefMessage> { |
| 221 | @Override |
| 222 | public NdefMessage doInBackground(Uri... args) { |
| 223 | Cursor cursor = getContentResolver().query(args[0], new String[] { NdefMessages.BYTES }, |
| 224 | null, null, null); |
| 225 | try { |
| 226 | if (cursor.moveToFirst()) { |
| 227 | return new NdefMessage(cursor.getBlob(0)); |
| 228 | } |
| 229 | } catch (FormatException e) { |
| 230 | Log.e(TAG, "invalid tag format", e); |
| 231 | } finally { |
| 232 | if (cursor != null) cursor.close(); |
| 233 | } |
| 234 | return null; |
| 235 | } |
| 236 | |
| 237 | @Override |
| 238 | public void onPostExecute(NdefMessage msg) { |
| 239 | buildTagViews(new NdefMessage[] { msg }); |
| 240 | } |
| 241 | } |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 242 | } |