blob: 147084b727cf61fb1ecbae3119bf4bfc002411ec [file] [log] [blame]
nxpandroid64fd68c2015-09-23 16:45:15 +05301/*
Ganesh Deva1ad7d6f2019-06-17 11:46:02 +05302 * Copyright (C) 2017 NXP Semiconductors
nxpandroid64fd68c2015-09-23 16:45:15 +05303 *
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
17package com.android.nfc;
18
19import java.util.List;
20
21import android.content.ComponentName;
22import android.content.Context;
23import android.content.Intent;
24import android.content.ServiceConnection;
25import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.os.Bundle;
28import android.os.IBinder;
29import android.os.Message;
30import android.os.Messenger;
31import android.os.RemoteException;
32
33public class DtaServiceConnector {
34
Bhupendra Pawar9f0c8382018-01-11 17:05:27 +053035
36 private static String sMessageService;
nxpandroid64fd68c2015-09-23 16:45:15 +053037 Context mContext;
38 Messenger dtaMessenger = null;
39 boolean isBound;
40
Bhupendra Pawar9f0c8382018-01-11 17:05:27 +053041
nxpandroid64fd68c2015-09-23 16:45:15 +053042 public DtaServiceConnector(Context mContext) {
43 this.mContext = mContext;
44 }
45
46 public void bindService() {
nxf500513a018e72019-04-23 17:11:41 +053047 if (!isBound) {
48 Intent intent = new Intent(sMessageService);
49 mContext.bindService(createExplicitFromImplicitIntent(mContext,intent),
50 myConnection,Context.BIND_AUTO_CREATE);
nxpandroid64fd68c2015-09-23 16:45:15 +053051 }
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
66 public void sendMessage(String ndefMessage) {
Bhupendra Pawar9f0c8382018-01-11 17:05:27 +053067 if (!isBound) return;
nxpandroid64fd68c2015-09-23 16:45:15 +053068 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();
Bhupendra Pawar9f0c8382018-01-11 17:05:27 +053076 } catch (NullPointerException e) {
nxpandroid64fd68c2015-09-23 16:45:15 +053077 e.printStackTrace();
Bhupendra Pawar9f0c8382018-01-11 17:05:27 +053078 }
nxpandroid64fd68c2015-09-23 16:45:15 +053079 }
80
Bhupendra Pawar9f0c8382018-01-11 17:05:27 +053081 public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) {
nxpandroid64fd68c2015-09-23 16:45:15 +053082 PackageManager pm = context.getPackageManager();
83 List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0);
84 if (resolveInfo == null || resolveInfo.size() != 1) {
Bhupendra Pawar9f0c8382018-01-11 17:05:27 +053085 return null;
nxpandroid64fd68c2015-09-23 16:45:15 +053086 }
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
Bhupendra Pawar9f0c8382018-01-11 17:05:27 +053096 public static void setMessageService(String service) {
97 sMessageService = service;
98 }
99
nxpandroid64fd68c2015-09-23 16:45:15 +0530100}