Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [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; |
| 21 | import com.android.apps.tag.record.ParsedNdefRecord; |
| 22 | import com.android.apps.tag.record.TextRecord; |
Jeff Hamilton | 3bfa1d4 | 2010-10-22 15:59:42 -0500 | [diff] [blame^] | 23 | import com.google.common.collect.Lists; |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 24 | |
| 25 | import android.app.Activity; |
| 26 | import android.nfc.NdefMessage; |
Ben Komalo | 266f7e5 | 2010-10-19 13:34:03 -0700 | [diff] [blame] | 27 | import android.nfc.NdefRecord; |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 28 | import android.nfc.NfcAdapter; |
| 29 | import android.os.Bundle; |
| 30 | import android.util.Log; |
| 31 | import android.view.View; |
| 32 | import android.view.View.OnClickListener; |
| 33 | import android.widget.CheckBox; |
| 34 | import android.widget.EditText; |
| 35 | import android.widget.Toast; |
| 36 | |
Ben Komalo | 9a984e5 | 2010-10-21 14:49:35 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 38 | import java.util.List; |
Ben Komalo | 266f7e5 | 2010-10-19 13:34:03 -0700 | [diff] [blame] | 39 | import java.util.Locale; |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 40 | |
| 41 | /** |
| 42 | * Editor {@link Activity} for the tag that can be programmed into the device. |
| 43 | */ |
Ben Komalo | 760fe58 | 2010-10-20 14:50:58 -0700 | [diff] [blame] | 44 | public class MyTagActivity extends EditTagActivity implements OnClickListener { |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 45 | |
| 46 | private static final String LOG_TAG = "TagEditor"; |
| 47 | |
Ben Komalo | 266f7e5 | 2010-10-19 13:34:03 -0700 | [diff] [blame] | 48 | private EditText mTitleView; |
| 49 | private EditText mTextView; |
| 50 | |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 51 | @Override |
| 52 | protected void onCreate(Bundle savedInstanceState) { |
| 53 | super.onCreate(savedInstanceState); |
| 54 | |
| 55 | setContentView(R.layout.my_tag_activity); |
| 56 | |
| 57 | findViewById(R.id.toggle_enabled_target).setOnClickListener(this); |
| 58 | findViewById(R.id.add_content_target).setOnClickListener(this); |
Ben Komalo | 266f7e5 | 2010-10-19 13:34:03 -0700 | [diff] [blame] | 59 | |
| 60 | mTitleView = (EditText) findViewById(R.id.input_tag_title); |
| 61 | mTextView = (EditText) findViewById(R.id.input_tag_text); |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
| 65 | public void onStart() { |
| 66 | super.onStart(); |
| 67 | |
| 68 | // Populate tag editor from stored tag info. |
| 69 | NdefMessage localMessage = NfcAdapter.getDefaultAdapter().getLocalNdefMessage(); |
| 70 | if (localMessage == null) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | ParsedNdefMessage parsed = NdefMessageParser.parse(localMessage); |
| 75 | |
| 76 | // There is always a "Title" and a "Text" record for the local message, if it exists. |
| 77 | List<ParsedNdefRecord> records = parsed.getRecords(); |
| 78 | if (records.size() < 2) { |
| 79 | Log.w(LOG_TAG, "Local record not in expected format"); |
| 80 | return; |
| 81 | } |
| 82 | TextRecord titleRecord = (TextRecord) records.get(0); |
| 83 | TextRecord textRecord = (TextRecord) records.get(1); |
| 84 | |
Ben Komalo | 266f7e5 | 2010-10-19 13:34:03 -0700 | [diff] [blame] | 85 | mTitleView.setText(titleRecord.getText()); |
| 86 | mTextView.setText(textRecord.getText()); |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Persists content to store. |
| 91 | */ |
| 92 | private void onSave() { |
Ben Komalo | 266f7e5 | 2010-10-19 13:34:03 -0700 | [diff] [blame] | 93 | String title = mTitleView.getText().toString(); |
| 94 | String text = mTextView.getText().toString(); |
| 95 | NfcAdapter nfc = NfcAdapter.getDefaultAdapter(); |
| 96 | |
| 97 | if (title.isEmpty() && text.isEmpty()) { |
| 98 | nfc.setLocalNdefMessage(null); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | Locale locale = getResources().getConfiguration().locale; |
Ben Komalo | 9a984e5 | 2010-10-21 14:49:35 -0700 | [diff] [blame] | 103 | ArrayList<NdefRecord> values = Lists.newArrayList( |
Ben Komalo | 266f7e5 | 2010-10-19 13:34:03 -0700 | [diff] [blame] | 104 | TextRecord.newTextRecord(title, locale), |
| 105 | TextRecord.newTextRecord(text, locale) |
Ben Komalo | 9a984e5 | 2010-10-21 14:49:35 -0700 | [diff] [blame] | 106 | ); |
Ben Komalo | 266f7e5 | 2010-10-19 13:34:03 -0700 | [diff] [blame] | 107 | |
Ben Komalo | 9a984e5 | 2010-10-21 14:49:35 -0700 | [diff] [blame] | 108 | values.addAll(getValues()); |
Ben Komalo | 760fe58 | 2010-10-20 14:50:58 -0700 | [diff] [blame] | 109 | |
Ben Komalo | 266f7e5 | 2010-10-19 13:34:03 -0700 | [diff] [blame] | 110 | Log.d(LOG_TAG, "Writing local NdefMessage from tag app...."); |
Ben Komalo | 9a984e5 | 2010-10-21 14:49:35 -0700 | [diff] [blame] | 111 | nfc.setLocalNdefMessage(new NdefMessage(values.toArray(new NdefRecord[values.size()]))); |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public void onPause() { |
| 116 | super.onPause(); |
| 117 | onSave(); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void onClick(View target) { |
| 122 | switch (target.getId()) { |
| 123 | case R.id.toggle_enabled_target: |
| 124 | CheckBox checkbox = ((CheckBox) target.findViewById(R.id.toggle_enabled_checkbox)); |
| 125 | boolean enabled = !checkbox.isChecked(); |
| 126 | checkbox.setChecked(enabled); |
| 127 | |
| 128 | // TODO: Persist to some store. |
| 129 | Toast.makeText(this, |
| 130 | "Just " + (enabled ? "enabled" : "disabled") + " NFC MyTag", |
| 131 | Toast.LENGTH_SHORT).show(); |
| 132 | break; |
| 133 | |
| 134 | case R.id.add_content_target: |
Ben Komalo | 8714198 | 2010-10-21 12:17:14 -0700 | [diff] [blame] | 135 | showAddContentDialog(); |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 136 | break; |
| 137 | } |
| 138 | } |
Ben Komalo | 760fe58 | 2010-10-20 14:50:58 -0700 | [diff] [blame] | 139 | |
Ben Komalo | d47bc64 | 2010-10-18 22:15:27 -0700 | [diff] [blame] | 140 | } |