Android Open Source - lamp Bluetooth Devices Adapter






From Project

Back to project page lamp.

License

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.

Java Source Code

/**    Copyright (C) 2013 Marek Sebera <marek@msebera.cz>
 */*  w ww .  j  a v a 2s .c o  m*/
 *    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.adapters;

import java.util.List;

import cz.tomsuch.lampicka.R;
import cz.tomsuch.lampicka.util.Preferences;

import android.bluetooth.BluetoothDevice;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * Adapter for LampsActivity, it is based on {@link Preferences} instance
 * 
 * @author Marek Sebera <marek@msebera.cz>
 * */
public class BluetoothDevicesAdapter extends BaseAdapter {

  private Preferences preferences = Preferences.getInstance();

  /**
   * Adds new device and notifies overlaying ListView
   * */
  public void addDevice(BluetoothDevice device) {
    preferences.addBluetoothDevice(device);
    notifyDataSetChanged();
  }

  @Override
  public int getCount() {
    return preferences.getAllDevices().size();
  }

  /**
   * Returns single BluetoothDevice from storage or null if 'position' is
   * wrong
   * 
   * @param position
   *            index of BluetoothDevice in adapter
   * */
  public BluetoothDevice getDeviceByPosition(int position) {
    return preferences.getAllDevices().get(position);
  }

  @Override
  public Object getItem(int position) {
    return preferences.getAllDevices().get(position);
  }

  @Override
  public long getItemId(int position) {
    return position;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    LinearLayout layout = new LinearLayout(parent.getContext());
    TextView tv = new TextView(parent.getContext());
    BluetoothDevice d = getDeviceByPosition(position);
    tv.setText(String.format("%s  [%s]", d.getName() == null ? parent
        .getContext().getString(R.string.no_name) : d.getName(), d
        .getAddress()));
    tv.setTextSize(20);
    layout.setPadding(10, 10, 10, 10);
    layout.addView(tv, new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.WRAP_CONTENT,
        LinearLayout.LayoutParams.WRAP_CONTENT));
    return layout;
  }

  @Override
  public boolean hasStableIds() {
    return true;
  }

  /**
   * Allows to set contents using whole list of BluetoothDevices. Any previous
   * content will be erased by calling this method.
   * 
   * @param newDevices
   *            devices to be displayed
   * */
  public void setData(List<BluetoothDevice> newDevices) {
    preferences.clearBluetoothDevices();
    preferences.addAllBluetoothDevices(newDevices);
    notifyDataSetInvalidated();
  }

}




Java Source Code List

cz.tomsuch.lampicka.AppController.java
cz.tomsuch.lampicka.abstracts.CustomSeekBar.java
cz.tomsuch.lampicka.activities.LampActivity.java
cz.tomsuch.lampicka.activities.LampsActivity.java
cz.tomsuch.lampicka.adapters.BluetoothDevicesAdapter.java
cz.tomsuch.lampicka.enums.BluetoothLampBacklightMode.java
cz.tomsuch.lampicka.enums.BluetoothLampColorSetMode.java
cz.tomsuch.lampicka.enums.BluetoothLampCommand.java
cz.tomsuch.lampicka.enums.BluetoothLampEffect.java
cz.tomsuch.lampicka.impl.DefaultBluetoothLamp.java
cz.tomsuch.lampicka.interfaces.BluetoothInputLineListener.java
cz.tomsuch.lampicka.interfaces.BluetoothLampCommandListener.java
cz.tomsuch.lampicka.interfaces.BluetoothLamp.java
cz.tomsuch.lampicka.interfaces.OnColorChangedListener.java
cz.tomsuch.lampicka.interfaces.PalleteDialogColorListener.java
cz.tomsuch.lampicka.pallete.ColorChooserDialog.java
cz.tomsuch.lampicka.pallete.PalleteDialog.java
cz.tomsuch.lampicka.util.BluetoothDeviceComparator.java
cz.tomsuch.lampicka.util.BluetoothDeviceWrapper.java
cz.tomsuch.lampicka.util.BluetoothSocketListener.java
cz.tomsuch.lampicka.util.ColorPickerView.java
cz.tomsuch.lampicka.util.FixedBluetoothSocket.java
cz.tomsuch.lampicka.util.HorizontalSeekBar.java
cz.tomsuch.lampicka.util.Preferences.java
cz.tomsuch.lampicka.util.VerticalSeekBar.java