Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3543)

Unified Diff: device/nfc/android/java/src/org/chromium/device/nfc/NfcTagHandler.java

Issue 1486043002: [webnfc] Implement push method for Android nfc mojo service. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@step_6_add_mojo_service_CL
Patch Set: Move WindowAndroidChangedObserver to other observers in content layer Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: device/nfc/android/java/src/org/chromium/device/nfc/NfcTagHandler.java
diff --git a/device/nfc/android/java/src/org/chromium/device/nfc/NfcTagHandler.java b/device/nfc/android/java/src/org/chromium/device/nfc/NfcTagHandler.java
new file mode 100644
index 0000000000000000000000000000000000000000..a7adab7047e2b3b879f0a905dbd9050c965f2433
--- /dev/null
+++ b/device/nfc/android/java/src/org/chromium/device/nfc/NfcTagHandler.java
@@ -0,0 +1,130 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.device.nfc;
+
+import android.nfc.FormatException;
+import android.nfc.NdefMessage;
+import android.nfc.Tag;
+import android.nfc.TagLostException;
+import android.nfc.tech.Ndef;
+import android.nfc.tech.NdefFormatable;
+import android.nfc.tech.TagTechnology;
+
+import java.io.IOException;
+
+/**
+ * Utility class that provides I/O operations for NFC tags.
+ */
+public final class NfcTagHandler {
+ private final TagTechnology mTech;
+ private final TagTechnologyHandler mTechHandler;
+ private boolean mWasConnected;
+
+ /**
+ * Factory method that creates NfcTagHandler for a given NFC Tag.
+ *
+ * @param tag @see android.nfc.Tag
+ * @return NfcTagHandler or null when unsupported Tag is provided.
+ */
+ public static NfcTagHandler create(Tag tag) {
+ if (tag == null) return null;
+
+ Ndef ndef = Ndef.get(tag);
+ if (ndef != null) return new NfcTagHandler(ndef, new NdefHandler(ndef));
+
+ NdefFormatable formattable = NdefFormatable.get(tag);
+ if (formattable != null) {
+ return new NfcTagHandler(formattable, new NdefFormattableHandler(formattable));
+ }
+
+ return null;
+ }
+
+ /**
+ * NdefFormatable and Ndef interfaces have different signatures for operating with NFC tags.
+ * This interface provides generic methods.
+ */
+ private interface TagTechnologyHandler {
+ public void write(NdefMessage message)
+ throws IOException, TagLostException, FormatException;
+ // TODO(crbug.com/625589): add read method for nfc.watch.
+ }
+
+ /**
+ * Implementation of TagTechnologyHandler that uses Ndef tag technology.
+ * @see android.nfc.tech.Ndef
+ */
+ private static class NdefHandler implements TagTechnologyHandler {
+ private final Ndef mNdef;
+
+ NdefHandler(Ndef ndef) {
+ mNdef = ndef;
+ }
+
+ public void write(NdefMessage message)
+ throws IOException, TagLostException, FormatException {
+ mNdef.writeNdefMessage(message);
+ }
+ }
+
+ /**
+ * Implementation of TagTechnologyHandler that uses NdefFormatable tag technology.
+ * @see android.nfc.tech.NdefFormatable
+ */
+ private static class NdefFormattableHandler implements TagTechnologyHandler {
+ private final NdefFormatable mNdefFormattable;
+
+ NdefFormattableHandler(NdefFormatable ndefFormattable) {
+ mNdefFormattable = ndefFormattable;
+ }
+
+ public void write(NdefMessage message)
+ throws IOException, TagLostException, FormatException {
+ mNdefFormattable.format(message);
+ }
+ }
+
+ private NfcTagHandler(TagTechnology tech, TagTechnologyHandler handler) {
+ mTech = tech;
+ mTechHandler = handler;
+ }
+
+ /**
+ * Connects to NFC tag.
+ */
+ public void connect() throws IOException, TagLostException {
+ if (!mTech.isConnected()) {
+ mTech.connect();
+ mWasConnected = true;
+ }
+ }
+
+ /**
+ * Closes connection.
+ */
+ public void close() throws IOException {
+ mTech.close();
+ }
+
+ /**
+ * Writes NdefMessage to NFC tag.
+ */
+ public void write(NdefMessage message) throws IOException, TagLostException, FormatException {
+ mTechHandler.write(message);
+ }
+
+ /**
+ * If tag was previously connected and subsequent connection to the same tag fails, consider
+ * tag to be out of range.
+ */
+ public boolean isTagOutOfRange() {
+ try {
+ connect();
+ } catch (IOException e) {
+ return mWasConnected;
+ }
+ return false;
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698