Home | History | Annotate | Download | only in opp
      1 /*
      2  * Copyright (c) 2008-2009, Motorola, Inc.
      3  *
      4  * All rights reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions are met:
      8  *
      9  * - Redistributions of source code must retain the above copyright notice,
     10  * this list of conditions and the following disclaimer.
     11  *
     12  * - Redistributions in binary form must reproduce the above copyright notice,
     13  * this list of conditions and the following disclaimer in the documentation
     14  * and/or other materials provided with the distribution.
     15  *
     16  * - Neither the name of the Motorola, Inc. nor the names of its contributors
     17  * may be used to endorse or promote products derived from this software
     18  * without specific prior written permission.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
     21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
     24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 package com.android.bluetooth.opp;
     34 
     35 import java.util.HashMap;
     36 
     37 import android.bluetooth.BluetoothDevice;
     38 import android.content.Context;
     39 import android.content.SharedPreferences;
     40 import android.content.SharedPreferences.Editor;
     41 import android.util.Log;
     42 
     43 /**
     44  * This class cache Bluetooth device name and channel locally. Its a temp
     45  * solution which should be replaced by bluetooth_devices in SettingsProvider
     46  */
     47 public class BluetoothOppPreference {
     48     private static final String TAG = "BluetoothOppPreference";
     49     private static final boolean D = Constants.DEBUG;
     50     private static final boolean V = Constants.VERBOSE;
     51 
     52     private static BluetoothOppPreference INSTANCE;
     53 
     54     /* Used when obtaining a reference to the singleton instance. */
     55     private static Object INSTANCE_LOCK = new Object();
     56 
     57     private boolean mInitialized;
     58 
     59     private Context mContext;
     60 
     61     private SharedPreferences mNamePreference;
     62 
     63     private SharedPreferences mChannelPreference;
     64 
     65     private HashMap<String, Integer> mChannels = new HashMap<String, Integer>();
     66 
     67     private HashMap<String, String> mNames = new HashMap<String, String>();
     68 
     69     public static BluetoothOppPreference getInstance(Context context) {
     70         synchronized (INSTANCE_LOCK) {
     71             if (INSTANCE == null) {
     72                 INSTANCE = new BluetoothOppPreference();
     73             }
     74             if (!INSTANCE.init(context)) {
     75                 return null;
     76             }
     77             return INSTANCE;
     78         }
     79     }
     80 
     81     private boolean init(Context context) {
     82         if (mInitialized)
     83             return true;
     84         mInitialized = true;
     85 
     86         mContext = context;
     87 
     88         mNamePreference = mContext.getSharedPreferences(Constants.BLUETOOTHOPP_NAME_PREFERENCE,
     89                 Context.MODE_PRIVATE);
     90         mChannelPreference = mContext.getSharedPreferences(
     91                 Constants.BLUETOOTHOPP_CHANNEL_PREFERENCE, Context.MODE_PRIVATE);
     92 
     93         mNames = (HashMap<String, String>) mNamePreference.getAll();
     94         mChannels = (HashMap<String, Integer>) mChannelPreference.getAll();
     95 
     96         return true;
     97     }
     98 
     99     private String getChannelKey(BluetoothDevice remoteDevice, int uuid) {
    100         return remoteDevice.getAddress() + "_" + Integer.toHexString(uuid);
    101     }
    102 
    103     public String getName(BluetoothDevice remoteDevice) {
    104         if (remoteDevice.getAddress().equals("FF:FF:FF:00:00:00")) {
    105             return "localhost";
    106         }
    107         if (!mNames.isEmpty()) {
    108             String name = mNames.get(remoteDevice.getAddress());
    109             if (name != null) {
    110                 return name;
    111             }
    112         }
    113         return null;
    114     }
    115 
    116     public int getChannel(BluetoothDevice remoteDevice, int uuid) {
    117         String key = getChannelKey(remoteDevice, uuid);
    118         if (V) Log.v(TAG, "getChannel " + key);
    119         Integer channel = null;
    120         if (mChannels != null) {
    121             channel = mChannels.get(key);
    122             if (V) Log.v(TAG, "getChannel for " + remoteDevice + "_" + Integer.toHexString(uuid) +
    123                         " as " + channel);
    124         }
    125         return (channel != null) ? channel : -1;
    126     }
    127 
    128     public void setName(BluetoothDevice remoteDevice, String name) {
    129         if (V) Log.v(TAG, "Setname for " + remoteDevice + " to " + name);
    130         if (!name.equals(getName(remoteDevice))) {
    131             Editor ed = mNamePreference.edit();
    132             ed.putString(remoteDevice.getAddress(), name);
    133             ed.commit();
    134             mNames.put(remoteDevice.getAddress(), name);
    135         }
    136     }
    137 
    138     public void setChannel(BluetoothDevice remoteDevice, int uuid, int channel) {
    139         if (V) Log.v(TAG, "Setchannel for " + remoteDevice + "_" + Integer.toHexString(uuid) + " to "
    140                     + channel);
    141         if (channel != getChannel(remoteDevice, uuid)) {
    142             String key = getChannelKey(remoteDevice, uuid);
    143             Editor ed = mChannelPreference.edit();
    144             ed.putInt(key, channel);
    145             ed.commit();
    146             mChannels.put(key, channel);
    147         }
    148     }
    149 
    150     public void removeChannel(BluetoothDevice remoteDevice, int uuid) {
    151         String key = getChannelKey(remoteDevice, uuid);
    152         Editor ed = mChannelPreference.edit();
    153         ed.remove(key);
    154         ed.commit();
    155         mChannels.remove(key);
    156     }
    157 
    158     public void dump() {
    159         Log.d(TAG, "Dumping Names:  ");
    160         Log.d(TAG, mNames.toString());
    161         Log.d(TAG, "Dumping Channels:  ");
    162         Log.d(TAG, mChannels.toString());
    163     }
    164 }
    165