BluetoothOppPreference.java revision 09e9cba205af60b3f42e7a4d891a7d1392e1f2a5
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.content.Context;
38import android.content.SharedPreferences;
39import android.content.SharedPreferences.Editor;
40import android.util.Log;
41
42/**
43 * This class cache Bluetooth device name and channel locally
44 * Its a temp solution which should be replaced by bluetooth_devices in
45 * SettingsProvider
46 */
47public class BluetoothOppPreference {
48    private static final String TAG = "BluetoothOppPreference";
49
50    private static BluetoothOppPreference INSTANCE;
51
52    /* Used when obtaining a reference to the singleton instance. */
53    private static Object INSTANCE_LOCK = new Object();
54
55    private boolean mInitialized;
56
57    private Context mContext;
58
59    private SharedPreferences mNamePreference;
60
61    private SharedPreferences mChannelPreference;
62
63    private HashMap<String, Integer> mChannels = new HashMap<String, Integer>();
64
65    private HashMap<String, String> mNames = new HashMap<String, String>();
66
67    public static BluetoothOppPreference getInstance(Context context) {
68        synchronized (INSTANCE_LOCK) {
69            if (INSTANCE == null) {
70                INSTANCE = new BluetoothOppPreference();
71            }
72            if (!INSTANCE.init(context)) {
73                return null;
74            }
75            return INSTANCE;
76        }
77    }
78
79    private boolean init(Context context) {
80        if (mInitialized)
81            return true;
82        mInitialized = true;
83
84        // This will be around as long as this process is
85        mContext = context.getApplicationContext();
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
98    private String getChannelKey(String address, int uuid) {
99        return address + "_" + Integer.toHexString(uuid);
100    }
101
102    public String getName(String address) {
103        if (address.equals("FF:FF:FF:00:00:00")) {
104            return "localhost";
105        }
106        if (!mNames.isEmpty()) {
107            String name = mNames.get(address);
108            if (name != null) {
109                return name;
110            }
111        }
112        return null;
113    }
114
115    public int getChannel(String address, int uuid) {
116        String key = getChannelKey(address, uuid);
117        if (Constants.LOGVV) {
118            Log.v(TAG, "getChannel " + key);
119        }
120        int channel = -1;
121        if (!mChannels.isEmpty()) {
122            channel = mChannels.get(key);
123            if (Constants.LOGVV) {
124                Log.v(TAG, "getChannel for " + address + "_" + Integer.toHexString(uuid) + " as "
125                        + channel);
126            }
127        }
128        return channel;
129    }
130
131    public void setName(String address, String name) {
132        if (Constants.LOGVV) {
133            Log.v(TAG, "Setname for " + address + " to " + name);
134        }
135        if (!name.equals(getName(address))) {
136            Editor ed = mNamePreference.edit();
137            ed.putString(address, name);
138            ed.commit();
139            mNames.put(address, name);
140        }
141    }
142
143    public void setChannel(String address, int uuid, int channel) {
144        if (Constants.LOGVV) {
145            Log.v(TAG, "Setchannel for " + address + "_" + Integer.toHexString(uuid) + " to "
146                    + channel);
147        }
148        if (channel == getChannel(address, uuid)) {
149            String key = getChannelKey(address, uuid);
150            Editor ed = mChannelPreference.edit();
151            ed.putInt(key, channel);
152            ed.commit();
153            mChannels.put(key, channel);
154        }
155    }
156
157    public void removeName(String address) {
158        Editor ed = mNamePreference.edit();
159        ed.remove(address);
160        ed.commit();
161        mNames.remove(address);
162    }
163
164    public void removeChannel(String address, int uuid) {
165        String key = getChannelKey(address, uuid);
166        Editor ed = mChannelPreference.edit();
167        ed.remove(key);
168        ed.commit();
169        mChannels.remove(key);
170    }
171
172    public void dump() {
173        Log.d(TAG, "Dumping Names:  ");
174        Log.d(TAG, mNames.toString());
175        Log.d(TAG, "Dumping Channels:  ");
176        Log.d(TAG, mChannels.toString());
177    }
178}
179