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