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