diff options
author | Jason Parks <jparks@google.com> | 2010-11-02 19:50:06 -0700 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2010-11-02 19:50:06 -0700 |
commit | 0c4c3a3e9db3cea445937fbe739df2334a143240 (patch) | |
tree | d13cb2e170e9739e43d6e54cd207d86474262e4b /src | |
parent | 07b51eedea198c6aca4f378120d5edfdfb2e4cf5 (diff) | |
parent | f8b4d49045f6e4693ff992def65b1fb146534ca1 (diff) | |
download | android_packages_apps_Tag-0c4c3a3e9db3cea445937fbe739df2334a143240.tar.gz android_packages_apps_Tag-0c4c3a3e9db3cea445937fbe739df2334a143240.tar.bz2 android_packages_apps_Tag-0c4c3a3e9db3cea445937fbe739df2334a143240.zip |
Merge "Kill MyTagActivity and merge it into EditTagActivity. (making way for a new MyTagActivity that is the list and manager for MyTag)" into gingerbread
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/apps/tag/EditTagActivity.java | 206 | ||||
-rw-r--r-- | src/com/android/apps/tag/MyTagActivity.java | 233 | ||||
-rw-r--r-- | src/com/android/apps/tag/TagBrowserActivity.java | 2 |
3 files changed, 196 insertions, 245 deletions
diff --git a/src/com/android/apps/tag/EditTagActivity.java b/src/com/android/apps/tag/EditTagActivity.java index 50c4df9..b6d9cde 100644 --- a/src/com/android/apps/tag/EditTagActivity.java +++ b/src/com/android/apps/tag/EditTagActivity.java @@ -16,12 +16,15 @@ package com.android.apps.tag; +import com.android.apps.tag.message.NdefMessageParser; +import com.android.apps.tag.message.ParsedNdefMessage; import com.android.apps.tag.record.ImageRecord; import com.android.apps.tag.record.ParsedNdefRecord; import com.android.apps.tag.record.RecordEditInfo; -import com.android.apps.tag.record.RecordEditInfo.EditCallbacks; +import com.android.apps.tag.record.TextRecord; import com.android.apps.tag.record.UriRecord; import com.android.apps.tag.record.VCardRecord; +import com.android.apps.tag.record.RecordEditInfo.EditCallbacks; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Lists; @@ -29,14 +32,26 @@ import com.google.common.collect.Lists; import android.app.Activity; import android.app.Dialog; import android.content.Intent; +import android.net.Uri; import android.nfc.NdefMessage; import android.nfc.NdefRecord; +import android.nfc.NfcAdapter; import android.os.Bundle; +import android.util.Log; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuItem; import android.view.View; -import android.view.View.OnClickListener; import android.view.ViewGroup; +import android.view.View.OnClickListener; +import android.widget.CheckBox; +import android.widget.EditText; + +import java.net.MalformedURLException; +import java.net.URL; import java.util.ArrayList; +import java.util.List; +import java.util.Locale; import java.util.Set; /** @@ -46,10 +61,11 @@ import java.util.Set; * {@link ParsedNdefRecord} types. Each type of {@link ParsedNdefRecord} can build views to * pick/select a new piece of content, or edit an existing content for the {@link NdefMessage}. */ -public abstract class EditTagActivity extends Activity implements OnClickListener, EditCallbacks { +public class EditTagActivity extends Activity implements OnClickListener, EditCallbacks { private static final String BUNDLE_KEY_OUTSTANDING_PICK = "outstanding-pick"; protected static final int DIALOG_ID_ADD_CONTENT = 0; + private static final String LOG_TAG = "Tags"; private static final Set<String> SUPPORTED_RECORD_TYPES = ImmutableSet.of( ImageRecord.RECORD_TYPE, @@ -72,23 +88,35 @@ public abstract class EditTagActivity extends Activity implements OnClickListene */ private RecordEditInfo mRecordWithOutstandingPick; + /** + * Whether or not data was already parsed from an {@link Intent}. This happens when the user + * shares data via the My tag feature. + */ + private boolean mParsedIntent = false; + + private EditText mTextView; + private CheckBox mEnabled; + private LayoutInflater mInflater; @Override protected void onCreate(Bundle savedState) { super.onCreate(savedState); + setContentView(R.layout.edit_tag_activity); if (savedState != null) { mRecordWithOutstandingPick = savedState.getParcelable(BUNDLE_KEY_OUTSTANDING_PICK); } mInflater = LayoutInflater.from(this); - } - protected ViewGroup getContentRoot() { - if (mContentRoot == null) { - mContentRoot = (ViewGroup) findViewById(R.id.content_parent); - } - return mContentRoot; + findViewById(R.id.toggle_enabled_target).setOnClickListener(this); + findViewById(R.id.add_content_target).setOnClickListener(this); + + mTextView = (EditText) findViewById(R.id.input_tag_text); + mEnabled = (CheckBox) findViewById(R.id.toggle_enabled_checkbox); + mContentRoot = (ViewGroup) findViewById(R.id.content_parent); + + populateEditor(); } /** @@ -137,14 +165,14 @@ public abstract class EditTagActivity extends Activity implements OnClickListene * Adds a child editor view for a record. */ public void addViewForRecord(RecordEditInfo editInfo) { - ViewGroup root = getContentRoot(); + ViewGroup root = mContentRoot; View editView = editInfo.getEditView(this, mInflater, root, this); root.addView(mInflater.inflate(R.layout.tag_divider, root, false)); root.addView(editView); } protected void rebuildChildViews() { - ViewGroup root = getContentRoot(); + ViewGroup root = mContentRoot; root.removeAllViews(); for (RecordEditInfo editInfo : mRecords) { addViewForRecord(editInfo); @@ -247,4 +275,160 @@ public abstract class EditTagActivity extends Activity implements OnClickListene outState.putParcelable(BUNDLE_KEY_OUTSTANDING_PICK, mRecordWithOutstandingPick); } } + + private void populateEditor() { + NdefMessage localMessage = NfcAdapter.getDefaultAdapter().getLocalNdefMessage(); + + if (Intent.ACTION_SEND.equals(getIntent().getAction()) && !mParsedIntent) { + if (localMessage != null) { + // TODO: prompt user for confirmation about wiping their old tag. + } + + if (buildFromIntent(getIntent())) { + return; + } + + mParsedIntent = true; + + } else if (localMessage == null) { + mEnabled.setChecked(false); + return; + + } else { + // Locally stored message. + ParsedNdefMessage parsed = NdefMessageParser.parse(localMessage); + List<ParsedNdefRecord> records = parsed.getRecords(); + + // There is always a "Text" record for a My Tag. + if (records.size() < 1) { + Log.w(LOG_TAG, "Local record not in expected format"); + return; + } + mEnabled.setChecked(true); + mTextView.setText(((TextRecord) records.get(0)).getText()); + + mRecords.clear(); + for (int i = 1, len = records.size(); i < len; i++) { + RecordEditInfo editInfo = records.get(i).getEditInfo(this); + if (editInfo != null) { + addRecord(editInfo); + } + } + rebuildChildViews(); + } + } + + /** + * Populates the editor from extras in a given {@link Intent} + * @param intent the {@link Intent} to parse. + * @return whether or not the {@link Intent} could be handled. + */ + private boolean buildFromIntent(final Intent intent) { + String type = intent.getType(); + + if ("text/plain".equals(type)) { + String title = getIntent().getStringExtra(Intent.EXTRA_SUBJECT); + mTextView.setText((title == null) ? "" : title); + + String text = getIntent().getStringExtra(Intent.EXTRA_TEXT); + try { + URL parsed = new URL(text); + + // Valid URL. + mTextView.setText(""); + mRecords.add(new UriRecord.UriRecordEditInfo(text)); + rebuildChildViews(); + + } catch (MalformedURLException ex) { + // Ignore. Just treat as plain text. + mTextView.setText((text == null) ? "" : text); + } + + mEnabled.setChecked(true); + onSave(); + return true; + } else if ("text/x-vcard".equals(type)) { + Uri stream = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM); + if (stream != null) { + RecordEditInfo editInfo = VCardRecord.editInfoForUri(stream); + if (editInfo != null) { + mRecords.add(editInfo); + rebuildChildViews(); + onSave(); + return true; + } + } + } + + // TODO: handle vcards and images. + return false; + } + + /** + * Persists content to store. + */ + private void onSave() { + String text = mTextView.getText().toString(); + NfcAdapter nfc = NfcAdapter.getDefaultAdapter(); + + if (!mEnabled.isChecked()) { + nfc.setLocalNdefMessage(null); + return; + } + + Locale locale = getResources().getConfiguration().locale; + ArrayList<NdefRecord> values = Lists.newArrayList( + TextRecord.newTextRecord(text, locale) + ); + + values.addAll(getValues()); + + Log.d(LOG_TAG, "Writing local NdefMessage from tag app...."); + nfc.setLocalNdefMessage(new NdefMessage(values.toArray(new NdefRecord[values.size()]))); + } + + @Override + public void onPause() { + super.onPause(); + onSave(); + } + + @Override + public void onClick(View target) { + switch (target.getId()) { + case R.id.toggle_enabled_target: + boolean enabled = !mEnabled.isChecked(); + mEnabled.setChecked(enabled); + + // TODO: Persist to some store. + if (enabled) { + onSave(); + } else { + NfcAdapter.getDefaultAdapter().setLocalNdefMessage(null); + } + break; + + case R.id.add_content_target: + showAddContentDialog(); + break; + } + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.menu, menu); + return true; + } + + @Override + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case R.id.help: + HelpUtils.openHelp(this); + return true; + + default: + return super.onOptionsItemSelected(item); + } + } } diff --git a/src/com/android/apps/tag/MyTagActivity.java b/src/com/android/apps/tag/MyTagActivity.java deleted file mode 100644 index 96bf0d2..0000000 --- a/src/com/android/apps/tag/MyTagActivity.java +++ /dev/null @@ -1,233 +0,0 @@ -/* - * Copyright (C) 2010 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.apps.tag; - -import com.android.apps.tag.message.NdefMessageParser; -import com.android.apps.tag.message.ParsedNdefMessage; -import com.android.apps.tag.record.ParsedNdefRecord; -import com.android.apps.tag.record.RecordEditInfo; -import com.android.apps.tag.record.TextRecord; -import com.android.apps.tag.record.UriRecord; -import com.android.apps.tag.record.VCardRecord; -import com.google.common.collect.Lists; - -import android.app.Activity; -import android.content.Intent; -import android.net.Uri; -import android.nfc.NdefMessage; -import android.nfc.NdefRecord; -import android.nfc.NfcAdapter; -import android.os.Bundle; -import android.util.Log; -import android.view.Menu; -import android.view.MenuItem; -import android.view.View; -import android.view.View.OnClickListener; -import android.widget.CheckBox; -import android.widget.EditText; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.ArrayList; -import java.util.List; -import java.util.Locale; - -/** - * Editor {@link Activity} for the tag that can be programmed into the device. - */ -public class MyTagActivity extends EditTagActivity implements OnClickListener { - - private static final String LOG_TAG = "TagEditor"; - - private EditText mTextView; - private CheckBox mEnabled; - - /** - * Whether or not data was already parsed from an {@link Intent}. This happens when the user - * shares data via the My tag feature. - */ - private boolean mParsedIntent = false; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - setContentView(R.layout.my_tag_activity); - - findViewById(R.id.toggle_enabled_target).setOnClickListener(this); - findViewById(R.id.add_content_target).setOnClickListener(this); - - mTextView = (EditText) findViewById(R.id.input_tag_text); - mEnabled = (CheckBox) findViewById(R.id.toggle_enabled_checkbox); - - populateEditor(); - } - - private void populateEditor() { - NdefMessage localMessage = NfcAdapter.getDefaultAdapter().getLocalNdefMessage(); - - if (Intent.ACTION_SEND.equals(getIntent().getAction()) && !mParsedIntent) { - if (localMessage != null) { - // TODO: prompt user for confirmation about wiping their old tag. - } - - if (buildFromIntent(getIntent())) { - return; - } - - mParsedIntent = true; - - } else if (localMessage == null) { - mEnabled.setChecked(false); - return; - - } else { - // Locally stored message. - ParsedNdefMessage parsed = NdefMessageParser.parse(localMessage); - List<ParsedNdefRecord> records = parsed.getRecords(); - - // There is always a "Text" record for a My Tag. - if (records.size() < 1) { - Log.w(LOG_TAG, "Local record not in expected format"); - return; - } - mEnabled.setChecked(true); - mTextView.setText(((TextRecord) records.get(0)).getText()); - - mRecords.clear(); - for (int i = 1, len = records.size(); i < len; i++) { - RecordEditInfo editInfo = records.get(i).getEditInfo(this); - if (editInfo != null) { - addRecord(editInfo); - } - } - rebuildChildViews(); - } - } - - /** - * Populates the editor from extras in a given {@link Intent} - * @param intent the {@link Intent} to parse. - * @return whether or not the {@link Intent} could be handled. - */ - private boolean buildFromIntent(final Intent intent) { - String type = intent.getType(); - - if ("text/plain".equals(type)) { - String title = getIntent().getStringExtra(Intent.EXTRA_SUBJECT); - mTextView.setText((title == null) ? "" : title); - - String text = getIntent().getStringExtra(Intent.EXTRA_TEXT); - try { - URL parsed = new URL(text); - - // Valid URL. - mTextView.setText(""); - mRecords.add(new UriRecord.UriRecordEditInfo(text)); - rebuildChildViews(); - - } catch (MalformedURLException ex) { - // Ignore. Just treat as plain text. - mTextView.setText((text == null) ? "" : text); - } - - mEnabled.setChecked(true); - onSave(); - return true; - - } else if ("text/x-vcard".equals(type)) { - Uri stream = (Uri) getIntent().getParcelableExtra(Intent.EXTRA_STREAM); - if (stream != null) { - RecordEditInfo editInfo = VCardRecord.editInfoForUri(stream); - if (editInfo != null) { - mRecords.add(editInfo); - rebuildChildViews(); - } - } - } - // TODO: handle images. - return false; - } - - /** - * Persists content to store. - */ - private void onSave() { - String text = mTextView.getText().toString(); - NfcAdapter nfc = NfcAdapter.getDefaultAdapter(); - - if (!mEnabled.isChecked()) { - nfc.setLocalNdefMessage(null); - return; - } - - Locale locale = getResources().getConfiguration().locale; - ArrayList<NdefRecord> values = Lists.newArrayList( - TextRecord.newTextRecord(text, locale) - ); - - values.addAll(getValues()); - - Log.d(LOG_TAG, "Writing local NdefMessage from tag app...."); - nfc.setLocalNdefMessage(new NdefMessage(values.toArray(new NdefRecord[values.size()]))); - } - - @Override - public void onPause() { - super.onPause(); - onSave(); - } - - @Override - public void onClick(View target) { - switch (target.getId()) { - case R.id.toggle_enabled_target: - boolean enabled = !mEnabled.isChecked(); - mEnabled.setChecked(enabled); - - // TODO: Persist to some store. - if (enabled) { - onSave(); - } else { - NfcAdapter.getDefaultAdapter().setLocalNdefMessage(null); - } - break; - - case R.id.add_content_target: - showAddContentDialog(); - break; - } - } - - @Override - public boolean onCreateOptionsMenu(Menu menu) { - getMenuInflater().inflate(R.menu.menu, menu); - return true; - } - - @Override - public boolean onOptionsItemSelected(MenuItem item) { - switch (item.getItemId()) { - case R.id.help: - HelpUtils.openHelp(this); - return true; - - default: - return super.onOptionsItemSelected(item); - } - } -} diff --git a/src/com/android/apps/tag/TagBrowserActivity.java b/src/com/android/apps/tag/TagBrowserActivity.java index 6df1567..f0e8bda 100644 --- a/src/com/android/apps/tag/TagBrowserActivity.java +++ b/src/com/android/apps/tag/TagBrowserActivity.java @@ -63,7 +63,7 @@ public class TagBrowserActivity extends TabActivity implements DialogInterface.O tabHost.addTab(tabHost.newTabSpec("mytag") .setIndicator(getText(R.string.tab_my_tag), res.getDrawable(R.drawable.ic_tab_my_tag)) - .setContent(new Intent().setClass(this, MyTagActivity.class))); + .setContent(new Intent().setClass(this, EditTagActivity.class))); SharedPreferences preferences = getPreferences(Context.MODE_PRIVATE); if (!preferences.getBoolean(PREF_KEY_SHOW_INTRO, false)) { |