nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 1 | /* |
Ganesh Deva | 1ad7d6f | 2019-06-17 11:46:02 +0530 | [diff] [blame] | 2 | * Copyright (C) 2017 NXP Semiconductors |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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 | |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 35 | |
| 36 | private static String sMessageService; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 37 | Context mContext; |
| 38 | Messenger dtaMessenger = null; |
| 39 | boolean isBound; |
| 40 | |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 41 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 42 | public DtaServiceConnector(Context mContext) { |
| 43 | this.mContext = mContext; |
| 44 | } |
| 45 | |
| 46 | public void bindService() { |
nxf50051 | 3a018e7 | 2019-04-23 17:11:41 +0530 | [diff] [blame] | 47 | if (!isBound) { |
| 48 | Intent intent = new Intent(sMessageService); |
| 49 | mContext.bindService(createExplicitFromImplicitIntent(mContext,intent), |
| 50 | myConnection,Context.BIND_AUTO_CREATE); |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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) { |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 67 | if (!isBound) return; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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(); |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 76 | } catch (NullPointerException e) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 77 | e.printStackTrace(); |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 78 | } |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 79 | } |
| 80 | |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 81 | public static Intent createExplicitFromImplicitIntent(Context context, Intent implicitIntent) { |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 82 | PackageManager pm = context.getPackageManager(); |
| 83 | List<ResolveInfo> resolveInfo = pm.queryIntentServices(implicitIntent, 0); |
| 84 | if (resolveInfo == null || resolveInfo.size() != 1) { |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 85 | return null; |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 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 | |
Bhupendra Pawar | 9f0c838 | 2018-01-11 17:05:27 +0530 | [diff] [blame] | 96 | public static void setMessageService(String service) { |
| 97 | sMessageService = service; |
| 98 | } |
| 99 | |
nxpandroid | 64fd68c | 2015-09-23 16:45:15 +0530 | [diff] [blame] | 100 | } |