• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.bluetooth.BluetoothDevice;
36 import android.content.Context;
37 import android.content.SharedPreferences;
38 import android.content.SharedPreferences.Editor;
39 import android.util.Log;
40 
41 import java.util.HashMap;
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 sInstance;
52 
53     /* Used when obtaining a reference to the singleton instance. */
54     private static final 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 (sInstance == null) {
71                 sInstance = new BluetoothOppPreference();
72             }
73             if (!sInstance.init(context)) {
74                 return null;
75             }
76             return sInstance;
77         }
78     }
79 
init(Context context)80     private boolean init(Context context) {
81         if (mInitialized) {
82             return true;
83         }
84         mInitialized = true;
85 
86         mContext = context;
87 
88         mNamePreference = mContext.getSharedPreferences(Constants.BLUETOOTHOPP_NAME_PREFERENCE,
89                 Context.MODE_PRIVATE);
90         mChannelPreference =
91                 mContext.getSharedPreferences(Constants.BLUETOOTHOPP_CHANNEL_PREFERENCE,
92                         Context.MODE_PRIVATE);
93 
94         mNames = (HashMap<String, String>) mNamePreference.getAll();
95         mChannels = (HashMap<String, Integer>) mChannelPreference.getAll();
96 
97         return true;
98     }
99 
getChannelKey(BluetoothDevice remoteDevice, int uuid)100     private String getChannelKey(BluetoothDevice remoteDevice, int uuid) {
101         return remoteDevice.getIdentityAddress() + "_" + Integer.toHexString(uuid);
102     }
103 
getName(BluetoothDevice remoteDevice)104     public String getName(BluetoothDevice remoteDevice) {
105         String identityAddress = remoteDevice.getIdentityAddress();
106         if (identityAddress != null && identityAddress.equals("FF:FF:FF:00:00:00")) {
107             return "localhost";
108         }
109         if (!mNames.isEmpty()) {
110             String name = mNames.get(remoteDevice.getIdentityAddress());
111             if (name != null) {
112                 return name;
113             }
114         }
115         return null;
116     }
117 
getChannel(BluetoothDevice remoteDevice, int uuid)118     public int getChannel(BluetoothDevice remoteDevice, int uuid) {
119         String key = getChannelKey(remoteDevice, uuid);
120         if (V) {
121             Log.v(TAG, "getChannel " + key);
122         }
123         Integer channel = null;
124         if (mChannels != null) {
125             channel = mChannels.get(key);
126             if (V) {
127                 Log.v(TAG,
128                         "getChannel for " + remoteDevice.getIdentityAddress() + "_" + Integer.toHexString(uuid) + " as "
129                                 + channel);
130             }
131         }
132         return (channel != null) ? channel : -1;
133     }
134 
setName(BluetoothDevice remoteDevice, String name)135     public void setName(BluetoothDevice remoteDevice, String name) {
136         if (V) {
137             Log.v(TAG, "Setname for " + remoteDevice.getIdentityAddress() + " to " + name);
138         }
139         if (name != null && !name.equals(getName(remoteDevice))) {
140             Editor ed = mNamePreference.edit();
141             ed.putString(remoteDevice.getIdentityAddress(), name);
142             ed.apply();
143             mNames.put(remoteDevice.getIdentityAddress(), name);
144         }
145     }
146 
setChannel(BluetoothDevice remoteDevice, int uuid, int channel)147     public void setChannel(BluetoothDevice remoteDevice, int uuid, int channel) {
148         if (V) {
149             Log.v(TAG, "Setchannel for " + remoteDevice.getIdentityAddress() + "_" + Integer.toHexString(uuid) + " to "
150                     + channel);
151         }
152         if (channel != getChannel(remoteDevice, uuid)) {
153             String key = getChannelKey(remoteDevice, uuid);
154             Editor ed = mChannelPreference.edit();
155             ed.putInt(key, channel);
156             ed.apply();
157             mChannels.put(key, channel);
158         }
159     }
160 
removeChannel(BluetoothDevice remoteDevice, int uuid)161     public void removeChannel(BluetoothDevice remoteDevice, int uuid) {
162         String key = getChannelKey(remoteDevice, uuid);
163         Editor ed = mChannelPreference.edit();
164         ed.remove(key);
165         ed.apply();
166         mChannels.remove(key);
167     }
168 
dump()169     public void dump() {
170         Log.d(TAG, "Dumping Names:  ");
171         Log.d(TAG, mNames.toString());
172         Log.d(TAG, "Dumping Channels:  ");
173         Log.d(TAG, mChannels.toString());
174     }
175 }
176