blob: 857052fb241c69565893a1fd342861696144dae0 [file] [log] [blame]
Ben Komalod47bc642010-10-18 22:15:27 -07001/*
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
17package com.android.apps.tag;
18
19import com.android.apps.tag.message.NdefMessageParser;
20import com.android.apps.tag.message.ParsedNdefMessage;
21import com.android.apps.tag.record.ParsedNdefRecord;
22import com.android.apps.tag.record.TextRecord;
Jeff Hamilton3bfa1d42010-10-22 15:59:42 -050023import com.google.common.collect.Lists;
Ben Komalod47bc642010-10-18 22:15:27 -070024
25import android.app.Activity;
26import android.nfc.NdefMessage;
Ben Komalo266f7e52010-10-19 13:34:03 -070027import android.nfc.NdefRecord;
Ben Komalod47bc642010-10-18 22:15:27 -070028import android.nfc.NfcAdapter;
29import android.os.Bundle;
30import android.util.Log;
31import android.view.View;
32import android.view.View.OnClickListener;
33import android.widget.CheckBox;
34import android.widget.EditText;
35import android.widget.Toast;
36
Ben Komalo9a984e52010-10-21 14:49:35 -070037import java.util.ArrayList;
Ben Komalod47bc642010-10-18 22:15:27 -070038import java.util.List;
Ben Komalo266f7e52010-10-19 13:34:03 -070039import java.util.Locale;
Ben Komalod47bc642010-10-18 22:15:27 -070040
41/**
42 * Editor {@link Activity} for the tag that can be programmed into the device.
43 */
Ben Komalo760fe582010-10-20 14:50:58 -070044public class MyTagActivity extends EditTagActivity implements OnClickListener {
Ben Komalod47bc642010-10-18 22:15:27 -070045
46 private static final String LOG_TAG = "TagEditor";
47
Ben Komalo266f7e52010-10-19 13:34:03 -070048 private EditText mTitleView;
49 private EditText mTextView;
50
Ben Komalod47bc642010-10-18 22:15:27 -070051 @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 Komalo266f7e52010-10-19 13:34:03 -070059
60 mTitleView = (EditText) findViewById(R.id.input_tag_title);
61 mTextView = (EditText) findViewById(R.id.input_tag_text);
Ben Komalod47bc642010-10-18 22:15:27 -070062 }
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 Komalo266f7e52010-10-19 13:34:03 -070085 mTitleView.setText(titleRecord.getText());
86 mTextView.setText(textRecord.getText());
Ben Komalod47bc642010-10-18 22:15:27 -070087 }
88
89 /**
90 * Persists content to store.
91 */
92 private void onSave() {
Ben Komalo266f7e52010-10-19 13:34:03 -070093 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 Komalo9a984e52010-10-21 14:49:35 -0700103 ArrayList<NdefRecord> values = Lists.newArrayList(
Ben Komalo266f7e52010-10-19 13:34:03 -0700104 TextRecord.newTextRecord(title, locale),
105 TextRecord.newTextRecord(text, locale)
Ben Komalo9a984e52010-10-21 14:49:35 -0700106 );
Ben Komalo266f7e52010-10-19 13:34:03 -0700107
Ben Komalo9a984e52010-10-21 14:49:35 -0700108 values.addAll(getValues());
Ben Komalo760fe582010-10-20 14:50:58 -0700109
Ben Komalo266f7e52010-10-19 13:34:03 -0700110 Log.d(LOG_TAG, "Writing local NdefMessage from tag app....");
Ben Komalo9a984e52010-10-21 14:49:35 -0700111 nfc.setLocalNdefMessage(new NdefMessage(values.toArray(new NdefRecord[values.size()])));
Ben Komalod47bc642010-10-18 22:15:27 -0700112 }
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 Komalo87141982010-10-21 12:17:14 -0700135 showAddContentDialog();
Ben Komalod47bc642010-10-18 22:15:27 -0700136 break;
137 }
138 }
Ben Komalo760fe582010-10-20 14:50:58 -0700139
Ben Komalod47bc642010-10-18 22:15:27 -0700140}