Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.device.nfc; | |
| 6 | |
| 7 import android.nfc.FormatException; | |
| 8 import android.nfc.NdefMessage; | |
| 9 import android.nfc.Tag; | |
| 10 import android.nfc.TagLostException; | |
| 11 import android.nfc.tech.Ndef; | |
| 12 import android.nfc.tech.NdefFormatable; | |
| 13 import android.nfc.tech.TagTechnology; | |
| 14 | |
| 15 import java.io.IOException; | |
| 16 | |
| 17 /** | |
| 18 * Utility class that provides I/O operations for NFC tags. | |
| 19 */ | |
| 20 public final class NfcTagHandler { | |
| 21 private final TagTechnology mTech; | |
| 22 private final TagTechnologyHandler mTechHandler; | |
| 23 private boolean mWasConnected; | |
| 24 | |
| 25 /** | |
| 26 * Factory method that creates NfcTagHandler for a given NFC Tag. | |
| 27 * | |
| 28 * @param tag @see android.nfc.Tag | |
| 29 * @return NfcTagHandler or null when unsupported Tag is provided. | |
| 30 */ | |
| 31 public static NfcTagHandler create(Tag tag) { | |
| 32 if (tag == null) return null; | |
| 33 | |
| 34 Ndef ndef = Ndef.get(tag); | |
| 35 if (ndef != null) return new NfcTagHandler(ndef, new NdefHandler(ndef)); | |
| 36 | |
| 37 NdefFormatable formattable = NdefFormatable.get(tag); | |
| 38 if (formattable != null) { | |
| 39 return new NfcTagHandler(formattable, new NdefFormattableHandler(for mattable)); | |
| 40 } | |
| 41 | |
| 42 return null; | |
| 43 } | |
| 44 | |
| 45 /** | |
| 46 * NdefFormatable and Ndef interfaces have different signatures for operatin g with NFC tags. | |
| 47 * This interface provides generic methods. | |
| 48 */ | |
| 49 private interface TagTechnologyHandler { | |
| 50 public void write(NdefMessage message) | |
| 51 throws IOException, TagLostException, FormatException; | |
| 52 // TODO(crbug.com/625589): add read method for nfc.watch. | |
| 53 } | |
| 54 | |
| 55 /** | |
| 56 * Implementation of TagTechnologyHandler that uses Ndef tag technology. | |
| 57 * @see android.nfc.tech.Ndef | |
| 58 */ | |
| 59 private static class NdefHandler implements TagTechnologyHandler { | |
| 60 private final Ndef mNdef; | |
| 61 | |
| 62 NdefHandler(Ndef ndef) { | |
| 63 mNdef = ndef; | |
| 64 } | |
| 65 | |
| 66 public void write(NdefMessage message) | |
| 67 throws IOException, TagLostException, FormatException { | |
| 68 mNdef.writeNdefMessage(message); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 /** | |
| 73 * Implementation of TagTechnologyHandler that uses NdefFormatable tag techn ology. | |
| 74 * @see android.nfc.tech.NdefFormatable | |
| 75 */ | |
| 76 private static class NdefFormattableHandler implements TagTechnologyHandler { | |
| 77 private final NdefFormatable mNdefFormattable; | |
| 78 | |
| 79 NdefFormattableHandler(NdefFormatable ndefFormattable) { | |
| 80 mNdefFormattable = ndefFormattable; | |
| 81 } | |
| 82 | |
| 83 public void write(NdefMessage message) | |
| 84 throws IOException, TagLostException, FormatException { | |
| 85 mNdefFormattable.format(message); | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 private NfcTagHandler(TagTechnology tech, TagTechnologyHandler handler) { | |
| 90 mTech = tech; | |
| 91 mTechHandler = handler; | |
| 92 } | |
| 93 | |
| 94 /** | |
| 95 * Connects to NFC tag. | |
| 96 */ | |
| 97 public void connect() throws IOException, TagLostException { | |
| 98 if (!mTech.isConnected()) { | |
| 99 mTech.connect(); | |
| 100 mWasConnected = true; | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 /** | |
| 105 * Closes connection. | |
| 106 */ | |
| 107 public void close() throws IOException { | |
| 108 mTech.close(); | |
| 109 } | |
| 110 | |
| 111 /** | |
| 112 * Writes NdefMessage to NFC tag. | |
| 113 */ | |
| 114 public void write(NdefMessage message) throws IOException, TagLostException, FormatException { | |
| 115 mTechHandler.write(message); | |
| 116 } | |
| 117 | |
| 118 /** | |
| 119 * If tag was previously connected and subsequent connection to the same tag fails, consider | |
| 120 * tag to be out of ragne. | |
|
dcheng
2016/08/16 20:50:08
Nit: range
| |
| 121 */ | |
| 122 public boolean isTagOutOfRange() { | |
| 123 try { | |
| 124 connect(); | |
| 125 } catch (IOException e) { | |
| 126 return mWasConnected; | |
| 127 } | |
| 128 return false; | |
| 129 } | |
| 130 } | |
| OLD | NEW |