Back to project page lamp.
The source code is released under:
GNU General Public License
If you think the Android project lamp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
/** Copyright (C) 2013 Marek Sebera <marek@msebera.cz> *// w ww.ja v a2 s. c om * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ package cz.tomsuch.lampicka.util; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Random; import android.app.Application; import android.bluetooth.BluetoothDevice; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Color; import android.os.ParcelUuid; import android.preference.PreferenceManager; import cz.tomsuch.lampicka.R; import cz.tomsuch.lampicka.pallete.PalleteDialog.PALLETE_CONTROL; /** * Preferences (Key-Value) storage, singleton-pattern * * @author Marek Sebera <marek@msebera.cz> * */ public class Preferences { private static volatile Preferences staticInstance; /** * Gets instance <b>Must be called after {@link #getInstance(Application)} * </b> * */ public static Preferences getInstance() { assert (staticInstance != null); return staticInstance; } /** * Initializes new singleton instance of Preferences class. Forcing usage of * Application class to not cause memory leaks. * * @param a * Application, must not be null * */ public static Preferences getInstance(Application a) { assert (a != null); synchronized (Preferences.class) { if (staticInstance == null) staticInstance = new Preferences(a.getApplicationContext()); } return staticInstance; } protected Context context; private List<BluetoothDevice> bt_devices = new ArrayList<BluetoothDevice>(); private Preferences(Context c) { this.context = c; } /** * Adds all devices from List <b>newDevices</b> * * Accessor method for BluetoothDevices storage * * @param newDevices * List of BluetoothDevices to be added, can be null * */ public void addAllBluetoothDevices(List<BluetoothDevice> newDevices) { synchronized (bt_devices) { if (newDevices != null) { for (BluetoothDevice device : newDevices) { addBluetoothDevice(device); } } } } /** * Adds single device, if it doesn't already exists in storage * * Accessor method for BluetoothDevices storage * * @param device * single BluetoothDevice to be added * */ public boolean addBluetoothDevice(BluetoothDevice device) { synchronized (bt_devices) { Iterator<BluetoothDevice> iterator = bt_devices.iterator(); while (iterator.hasNext()) { BluetoothDevice bd = iterator.next(); if (bd.getAddress().equals(device.getAddress())) { bt_devices.remove(bd); break; } } bt_devices.add(device); Collections.sort(bt_devices, new BluetoothDeviceComparator()); } return true; } /** * Removes all BluetoothDevices stored * * Accessor method for BluetoothDevices storage * */ public boolean clearBluetoothDevices() { synchronized (bt_devices) { bt_devices.clear(); } return true; } /** * Retrieves all stored devices * */ public List<BluetoothDevice> getAllDevices() { return this.bt_devices; } /** * Retrieves single device by it's MAC address * * @param address * MAC address of bluetooth device * */ public BluetoothDevice getDeviceByAddress(String address) { synchronized (bt_devices) { Iterator<BluetoothDevice> iterator = bt_devices.iterator(); while (iterator.hasNext()) { BluetoothDevice bd = iterator.next(); if (bd.getAddress().equals(address)) { return bd; } } Collections.sort(bt_devices, new BluetoothDeviceComparator()); } return null; } /** * Returns last used BluetoothDevice * */ public BluetoothDeviceWrapper getLastBluetoothDevice() { return new BluetoothDeviceWrapper(getDeviceAddress(), getDeviceName(), getDeviceUuids()); } private static final String DEVICE_ADDRESS = "DEVICE_ADDRESS"; private static final String DEVICE_NAME = "DEVICE_NAME"; private static final String DEVICE_UUIDS = "DEVICE_UUIDS"; private static final String DEVICE_SERVICE_UUID = "SERVICE_UUID"; private SharedPreferences preferences; /** * Returns SharedPreferences instance * */ private SharedPreferences getPreferences() { if (preferences == null) { preferences = PreferenceManager .getDefaultSharedPreferences(context); } return preferences; } /** * Saves used bluetooth device, even if it is the same as last * */ public void saveBluetoothDevice(BluetoothDevice bd) { saveDeviceAddress(bd.getAddress()); saveDeviceName(bd.getName()); saveDeviceUuids(bd.getUuids()); } /** * Helper to persist bluetooth device address * * @param address * last bluetooth device address (mac address) * */ private boolean saveDeviceAddress(String address) { return getPreferences().edit().putString(DEVICE_ADDRESS, address) .commit(); } /** * Helper to persist bluetooth device name * * @param name * last bluetooth device name * */ private boolean saveDeviceName(String name) { return getPreferences().edit().putString(DEVICE_NAME, name).commit(); } /** * Helper to persist bluetooth device service uuids * * @param uuids * device uuids, can be null * */ private boolean saveDeviceUuids(ParcelUuid[] uuids) { String _uuids = ""; if (uuids != null) { for (ParcelUuid u : uuids) { _uuids += u.toString() + ","; } _uuids.substring(0, _uuids.length() - 1); } return getPreferences().edit().putString(DEVICE_UUIDS, _uuids).commit(); } /** * Helper to retrieve last bluetooth device address * */ private String getDeviceAddress() { return getPreferences().getString(DEVICE_ADDRESS, null); } /** * Helper to retrieve last bluetooth device name * */ private String getDeviceName() { return getPreferences().getString(DEVICE_NAME, context.getString(R.string.no_name)); } /** * Helper to retrieve last bluetooth device service uuids * */ private ParcelUuid[] getDeviceUuids() { String _uuids = getPreferences().getString(DEVICE_UUIDS, null); if (_uuids == null) return null; String[] parts = _uuids.split(","); ParcelUuid[] uuids = new ParcelUuid[parts.length]; int index = 0; try { for (String part : parts) { uuids[index] = ParcelUuid.fromString(part); index++; } } catch (Exception e) { } return uuids; } /** * Persists service UUID from SDP * * @param serviceUuid * discovered service UUID, can be null * */ public boolean saveLastServiceUuid(ParcelUuid serviceUuid) { if (serviceUuid == null) return false; return getPreferences().edit() .putString(DEVICE_SERVICE_UUID, serviceUuid.toString()) .commit(); } /** * Retrieves last saved service UUID, to overcome bug with SDP on some * devices * */ public ParcelUuid getLastBluetoothServiceUuid() { String savedUuid = getPreferences() .getString(DEVICE_SERVICE_UUID, null); if (savedUuid == null) return null; else return ParcelUuid.fromString(savedUuid); } public int getSavedColor(PALLETE_CONTROL e, int defaultValue) { return getPreferences().getInt("color_" + e.toString(), defaultValue); } public void clearSavedColor(PALLETE_CONTROL e) { getPreferences().edit().remove("color_" + e.toString()).commit(); } public static final int getRandomColor() { Random rnd = new Random(); return Color.rgb(rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256)); } public boolean saveCurrentColor(int color, PALLETE_CONTROL e) { return getPreferences().edit().putInt("color_" + e.toString(), color) .commit(); } }