Home | History | Annotate | Download | only in nfc
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.nfc;
     18 
     19 import android.annotation.SdkConstant;
     20 import android.annotation.SdkConstant.SdkConstantType;
     21 import android.content.Context;
     22 import android.content.Intent;
     23 import android.nfc.NdefTag;
     24 import android.nfc.NfcAdapter;
     25 import android.nfc.NdefMessage;
     26 import android.nfc.Tag;
     27 import android.os.Handler;
     28 import android.os.Message;
     29 import android.util.Log;
     30 
     31 /**
     32  * Native interface to the NFC Manager functions
     33  */
     34 public class NativeNfcManager {
     35     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
     36     public static final String INTERNAL_LLCP_LINK_STATE_CHANGED_ACTION = "com.android.nfc.action.INTERNAL_LLCP_LINK_STATE_CHANGED";
     37 
     38     public static final String INTERNAL_LLCP_LINK_STATE_CHANGED_EXTRA = "com.android.nfc.extra.INTERNAL_LLCP_LINK_STATE";
     39 
     40     @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
     41     public static final String INTERNAL_TARGET_DESELECTED_ACTION = "com.android.nfc.action.INTERNAL_TARGET_DESELECTED";
     42 
     43     private static final String NFC_PERM = android.Manifest.permission.NFC;
     44 
     45     /* Native structure */
     46     private int mNative;
     47 
     48     private final Context mContext;
     49 
     50     private final NfcService mNfcService;
     51 
     52     private static final String TAG = "NativeNfcManager";
     53 
     54     public NativeNfcManager(Context context, NfcService service) {
     55         mContext = context;
     56         mNfcService = service;
     57     }
     58 
     59     /**
     60      * Initializes Native structure
     61      */
     62     public native boolean initializeNativeStructure();
     63 
     64     /**
     65      * Initializes NFC stack.
     66      */
     67     public native boolean initialize();
     68 
     69     /**
     70      * Deinitializes NFC stack.
     71      */
     72     public native boolean deinitialize();
     73 
     74     /**
     75      * Enable discory for the NdefMessage and Transaction notification
     76      */
     77     public native void enableDiscovery(int mode);
     78 
     79     public native void disableDiscovery();
     80 
     81     public native int[] doGetSecureElementList();
     82 
     83     public native void doSelectSecureElement(int seID);
     84 
     85     public native void doDeselectSecureElement(int seID);
     86 
     87     public native int doGetLastError();
     88 
     89     public native void doSetProperties(int param, int value);
     90 
     91     public native void doCancel();
     92 
     93     public native NativeLlcpConnectionlessSocket doCreateLlcpConnectionlessSocket(int nSap);
     94 
     95     public native NativeLlcpServiceSocket doCreateLlcpServiceSocket(int nSap, String sn, int miu,
     96             int rw, int linearBufferLength);
     97 
     98     public native NativeLlcpSocket doCreateLlcpSocket(int sap, int miu, int rw,
     99             int linearBufferLength);
    100 
    101     public native boolean doCheckLlcp();
    102 
    103     public native boolean doActivateLlcp();
    104 
    105 
    106     /**
    107      * Notifies Ndef Message (TODO: rename into notifyTargetDiscovered)
    108      */
    109     private void notifyNdefMessageListeners(NativeNfcTag tag) {
    110         mNfcService.sendMessage(NfcService.MSG_NDEF_TAG, tag);
    111     }
    112 
    113     /**
    114      * Notifies transaction
    115      */
    116     private void notifyTargetDeselected() {
    117         mNfcService.sendMessage(NfcService.MSG_TARGET_DESELECTED, null);
    118     }
    119 
    120     /**
    121      * Notifies transaction
    122      */
    123     private void notifyTransactionListeners(byte[] aid) {
    124         mNfcService.sendMessage(NfcService.MSG_CARD_EMULATION, aid);
    125     }
    126 
    127     /**
    128      * Notifies P2P Device detected, to activate LLCP link
    129      */
    130     private void notifyLlcpLinkActivation(NativeP2pDevice device) {
    131         mNfcService.sendMessage(NfcService.MSG_LLCP_LINK_ACTIVATION, device);
    132     }
    133 
    134     /**
    135      * Notifies P2P Device detected, to activate LLCP link
    136      */
    137     private void notifyLlcpLinkDeactivated(NativeP2pDevice device) {
    138         mNfcService.sendMessage(NfcService.MSG_LLCP_LINK_DEACTIVATED, device);
    139     }
    140 
    141 }
    142