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
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
35
36    private static String sMessageService;
37    Context mContext;
38    Messenger dtaMessenger = null;
39    boolean isBound;
40
41
42    public DtaServiceConnector(Context mContext) {
43        this.mContext = mContext;
44    }
45
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
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
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
96    public static void setMessageService(String service) {
97        sMessageService = service;
98    }
99
100}
101