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; |
Jason parks | 40d9a0c | 2010-10-26 14:42:45 -0500 | [diff] [blame] | 25 | import android.app.PendingIntent; |
Jason parks | c2bab52 | 2010-11-02 13:33:01 -0500 | [diff] [blame] | 26 | import android.content.BroadcastReceiver; |
Jeff Hamilton | a6f38c7 | 2010-12-17 16:16:21 -0600 | [diff] [blame] | 27 | import android.content.ContentUris; |
Jeff Hamilton | 383e6d1 | 2010-10-29 16:21:55 -0500 | [diff] [blame] | 28 | import android.content.Context; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 29 | import android.content.Intent; |
Jason parks | c2bab52 | 2010-11-02 13:33:01 -0500 | [diff] [blame] | 30 | import android.content.IntentFilter; |
Jeff Hamilton | a6f38c7 | 2010-12-17 16:16:21 -0600 | [diff] [blame] | 31 | import android.content.SharedPreferences; |
Ben Komalo | c27e0b7 | 2010-12-23 15:25:58 -0500 | [diff] [blame] | 32 | import android.content.res.AssetFileDescriptor; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 33 | import android.database.Cursor; |
Jeff Hamilton | f34e4d5 | 2010-10-21 17:42:09 -0500 | [diff] [blame] | 34 | import android.media.AudioManager; |
Ben Komalo | 9330d7c | 2010-10-19 14:16:49 -0700 | [diff] [blame] | 35 | import android.media.MediaPlayer; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 36 | import android.net.Uri; |
| 37 | import android.nfc.FormatException; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 38 | import android.nfc.NdefMessage; |
Jeff Hamilton | a29dc1b | 2010-10-28 17:44:37 -0500 | [diff] [blame] | 39 | import android.nfc.NdefRecord; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 40 | import android.nfc.NfcAdapter; |
Jeff Hamilton | a6f38c7 | 2010-12-17 16:16:21 -0600 | [diff] [blame] | 41 | import android.nfc.Tag; |
Jeff Hamilton | 42aa029 | 2011-01-21 13:30:53 -0600 | [diff] [blame] | 42 | import android.nfc.tech.Ndef; |
| 43 | import android.nfc.tech.NdefFormatable; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 44 | import android.os.AsyncTask; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 45 | import android.os.Bundle; |
Jeff Hamilton | a29dc1b | 2010-10-28 17:44:37 -0500 | [diff] [blame] | 46 | import android.os.Parcelable; |
Jeff Hamilton | 383e6d1 | 2010-10-29 16:21:55 -0500 | [diff] [blame] | 47 | import android.os.PowerManager; |
| 48 | import android.os.PowerManager.WakeLock; |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 49 | import android.text.format.DateUtils; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 50 | import android.util.Log; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 51 | import android.view.LayoutInflater; |
| 52 | import android.view.View; |
| 53 | import android.view.View.OnClickListener; |
| 54 | import android.view.WindowManager; |
| 55 | import android.widget.Button; |
| 56 | import android.widget.CheckBox; |
| 57 | import android.widget.ImageView; |
| 58 | import android.widget.LinearLayout; |
| 59 | import android.widget.TextView; |
Ben Komalo | 9299bbc | 2010-11-01 13:45:40 -0700 | [diff] [blame] | 60 | import android.widget.Toast; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 61 | |
Ben Komalo | c27e0b7 | 2010-12-23 15:25:58 -0500 | [diff] [blame] | 62 | import java.io.IOException; |
Jason parks | 40d9a0c | 2010-10-26 14:42:45 -0500 | [diff] [blame] | 63 | import java.util.List; |
Ben Komalo | 9330d7c | 2010-10-19 14:16:49 -0700 | [diff] [blame] | 64 | |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 65 | /** |
| 66 | * An {@link Activity} which handles a broadcast of a new tag that the device just discovered. |
| 67 | */ |
Jeff Hamilton | 52baa66 | 2010-10-22 01:42:44 -0500 | [diff] [blame] | 68 | public class TagViewer extends Activity implements OnClickListener { |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 69 | static final String TAG = "SaveTag"; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 70 | static final String EXTRA_TAG_DB_ID = "db_id"; |
| 71 | static final String EXTRA_MESSAGE = "msg"; |
Jason parks | 4a9765f | 2010-10-29 16:14:40 -0500 | [diff] [blame] | 72 | static final String EXTRA_KEEP_TITLE = "keepTitle"; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 73 | |
Jeff Hamilton | 39523c7 | 2010-11-03 19:37:58 -0500 | [diff] [blame] | 74 | static final boolean SHOW_OVER_LOCK_SCREEN = false; |
| 75 | |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 76 | /** This activity will finish itself in this amount of time if the user doesn't do anything. */ |
Jeff Hamilton | 383e6d1 | 2010-10-29 16:21:55 -0500 | [diff] [blame] | 77 | static final int ACTIVITY_TIMEOUT_MS = 7 * 1000; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 78 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 79 | Uri mTagUri; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 80 | ImageView mIcon; |
| 81 | TextView mTitle; |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 82 | TextView mDate; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 83 | CheckBox mStar; |
| 84 | Button mDeleteButton; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 85 | Button mDoneButton; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 86 | LinearLayout mTagContent; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 87 | |
Jason parks | c2bab52 | 2010-11-02 13:33:01 -0500 | [diff] [blame] | 88 | BroadcastReceiver mReceiver; |
| 89 | |
| 90 | private class ScreenOffReceiver extends BroadcastReceiver { |
| 91 | @Override |
| 92 | public void onReceive(Context context, Intent intent) { |
| 93 | if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) { |
| 94 | if (!isFinishing()) { |
| 95 | finish(); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 101 | @Override |
| 102 | protected void onCreate(Bundle savedInstanceState) { |
| 103 | super.onCreate(savedInstanceState); |
| 104 | |
Jeff Hamilton | 39523c7 | 2010-11-03 19:37:58 -0500 | [diff] [blame] | 105 | if (SHOW_OVER_LOCK_SCREEN) { |
| 106 | getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); |
| 107 | } |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 108 | |
| 109 | setContentView(R.layout.tag_viewer); |
| 110 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 111 | mTagContent = (LinearLayout) findViewById(R.id.list); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 112 | mTitle = (TextView) findViewById(R.id.title); |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 113 | mDate = (TextView) findViewById(R.id.date); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 114 | mIcon = (ImageView) findViewById(R.id.icon); |
| 115 | mStar = (CheckBox) findViewById(R.id.star); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 116 | mDeleteButton = (Button) findViewById(R.id.button_delete); |
| 117 | mDoneButton = (Button) findViewById(R.id.button_done); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 118 | |
| 119 | mDeleteButton.setOnClickListener(this); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 120 | mDoneButton.setOnClickListener(this); |
Jeff Hamilton | 52baa66 | 2010-10-22 01:42:44 -0500 | [diff] [blame] | 121 | mStar.setOnClickListener(this); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 122 | mIcon.setImageResource(R.drawable.ic_launcher_nfc); |
| 123 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 124 | resolveIntent(getIntent()); |
| 125 | } |
| 126 | |
Jason parks | 40d9a0c | 2010-10-26 14:42:45 -0500 | [diff] [blame] | 127 | @Override |
| 128 | public void onRestart() { |
| 129 | super.onRestart(); |
| 130 | if (mTagUri == null) { |
| 131 | // Someone how the user was fast enough to navigate away from the activity |
| 132 | // before the service was able to save the tag and call back onto this |
| 133 | // activity with the pending intent. Since we don't know what do display here |
| 134 | // just finish the activity. |
| 135 | finish(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | @Override |
| 140 | public void onStop() { |
| 141 | super.onStop(); |
| 142 | |
| 143 | PendingIntent pending = getPendingIntent(); |
| 144 | pending.cancel(); |
Jason parks | c2bab52 | 2010-11-02 13:33:01 -0500 | [diff] [blame] | 145 | |
| 146 | if (mReceiver != null) { |
| 147 | unregisterReceiver(mReceiver); |
| 148 | mReceiver = null; |
| 149 | } |
Jason parks | 40d9a0c | 2010-10-26 14:42:45 -0500 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | private PendingIntent getPendingIntent() { |
| 153 | Intent callback = new Intent(); |
| 154 | callback.setClass(this, TagViewer.class); |
| 155 | callback.setAction(Intent.ACTION_VIEW); |
| 156 | callback.setFlags(Intent. FLAG_ACTIVITY_CLEAR_TOP); |
Jason parks | 4a9765f | 2010-10-29 16:14:40 -0500 | [diff] [blame] | 157 | callback.putExtra(EXTRA_KEEP_TITLE, true); |
Jason parks | 40d9a0c | 2010-10-26 14:42:45 -0500 | [diff] [blame] | 158 | |
| 159 | return PendingIntent.getActivity(this, 0, callback, PendingIntent.FLAG_CANCEL_CURRENT); |
| 160 | } |
| 161 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 162 | void resolveIntent(Intent intent) { |
| 163 | // Parse the intent |
| 164 | String action = intent.getAction(); |
Jeff Hamilton | 80e0234 | 2010-12-03 17:34:31 -0600 | [diff] [blame] | 165 | if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action) |
Nick Pelly | 42d7414 | 2011-02-01 08:26:41 -0800 | [diff] [blame^] | 166 | || NfcAdapter.ACTION_TECH_DISCOVERED.equals(action)) { |
Jeff Hamilton | 39523c7 | 2010-11-03 19:37:58 -0500 | [diff] [blame] | 167 | if (SHOW_OVER_LOCK_SCREEN) { |
| 168 | // A tag was just scanned so poke the user activity wake lock to keep |
| 169 | // the screen on a bit longer in the event that the activity has |
| 170 | // hidden the lock screen. |
| 171 | PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); |
| 172 | WakeLock wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK, TAG); |
| 173 | // This lock CANNOT be manually released in onStop() since that may |
| 174 | // cause a lock under run exception to be thrown when the timeout |
| 175 | // hits. |
| 176 | wakeLock.acquire(ACTIVITY_TIMEOUT_MS); |
Jeff Hamilton | 383e6d1 | 2010-10-29 16:21:55 -0500 | [diff] [blame] | 177 | |
Jeff Hamilton | 39523c7 | 2010-11-03 19:37:58 -0500 | [diff] [blame] | 178 | if (mReceiver == null) { |
| 179 | mReceiver = new ScreenOffReceiver(); |
| 180 | IntentFilter filter = new IntentFilter(); |
| 181 | filter.addAction(Intent.ACTION_SCREEN_OFF); |
| 182 | registerReceiver(mReceiver, filter); |
| 183 | } |
Jason parks | c2bab52 | 2010-11-02 13:33:01 -0500 | [diff] [blame] | 184 | } |
| 185 | |
Jeff Hamilton | 383e6d1 | 2010-10-29 16:21:55 -0500 | [diff] [blame] | 186 | // When a tag is discovered we send it to the service to be save. We |
Jason parks | 40d9a0c | 2010-10-26 14:42:45 -0500 | [diff] [blame] | 187 | // include a PendingIntent for the service to call back onto. This |
| 188 | // will cause this activity to be restarted with onNewIntent(). At |
| 189 | // that time we read it from the database and view it. |
Jeff Hamilton | a29dc1b | 2010-10-28 17:44:37 -0500 | [diff] [blame] | 190 | Parcelable[] rawMsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES); |
| 191 | NdefMessage[] msgs; |
Jason parks | f38cc72 | 2010-11-02 22:50:09 -0500 | [diff] [blame] | 192 | if (rawMsgs != null && rawMsgs.length > 0) { |
Jeff Hamilton | a29dc1b | 2010-10-28 17:44:37 -0500 | [diff] [blame] | 193 | // stupid java, need to cast one-by-one |
| 194 | msgs = new NdefMessage[rawMsgs.length]; |
| 195 | for (int i=0; i<rawMsgs.length; i++) { |
| 196 | msgs[i] = (NdefMessage) rawMsgs[i]; |
| 197 | } |
| 198 | } else { |
| 199 | // Unknown tag type |
| 200 | byte[] empty = new byte[] {}; |
| 201 | NdefRecord record = new NdefRecord(NdefRecord.TNF_UNKNOWN, empty, empty, empty); |
| 202 | NdefMessage msg = new NdefMessage(new NdefRecord[] { record }); |
| 203 | msgs = new NdefMessage[] { msg }; |
| 204 | } |
| 205 | TagService.saveMessages(this, msgs, false, getPendingIntent()); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 206 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 207 | // Setup the views |
| 208 | setTitle(R.string.title_scanned_tag); |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 209 | mDate.setVisibility(View.GONE); |
Jeff Hamilton | 52baa66 | 2010-10-22 01:42:44 -0500 | [diff] [blame] | 210 | mStar.setChecked(false); |
| 211 | mStar.setEnabled(true); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 212 | |
Ben Komalo | 9330d7c | 2010-10-19 14:16:49 -0700 | [diff] [blame] | 213 | // Play notification. |
Ben Komalo | c27e0b7 | 2010-12-23 15:25:58 -0500 | [diff] [blame] | 214 | try { |
| 215 | AssetFileDescriptor afd = getResources().openRawResourceFd( |
| 216 | R.raw.discovered_tag_notification); |
| 217 | if (afd != null) { |
| 218 | MediaPlayer player = new MediaPlayer(); |
| 219 | player.setDataSource( |
| 220 | afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength()); |
| 221 | afd.close(); |
| 222 | player.setAudioStreamType(AudioManager.STREAM_NOTIFICATION); |
| 223 | player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { |
| 224 | @Override |
| 225 | public void onCompletion(MediaPlayer mp) { |
| 226 | mp.release(); |
| 227 | } |
| 228 | }); |
| 229 | player.prepare(); |
| 230 | player.start(); |
Jason parks | 7cce655 | 2010-11-04 17:11:10 -0500 | [diff] [blame] | 231 | } |
Ben Komalo | c27e0b7 | 2010-12-23 15:25:58 -0500 | [diff] [blame] | 232 | } catch (IOException ex) { |
| 233 | Log.d(TAG, "Unable to play sound for tag discovery", ex); |
| 234 | } catch (IllegalArgumentException ex) { |
| 235 | Log.d(TAG, "Unable to play sound for tag discovery", ex); |
| 236 | } catch (SecurityException ex) { |
| 237 | Log.d(TAG, "Unable to play sound for tag discovery", ex); |
| 238 | } |
Ben Komalo | 9330d7c | 2010-10-19 14:16:49 -0700 | [diff] [blame] | 239 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 240 | } else if (Intent.ACTION_VIEW.equals(action)) { |
| 241 | // Setup the views |
Jason parks | 4a9765f | 2010-10-29 16:14:40 -0500 | [diff] [blame] | 242 | if (!intent.getBooleanExtra(EXTRA_KEEP_TITLE, false)) { |
| 243 | setTitle(R.string.title_existing_tag); |
Ben Komalo | 81e7cec | 2010-11-01 13:42:23 -0700 | [diff] [blame] | 244 | mDate.setVisibility(View.VISIBLE); |
Jason parks | 4a9765f | 2010-10-29 16:14:40 -0500 | [diff] [blame] | 245 | } |
| 246 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 247 | mStar.setVisibility(View.VISIBLE); |
Jeff Hamilton | 52baa66 | 2010-10-22 01:42:44 -0500 | [diff] [blame] | 248 | mStar.setEnabled(false); // it's reenabled when the async load completes |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 249 | |
| 250 | // Read the tag from the database asynchronously |
| 251 | mTagUri = intent.getData(); |
| 252 | new LoadTagTask().execute(mTagUri); |
| 253 | } else { |
| 254 | Log.e(TAG, "Unknown intent " + intent); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 255 | finish(); |
| 256 | return; |
| 257 | } |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 258 | } |
| 259 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 260 | void buildTagViews(NdefMessage[] msgs) { |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 261 | if (msgs == null || msgs.length == 0) { |
| 262 | return; |
| 263 | } |
| 264 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 265 | LayoutInflater inflater = LayoutInflater.from(this); |
| 266 | LinearLayout content = mTagContent; |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 267 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 268 | // Clear out any old views in the content area, for example if you scan two tags in a row. |
| 269 | content.removeAllViews(); |
| 270 | |
| 271 | // Parse the first message in the list |
| 272 | //TODO figure out what to do when/if we support multiple messages per tag |
| 273 | ParsedNdefMessage parsedMsg = NdefMessageParser.parse(msgs[0]); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 274 | |
| 275 | // Build views for all of the sub records |
Jason parks | 40d9a0c | 2010-10-26 14:42:45 -0500 | [diff] [blame] | 276 | List<ParsedNdefRecord> records = parsedMsg.getRecords(); |
| 277 | final int size = records.size(); |
| 278 | |
| 279 | for (int i = 0 ; i < size ; i++) { |
| 280 | ParsedNdefRecord record = records.get(i); |
| 281 | content.addView(record.getView(this, inflater, content, i)); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 282 | inflater.inflate(R.layout.tag_divider, content, true); |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
| 286 | @Override |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 287 | public void onNewIntent(Intent intent) { |
Jason parks | 40d9a0c | 2010-10-26 14:42:45 -0500 | [diff] [blame] | 288 | setIntent(intent); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 289 | resolveIntent(intent); |
| 290 | } |
| 291 | |
| 292 | @Override |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 293 | public void setTitle(CharSequence title) { |
| 294 | mTitle.setText(title); |
| 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public void onClick(View view) { |
| 299 | if (view == mDeleteButton) { |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 300 | if (mTagUri == null) { |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 301 | finish(); |
| 302 | } else { |
| 303 | // The tag came from the database, start a service to delete it |
Jeff Hamilton | 52baa66 | 2010-10-22 01:42:44 -0500 | [diff] [blame] | 304 | TagService.delete(this, mTagUri); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 305 | finish(); |
| 306 | } |
Ben Komalo | 9299bbc | 2010-11-01 13:45:40 -0700 | [diff] [blame] | 307 | Toast.makeText(this, getResources().getString(R.string.tag_deleted), Toast.LENGTH_SHORT) |
| 308 | .show(); |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 309 | } else if (view == mDoneButton) { |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 310 | finish(); |
Jeff Hamilton | 52baa66 | 2010-10-22 01:42:44 -0500 | [diff] [blame] | 311 | } else if (view == mStar) { |
| 312 | if (mTagUri != null) { |
| 313 | TagService.setStar(this, mTagUri, mStar.isChecked()); |
| 314 | } |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 315 | } |
| 316 | } |
| 317 | |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 318 | interface ViewTagQuery { |
| 319 | final static String[] PROJECTION = new String[] { |
| 320 | NdefMessages.BYTES, // 0 |
| 321 | NdefMessages.STARRED, // 1 |
| 322 | NdefMessages.DATE, // 2 |
| 323 | }; |
| 324 | |
| 325 | static final int COLUMN_BYTES = 0; |
| 326 | static final int COLUMN_STARRED = 1; |
| 327 | static final int COLUMN_DATE = 2; |
| 328 | } |
| 329 | |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 330 | /** |
| 331 | * Loads a tag from the database, parses it, and builds the views |
| 332 | */ |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 333 | final class LoadTagTask extends AsyncTask<Uri, Void, Cursor> { |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 334 | @Override |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 335 | public Cursor doInBackground(Uri... args) { |
| 336 | Cursor cursor = getContentResolver().query(args[0], ViewTagQuery.PROJECTION, |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 337 | null, null, null); |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 338 | |
| 339 | // Ensure the cursor loads its window |
| 340 | if (cursor != null) cursor.getCount(); |
| 341 | return cursor; |
| 342 | } |
| 343 | |
| 344 | @Override |
| 345 | public void onPostExecute(Cursor cursor) { |
| 346 | NdefMessage msg = null; |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 347 | try { |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 348 | if (cursor != null && cursor.moveToFirst()) { |
| 349 | msg = new NdefMessage(cursor.getBlob(ViewTagQuery.COLUMN_BYTES)); |
| 350 | if (msg != null) { |
| 351 | mDate.setText(DateUtils.getRelativeTimeSpanString(TagViewer.this, |
| 352 | cursor.getLong(ViewTagQuery.COLUMN_DATE))); |
| 353 | mStar.setChecked(cursor.getInt(ViewTagQuery.COLUMN_STARRED) != 0); |
Jeff Hamilton | 52baa66 | 2010-10-22 01:42:44 -0500 | [diff] [blame] | 354 | mStar.setEnabled(true); |
Jeff Hamilton | f979f1c | 2010-10-18 18:06:53 -0500 | [diff] [blame] | 355 | buildTagViews(new NdefMessage[] { msg }); |
| 356 | } |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 357 | } |
| 358 | } catch (FormatException e) { |
| 359 | Log.e(TAG, "invalid tag format", e); |
| 360 | } finally { |
| 361 | if (cursor != null) cursor.close(); |
| 362 | } |
Jeff Hamilton | 0215c98 | 2010-10-18 17:27:21 -0500 | [diff] [blame] | 363 | } |
| 364 | } |
Jeff Hamilton | 7949534 | 2010-10-18 13:13:10 -0500 | [diff] [blame] | 365 | } |