Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / ca / CaNfcInterface.java
1 /* *****************************************************************
2  *
3  * Copyright 2015 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 package org.iotivity.ca;
22
23 import java.util.ArrayList;
24
25 import android.app.Activity;
26 import android.content.BroadcastReceiver;
27 import android.content.Context;
28 import android.content.Intent;
29 import android.content.IntentFilter;
30 import android.app.PendingIntent;
31 import android.content.IntentFilter.MalformedMimeTypeException;
32 import android.net.NetworkInfo;
33 import android.nfc.NdefMessage;
34 import android.nfc.NdefRecord;
35 import android.nfc.NfcAdapter;
36 import android.nfc.NfcEvent;
37 import android.os.Parcelable;
38 import android.util.Log;
39
40
41
42 public class CaNfcInterface implements NfcAdapter.CreateNdefMessageCallback {
43     private final static String MYTAG = CaNfcInterface.class.getSimpleName();
44     private static Context mContext;
45     private Activity  mActivity;
46     private NfcAdapter mAdapter;
47     private NdefMessage mMessage;
48     private boolean misInvokeBeam;
49
50     private CaNfcInterface(Context context, Activity activity) {
51         Log.d(MYTAG, "NFC  registerNfcReceiver");
52         mContext = context;
53         mActivity = activity;
54
55         mAdapter = NfcAdapter.getDefaultAdapter(mContext);
56         if (null == mAdapter)
57         {
58             Log.e(MYTAG, "Failed to get the Adapter");
59             return;
60         }
61     }
62
63     private native static void caNativeNfcPacketReceived(byte[] receivedData);
64     private native static NdefMessage caNativeNfcCreateNdefMessage(byte[] sendData);
65     private native static boolean caNativeNfcInvokeBeam();
66
67     @Override
68     public NdefMessage createNdefMessage(NfcEvent event) {
69         Log.d(MYTAG, "NFC  createNdefMessage");
70
71         // Returns the already created message
72         return mMessage;
73     }
74
75     public void processSendRquest(byte[] sendData) {
76
77         Log.d(MYTAG, "NFC  processSendRquest IN");
78
79         mMessage = caNativeNfcCreateNdefMessage(sendData);
80         misInvokeBeam = caNativeNfcInvokeBeam();
81
82         if (!misInvokeBeam)
83         {
84             Log.e(MYTAG, "NFC  Beam error");
85         }
86     }
87
88     public void caNfcInitialize() {
89         Log.d(MYTAG, "caNfcInitialize");
90
91         if ((null == mActivity) || (null == mContext) || (null == mAdapter)) {
92             Log.e(MYTAG, "caNfcInitialize failed, invalid parameters");
93             return;
94         }
95
96         PendingIntent pendingIntent = PendingIntent.getActivity(mActivity, 0,
97                                                     new Intent(mActivity, mActivity.getClass())
98                                                     .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
99                                                     | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);
100
101         IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
102
103         try {
104             ndef.addDataType("*/*");    /* Handles all MIME based dispatches.
105                                            You should specify only the ones that you need. */
106         } catch (MalformedMimeTypeException e) {
107             throw new RuntimeException("fail", e);
108         }
109
110         IntentFilter[] intentFiltersArray = new IntentFilter[] {ndef, };
111
112         mAdapter.enableForegroundDispatch(mActivity, pendingIntent, intentFiltersArray, null);
113         Log.d(MYTAG, " enableForegroundDispatch ");
114
115         mContext.registerReceiver(mReceiver, new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED));
116         Log.d(MYTAG, "NFC  caNfcInitialize OUT");
117     }
118
119     private static BroadcastReceiver mReceiver = new BroadcastReceiver() {
120         @Override
121         public void onReceive(Context context, Intent intent) {
122             Log.d(MYTAG, "onReceive broadcast intent updated - disable callback");
123
124             if (intent.getAction().equals(NfcAdapter.ACTION_NDEF_DISCOVERED))
125             {
126                 processIntent(intent);
127             }
128         }
129
130         private void processIntent(Intent intent) {
131
132             Log.d(MYTAG, "processIntent");
133             Parcelable[] rawMsgs = intent
134                     .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
135             NdefMessage msg = (NdefMessage) rawMsgs[0];
136             Log.d(MYTAG, msg.getRecords()[0].toMimeType().toString());
137             caNativeNfcPacketReceived(msg.getRecords()[0].getPayload());
138         }
139     };
140 }