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; |
(...skipping 30 matching lines...) Expand all Loading... |
41 | 41 |
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, IllegalSt
ateException; |
52 public NdefMessage read() throws IOException, TagLostException, FormatEx
ception; | 52 public NdefMessage read() |
| 53 throws IOException, TagLostException, FormatException, IllegalSt
ateException; |
53 } | 54 } |
54 | 55 |
55 /** | 56 /** |
56 * Implementation of TagTechnologyHandler that uses Ndef tag technology. | 57 * Implementation of TagTechnologyHandler that uses Ndef tag technology. |
57 * @see android.nfc.tech.Ndef | 58 * @see android.nfc.tech.Ndef |
58 */ | 59 */ |
59 private static class NdefHandler implements TagTechnologyHandler { | 60 private static class NdefHandler implements TagTechnologyHandler { |
60 private final Ndef mNdef; | 61 private final Ndef mNdef; |
61 | 62 |
62 NdefHandler(Ndef ndef) { | 63 NdefHandler(Ndef ndef) { |
63 mNdef = ndef; | 64 mNdef = ndef; |
64 } | 65 } |
65 | 66 |
66 @Override | 67 @Override |
67 public void write(NdefMessage message) | 68 public void write(NdefMessage message) |
68 throws IOException, TagLostException, FormatException { | 69 throws IOException, TagLostException, FormatException, IllegalSt
ateException { |
69 mNdef.writeNdefMessage(message); | 70 mNdef.writeNdefMessage(message); |
70 } | 71 } |
71 | 72 |
72 @Override | 73 @Override |
73 public NdefMessage read() throws IOException, TagLostException, FormatEx
ception { | 74 public NdefMessage read() |
| 75 throws IOException, TagLostException, FormatException, IllegalSt
ateException { |
74 return mNdef.getNdefMessage(); | 76 return mNdef.getNdefMessage(); |
75 } | 77 } |
76 } | 78 } |
77 | 79 |
78 /** | 80 /** |
79 * Implementation of TagTechnologyHandler that uses NdefFormatable tag techn
ology. | 81 * Implementation of TagTechnologyHandler that uses NdefFormatable tag techn
ology. |
80 * @see android.nfc.tech.NdefFormatable | 82 * @see android.nfc.tech.NdefFormatable |
81 */ | 83 */ |
82 private static class NdefFormattableHandler implements TagTechnologyHandler
{ | 84 private static class NdefFormattableHandler implements TagTechnologyHandler
{ |
83 private final NdefFormatable mNdefFormattable; | 85 private final NdefFormatable mNdefFormattable; |
84 | 86 |
85 NdefFormattableHandler(NdefFormatable ndefFormattable) { | 87 NdefFormattableHandler(NdefFormatable ndefFormattable) { |
86 mNdefFormattable = ndefFormattable; | 88 mNdefFormattable = ndefFormattable; |
87 } | 89 } |
88 | 90 |
89 @Override | 91 @Override |
90 public void write(NdefMessage message) | 92 public void write(NdefMessage message) |
91 throws IOException, TagLostException, FormatException { | 93 throws IOException, TagLostException, FormatException, IllegalSt
ateException { |
92 mNdefFormattable.format(message); | 94 mNdefFormattable.format(message); |
93 } | 95 } |
94 | 96 |
95 @Override | 97 @Override |
96 public NdefMessage read() throws IOException, TagLostException, FormatEx
ception { | 98 public NdefMessage read() throws FormatException { |
97 return NfcTypeConverter.emptyNdefMessage(); | 99 return NfcTypeConverter.emptyNdefMessage(); |
98 } | 100 } |
99 } | 101 } |
100 | 102 |
101 protected NfcTagHandler(TagTechnology tech, TagTechnologyHandler handler) { | 103 protected NfcTagHandler(TagTechnology tech, TagTechnologyHandler handler) { |
102 mTech = tech; | 104 mTech = tech; |
103 mTechHandler = handler; | 105 mTechHandler = handler; |
104 } | 106 } |
105 | 107 |
106 /** | 108 /** |
(...skipping 16 matching lines...) Expand all Loading... |
123 /** | 125 /** |
124 * Closes connection. | 126 * Closes connection. |
125 */ | 127 */ |
126 public void close() throws IOException { | 128 public void close() throws IOException { |
127 mTech.close(); | 129 mTech.close(); |
128 } | 130 } |
129 | 131 |
130 /** | 132 /** |
131 * Writes NdefMessage to NFC tag. | 133 * Writes NdefMessage to NFC tag. |
132 */ | 134 */ |
133 public void write(NdefMessage message) throws IOException, TagLostException,
FormatException { | 135 public void write(NdefMessage message) |
| 136 throws IOException, TagLostException, FormatException, IllegalStateE
xception { |
134 mTechHandler.write(message); | 137 mTechHandler.write(message); |
135 } | 138 } |
136 | 139 |
137 public NdefMessage read() throws IOException, TagLostException, FormatExcept
ion { | 140 public NdefMessage read() |
| 141 throws IOException, TagLostException, FormatException, IllegalStateE
xception { |
138 return mTechHandler.read(); | 142 return mTechHandler.read(); |
139 } | 143 } |
140 | 144 |
141 /** | 145 /** |
142 * If tag was previously connected and subsequent connection to the same tag
fails, consider | 146 * If tag was previously connected and subsequent connection to the same tag
fails, consider |
143 * tag to be out of range. | 147 * tag to be out of range. |
144 */ | 148 */ |
145 public boolean isTagOutOfRange() { | 149 public boolean isTagOutOfRange() { |
146 try { | 150 try { |
147 connect(); | 151 connect(); |
148 } catch (IOException e) { | 152 } catch (IOException e) { |
149 return mWasConnected; | 153 return mWasConnected; |
150 } | 154 } |
151 return false; | 155 return false; |
152 } | 156 } |
153 } | 157 } |
OLD | NEW |