Home | History | Annotate | Download | only in server
      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             if (name == null) {
     80                 Log.e(TAG, "Error:Adapter Property at index " + i + " is null");
     81                 continue;
     82             }
     83             if (name.equals("Devices") || name.equals("UUIDs")) {
     84                 StringBuilder str = new StringBuilder();
     85                 int len = Integer.valueOf(properties[++i]);
     86                 for (int j = 0; j < len; j++) {
     87                     str.append(properties[++i]);
     88                     str.append(",");
     89                 }
     90                 if (len > 0) {
     91                     newValue = str.toString();
     92                 }
     93             } else {
     94                 newValue = properties[++i];
     95             }
     96             mPropertiesMap.put(name, newValue);
     97         }
     98 
     99         // Add adapter object path property.
    100         String adapterPath = mService.getAdapterPathNative();
    101         if (adapterPath != null) {
    102             mPropertiesMap.put("ObjectPath", adapterPath + "/dev_");
    103         }
    104     }
    105 }
    106