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 V = Constants.VERBOSE;
50 
51     private static BluetoothOppPreference INSTANCE;
52 
53     /* Used when obtaining a reference to the singleton instance. */
54     private static Object INSTANCE_LOCK = new Object();
55 
56     private boolean mInitialized;
57 
58     private Context mContext;
59 
60     private SharedPreferences mNamePreference;
61 
62     private SharedPreferences mChannelPreference;
63 
64     private HashMap<String, Integer> mChannels = new HashMap<String, Integer>();
65 
66     private HashMap<String, String> mNames = new HashMap<String, String>();
67 
getInstance(Context context)68     public static BluetoothOppPreference getInstance(Context context) {
69         synchronized (INSTANCE_LOCK) {
70             if (INSTANCE == null) {
71                 INSTANCE = new BluetoothOppPreference();
72             }
73             if (!INSTANCE.init(context)) {
74                 return null;
75             }
76             return INSTANCE;
77         }
78     }
79 
init(Context context)80     private boolean init(Context context) {
81         if (mInitialized)
82             return true;
83         mInitialized = true;
84 
85         mContext = context;
86 
87         mNamePreference = mContext.getSharedPreferences(Constants.BLUETOOTHOPP_NAME_PREFERENCE,
88                 Context.MODE_PRIVATE);
89         mChannelPreference = mContext.getSharedPreferences(
90                 Constants.BLUETOOTHOPP_CHANNEL_PREFERENCE, Context.MODE_PRIVATE);
91 
92         mNames = (HashMap<String, String>) mNamePreference.getAll();
93         mChannels = (HashMap<String, Integer>) mChannelPreference.getAll();
94 
95         return true;
96     }
97 
getChannelKey(BluetoothDevice remoteDevice, int uuid)98     private String getChannelKey(BluetoothDevice remoteDevice, int uuid) {
99         return remoteDevice.getAddress() + "_" + Integer.toHexString(uuid);
100     }
101 
getName(BluetoothDevice remoteDevice)102     public String getName(BluetoothDevice remoteDevice) {
103         if (remoteDevice.getAddress().equals("FF:FF:FF:00:00:00")) {
104             return "localhost";
105         }
106         if (!mNames.isEmpty()) {
107             String name = mNames.get(remoteDevice.getAddress());
108             if (name != null) {
109                 return name;
110             }
111         }
112         return null;
113     }
114 
getChannel(BluetoothDevice remoteDevice, int uuid)115     public int getChannel(BluetoothDevice remoteDevice, int uuid) {
116         String key = getChannelKey(remoteDevice, uuid);
117         if (V) Log.v(TAG, "getChannel " + key);
118         Integer channel = null;
119         if (mChannels != null) {
120             channel = mChannels.get(key);
121             if (V) Log.v(TAG, "getChannel for " + remoteDevice + "_" + Integer.toHexString(uuid) +
122                         " as " + channel);
123         }
124         return (channel != null) ? channel : -1;
125     }
126 
setName(BluetoothDevice remoteDevice, String name)127     public void setName(BluetoothDevice remoteDevice, String name) {
128         if (V) Log.v(TAG, "Setname for " + remoteDevice + " to " + name);
129         if (name != null && !name.equals(getName(remoteDevice))) {
130             Editor ed = mNamePreference.edit();
131             ed.putString(remoteDevice.getAddress(), name);
132             ed.apply();
133             mNames.put(remoteDevice.getAddress(), name);
134         }
135     }
136 
setChannel(BluetoothDevice remoteDevice, int uuid, int channel)137     public void setChannel(BluetoothDevice remoteDevice, int uuid, int channel) {
138         if (V) Log.v(TAG, "Setchannel for " + remoteDevice + "_" + Integer.toHexString(uuid) + " to "
139                     + channel);
140         if (channel != getChannel(remoteDevice, uuid)) {
141             String key = getChannelKey(remoteDevice, uuid);
142             Editor ed = mChannelPreference.edit();
143             ed.putInt(key, channel);
144             ed.apply();
145             mChannels.put(key, channel);
146         }
147     }
148 
removeChannel(BluetoothDevice remoteDevice, int uuid)149     public void removeChannel(BluetoothDevice remoteDevice, int uuid) {
150         String key = getChannelKey(remoteDevice, uuid);
151         Editor ed = mChannelPreference.edit();
152         ed.remove(key);
153         ed.apply();
154         mChannels.remove(key);
155     }
156 
dump()157     public void dump() {
158         Log.d(TAG, "Dumping Names:  ");
159         Log.d(TAG, mNames.toString());
160         Log.d(TAG, "Dumping Channels:  ");
161         Log.d(TAG, mChannels.toString());
162     }
163 }
164