Chromium Code Reviews| Index: device/nfc/android/java/src/org/chromium/device/nfc/NfcImpl.java |
| diff --git a/device/nfc/android/java/src/org/chromium/device/nfc/NfcImpl.java b/device/nfc/android/java/src/org/chromium/device/nfc/NfcImpl.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..1c93b114255f8be4c6513c622a71f3356584b1e1 |
| --- /dev/null |
| +++ b/device/nfc/android/java/src/org/chromium/device/nfc/NfcImpl.java |
| @@ -0,0 +1,411 @@ |
| +// Copyright 2015 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.Manifest; |
| +import android.annotation.TargetApi; |
| +import android.app.Activity; |
| +import android.content.Context; |
| +import android.content.pm.PackageManager; |
| +import android.nfc.FormatException; |
| +import android.nfc.NdefMessage; |
| +import android.nfc.NdefRecord; |
| +import android.nfc.NfcAdapter; |
| +import android.nfc.NfcAdapter.ReaderCallback; |
| +import android.nfc.NfcManager; |
| +import android.nfc.Tag; |
| +import android.nfc.TagLostException; |
| +import android.nfc.tech.Ndef; |
| +import android.nfc.tech.NdefFormatable; |
| +import android.nfc.tech.TagTechnology; |
| +import android.os.Build; |
| +import android.os.Process; |
| +import android.support.annotation.Nullable; |
| + |
| +import org.chromium.base.ApplicationStatus; |
| +import org.chromium.base.Log; |
| +import org.chromium.mojo.bindings.Callbacks; |
| +import org.chromium.mojo.system.MojoException; |
| +import org.chromium.mojom.device.Nfc; |
| +import org.chromium.mojom.device.NfcClient; |
| +import org.chromium.mojom.device.NfcError; |
| +import org.chromium.mojom.device.NfcErrorType; |
| +import org.chromium.mojom.device.NfcMessage; |
| +import org.chromium.mojom.device.NfcPushOptions; |
| +import org.chromium.mojom.device.NfcPushTarget; |
| +import org.chromium.mojom.device.NfcRecord; |
| +import org.chromium.mojom.device.NfcRecordType; |
| +import org.chromium.mojom.device.NfcWatchOptions; |
| + |
| +import java.io.IOException; |
| +import java.io.UnsupportedEncodingException; |
| +import java.util.ArrayList; |
| +import java.util.List; |
| + |
| +/** |
| + * Android implementation of the NFC mojo service defined in |
| + * device/nfc/nfc.mojom. |
| + */ |
| +public class NfcImpl implements Nfc { |
| + private static final String TAG = "NfcImpl"; |
| + private static final String DOMAIN = "w3.org"; |
| + private static final String TYPE = "webnfc"; |
| + private static final String TEXT_MIME = "text/plain"; |
| + private static final String CHARSET_UTF8 = ";charset=UTF-8"; |
| + private static final String CHARSET_UTF16 = ";charset=UTF-16"; |
| + |
| + private NfcManager mNfcManager; |
| + private NfcAdapter mNfcAdapter; |
| + private final Context mContext; |
| + private Activity mActivity; |
| + private boolean mHasPermission; |
| + private ReaderCallbackHandler mReaderCallbackHandler; |
| + private PendingPushOperation mPendingPushOperation; |
| + private NfcTagWriter mTagWriter; |
| + |
| + public NfcImpl(Context context) { |
| + mContext = context; |
| + int permission = |
| + context.checkPermission(Manifest.permission.NFC, Process.myPid(), Process.myUid()); |
| + mHasPermission = permission == PackageManager.PERMISSION_GRANTED; |
| + if (mHasPermission) { |
| + mActivity = ApplicationStatus.getLastTrackedFocusedActivity(); |
| + mNfcManager = (NfcManager) mContext.getSystemService(Context.NFC_SERVICE); |
| + if (mNfcManager == null) { |
| + Log.w(TAG, "NFC is not supported."); |
| + } else { |
| + mNfcAdapter = mNfcManager.getDefaultAdapter(); |
| + } |
| + } else { |
| + Log.w(TAG, "NFC operations are not permitted."); |
| + } |
| + } |
| + |
| + // Public methods |
| + @Override |
| + public void setClient(NfcClient client) {} |
| + |
| + @Override |
| + public void push(NfcMessage message, NfcPushOptions options, PushResponse callback) { |
| + if (!checkIfReady(callback)) return; |
| + |
| + if (options.target == NfcPushTarget.PEER) { |
| + callback.call(createError(NfcErrorType.NOT_SUPPORTED)); |
| + return; |
| + } |
| + |
| + if (mPendingPushOperation != null) { |
| + mPendingPushOperation.complete(createError(NfcErrorType.OPERATION_CANCELLED)); |
| + } |
| + |
| + mPendingPushOperation = new PendingPushOperation(message, options, callback); |
| + enableReaderMode(); |
| + processPendingPushOperation(); |
| + } |
| + |
| + @Override |
| + public void cancelPush(int target, CancelPushResponse callback) { |
| + if (!checkIfReady(callback)) return; |
| + |
| + if (target == NfcPushTarget.PEER) { |
| + callback.call(createError(NfcErrorType.NOT_SUPPORTED)); |
| + return; |
| + } |
| + |
| + if (mPendingPushOperation != null) { |
| + mPendingPushOperation.complete(createError(NfcErrorType.OPERATION_CANCELLED)); |
| + mPendingPushOperation = null; |
| + callback.call(null); |
| + } else { |
| + callback.call(createError(NfcErrorType.NOT_FOUND)); |
| + } |
| + } |
| + |
| + @Override |
| + public void watch(NfcWatchOptions options, WatchResponse callback) { |
| + if (!checkIfReady(callback)) return; |
| + } |
| + |
| + @Override |
| + public void cancelWatch(int id, CancelWatchResponse callback) { |
| + if (!checkIfReady(callback)) return; |
| + } |
| + |
| + @Override |
| + public void cancelAllWatches(CancelAllWatchesResponse callback) { |
| + if (!checkIfReady(callback)) return; |
| + } |
| + |
| + @Override |
| + public void suspendNfcOperations() {} |
| + |
| + @Override |
| + public void resumeNfcOperations() {} |
| + |
| + @Override |
| + public void close() {} |
| + |
| + @Override |
| + public void onConnectionError(MojoException e) {} |
| + |
| + // Implementation |
| + private static class PendingPushOperation { |
| + private final NfcMessage mNfcMessage; |
| + private final NfcPushOptions mNfcPushOptions; |
| + private final PushResponse mPushResponseCallback; |
| + |
| + public PendingPushOperation( |
| + NfcMessage message, NfcPushOptions options, PushResponse callback) { |
| + mNfcMessage = message; |
| + mNfcPushOptions = options; |
| + mPushResponseCallback = callback; |
| + } |
| + |
| + public void complete(NfcError error) { |
| + if (mPushResponseCallback != null) mPushResponseCallback.call(error); |
| + } |
| + |
| + public NfcMessage message() { |
| + return mNfcMessage; |
| + } |
| + public NfcPushOptions pushOptions() { |
| + return mNfcPushOptions; |
| + } |
| + } |
| + |
| + private NfcError createError(int errorType) { |
| + NfcError error = new NfcError(); |
| + error.errorType = errorType; |
| + return error; |
| + } |
| + |
| + @Nullable |
| + private NfcError checkIfReady() { |
| + if (!mHasPermission) { |
| + return createError(NfcErrorType.SECURITY); |
| + } else if (mNfcManager == null || mNfcAdapter == null |
| + || Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { |
| + return createError(NfcErrorType.NOT_SUPPORTED); |
| + } else if (!mNfcAdapter.isEnabled()) { |
| + return createError(NfcErrorType.DEVICE_DISABLED); |
| + } |
| + return null; |
| + } |
| + |
| + private boolean checkIfReady(Callbacks.Callback2<Integer, NfcError> callback) { |
| + NfcError error = checkIfReady(); |
| + if (error == null) return true; |
| + callback.call(0, error); |
| + return false; |
| + } |
| + |
| + private boolean checkIfReady(Callbacks.Callback1<NfcError> callback) { |
| + NfcError error = checkIfReady(); |
| + if (error == null) return true; |
| + callback.call(error); |
| + return false; |
| + } |
| + |
| + @TargetApi(Build.VERSION_CODES.KITKAT) |
| + private static class ReaderCallbackHandler implements ReaderCallback { |
| + private final NfcImpl mNfcImpl; |
| + |
| + public ReaderCallbackHandler(NfcImpl impl) { |
| + mNfcImpl = impl; |
| + } |
| + |
| + @Override |
| + public void onTagDiscovered(Tag tag) { |
| + mNfcImpl.onTagDiscovered(tag); |
| + } |
| + } |
| + |
| + private void enableReaderMode() { |
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { |
| + if (mReaderCallbackHandler == null) { |
|
kenneth.r.christiansen
2016/04/18 16:00:30
wouldnt it be nicer with early returns?
shalamov
2016/04/19 12:17:13
Done.
|
| + mReaderCallbackHandler = new ReaderCallbackHandler(this); |
| + mNfcAdapter.enableReaderMode(mActivity, mReaderCallbackHandler, |
| + NfcAdapter.FLAG_READER_NFC_A | NfcAdapter.FLAG_READER_NFC_B |
| + | NfcAdapter.FLAG_READER_NFC_F | NfcAdapter.FLAG_READER_NFC_V, |
| + null); |
| + } |
| + } |
| + } |
| + |
| + private void disableReaderMode() { |
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { |
| + mReaderCallbackHandler = null; |
| + mNfcAdapter.disableReaderMode(mActivity); |
| + } |
| + } |
| + |
| + private interface TagTechnologyWriter { |
| + public void write(NdefMessage message) |
| + throws IOException, TagLostException, FormatException; |
| + } |
| + |
| + private static class NdefWriter implements TagTechnologyWriter { |
| + private final Ndef mNdef; |
| + |
| + NdefWriter(Ndef ndef) { |
| + mNdef = ndef; |
| + } |
| + |
| + public void write(NdefMessage message) |
| + throws IOException, TagLostException, FormatException { |
| + mNdef.writeNdefMessage(message); |
| + } |
| + } |
| + |
| + private static class NdefFormattableWriter implements TagTechnologyWriter { |
| + private final NdefFormatable mNdefFormattable; |
| + |
| + NdefFormattableWriter(NdefFormatable ndefFormattable) { |
| + mNdefFormattable = ndefFormattable; |
| + } |
| + |
| + public void write(NdefMessage message) |
| + throws IOException, TagLostException, FormatException { |
| + mNdefFormattable.format(message); |
| + } |
| + } |
| + |
| + private static class NfcTagWriter { |
| + private final TagTechnology mTech; |
| + private final TagTechnologyWriter mTechWriter; |
| + private boolean mWasConnected = false; |
| + |
| + public static NfcTagWriter create(Tag tag) { |
| + if (tag == null) return null; |
| + |
| + Ndef ndef = Ndef.get(tag); |
| + if (ndef != null) return new NfcTagWriter(ndef, new NdefWriter(ndef)); |
| + |
| + NdefFormatable formattable = NdefFormatable.get(tag); |
| + if (formattable != null) { |
| + return new NfcTagWriter(formattable, new NdefFormattableWriter(formattable)); |
| + } |
| + |
| + return null; |
| + } |
| + |
| + private NfcTagWriter(TagTechnology tech, TagTechnologyWriter writer) { |
| + mTech = tech; |
| + mTechWriter = writer; |
| + } |
| + |
| + public void connect() throws IOException, TagLostException { |
| + if (!mTech.isConnected()) { |
| + mTech.connect(); |
| + mWasConnected = true; |
|
kenneth.r.christiansen
2016/04/18 16:00:30
mDidConnect?
|
| + } |
| + } |
| + |
| + public void close() throws IOException { |
| + mTech.close(); |
| + } |
| + |
| + public void write(NdefMessage message) |
| + throws IOException, TagLostException, FormatException { |
| + mTechWriter.write(message); |
| + } |
| + |
| + public boolean isTagOutOfRange() { |
| + try { |
| + connect(); |
| + } catch (IOException e) { |
| + return mWasConnected; |
| + } |
| + return false; |
| + } |
| + } |
| + |
| + private static class InvalidMessageException extends Exception {} |
| + |
| + private NdefMessage toNdefMessage(NfcMessage message) throws InvalidMessageException { |
| + if (message == null || message.data.length == 0) throw new InvalidMessageException(); |
| + |
| + try { |
| + List<NdefRecord> records = new ArrayList<NdefRecord>(); |
| + for (NfcRecord record : message.data) { |
| + records.add(toNdefRecord(record)); |
| + } |
| + records.add(NdefRecord.createExternal(DOMAIN, TYPE, message.url.getBytes())); |
| + NdefRecord[] ndefRecords = new NdefRecord[records.size()]; |
| + records.toArray(ndefRecords); |
| + return new NdefMessage(ndefRecords); |
| + } catch (UnsupportedEncodingException | InvalidMessageException |
| + | IllegalArgumentException e) { |
| + throw new InvalidMessageException(); |
| + } |
| + } |
| + |
| + private String getCharset(NfcRecord record) { |
| + if (record.mediaType.endsWith(CHARSET_UTF8)) return "UTF-8"; |
| + |
| + if (record.mediaType.endsWith(CHARSET_UTF16)) return "UTF-16"; |
| + |
| + return "UTF-8"; |
|
kenneth.r.christiansen
2016/04/18 16:00:30
no warning?
shalamov
2016/04/19 12:17:13
Done.
|
| + } |
| + |
| + private NdefRecord toNdefRecord(NfcRecord record) |
| + throws InvalidMessageException, IllegalArgumentException, UnsupportedEncodingException { |
| + switch (record.recordType) { |
| + case NfcRecordType.URL: |
| + return NdefRecord.createUri(new String(record.data, getCharset(record))); |
| + case NfcRecordType.TEXT: |
| + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { |
| + return NdefRecord.createTextRecord( |
| + "en-US", new String(record.data, getCharset(record))); |
| + } else { |
| + return NdefRecord.createMime(TEXT_MIME, record.data); |
| + } |
| + case NfcRecordType.JSON: |
| + case NfcRecordType.OPAQUE_RECORD: |
| + return NdefRecord.createMime(record.mediaType, record.data); |
| + default: |
| + throw new InvalidMessageException(); |
| + } |
| + } |
| + |
| + private void pendingPushOperationCompleted(NfcError error) { |
| + if (mPendingPushOperation != null) { |
| + mPendingPushOperation.complete(error); |
| + mPendingPushOperation = null; |
| + } |
| + |
| + if (error != null) mTagWriter = null; |
| + } |
| + |
| + private void processPendingPushOperation() { |
| + if (mTagWriter == null || mPendingPushOperation == null) return; |
| + |
| + if (mTagWriter.isTagOutOfRange()) { |
| + mTagWriter = null; |
| + return; |
| + } |
| + |
| + try { |
| + mTagWriter.connect(); |
| + mTagWriter.write(toNdefMessage(mPendingPushOperation.message())); |
| + pendingPushOperationCompleted(null); |
| + mTagWriter.close(); |
| + } catch (InvalidMessageException e) { |
| + Log.w(TAG, "Cannot write data to NFC tag. Invalid NfcMessage."); |
| + pendingPushOperationCompleted(createError(NfcErrorType.INVALID_MESSAGE)); |
| + } catch (TagLostException e) { |
| + Log.w(TAG, "Cannot write data to NFC tag. Tag is lost"); |
| + pendingPushOperationCompleted(createError(NfcErrorType.IO_ERROR)); |
| + } catch (FormatException | IOException e) { |
| + Log.w(TAG, "Cannot write data to NFC tag. IO_ERROR."); |
| + pendingPushOperationCompleted(createError(NfcErrorType.IO_ERROR)); |
| + } |
| + } |
| + |
| + public void onTagDiscovered(Tag tag) { |
| + mTagWriter = NfcTagWriter.create(tag); |
| + processPendingPushOperation(); |
| + } |
| +} |