1 /*
2  * Copyright (C) 2017 NXP Semiconductors
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 java.util.List;
20 
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.ServiceConnection;
25 import android.content.pm.PackageManager;
26 import android.content.pm.ResolveInfo;
27 import android.os.Bundle;
28 import android.os.IBinder;
29 import android.os.Message;
30 import android.os.Messenger;
31 import android.os.RemoteException;
32 
33 public class DtaServiceConnector {
34 
35 
36     private static String sMessageService;
37     Context mContext;
38     Messenger dtaMessenger = null;
39     boolean isBound;
40 
41 
DtaServiceConnector(Context mContext)42     public DtaServiceConnector(Context mContext) {
43         this.mContext = mContext;
44     }
45 
bindService()46     public void bindService() {
47         if (!isBound) {
48             Intent intent = new Intent(sMessageService);
49             mContext.bindService(createExplicitFromImplicitIntent(mContext,intent),
50                                    myConnection,Context.BIND_AUTO_CREATE);
51         }
52     }
53 
54     private ServiceConnection myConnection = new ServiceConnection() {
55         public void onServiceConnected(ComponentName className, IBinder service) {
56             dtaMessenger = new Messenger(service);
57             isBound = true;
58         }
59 
60         public void onServiceDisconnected(ComponentName className) {
61             dtaMessenger = null;
62             isBound = false;
63         }
64     };
65 
sendMessage(String ndefMessage)66     public void sendMessage(String ndefMessage) {
67         if (!isBound) return;
68         Message msg = Message.obtain();
69         Bundle bundle = new Bundle();
70         bundle.putString("NDEF_MESSAGE", ndefMessage);
71         msg.setData(bundle);
72         try {
73             dtaMessenger.send(msg);
74         } catch (RemoteException e) {
75             e.printStackTrace();
76         } catch (NullPointerException e) {
77             e.printStackTrace();
78         }
79     }
80 
createExplicitFromImplicitIntent(Context context, Intent implicitIntent)81     public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
82         PackageManager pm = context.getPackageManager();
83         List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
84         if (resolveInfo == null || resolveInfo.size() != 1) {
85             return null;
86         }
87         ResolveInfo serviceInfo = resolveInfo.get(0);
88         String packageName = serviceInfo.serviceInfo.packageName;
89         String className = serviceInfo.serviceInfo.name;
90         ComponentName component = new ComponentName(packageName, className);
91         Intent explicitIntent = new Intent(implicitIntent);
92         explicitIntent.setComponent(component);
93         return explicitIntent;
94     }
95 
setMessageService(String service)96     public static void setMessageService(String service) {
97         sMessageService = service;
98     }
99 
100 }
101