Jake Hamby | 9a62c9c | 2010-12-09 14:47:57 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 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 android.server; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.util.Log; |
| 21 | |
| 22 | import java.util.HashMap; |
| 23 | import java.util.Map; |
| 24 | |
| 25 | class BluetoothAdapterProperties { |
| 26 | |
| 27 | private static final String TAG = "BluetoothAdapterProperties"; |
| 28 | |
| 29 | private final Map<String, String> mPropertiesMap; |
| 30 | private final Context mContext; |
| 31 | private final BluetoothService mService; |
| 32 | |
| 33 | BluetoothAdapterProperties(Context context, BluetoothService service) { |
| 34 | mPropertiesMap = new HashMap<String, String>(); |
| 35 | mContext = context; |
| 36 | mService = service; |
| 37 | } |
| 38 | |
| 39 | synchronized String getProperty(String name) { |
| 40 | if (mPropertiesMap.isEmpty()) { |
| 41 | getAllProperties(); |
| 42 | } |
| 43 | return mPropertiesMap.get(name); |
| 44 | } |
| 45 | |
| 46 | String getObjectPath() { |
| 47 | return getProperty("ObjectPath"); |
| 48 | } |
| 49 | |
| 50 | synchronized void clear() { |
| 51 | mPropertiesMap.clear(); |
| 52 | } |
| 53 | |
| 54 | synchronized boolean isEmpty() { |
| 55 | return mPropertiesMap.isEmpty(); |
| 56 | } |
| 57 | |
| 58 | synchronized void setProperty(String name, String value) { |
| 59 | mPropertiesMap.put(name, value); |
| 60 | } |
| 61 | |
| 62 | synchronized void getAllProperties() { |
| 63 | mContext.enforceCallingOrSelfPermission( |
| 64 | BluetoothService.BLUETOOTH_PERM, |
| 65 | "Need BLUETOOTH permission"); |
| 66 | mPropertiesMap.clear(); |
| 67 | |
| 68 | String properties[] = (String[]) mService |
| 69 | .getAdapterPropertiesNative(); |
| 70 | // The String Array consists of key-value pairs. |
| 71 | if (properties == null) { |
| 72 | Log.e(TAG, "*Error*: GetAdapterProperties returned NULL"); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | for (int i = 0; i < properties.length; i++) { |
| 77 | String name = properties[i]; |
| 78 | String newValue = null; |
| 79 | int len; |
| 80 | if (name == null) { |
| 81 | Log.e(TAG, "Error:Adapter Property at index " + i + " is null"); |
| 82 | continue; |
| 83 | } |
| 84 | if (name.equals("Devices") || name.equals("UUIDs")) { |
| 85 | StringBuilder str = new StringBuilder(); |
| 86 | len = Integer.valueOf(properties[++i]); |
| 87 | for (int j = 0; j < len; j++) { |
| 88 | str.append(properties[++i]); |
| 89 | str.append(","); |
| 90 | } |
| 91 | if (len > 0) { |
| 92 | newValue = str.toString(); |
| 93 | } |
| 94 | } else { |
| 95 | newValue = properties[++i]; |
| 96 | } |
| 97 | mPropertiesMap.put(name, newValue); |
| 98 | } |
| 99 | |
| 100 | // Add adapter object path property. |
| 101 | String adapterPath = mService.getAdapterPathNative(); |
| 102 | if (adapterPath != null) { |
| 103 | mPropertiesMap.put("ObjectPath", adapterPath + "/dev_"); |
| 104 | } |
| 105 | } |
| 106 | } |