109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly/*
209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Copyright (c) 2008-2009, Motorola, Inc.
309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * All rights reserved.
509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * Redistribution and use in source and binary forms, with or without
709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * modification, are permitted provided that the following conditions are met:
809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * - Redistributions of source code must retain the above copyright notice,
1009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * this list of conditions and the following disclaimer.
1109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
1209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * - Redistributions in binary form must reproduce the above copyright notice,
1309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * this list of conditions and the following disclaimer in the documentation
1409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * and/or other materials provided with the distribution.
1509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
1609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * - Neither the name of the Motorola, Inc. nor the names of its contributors
1709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * may be used to endorse or promote products derived from this software
1809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * without specific prior written permission.
1909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly *
2009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
2409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly * POSSIBILITY OF SUCH DAMAGE.
3109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */
3209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
3309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellypackage com.android.bluetooth.opp;
3409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
3541ef8d494511c040451f2f887cb31c3100746b61Nick Pellyimport android.bluetooth.BluetoothDevice;
3609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.Context;
3709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.SharedPreferences;
3809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.content.SharedPreferences.Editor;
3909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellyimport android.util.Log;
4009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
41c4fbd756e2645147470c486ae96f2253f5e13a52Jack Heimport java.util.HashMap;
42c4fbd756e2645147470c486ae96f2253f5e13a52Jack He
4309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly/**
44888485a3f5fe991116c5536bb6d6903d47b63a70Tao Liejun * This class cache Bluetooth device name and channel locally. Its a temp
456769b59d715ea98bd72eafcfea9acd2714a887daTao Liejun * solution which should be replaced by bluetooth_devices in SettingsProvider
4609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly */
4709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pellypublic class BluetoothOppPreference {
4809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private static final String TAG = "BluetoothOppPreference";
49ce4d93666275df294cb073fe41de5b85932570a8Nick Pelly    private static final boolean V = Constants.VERBOSE;
5009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
511bd017d12cf16ecd52fb486722e300790bddeefcJack He    private static BluetoothOppPreference sInstance;
5209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
5309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    /* Used when obtaining a reference to the singleton instance. */
541bd017d12cf16ecd52fb486722e300790bddeefcJack He    private static final Object INSTANCE_LOCK = new Object();
5509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
5609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private boolean mInitialized;
5709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
5809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private Context mContext;
5909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
6009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private SharedPreferences mNamePreference;
6109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
6209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private SharedPreferences mChannelPreference;
6309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
6409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private HashMap<String, Integer> mChannels = new HashMap<String, Integer>();
6509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
6609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private HashMap<String, String> mNames = new HashMap<String, String>();
6709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
6809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    public static BluetoothOppPreference getInstance(Context context) {
6909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        synchronized (INSTANCE_LOCK) {
701bd017d12cf16ecd52fb486722e300790bddeefcJack He            if (sInstance == null) {
711bd017d12cf16ecd52fb486722e300790bddeefcJack He                sInstance = new BluetoothOppPreference();
7209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
731bd017d12cf16ecd52fb486722e300790bddeefcJack He            if (!sInstance.init(context)) {
7409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                return null;
7509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
761bd017d12cf16ecd52fb486722e300790bddeefcJack He            return sInstance;
7709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
7809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
7909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
8009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    private boolean init(Context context) {
81c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        if (mInitialized) {
8209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            return true;
83c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        }
8409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        mInitialized = true;
8509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
860bd5f7b5c36a60687ffe895368fd8df6df38acabJaikumar Ganesh        mContext = context;
8709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
8809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        mNamePreference = mContext.getSharedPreferences(Constants.BLUETOOTHOPP_NAME_PREFERENCE,
8909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                Context.MODE_PRIVATE);
90c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        mChannelPreference =
91c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                mContext.getSharedPreferences(Constants.BLUETOOTHOPP_CHANNEL_PREFERENCE,
92c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                        Context.MODE_PRIVATE);
9309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
9441ef8d494511c040451f2f887cb31c3100746b61Nick Pelly        mNames = (HashMap<String, String>) mNamePreference.getAll();
9541ef8d494511c040451f2f887cb31c3100746b61Nick Pelly        mChannels = (HashMap<String, Integer>) mChannelPreference.getAll();
9609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
9709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        return true;
9809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
9909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
10041ef8d494511c040451f2f887cb31c3100746b61Nick Pelly    private String getChannelKey(BluetoothDevice remoteDevice, int uuid) {
10141ef8d494511c040451f2f887cb31c3100746b61Nick Pelly        return remoteDevice.getAddress() + "_" + Integer.toHexString(uuid);
10209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
10309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
10441ef8d494511c040451f2f887cb31c3100746b61Nick Pelly    public String getName(BluetoothDevice remoteDevice) {
10541ef8d494511c040451f2f887cb31c3100746b61Nick Pelly        if (remoteDevice.getAddress().equals("FF:FF:FF:00:00:00")) {
10609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            return "localhost";
10709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
10809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        if (!mNames.isEmpty()) {
10941ef8d494511c040451f2f887cb31c3100746b61Nick Pelly            String name = mNames.get(remoteDevice.getAddress());
11009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            if (name != null) {
11109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                return name;
11209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            }
11309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
11409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        return null;
11509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
11609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
11741ef8d494511c040451f2f887cb31c3100746b61Nick Pelly    public int getChannel(BluetoothDevice remoteDevice, int uuid) {
11841ef8d494511c040451f2f887cb31c3100746b61Nick Pelly        String key = getChannelKey(remoteDevice, uuid);
119c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        if (V) {
120c4fbd756e2645147470c486ae96f2253f5e13a52Jack He            Log.v(TAG, "getChannel " + key);
121c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        }
122888485a3f5fe991116c5536bb6d6903d47b63a70Tao Liejun        Integer channel = null;
123888485a3f5fe991116c5536bb6d6903d47b63a70Tao Liejun        if (mChannels != null) {
12409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            channel = mChannels.get(key);
125c4fbd756e2645147470c486ae96f2253f5e13a52Jack He            if (V) {
126c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                Log.v(TAG,
127c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                        "getChannel for " + remoteDevice + "_" + Integer.toHexString(uuid) + " as "
128c4fbd756e2645147470c486ae96f2253f5e13a52Jack He                                + channel);
129c4fbd756e2645147470c486ae96f2253f5e13a52Jack He            }
13009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
131888485a3f5fe991116c5536bb6d6903d47b63a70Tao Liejun        return (channel != null) ? channel : -1;
13209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
13309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
13441ef8d494511c040451f2f887cb31c3100746b61Nick Pelly    public void setName(BluetoothDevice remoteDevice, String name) {
135c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        if (V) {
136c4fbd756e2645147470c486ae96f2253f5e13a52Jack He            Log.v(TAG, "Setname for " + remoteDevice + " to " + name);
137c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        }
13898a53e5bdb2500109c304f2b5364c1715ab745d8martin ma        if (name != null && !name.equals(getName(remoteDevice))) {
13909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            Editor ed = mNamePreference.edit();
14041ef8d494511c040451f2f887cb31c3100746b61Nick Pelly            ed.putString(remoteDevice.getAddress(), name);
14155470710c2624bde2aa56275d29b60055638c06cBrad Fitzpatrick            ed.apply();
14241ef8d494511c040451f2f887cb31c3100746b61Nick Pelly            mNames.put(remoteDevice.getAddress(), name);
14309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
14409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
14509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
14641ef8d494511c040451f2f887cb31c3100746b61Nick Pelly    public void setChannel(BluetoothDevice remoteDevice, int uuid, int channel) {
147c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        if (V) {
148c4fbd756e2645147470c486ae96f2253f5e13a52Jack He            Log.v(TAG, "Setchannel for " + remoteDevice + "_" + Integer.toHexString(uuid) + " to "
14909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly                    + channel);
150c4fbd756e2645147470c486ae96f2253f5e13a52Jack He        }
151888485a3f5fe991116c5536bb6d6903d47b63a70Tao Liejun        if (channel != getChannel(remoteDevice, uuid)) {
15241ef8d494511c040451f2f887cb31c3100746b61Nick Pelly            String key = getChannelKey(remoteDevice, uuid);
15309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            Editor ed = mChannelPreference.edit();
15409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            ed.putInt(key, channel);
15555470710c2624bde2aa56275d29b60055638c06cBrad Fitzpatrick            ed.apply();
15609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly            mChannels.put(key, channel);
15709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        }
15809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
15909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
16041ef8d494511c040451f2f887cb31c3100746b61Nick Pelly    public void removeChannel(BluetoothDevice remoteDevice, int uuid) {
16141ef8d494511c040451f2f887cb31c3100746b61Nick Pelly        String key = getChannelKey(remoteDevice, uuid);
16209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Editor ed = mChannelPreference.edit();
16309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        ed.remove(key);
16455470710c2624bde2aa56275d29b60055638c06cBrad Fitzpatrick        ed.apply();
16509e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        mChannels.remove(key);
16609e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
16709e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly
16809e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    public void dump() {
16909e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Log.d(TAG, "Dumping Names:  ");
17009e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Log.d(TAG, mNames.toString());
17109e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Log.d(TAG, "Dumping Channels:  ");
17209e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly        Log.d(TAG, mChannels.toString());
17309e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly    }
17409e9cba205af60b3f42e7a4d891a7d1392e1f2a5Nick Pelly}
175