BluetoothOppPreference.java revision c4fbd756e2645147470c486ae96f2253f5e13a52
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 android.bluetooth.BluetoothDevice;
36import android.content.Context;
37import android.content.SharedPreferences;
38import android.content.SharedPreferences.Editor;
39import android.util.Log;
40
41import 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 */
47public 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
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
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
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) {
120            Log.v(TAG, "getChannel " + key);
121        }
122        Integer channel = null;
123        if (mChannels != null) {
124            channel = mChannels.get(key);
125            if (V) {
126                Log.v(TAG,
127                        "getChannel for " + remoteDevice + "_" + Integer.toHexString(uuid) + " as "
128                                + channel);
129            }
130        }
131        return (channel != null) ? channel : -1;
132    }
133
134    public void setName(BluetoothDevice remoteDevice, String name) {
135        if (V) {
136            Log.v(TAG, "Setname for " + remoteDevice + " to " + name);
137        }
138        if (name != null && !name.equals(getName(remoteDevice))) {
139            Editor ed = mNamePreference.edit();
140            ed.putString(remoteDevice.getAddress(), name);
141            ed.apply();
142            mNames.put(remoteDevice.getAddress(), name);
143        }
144    }
145
146    public void setChannel(BluetoothDevice remoteDevice, int uuid, int channel) {
147        if (V) {
148            Log.v(TAG, "Setchannel for " + remoteDevice + "_" + Integer.toHexString(uuid) + " to "
149                    + channel);
150        }
151        if (channel != getChannel(remoteDevice, uuid)) {
152            String key = getChannelKey(remoteDevice, uuid);
153            Editor ed = mChannelPreference.edit();
154            ed.putInt(key, channel);
155            ed.apply();
156            mChannels.put(key, channel);
157        }
158    }
159
160    public void removeChannel(BluetoothDevice remoteDevice, int uuid) {
161        String key = getChannelKey(remoteDevice, uuid);
162        Editor ed = mChannelPreference.edit();
163        ed.remove(key);
164        ed.apply();
165        mChannels.remove(key);
166    }
167
168    public void dump() {
169        Log.d(TAG, "Dumping Names:  ");
170        Log.d(TAG, mNames.toString());
171        Log.d(TAG, "Dumping Channels:  ");
172        Log.d(TAG, mChannels.toString());
173    }
174}
175