BluetoothOppPreference.java revision 888485a3f5fe991116c5536bb6d6903d47b63a70
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 33package com.android.bluetooth.opp; 34 35import java.util.HashMap; 36 37import android.bluetooth.BluetoothDevice; 38import android.content.Context; 39import android.content.SharedPreferences; 40import android.content.SharedPreferences.Editor; 41import 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 */ 47public 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 // This will be around as long as this process is 87 mContext = context.getApplicationContext(); 88 89 mNamePreference = mContext.getSharedPreferences(Constants.BLUETOOTHOPP_NAME_PREFERENCE, 90 Context.MODE_PRIVATE); 91 mChannelPreference = mContext.getSharedPreferences( 92 Constants.BLUETOOTHOPP_CHANNEL_PREFERENCE, Context.MODE_PRIVATE); 93 94 mNames = (HashMap<String, String>) mNamePreference.getAll(); 95 mChannels = (HashMap<String, Integer>) mChannelPreference.getAll(); 96 97 return true; 98 } 99 100 private String getChannelKey(BluetoothDevice remoteDevice, int uuid) { 101 return remoteDevice.getAddress() + "_" + Integer.toHexString(uuid); 102 } 103 104 public String getName(BluetoothDevice remoteDevice) { 105 if (remoteDevice.getAddress().equals("FF:FF:FF:00:00:00")) { 106 return "localhost"; 107 } 108 if (!mNames.isEmpty()) { 109 String name = mNames.get(remoteDevice.getAddress()); 110 if (name != null) { 111 return name; 112 } 113 } 114 return null; 115 } 116 117 public int getChannel(BluetoothDevice remoteDevice, int uuid) { 118 String key = getChannelKey(remoteDevice, uuid); 119 if (V) Log.v(TAG, "getChannel " + key); 120 Integer channel = null; 121 if (mChannels != null) { 122 channel = mChannels.get(key); 123 if (V) Log.v(TAG, "getChannel for " + remoteDevice + "_" + Integer.toHexString(uuid) + 124 " as " + channel); 125 } 126 return (channel != null) ? channel : -1; 127 } 128 129 public void setName(BluetoothDevice remoteDevice, String name) { 130 if (V) Log.v(TAG, "Setname for " + remoteDevice + " to " + name); 131 if (!name.equals(getName(remoteDevice))) { 132 Editor ed = mNamePreference.edit(); 133 ed.putString(remoteDevice.getAddress(), name); 134 ed.commit(); 135 mNames.put(remoteDevice.getAddress(), name); 136 } 137 } 138 139 public void setChannel(BluetoothDevice remoteDevice, int uuid, int channel) { 140 if (V) Log.v(TAG, "Setchannel for " + remoteDevice + "_" + Integer.toHexString(uuid) + " to " 141 + channel); 142 if (channel != getChannel(remoteDevice, uuid)) { 143 String key = getChannelKey(remoteDevice, uuid); 144 Editor ed = mChannelPreference.edit(); 145 ed.putInt(key, channel); 146 ed.commit(); 147 mChannels.put(key, channel); 148 } 149 } 150 151 public void removeChannel(BluetoothDevice remoteDevice, int uuid) { 152 String key = getChannelKey(remoteDevice, uuid); 153 Editor ed = mChannelPreference.edit(); 154 ed.remove(key); 155 ed.commit(); 156 mChannels.remove(key); 157 } 158 159 public void dump() { 160 Log.d(TAG, "Dumping Names: "); 161 Log.d(TAG, mNames.toString()); 162 Log.d(TAG, "Dumping Channels: "); 163 Log.d(TAG, mChannels.toString()); 164 } 165} 166