frameworks/base
Révision | 65cecae44357f0507c7e1dda7eb0172671682d81 (tree) |
---|---|
l'heure | 2011-10-30 17:33:22 |
Auteur | Chih-Wei Huang <cwhuang@linu...> |
Commiter | Chih-Wei Huang |
Merge remote branch 'aosp/gingerbread-mr4-release' into gingerbread-x86
@@ -269,6 +269,22 @@ public final class BluetoothA2dp { | ||
269 | 269 | } |
270 | 270 | } |
271 | 271 | |
272 | + /** | |
273 | + * Allow or disallow incoming connection | |
274 | + * @param device Sink | |
275 | + * @param value True / False | |
276 | + * @return Success or Failure of the binder call. | |
277 | + */ | |
278 | + public boolean allowIncomingConnect(BluetoothDevice device, boolean value) { | |
279 | + if (DBG) log("allowIncomingConnect(" + device + ":" + value + ")"); | |
280 | + try { | |
281 | + return mService.allowIncomingConnect(device, value); | |
282 | + } catch (RemoteException e) { | |
283 | + Log.e(TAG, "", e); | |
284 | + return false; | |
285 | + } | |
286 | + } | |
287 | + | |
272 | 288 | /** Helper for converting a state to a string. |
273 | 289 | * For debug use only - strings are not internationalized. |
274 | 290 | * @hide |
@@ -276,6 +276,33 @@ public final class BluetoothDevice implements Parcelable { | ||
276 | 276 | public static final String ACTION_PAIRING_CANCEL = |
277 | 277 | "android.bluetooth.device.action.PAIRING_CANCEL"; |
278 | 278 | |
279 | + /** @hide */ | |
280 | + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) | |
281 | + public static final String ACTION_CONNECTION_ACCESS_REQUEST = | |
282 | + "android.bluetooth.device.action.CONNECTION_ACCESS_REQUEST"; | |
283 | + | |
284 | + /** @hide */ | |
285 | + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) | |
286 | + public static final String ACTION_CONNECTION_ACCESS_REPLY = | |
287 | + "android.bluetooth.device.action.CONNECTION_ACCESS_REPLY"; | |
288 | + | |
289 | + /** @hide */ | |
290 | + @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) | |
291 | + public static final String ACTION_CONNECTION_ACCESS_CANCEL = | |
292 | + "android.bluetooth.device.action.CONNECTION_ACCESS_CANCEL"; | |
293 | + /** | |
294 | + * Used as an extra field in {@link #ACTION_CONNECTION_ACCESS_REPLY} intent. | |
295 | + * @hide | |
296 | + */ | |
297 | + public static final String EXTRA_CONNECTION_ACCESS_RESULT = | |
298 | + "android.bluetooth.device.extra.CONNECTION_ACCESS_RESULT"; | |
299 | + | |
300 | + /**@hide*/ | |
301 | + public static final int CONNECTION_ACCESS_YES = 1; | |
302 | + | |
303 | + /**@hide*/ | |
304 | + public static final int CONNECTION_ACCESS_NO = 2; | |
305 | + | |
279 | 306 | /** A bond attempt succeeded |
280 | 307 | * @hide */ |
281 | 308 | public static final int BOND_SUCCESS = 0; |
@@ -21,9 +21,11 @@ import android.content.Context; | ||
21 | 21 | import android.content.Intent; |
22 | 22 | import android.content.IntentFilter; |
23 | 23 | import android.os.Message; |
24 | +import android.os.PowerManager; | |
24 | 25 | import android.server.BluetoothA2dpService; |
25 | 26 | import android.server.BluetoothService; |
26 | 27 | import android.util.Log; |
28 | +import android.util.Pair; | |
27 | 29 | |
28 | 30 | import com.android.internal.util.HierarchicalState; |
29 | 31 | import com.android.internal.util.HierarchicalStateMachine; |
@@ -73,9 +75,17 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
73 | 75 | public static final int AUTO_CONNECT_PROFILES = 101; |
74 | 76 | public static final int TRANSITION_TO_STABLE = 102; |
75 | 77 | public static final int CONNECT_OTHER_PROFILES = 103; |
78 | + private static final int CONNECTION_ACCESS_REQUEST_REPLY = 104; | |
79 | + private static final int CONNECTION_ACCESS_REQUEST_EXPIRY = 105; | |
76 | 80 | |
77 | 81 | private static final int AUTO_CONNECT_DELAY = 6000; // 6 secs |
78 | 82 | private static final int CONNECT_OTHER_PROFILES_DELAY = 4000; // 4 secs |
83 | + private static final int CONNECTION_ACCESS_REQUEST_EXPIRY_TIMEOUT = 7000; // 7 secs | |
84 | + private static final int CONNECTION_ACCESS_UNDEFINED = -1; | |
85 | + private static final long INIT_INCOMING_REJECT_TIMER = 1000; // 1 sec | |
86 | + private static final long MAX_INCOMING_REJECT_TIMER = 3600 * 1000 * 4; // 4 hours | |
87 | + | |
88 | + private static final String PREFS_NAME = "ConnectionAccess"; | |
79 | 89 | |
80 | 90 | private BondedDevice mBondedDevice = new BondedDevice(); |
81 | 91 | private OutgoingHandsfree mOutgoingHandsfree = new OutgoingHandsfree(); |
@@ -90,10 +100,16 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
90 | 100 | private BluetoothPbap mPbapService; |
91 | 101 | private boolean mHeadsetServiceConnected; |
92 | 102 | private boolean mPbapServiceConnected; |
103 | + private static final String BLUETOOTH_ADMIN_PERM = android.Manifest.permission.BLUETOOTH_ADMIN; | |
93 | 104 | |
94 | 105 | private BluetoothDevice mDevice; |
95 | 106 | private int mHeadsetState; |
96 | 107 | private int mA2dpState; |
108 | + private long mIncomingRejectTimer; | |
109 | + private boolean mConnectionAccessReplyReceived = false; | |
110 | + private Pair<Integer, String> mIncomingConnections; | |
111 | + private PowerManager.WakeLock mWakeLock; | |
112 | + private PowerManager mPowerManager; | |
97 | 113 | |
98 | 114 | private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() { |
99 | 115 | @Override |
@@ -108,6 +124,10 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
108 | 124 | int initiator = intent.getIntExtra( |
109 | 125 | BluetoothHeadset.EXTRA_DISCONNECT_INITIATOR, |
110 | 126 | BluetoothHeadset.LOCAL_DISCONNECT); |
127 | + // We trust this device now | |
128 | + if (newState == BluetoothHeadset.STATE_CONNECTED) { | |
129 | + setTrust(BluetoothDevice.CONNECTION_ACCESS_YES); | |
130 | + } | |
111 | 131 | mHeadsetState = newState; |
112 | 132 | if (newState == BluetoothHeadset.STATE_DISCONNECTED && |
113 | 133 | initiator == BluetoothHeadset.REMOTE_DISCONNECT) { |
@@ -121,6 +141,10 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
121 | 141 | int newState = intent.getIntExtra(BluetoothA2dp.EXTRA_SINK_STATE, 0); |
122 | 142 | int oldState = intent.getIntExtra(BluetoothA2dp.EXTRA_PREVIOUS_SINK_STATE, 0); |
123 | 143 | mA2dpState = newState; |
144 | + // We trust this device now | |
145 | + if (newState == BluetoothA2dp.STATE_CONNECTED) { | |
146 | + setTrust(BluetoothDevice.CONNECTION_ACCESS_YES); | |
147 | + } | |
124 | 148 | if ((oldState == BluetoothA2dp.STATE_CONNECTED || |
125 | 149 | oldState == BluetoothA2dp.STATE_PLAYING) && |
126 | 150 | newState == BluetoothA2dp.STATE_DISCONNECTED) { |
@@ -134,6 +158,13 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
134 | 158 | // This is technically not needed, but we can get stuck sometimes. |
135 | 159 | // For example, if incoming A2DP fails, we are not informed by Bluez |
136 | 160 | sendMessage(TRANSITION_TO_STABLE); |
161 | + } else if (action.equals(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY)) { | |
162 | + mWakeLock.release(); | |
163 | + int val = intent.getIntExtra(BluetoothDevice.EXTRA_CONNECTION_ACCESS_RESULT, | |
164 | + BluetoothDevice.CONNECTION_ACCESS_NO); | |
165 | + Message msg = obtainMessage(CONNECTION_ACCESS_REQUEST_REPLY); | |
166 | + msg.arg1 = val; | |
167 | + sendMessage(msg); | |
137 | 168 | } |
138 | 169 | } |
139 | 170 | }; |
@@ -174,11 +205,20 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
174 | 205 | filter.addAction(BluetoothHeadset.ACTION_STATE_CHANGED); |
175 | 206 | filter.addAction(BluetoothDevice.ACTION_ACL_CONNECTED); |
176 | 207 | filter.addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED); |
208 | + filter.addAction(BluetoothDevice.ACTION_CONNECTION_ACCESS_REPLY); | |
177 | 209 | |
178 | 210 | mContext.registerReceiver(mBroadcastReceiver, filter); |
179 | 211 | |
180 | 212 | HeadsetServiceListener l = new HeadsetServiceListener(); |
181 | 213 | PbapServiceListener p = new PbapServiceListener(); |
214 | + | |
215 | + mIncomingConnections = mService.getIncomingState(address); | |
216 | + mIncomingRejectTimer = readTimerValue(); | |
217 | + mPowerManager = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE); | |
218 | + mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK | | |
219 | + PowerManager.ACQUIRE_CAUSES_WAKEUP | | |
220 | + PowerManager.ON_AFTER_RELEASE, TAG); | |
221 | + mWakeLock.setReferenceCounted(false); | |
182 | 222 | } |
183 | 223 | |
184 | 224 | private class HeadsetServiceListener implements BluetoothHeadset.ServiceListener { |
@@ -438,6 +478,24 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
438 | 478 | // Ignore |
439 | 479 | Log.e(TAG, "Error: Incoming connection with a pending incoming connection"); |
440 | 480 | break; |
481 | + case CONNECTION_ACCESS_REQUEST_REPLY: | |
482 | + int val = message.arg1; | |
483 | + mConnectionAccessReplyReceived = true; | |
484 | + boolean value = false; | |
485 | + if (val == BluetoothDevice.CONNECTION_ACCESS_YES) { | |
486 | + value = true; | |
487 | + } | |
488 | + setTrust(val); | |
489 | + | |
490 | + handleIncomingConnection(CONNECT_HFP_INCOMING, value); | |
491 | + break; | |
492 | + case CONNECTION_ACCESS_REQUEST_EXPIRY: | |
493 | + if (!mConnectionAccessReplyReceived) { | |
494 | + handleIncomingConnection(CONNECT_HFP_INCOMING, false); | |
495 | + sendConnectionAccessRemovalIntent(); | |
496 | + sendMessage(TRANSITION_TO_STABLE); | |
497 | + } | |
498 | + break; | |
441 | 499 | case CONNECT_A2DP_INCOMING: |
442 | 500 | // Serialize the commands. |
443 | 501 | deferMessage(message); |
@@ -607,6 +665,25 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
607 | 665 | case CONNECT_A2DP_INCOMING: |
608 | 666 | // ignore |
609 | 667 | break; |
668 | + case CONNECTION_ACCESS_REQUEST_REPLY: | |
669 | + int val = message.arg1; | |
670 | + mConnectionAccessReplyReceived = true; | |
671 | + boolean value = false; | |
672 | + if (val == BluetoothDevice.CONNECTION_ACCESS_YES) { | |
673 | + value = true; | |
674 | + } | |
675 | + setTrust(val); | |
676 | + handleIncomingConnection(CONNECT_A2DP_INCOMING, value); | |
677 | + break; | |
678 | + case CONNECTION_ACCESS_REQUEST_EXPIRY: | |
679 | + // The check protects the race condition between REQUEST_REPLY | |
680 | + // and the timer expiry. | |
681 | + if (!mConnectionAccessReplyReceived) { | |
682 | + handleIncomingConnection(CONNECT_A2DP_INCOMING, false); | |
683 | + sendConnectionAccessRemovalIntent(); | |
684 | + sendMessage(TRANSITION_TO_STABLE); | |
685 | + } | |
686 | + break; | |
610 | 687 | case CONNECT_A2DP_OUTGOING: |
611 | 688 | // Defer message and retry |
612 | 689 | deferMessage(message); |
@@ -662,8 +739,138 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
662 | 739 | deferMessage(msg); |
663 | 740 | } |
664 | 741 | |
742 | + private void updateIncomingAllowedTimer() { | |
743 | + // Not doing a perfect exponential backoff because | |
744 | + // we want two different rates. For all practical | |
745 | + // purposes, this is good enough. | |
746 | + if (mIncomingRejectTimer == 0) mIncomingRejectTimer = INIT_INCOMING_REJECT_TIMER; | |
747 | + | |
748 | + mIncomingRejectTimer *= 5; | |
749 | + if (mIncomingRejectTimer > MAX_INCOMING_REJECT_TIMER) { | |
750 | + mIncomingRejectTimer = MAX_INCOMING_REJECT_TIMER; | |
751 | + } | |
752 | + writeTimerValue(mIncomingRejectTimer); | |
753 | + } | |
754 | + | |
755 | + private boolean handleIncomingConnection(int command, boolean accept) { | |
756 | + boolean ret = false; | |
757 | + Log.i(TAG, "handleIncomingConnection:" + command + ":" + accept); | |
758 | + switch (command) { | |
759 | + case CONNECT_HFP_INCOMING: | |
760 | + if (!accept) { | |
761 | + ret = mHeadsetService.rejectIncomingConnect(mDevice); | |
762 | + sendMessage(TRANSITION_TO_STABLE); | |
763 | + updateIncomingAllowedTimer(); | |
764 | + } else if (mHeadsetState == BluetoothHeadset.STATE_CONNECTING) { | |
765 | + writeTimerValue(0); | |
766 | + ret = mHeadsetService.acceptIncomingConnect(mDevice); | |
767 | + } else if (mHeadsetState == BluetoothHeadset.STATE_DISCONNECTED) { | |
768 | + writeTimerValue(0); | |
769 | + handleConnectionOfOtherProfiles(command); | |
770 | + ret = mHeadsetService.createIncomingConnect(mDevice); | |
771 | + } | |
772 | + break; | |
773 | + case CONNECT_A2DP_INCOMING: | |
774 | + if (!accept) { | |
775 | + ret = mA2dpService.allowIncomingConnect(mDevice, false); | |
776 | + sendMessage(TRANSITION_TO_STABLE); | |
777 | + updateIncomingAllowedTimer(); | |
778 | + } else { | |
779 | + writeTimerValue(0); | |
780 | + ret = mA2dpService.allowIncomingConnect(mDevice, true); | |
781 | + handleConnectionOfOtherProfiles(command); | |
782 | + } | |
783 | + break; | |
784 | + default: | |
785 | + Log.e(TAG, "Waiting for incoming connection but state changed to:" + command); | |
786 | + break; | |
787 | + } | |
788 | + return ret; | |
789 | + } | |
790 | + | |
791 | + private void sendConnectionAccessIntent() { | |
792 | + mConnectionAccessReplyReceived = false; | |
793 | + | |
794 | + if (!mPowerManager.isScreenOn()) mWakeLock.acquire(); | |
795 | + | |
796 | + Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_REQUEST); | |
797 | + intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); | |
798 | + mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); | |
799 | + } | |
800 | + | |
801 | + private void sendConnectionAccessRemovalIntent() { | |
802 | + mWakeLock.release(); | |
803 | + Intent intent = new Intent(BluetoothDevice.ACTION_CONNECTION_ACCESS_CANCEL); | |
804 | + intent.putExtra(BluetoothDevice.EXTRA_DEVICE, mDevice); | |
805 | + mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); | |
806 | + } | |
807 | + | |
808 | + private int getTrust() { | |
809 | + String address = mDevice.getAddress(); | |
810 | + if (mIncomingConnections != null) return mIncomingConnections.first; | |
811 | + return CONNECTION_ACCESS_UNDEFINED; | |
812 | + } | |
813 | + | |
814 | + | |
815 | + private String getStringValue(long value) { | |
816 | + StringBuilder sbr = new StringBuilder(); | |
817 | + sbr.append(Long.toString(System.currentTimeMillis())); | |
818 | + sbr.append("-"); | |
819 | + sbr.append(Long.toString(value)); | |
820 | + return sbr.toString(); | |
821 | + } | |
822 | + | |
823 | + private void setTrust(int value) { | |
824 | + String second; | |
825 | + if (mIncomingConnections == null) { | |
826 | + second = getStringValue(INIT_INCOMING_REJECT_TIMER); | |
827 | + } else { | |
828 | + second = mIncomingConnections.second; | |
829 | + } | |
830 | + | |
831 | + mIncomingConnections = new Pair(value, second); | |
832 | + mService.writeIncomingConnectionState(mDevice.getAddress(), mIncomingConnections); | |
833 | + } | |
834 | + | |
835 | + private void writeTimerValue(long value) { | |
836 | + Integer first; | |
837 | + if (mIncomingConnections == null) { | |
838 | + first = CONNECTION_ACCESS_UNDEFINED; | |
839 | + } else { | |
840 | + first = mIncomingConnections.first; | |
841 | + } | |
842 | + mIncomingConnections = new Pair(first, getStringValue(value)); | |
843 | + mService.writeIncomingConnectionState(mDevice.getAddress(), mIncomingConnections); | |
844 | + } | |
845 | + | |
846 | + private long readTimerValue() { | |
847 | + if (mIncomingConnections == null) | |
848 | + return 0; | |
849 | + String value = mIncomingConnections.second; | |
850 | + String[] splits = value.split("-"); | |
851 | + if (splits != null && splits.length == 2) { | |
852 | + return Long.parseLong(splits[1]); | |
853 | + } | |
854 | + return 0; | |
855 | + } | |
856 | + | |
857 | + private boolean readIncomingAllowedValue() { | |
858 | + if (readTimerValue() == 0) return true; | |
859 | + String value = mIncomingConnections.second; | |
860 | + String[] splits = value.split("-"); | |
861 | + if (splits != null && splits.length == 2) { | |
862 | + long val1 = Long.parseLong(splits[0]); | |
863 | + long val2 = Long.parseLong(splits[1]); | |
864 | + if (val1 + val2 <= System.currentTimeMillis()) { | |
865 | + return true; | |
866 | + } | |
867 | + } | |
868 | + return false; | |
869 | + } | |
870 | + | |
665 | 871 | synchronized boolean processCommand(int command) { |
666 | - Log.i(TAG, "Processing command:" + command); | |
872 | + Log.e(TAG, "Processing command:" + command); | |
873 | + Message msg; | |
667 | 874 | switch(command) { |
668 | 875 | case CONNECT_HFP_OUTGOING: |
669 | 876 | if (mHeadsetService != null) { |
@@ -673,11 +880,21 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
673 | 880 | case CONNECT_HFP_INCOMING: |
674 | 881 | if (!mHeadsetServiceConnected) { |
675 | 882 | deferProfileServiceMessage(command); |
676 | - } else if (mHeadsetState == BluetoothHeadset.STATE_CONNECTING) { | |
677 | - return mHeadsetService.acceptIncomingConnect(mDevice); | |
678 | - } else if (mHeadsetState == BluetoothHeadset.STATE_DISCONNECTED) { | |
679 | - handleConnectionOfOtherProfiles(command); | |
680 | - return mHeadsetService.createIncomingConnect(mDevice); | |
883 | + } else { | |
884 | + // Check if device is already trusted | |
885 | + int access = getTrust(); | |
886 | + if (access == BluetoothDevice.CONNECTION_ACCESS_YES) { | |
887 | + handleIncomingConnection(command, true); | |
888 | + } else if (access == BluetoothDevice.CONNECTION_ACCESS_NO && | |
889 | + !readIncomingAllowedValue()) { | |
890 | + handleIncomingConnection(command, false); | |
891 | + } else { | |
892 | + sendConnectionAccessIntent(); | |
893 | + msg = obtainMessage(CONNECTION_ACCESS_REQUEST_EXPIRY); | |
894 | + sendMessageDelayed(msg, | |
895 | + CONNECTION_ACCESS_REQUEST_EXPIRY_TIMEOUT); | |
896 | + } | |
897 | + return true; | |
681 | 898 | } |
682 | 899 | break; |
683 | 900 | case CONNECT_A2DP_OUTGOING: |
@@ -686,8 +903,19 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
686 | 903 | } |
687 | 904 | break; |
688 | 905 | case CONNECT_A2DP_INCOMING: |
689 | - handleConnectionOfOtherProfiles(command); | |
690 | - // ignore, Bluez takes care | |
906 | + // Check if device is already trusted | |
907 | + int access = getTrust(); | |
908 | + if (access == BluetoothDevice.CONNECTION_ACCESS_YES) { | |
909 | + handleIncomingConnection(command, true); | |
910 | + } else if (access == BluetoothDevice.CONNECTION_ACCESS_NO && | |
911 | + !readIncomingAllowedValue()) { | |
912 | + handleIncomingConnection(command, false); | |
913 | + } else { | |
914 | + sendConnectionAccessIntent(); | |
915 | + msg = obtainMessage(CONNECTION_ACCESS_REQUEST_EXPIRY); | |
916 | + sendMessageDelayed(msg, | |
917 | + CONNECTION_ACCESS_REQUEST_EXPIRY_TIMEOUT); | |
918 | + } | |
691 | 919 | return true; |
692 | 920 | case DISCONNECT_HFP_OUTGOING: |
693 | 921 | if (!mHeadsetServiceConnected) { |
@@ -728,6 +956,8 @@ public final class BluetoothDeviceProfileState extends HierarchicalStateMachine | ||
728 | 956 | } |
729 | 957 | break; |
730 | 958 | case UNPAIR: |
959 | + writeTimerValue(INIT_INCOMING_REJECT_TIMER); | |
960 | + setTrust(CONNECTION_ACCESS_UNDEFINED); | |
731 | 961 | return mService.removeBondInternal(mDevice.getAddress()); |
732 | 962 | default: |
733 | 963 | Log.e(TAG, "Error: Unknown Command"); |
@@ -457,6 +457,23 @@ public final class BluetoothHeadset { | ||
457 | 457 | } |
458 | 458 | |
459 | 459 | /** |
460 | + * Reject the incoming connection. | |
461 | + * @hide | |
462 | + */ | |
463 | + public boolean rejectIncomingConnect(BluetoothDevice device) { | |
464 | + if (DBG) log("rejectIncomingConnect"); | |
465 | + if (mService != null) { | |
466 | + try { | |
467 | + return mService.rejectIncomingConnect(device); | |
468 | + } catch (RemoteException e) {Log.e(TAG, e.toString());} | |
469 | + } else { | |
470 | + Log.w(TAG, "Proxy not attached to service"); | |
471 | + if (DBG) Log.d(TAG, Log.getStackTraceString(new Throwable())); | |
472 | + } | |
473 | + return false; | |
474 | + } | |
475 | + | |
476 | + /** | |
460 | 477 | * Connect to a Bluetooth Headset. |
461 | 478 | * Note: This is an internal function and shouldn't be exposed |
462 | 479 | * @hide |
@@ -36,4 +36,6 @@ interface IBluetoothA2dp { | ||
36 | 36 | |
37 | 37 | boolean connectSinkInternal(in BluetoothDevice device); |
38 | 38 | boolean disconnectSinkInternal(in BluetoothDevice device); |
39 | + boolean allowIncomingConnect(in BluetoothDevice device, boolean value); | |
40 | + | |
39 | 41 | } |
@@ -37,6 +37,7 @@ interface IBluetoothHeadset { | ||
37 | 37 | |
38 | 38 | boolean createIncomingConnect(in BluetoothDevice device); |
39 | 39 | boolean acceptIncomingConnect(in BluetoothDevice device); |
40 | + boolean rejectIncomingConnect(in BluetoothDevice device); | |
40 | 41 | boolean cancelConnectThread(); |
41 | 42 | boolean connectHeadsetInternal(in BluetoothDevice device); |
42 | 43 | boolean disconnectHeadsetInternal(in BluetoothDevice device); |
@@ -16,7 +16,6 @@ | ||
16 | 16 | |
17 | 17 | package android.nfc; |
18 | 18 | |
19 | -import android.nfc.ApduList; | |
20 | 19 | import android.os.Bundle; |
21 | 20 | |
22 | 21 |
@@ -29,6 +28,5 @@ interface INfcAdapterExtras { | ||
29 | 28 | Bundle transceive(in byte[] data_in); |
30 | 29 | int getCardEmulationRoute(); |
31 | 30 | void setCardEmulationRoute(int route); |
32 | - void registerTearDownApdus(String packageName, in ApduList apdu); | |
33 | - void unregisterTearDownApdus(String packageName); | |
31 | + void authenticate(in byte[] token); | |
34 | 32 | } |
@@ -457,6 +457,22 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { | ||
457 | 457 | Settings.Secure.getBluetoothA2dpSinkPriorityKey(device.getAddress()), priority); |
458 | 458 | } |
459 | 459 | |
460 | + public synchronized boolean allowIncomingConnect(BluetoothDevice device, boolean value) { | |
461 | + mContext.enforceCallingOrSelfPermission(BLUETOOTH_ADMIN_PERM, | |
462 | + "Need BLUETOOTH_ADMIN permission"); | |
463 | + String address = device.getAddress(); | |
464 | + if (!BluetoothAdapter.checkBluetoothAddress(address)) { | |
465 | + return false; | |
466 | + } | |
467 | + Integer data = mBluetoothService.getAuthorizationAgentRequestData(address); | |
468 | + if (data == null) { | |
469 | + Log.w(TAG, "allowIncomingConnect(" + device + ") called but no native data available"); | |
470 | + return false; | |
471 | + } | |
472 | + log("allowIncomingConnect: A2DP: " + device + ":" + value); | |
473 | + return mBluetoothService.setAuthorizationNative(address, value, data.intValue()); | |
474 | + } | |
475 | + | |
460 | 476 | private synchronized void onSinkPropertyChanged(String path, String []propValues) { |
461 | 477 | if (!mBluetoothService.isEnabled()) { |
462 | 478 | return; |
@@ -50,6 +50,7 @@ class BluetoothEventLoop { | ||
50 | 50 | private boolean mInterrupted; |
51 | 51 | |
52 | 52 | private final HashMap<String, Integer> mPasskeyAgentRequestData; |
53 | + private final HashMap<String, Integer> mAuthorizationAgentRequestData; | |
53 | 54 | private final BluetoothService mBluetoothService; |
54 | 55 | private final BluetoothAdapter mAdapter; |
55 | 56 | private final Context mContext; |
@@ -109,6 +110,7 @@ class BluetoothEventLoop { | ||
109 | 110 | mBluetoothService = bluetoothService; |
110 | 111 | mContext = context; |
111 | 112 | mPasskeyAgentRequestData = new HashMap(); |
113 | + mAuthorizationAgentRequestData = new HashMap<String, Integer>(); | |
112 | 114 | mAdapter = adapter; |
113 | 115 | //WakeLock instantiation in BluetoothEventLoop class |
114 | 116 | PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE); |
@@ -130,6 +132,10 @@ class BluetoothEventLoop { | ||
130 | 132 | return mPasskeyAgentRequestData; |
131 | 133 | } |
132 | 134 | |
135 | + /* package */ HashMap<String, Integer> getAuthorizationAgentRequestData() { | |
136 | + return mAuthorizationAgentRequestData; | |
137 | + } | |
138 | + | |
133 | 139 | /* package */ void start() { |
134 | 140 | |
135 | 141 | if (!isEventLoopRunningNative()) { |
@@ -518,27 +524,29 @@ class BluetoothEventLoop { | ||
518 | 524 | mContext.sendBroadcast(intent, BLUETOOTH_ADMIN_PERM); |
519 | 525 | } |
520 | 526 | |
521 | - private boolean onAgentAuthorize(String objectPath, String deviceUuid) { | |
527 | + private void onAgentAuthorize(String objectPath, String deviceUuid, int nativeData) { | |
522 | 528 | String address = mBluetoothService.getAddressFromObjectPath(objectPath); |
523 | 529 | if (address == null) { |
524 | 530 | Log.e(TAG, "Unable to get device address in onAuthAgentAuthorize"); |
525 | - return false; | |
531 | + return; | |
526 | 532 | } |
527 | 533 | |
528 | 534 | boolean authorized = false; |
529 | 535 | ParcelUuid uuid = ParcelUuid.fromString(deviceUuid); |
530 | 536 | BluetoothA2dp a2dp = new BluetoothA2dp(mContext); |
531 | 537 | |
538 | + BluetoothDevice device = mAdapter.getRemoteDevice(address); | |
539 | + mAuthorizationAgentRequestData.put(address, new Integer(nativeData)); | |
540 | + | |
532 | 541 | // Bluez sends the UUID of the local service being accessed, _not_ the |
533 | 542 | // remote service |
534 | 543 | if (mBluetoothService.isEnabled() && |
535 | 544 | (BluetoothUuid.isAudioSource(uuid) || BluetoothUuid.isAvrcpTarget(uuid) |
536 | 545 | || BluetoothUuid.isAdvAudioDist(uuid)) && |
537 | 546 | !isOtherSinkInNonDisconnectingState(address)) { |
538 | - BluetoothDevice device = mAdapter.getRemoteDevice(address); | |
539 | 547 | authorized = a2dp.getSinkPriority(device) > BluetoothA2dp.PRIORITY_OFF; |
540 | 548 | if (authorized) { |
541 | - Log.i(TAG, "Allowing incoming A2DP / AVRCP connection from " + address); | |
549 | + Log.i(TAG, "First check pass for incoming A2DP / AVRCP connection from " + address); | |
542 | 550 | // Some headsets try to connect AVCTP before AVDTP - against the recommendation |
543 | 551 | // If AVCTP connection fails, we get stuck in IncomingA2DP state in the state |
544 | 552 | // machine. We don't handle AVCTP signals currently. We only send |
@@ -546,6 +554,8 @@ class BluetoothEventLoop { | ||
546 | 554 | // some cases. For now, just don't move to incoming state in this case. |
547 | 555 | if (!BluetoothUuid.isAvrcpTarget(uuid)) { |
548 | 556 | mBluetoothService.notifyIncomingA2dpConnection(address); |
557 | + } else { | |
558 | + a2dp.allowIncomingConnect(device, authorized); | |
549 | 559 | } |
550 | 560 | } else { |
551 | 561 | Log.i(TAG, "Rejecting incoming A2DP / AVRCP connection from " + address); |
@@ -554,7 +564,7 @@ class BluetoothEventLoop { | ||
554 | 564 | Log.i(TAG, "Rejecting incoming " + deviceUuid + " connection from " + address); |
555 | 565 | } |
556 | 566 | log("onAgentAuthorize(" + objectPath + ", " + deviceUuid + ") = " + authorized); |
557 | - return authorized; | |
567 | + if (!authorized) a2dp.allowIncomingConnect(device, authorized); | |
558 | 568 | } |
559 | 569 | |
560 | 570 | private boolean onAgentOutOfBandDataAvailable(String objectPath) { |
@@ -27,8 +27,8 @@ package android.server; | ||
27 | 27 | import android.bluetooth.BluetoothAdapter; |
28 | 28 | import android.bluetooth.BluetoothClass; |
29 | 29 | import android.bluetooth.BluetoothDevice; |
30 | -import android.bluetooth.BluetoothHeadset; | |
31 | 30 | import android.bluetooth.BluetoothDeviceProfileState; |
31 | +import android.bluetooth.BluetoothHeadset; | |
32 | 32 | import android.bluetooth.BluetoothProfileState; |
33 | 33 | import android.bluetooth.BluetoothSocket; |
34 | 34 | import android.bluetooth.BluetoothUuid; |
@@ -67,6 +67,7 @@ import java.io.FileWriter; | ||
67 | 67 | import java.io.IOException; |
68 | 68 | import java.io.InputStreamReader; |
69 | 69 | import java.io.PrintWriter; |
70 | +import java.io.RandomAccessFile; | |
70 | 71 | import java.io.UnsupportedEncodingException; |
71 | 72 | import java.util.ArrayList; |
72 | 73 | import java.util.Arrays; |
@@ -142,6 +143,11 @@ public class BluetoothService extends IBluetooth.Stub { | ||
142 | 143 | private static String mDockAddress; |
143 | 144 | private String mDockPin; |
144 | 145 | |
146 | + private static final String INCOMING_CONNECTION_FILE = | |
147 | + "/data/misc/bluetooth/incoming_connection.conf"; | |
148 | + private HashMap<String, Pair<Integer, String>> mIncomingConnections; | |
149 | + | |
150 | + | |
145 | 151 | private static class RemoteService { |
146 | 152 | public String address; |
147 | 153 | public ParcelUuid uuid; |
@@ -209,6 +215,7 @@ public class BluetoothService extends IBluetooth.Stub { | ||
209 | 215 | |
210 | 216 | filter.addAction(Intent.ACTION_DOCK_EVENT); |
211 | 217 | mContext.registerReceiver(mReceiver, filter); |
218 | + mIncomingConnections = new HashMap<String, Pair<Integer, String>>(); | |
212 | 219 | } |
213 | 220 | |
214 | 221 | public static synchronized String readDockBluetoothAddress() { |
@@ -733,8 +740,6 @@ public class BluetoothService extends IBluetooth.Stub { | ||
733 | 740 | |
734 | 741 | if (state == BluetoothDevice.BOND_BONDED) { |
735 | 742 | addProfileState(address); |
736 | - } else if (state == BluetoothDevice.BOND_NONE) { | |
737 | - removeProfileState(address); | |
738 | 743 | } |
739 | 744 | |
740 | 745 | if (DBG) log(address + " bond state " + oldState + " -> " + state + " (" + |
@@ -1312,6 +1317,8 @@ public class BluetoothService extends IBluetooth.Stub { | ||
1312 | 1317 | } |
1313 | 1318 | |
1314 | 1319 | public synchronized boolean removeBondInternal(String address) { |
1320 | + // Unset the trusted device state and then unpair | |
1321 | + setTrust(address, false); | |
1315 | 1322 | return removeDeviceNative(getObjectPathFromAddress(address)); |
1316 | 1323 | } |
1317 | 1324 |
@@ -2161,10 +2168,6 @@ public class BluetoothService extends IBluetooth.Stub { | ||
2161 | 2168 | return state; |
2162 | 2169 | } |
2163 | 2170 | |
2164 | - private void removeProfileState(String address) { | |
2165 | - mDeviceProfileState.remove(address); | |
2166 | - } | |
2167 | - | |
2168 | 2171 | private void initProfileState() { |
2169 | 2172 | String []bonds = null; |
2170 | 2173 | String val = getPropertyInternal("Devices"); |
@@ -2213,6 +2216,11 @@ public class BluetoothService extends IBluetooth.Stub { | ||
2213 | 2216 | mA2dpService = a2dpService; |
2214 | 2217 | } |
2215 | 2218 | |
2219 | + /*package*/ Integer getAuthorizationAgentRequestData(String address) { | |
2220 | + Integer data = mEventLoop.getAuthorizationAgentRequestData().remove(address); | |
2221 | + return data; | |
2222 | + } | |
2223 | + | |
2216 | 2224 | public void sendProfileStateMessage(int profile, int cmd) { |
2217 | 2225 | Message msg = new Message(); |
2218 | 2226 | msg.what = cmd; |
@@ -2223,6 +2231,116 @@ public class BluetoothService extends IBluetooth.Stub { | ||
2223 | 2231 | } |
2224 | 2232 | } |
2225 | 2233 | |
2234 | + private void createIncomingConnectionStateFile() { | |
2235 | + File f = new File(INCOMING_CONNECTION_FILE); | |
2236 | + if (!f.exists()) { | |
2237 | + try { | |
2238 | + f.createNewFile(); | |
2239 | + } catch (IOException e) { | |
2240 | + Log.e(TAG, "IOException: cannot create file"); | |
2241 | + } | |
2242 | + } | |
2243 | + } | |
2244 | + | |
2245 | + /** @hide */ | |
2246 | + public Pair<Integer, String> getIncomingState(String address) { | |
2247 | + if (mIncomingConnections.isEmpty()) { | |
2248 | + createIncomingConnectionStateFile(); | |
2249 | + readIncomingConnectionState(); | |
2250 | + } | |
2251 | + return mIncomingConnections.get(address); | |
2252 | + } | |
2253 | + | |
2254 | + private void readIncomingConnectionState() { | |
2255 | + synchronized(mIncomingConnections) { | |
2256 | + FileInputStream fstream = null; | |
2257 | + try { | |
2258 | + fstream = new FileInputStream(INCOMING_CONNECTION_FILE); | |
2259 | + DataInputStream in = new DataInputStream(fstream); | |
2260 | + BufferedReader file = new BufferedReader(new InputStreamReader(in)); | |
2261 | + String line; | |
2262 | + while((line = file.readLine()) != null) { | |
2263 | + line = line.trim(); | |
2264 | + if (line.length() == 0) continue; | |
2265 | + String[] value = line.split(","); | |
2266 | + if (value != null && value.length == 3) { | |
2267 | + Integer val1 = Integer.parseInt(value[1]); | |
2268 | + Pair<Integer, String> val = new Pair(val1, value[2]); | |
2269 | + mIncomingConnections.put(value[0], val); | |
2270 | + } | |
2271 | + } | |
2272 | + } catch (FileNotFoundException e) { | |
2273 | + log("FileNotFoundException: readIncomingConnectionState" + e.toString()); | |
2274 | + } catch (IOException e) { | |
2275 | + log("IOException: readIncomingConnectionState" + e.toString()); | |
2276 | + } finally { | |
2277 | + if (fstream != null) { | |
2278 | + try { | |
2279 | + fstream.close(); | |
2280 | + } catch (IOException e) { | |
2281 | + // Ignore | |
2282 | + } | |
2283 | + } | |
2284 | + } | |
2285 | + } | |
2286 | + } | |
2287 | + | |
2288 | + private void truncateIncomingConnectionFile() { | |
2289 | + RandomAccessFile r = null; | |
2290 | + try { | |
2291 | + r = new RandomAccessFile(INCOMING_CONNECTION_FILE, "rw"); | |
2292 | + r.setLength(0); | |
2293 | + } catch (FileNotFoundException e) { | |
2294 | + log("FileNotFoundException: truncateIncomingConnectionState" + e.toString()); | |
2295 | + } catch (IOException e) { | |
2296 | + log("IOException: truncateIncomingConnectionState" + e.toString()); | |
2297 | + } finally { | |
2298 | + if (r != null) { | |
2299 | + try { | |
2300 | + r.close(); | |
2301 | + } catch (IOException e) { | |
2302 | + // ignore | |
2303 | + } | |
2304 | + } | |
2305 | + } | |
2306 | + } | |
2307 | + | |
2308 | + /** @hide */ | |
2309 | + public void writeIncomingConnectionState(String address, Pair<Integer, String> data) { | |
2310 | + synchronized(mIncomingConnections) { | |
2311 | + mIncomingConnections.put(address, data); | |
2312 | + | |
2313 | + truncateIncomingConnectionFile(); | |
2314 | + BufferedWriter out = null; | |
2315 | + StringBuilder value = new StringBuilder(); | |
2316 | + try { | |
2317 | + out = new BufferedWriter(new FileWriter(INCOMING_CONNECTION_FILE, true)); | |
2318 | + for (String devAddress: mIncomingConnections.keySet()) { | |
2319 | + Pair<Integer, String> val = mIncomingConnections.get(devAddress); | |
2320 | + value.append(devAddress); | |
2321 | + value.append(","); | |
2322 | + value.append(val.first.toString()); | |
2323 | + value.append(","); | |
2324 | + value.append(val.second); | |
2325 | + value.append("\n"); | |
2326 | + } | |
2327 | + out.write(value.toString()); | |
2328 | + } catch (FileNotFoundException e) { | |
2329 | + log("FileNotFoundException: writeIncomingConnectionState" + e.toString()); | |
2330 | + } catch (IOException e) { | |
2331 | + log("IOException: writeIncomingConnectionState" + e.toString()); | |
2332 | + } finally { | |
2333 | + if (out != null) { | |
2334 | + try { | |
2335 | + out.close(); | |
2336 | + } catch (IOException e) { | |
2337 | + // Ignore | |
2338 | + } | |
2339 | + } | |
2340 | + } | |
2341 | + } | |
2342 | + } | |
2343 | + | |
2226 | 2344 | private static void log(String msg) { |
2227 | 2345 | Log.d(TAG, msg); |
2228 | 2346 | } |
@@ -2273,4 +2391,5 @@ public class BluetoothService extends IBluetooth.Stub { | ||
2273 | 2391 | short channel); |
2274 | 2392 | private native boolean removeServiceRecordNative(int handle); |
2275 | 2393 | private native boolean setLinkTimeoutNative(String path, int num_slots); |
2394 | + native boolean setAuthorizationNative(String address, boolean value, int data); | |
2276 | 2395 | } |
@@ -106,7 +106,7 @@ static void classInitNative(JNIEnv* env, jclass clazz) { | ||
106 | 106 | "(Ljava/lang/String;Z)V"); |
107 | 107 | |
108 | 108 | method_onAgentAuthorize = env->GetMethodID(clazz, "onAgentAuthorize", |
109 | - "(Ljava/lang/String;Ljava/lang/String;)Z"); | |
109 | + "(Ljava/lang/String;Ljava/lang/String;I)V"); | |
110 | 110 | method_onAgentOutOfBandDataAvailable = env->GetMethodID(clazz, "onAgentOutOfBandDataAvailable", |
111 | 111 | "(Ljava/lang/String;)Z"); |
112 | 112 | method_onAgentCancel = env->GetMethodID(clazz, "onAgentCancel", "()V"); |
@@ -917,29 +917,11 @@ DBusHandlerResult agent_event_filter(DBusConnection *conn, | ||
917 | 917 | LOGV("... object_path = %s", object_path); |
918 | 918 | LOGV("... uuid = %s", uuid); |
919 | 919 | |
920 | - bool auth_granted = | |
921 | - env->CallBooleanMethod(nat->me, method_onAgentAuthorize, | |
922 | - env->NewStringUTF(object_path), env->NewStringUTF(uuid)); | |
920 | + dbus_message_ref(msg); // increment refcount because we pass to java | |
921 | + env->CallBooleanMethod(nat->me, method_onAgentAuthorize, | |
922 | + env->NewStringUTF(object_path), env->NewStringUTF(uuid), | |
923 | + int(msg)); | |
923 | 924 | |
924 | - // reply | |
925 | - if (auth_granted) { | |
926 | - DBusMessage *reply = dbus_message_new_method_return(msg); | |
927 | - if (!reply) { | |
928 | - LOGE("%s: Cannot create message reply\n", __FUNCTION__); | |
929 | - goto failure; | |
930 | - } | |
931 | - dbus_connection_send(nat->conn, reply, NULL); | |
932 | - dbus_message_unref(reply); | |
933 | - } else { | |
934 | - DBusMessage *reply = dbus_message_new_error(msg, | |
935 | - "org.bluez.Error.Rejected", "Authorization rejected"); | |
936 | - if (!reply) { | |
937 | - LOGE("%s: Cannot create message reply\n", __FUNCTION__); | |
938 | - goto failure; | |
939 | - } | |
940 | - dbus_connection_send(nat->conn, reply, NULL); | |
941 | - dbus_message_unref(reply); | |
942 | - } | |
943 | 925 | goto success; |
944 | 926 | } else if (dbus_message_is_method_call(msg, |
945 | 927 | "org.bluez.Agent", "OutOfBandAvailable")) { |
@@ -599,6 +599,35 @@ static jboolean setRemoteOutOfBandDataNative(JNIEnv *env, jobject object, jstrin | ||
599 | 599 | return JNI_FALSE; |
600 | 600 | } |
601 | 601 | |
602 | +static jboolean setAuthorizationNative(JNIEnv *env, jobject object, jstring address, | |
603 | + jboolean val, int nativeData) { | |
604 | +#ifdef HAVE_BLUETOOTH | |
605 | + LOGV(__FUNCTION__); | |
606 | + native_data_t *nat = get_native_data(env, object); | |
607 | + if (nat) { | |
608 | + DBusMessage *msg = (DBusMessage *)nativeData; | |
609 | + DBusMessage *reply; | |
610 | + if (val) { | |
611 | + reply = dbus_message_new_method_return(msg); | |
612 | + } else { | |
613 | + reply = dbus_message_new_error(msg, | |
614 | + "org.bluez.Error.Rejected", "Authorization rejected"); | |
615 | + } | |
616 | + if (!reply) { | |
617 | + LOGE("%s: Cannot create message reply D-Bus\n", __FUNCTION__); | |
618 | + dbus_message_unref(msg); | |
619 | + return JNI_FALSE; | |
620 | + } | |
621 | + | |
622 | + dbus_connection_send(nat->conn, reply, NULL); | |
623 | + dbus_message_unref(msg); | |
624 | + dbus_message_unref(reply); | |
625 | + return JNI_TRUE; | |
626 | + } | |
627 | +#endif | |
628 | + return JNI_FALSE; | |
629 | +} | |
630 | + | |
602 | 631 | static jboolean setPinNative(JNIEnv *env, jobject object, jstring address, |
603 | 632 | jstring pin, int nativeData) { |
604 | 633 | #ifdef HAVE_BLUETOOTH |
@@ -1029,6 +1058,7 @@ static JNINativeMethod sMethods[] = { | ||
1029 | 1058 | (void *)setPairingConfirmationNative}, |
1030 | 1059 | {"setPasskeyNative", "(Ljava/lang/String;II)Z", (void *)setPasskeyNative}, |
1031 | 1060 | {"setRemoteOutOfBandDataNative", "(Ljava/lang/String;[B[BI)Z", (void *)setRemoteOutOfBandDataNative}, |
1061 | + {"setAuthorizationNative", "(Ljava/lang/String;ZI)Z", (void *)setAuthorizationNative}, | |
1032 | 1062 | {"setPinNative", "(Ljava/lang/String;Ljava/lang/String;I)Z", (void *)setPinNative}, |
1033 | 1063 | {"cancelPairingUserInputNative", "(Ljava/lang/String;I)Z", |
1034 | 1064 | (void *)cancelPairingUserInputNative}, |
@@ -18,7 +18,6 @@ package com.android.nfc_extras; | ||
18 | 18 | |
19 | 19 | import android.annotation.SdkConstant; |
20 | 20 | import android.annotation.SdkConstant.SdkConstantType; |
21 | -import android.nfc.ApduList; | |
22 | 21 | import android.nfc.INfcAdapterExtras; |
23 | 22 | import android.nfc.NfcAdapter; |
24 | 23 | import android.os.RemoteException; |
@@ -68,7 +67,11 @@ public final class NfcAdapterExtras { | ||
68 | 67 | |
69 | 68 | /** get service handles */ |
70 | 69 | private static void initService() { |
71 | - sService = sAdapter.getNfcAdapterExtrasInterface(); | |
70 | + final INfcAdapterExtras service = sAdapter.getNfcAdapterExtrasInterface(); | |
71 | + if (service != null) { | |
72 | + // Leave stale rather than receive a null value. | |
73 | + sService = service; | |
74 | + } | |
72 | 75 | } |
73 | 76 | |
74 | 77 | /** |
@@ -85,18 +88,19 @@ public final class NfcAdapterExtras { | ||
85 | 88 | if (sSingleton == null) { |
86 | 89 | try { |
87 | 90 | sAdapter = adapter; |
88 | - sRouteOff = new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null); | |
89 | 91 | sSingleton = new NfcAdapterExtras(); |
90 | 92 | sEmbeddedEe = new NfcExecutionEnvironment(sSingleton); |
93 | + sRouteOff = new CardEmulationRoute(CardEmulationRoute.ROUTE_OFF, null); | |
91 | 94 | sRouteOnWhenScreenOn = new CardEmulationRoute( |
92 | 95 | CardEmulationRoute.ROUTE_ON_WHEN_SCREEN_ON, sEmbeddedEe); |
93 | 96 | initService(); |
94 | 97 | } finally { |
95 | - if (sSingleton == null) { | |
96 | - sService = null; | |
97 | - sEmbeddedEe = null; | |
98 | - sRouteOff = null; | |
98 | + if (sService == null) { | |
99 | 99 | sRouteOnWhenScreenOn = null; |
100 | + sRouteOff = null; | |
101 | + sEmbeddedEe = null; | |
102 | + sSingleton = null; | |
103 | + sAdapter = null; | |
100 | 104 | } |
101 | 105 | } |
102 | 106 | } |
@@ -208,17 +212,18 @@ public final class NfcAdapterExtras { | ||
208 | 212 | return sEmbeddedEe; |
209 | 213 | } |
210 | 214 | |
211 | - public void registerTearDownApdus(String packageName, ApduList apdus) { | |
212 | - try { | |
213 | - sService.registerTearDownApdus(packageName, apdus); | |
214 | - } catch (RemoteException e) { | |
215 | - attemptDeadServiceRecovery(e); | |
216 | - } | |
217 | - } | |
218 | - | |
219 | - public void unregisterTearDownApdus(String packageName) { | |
215 | + /** | |
216 | + * Authenticate the client application. | |
217 | + * | |
218 | + * Some implementations of NFC Adapter Extras may require applications | |
219 | + * to authenticate with a token, before using other methods. | |
220 | + * | |
221 | + * @param a implementation specific token | |
222 | + * @throws a {@link java.lang.SecurityException} if authentication failed | |
223 | + */ | |
224 | + public void authenticate(byte[] token) { | |
220 | 225 | try { |
221 | - sService.unregisterTearDownApdus(packageName); | |
226 | + sService.authenticate(token); | |
222 | 227 | } catch (RemoteException e) { |
223 | 228 | attemptDeadServiceRecovery(e); |
224 | 229 | } |
@@ -55,6 +55,64 @@ public class NfcExecutionEnvironment { | ||
55 | 55 | */ |
56 | 56 | public static final String EXTRA_AID = "com.android.nfc_extras.extra.AID"; |
57 | 57 | |
58 | + /** | |
59 | + * Broadcast action: A filtered APDU was received. | |
60 | + * | |
61 | + * <p>This happens when an APDU of interest was matched by the Nfc adapter, | |
62 | + * for instance as the result of matching an externally-configured filter. | |
63 | + * | |
64 | + * <p>The filter configuration mechanism is not currently defined. | |
65 | + * | |
66 | + * <p>Always contains the extra field {@link EXTRA_APDU_BYTES}. | |
67 | + * | |
68 | + * @hide | |
69 | + */ | |
70 | + public static final String ACTION_APDU_RECEIVED = | |
71 | + "com.android.nfc_extras.action.APDU_RECEIVED"; | |
72 | + | |
73 | + /** | |
74 | + * Mandatory byte array extra field in {@link #ACTION_APDU_RECEIVED}. | |
75 | + * | |
76 | + * <p>Contains the bytes of the received APDU. | |
77 | + * | |
78 | + * @hide | |
79 | + */ | |
80 | + public static final String EXTRA_APDU_BYTES = | |
81 | + "com.android.nfc_extras.extra.APDU_BYTES"; | |
82 | + | |
83 | + /** | |
84 | + * Broadcast action: An EMV card removal event was detected. | |
85 | + * | |
86 | + * @hide | |
87 | + */ | |
88 | + public static final String ACTION_EMV_CARD_REMOVAL = | |
89 | + "com.android.nfc_extras.action.EMV_CARD_REMOVAL"; | |
90 | + | |
91 | + /** | |
92 | + * Broadcast action: An adapter implementing MIFARE Classic via card | |
93 | + * emulation detected that a block has been accessed. | |
94 | + * | |
95 | + * <p>This may only be issued for the first block that the reader | |
96 | + * authenticates to. | |
97 | + * | |
98 | + * <p>May contain the extra field {@link #EXTRA_MIFARE_BLOCK}. | |
99 | + * | |
100 | + * @hide | |
101 | + */ | |
102 | + public static final String ACTION_MIFARE_ACCESS_DETECTED = | |
103 | + "com.android.nfc_extras.action.MIFARE_ACCESS_DETECTED"; | |
104 | + | |
105 | + /** | |
106 | + * Optional integer extra field in {@link #ACTION_MIFARE_ACCESS_DETECTED}. | |
107 | + * | |
108 | + * <p>Provides the block number being accessed. If not set, the block | |
109 | + * number being accessed is unknown. | |
110 | + * | |
111 | + * @hide | |
112 | + */ | |
113 | + public static final String EXTRA_MIFARE_BLOCK = | |
114 | + "com.android.nfc_extras.extra.MIFARE_BLOCK"; | |
115 | + | |
58 | 116 | NfcExecutionEnvironment(NfcAdapterExtras extras) { |
59 | 117 | mExtras = extras; |
60 | 118 | } |