2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.apps.tag;
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;
23 import com.google.common.collect.Lists;
25 import android.app.Activity;
26 import android.nfc.NdefMessage;
27 import android.nfc.NdefRecord;
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 java.util.ArrayList;
36 import java.util.List;
37 import java.util.Locale;
40 * Editor {@link Activity} for the tag that can be programmed into the device.
42 public class MyTagActivity extends EditTagActivity implements OnClickListener {
44 private static final String LOG_TAG = "TagEditor";
46 private EditText mTitleView;
47 private EditText mTextView;
48 private CheckBox mEnabled;
51 protected void onCreate(Bundle savedInstanceState) {
52 super.onCreate(savedInstanceState);
54 setContentView(R.layout.my_tag_activity);
56 findViewById(R.id.toggle_enabled_target).setOnClickListener(this);
57 findViewById(R.id.add_content_target).setOnClickListener(this);
59 mTitleView = (EditText) findViewById(R.id.input_tag_title);
60 mTextView = (EditText) findViewById(R.id.input_tag_text);
61 mEnabled = (CheckBox) findViewById(R.id.toggle_enabled_checkbox);
65 public void onStart() {
68 // Populate tag editor from stored tag info.
69 NdefMessage localMessage = NfcAdapter.getDefaultAdapter().getLocalNdefMessage();
70 if (localMessage == null) {
71 mEnabled.setChecked(false);
75 mEnabled.setChecked(true);
76 ParsedNdefMessage parsed = NdefMessageParser.parse(localMessage);
78 // There is always a "Title" and a "Text" record for the local message, if it exists.
79 List<ParsedNdefRecord> records = parsed.getRecords();
80 if (records.size() < 2) {
81 Log.w(LOG_TAG, "Local record not in expected format");
84 TextRecord titleRecord = (TextRecord) records.get(0);
85 TextRecord textRecord = (TextRecord) records.get(1);
87 mTitleView.setText(titleRecord.getText());
88 mTextView.setText(textRecord.getText());
92 * Persists content to store.
94 private void onSave() {
95 String title = mTitleView.getText().toString();
96 String text = mTextView.getText().toString();
97 NfcAdapter nfc = NfcAdapter.getDefaultAdapter();
99 if ((title.isEmpty() && text.isEmpty()) || !mEnabled.isChecked()) {
100 nfc.setLocalNdefMessage(null);
104 Locale locale = getResources().getConfiguration().locale;
105 ArrayList<NdefRecord> values = Lists.newArrayList(
106 TextRecord.newTextRecord(title, locale),
107 TextRecord.newTextRecord(text, locale)
110 values.addAll(getValues());
112 Log.d(LOG_TAG, "Writing local NdefMessage from tag app....");
113 nfc.setLocalNdefMessage(new NdefMessage(values.toArray(new NdefRecord[values.size()])));
117 public void onPause() {
123 public void onClick(View target) {
124 switch (target.getId()) {
125 case R.id.toggle_enabled_target:
126 boolean enabled = !mEnabled.isChecked();
127 mEnabled.setChecked(enabled);
129 // TODO: Persist to some store.
133 NfcAdapter.getDefaultAdapter().setLocalNdefMessage(null);
137 case R.id.add_content_target:
138 showAddContentDialog();