Android Open Source - metawear-antifitness Device List Adapter






From Project

Back to project page metawear-antifitness.

License

The source code is released under:

GNU General Public License

If you think the Android project metawear-antifitness 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 Nordic Semiconductor. All Rights Reserved.
 * //from   ww w  .  ja  v  a 2 s  .  c  o m
 * The information contained herein is property of Nordic Semiconductor ASA.
 * Terms and conditions of usage are described in detail in NORDIC SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
 * Licensees are granted free, non-transferable use of the information. NO WARRANTY of ANY KIND is provided. 
 * This heading must NOT be removed from the file.
 ******************************************************************************/

/*
 * NORDIC SEMICONDUTOR EXAMPLE CODE AND LICENSE AGREEMENT
 *
 * You are receiving this document because you have obtained example code (?Software?) 
 * from Nordic Semiconductor ASA * (?Licensor?). The Software is protected by copyright 
 * laws and international treaties. All intellectual property rights related to the 
 * Software is the property of the Licensor. This document is a license agreement governing 
 * your rights and obligations regarding usage of the Software. Any variation to the terms 
 * of this Agreement shall only be valid if made in writing by the Licensor.
 * 
 * == Scope of license rights ==
 * 
 * You are hereby granted a limited, non-exclusive, perpetual right to use and modify the 
 * Software in order to create your own software. You are entitled to distribute the 
 * Software in original or modified form as part of your own software.
 *
 * If distributing your software in source code form, a copy of this license document shall 
 * follow with the distribution.
 *   
 * The Licensor can at any time terminate your rights under this license agreement.
 * 
 * == Restrictions on license rights ==
 * 
 * You are not allowed to distribute the Software on its own, without incorporating it into 
 * your own software.  
 * 
 * You are not allowed to remove, alter or destroy any proprietary, 
 * trademark or copyright markings or notices placed upon or contained with the Software.
 *     
 * You shall not use Licensor?s name or trademarks without Licensor?s prior consent.
 * 
 * == Disclaimer of warranties and limitation of liability ==
 * 
 * YOU EXPRESSLY ACKNOWLEDGE AND AGREE THAT USE OF THE SOFTWARE IS AT YOUR OWN RISK AND THAT THE 
 * SOFTWARE IS PROVIDED *AS IS" WITHOUT ANY WARRANTIES OR CONDITIONS WHATSOEVER. NORDIC SEMICONDUCTOR ASA 
 * DOES NOT WARRANT THAT THE FUNCTIONS OF THE SOFTWARE WILL MEET YOUR REQUIREMENTS OR THAT THE 
 * OPERATION OF THE SOFTWARE WILL BE UNINTERRUPTED OR ERROR FREE. YOU ASSUME RESPONSIBILITY FOR 
 * SELECTING THE SOFTWARE TO ACHIEVE YOUR INTENDED RESULTS, AND FOR THE *USE AND THE RESULTS 
 * OBTAINED FROM THE SOFTWARE.
 * 
 * NORDIC SEMICONDUCTOR ASA DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO WARRANTIES RELATED TO: NON-INFRINGEMENT, LACK OF VIRUSES, ACCURACY OR COMPLETENESS OF RESPONSES 
 * OR RESULTS, IMPLIED  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 * 
 * IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA BE LIABLE FOR ANY INDIRECT, INCIDENTAL, SPECIAL OR 
 * CONSEQUENTIAL DAMAGES OR FOR ANY DAMAGES WHATSOEVER (INCLUDING BUT NOT LIMITED TO DAMAGES FOR 
 * LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS INFORMATION, PERSONAL INJURY, 
 * LOSS OF PRIVACY OR OTHER PECUNIARY OR OTHER LOSS WHATSOEVER) ARISING OUT OF USE OR INABILITY TO 
 * USE THE SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 * 
 * REGARDLESS OF THE FORM OF ACTION, NORDIC SEMICONDUCTOR ASA AGGREGATE LIABILITY ARISING OUT OF 
 * OR RELATED TO THIS AGREEMENT SHALL NOT EXCEED THE TOTAL AMOUNT PAYABLE BY YOU UNDER THIS AGREEMENT. 
 * THE FOREGOING LIMITATIONS, EXCLUSIONS AND DISCLAIMERS SHALL APPLY TO THE MAXIMUM EXTENT ALLOWED BY 
 * APPLICABLE LAW.
 * 
 * == Dispute resolution and legal venue ==
 * 
 * Any and all disputes arising out of the rights and obligations in this license agreement shall be 
 * submitted to ordinary court proceedings. You accept the Oslo City Court as legal venue under this agreement.
 * 
 * This license agreement shall be governed by Norwegian law.
 * 
 * == Contact information ==
 * 
 * All requests regarding the Software or the API shall be directed to: 
 * Nordic Semiconductor ASA, P.O. Box 436, Sk?yen, 0213 Oslo, Norway.
 * 
 * http://www.nordicsemi.com/eng/About-us/Contact-us
 */
package no.nordicsemi.android.nrftoolbox.scanner;

import java.util.ArrayList;

import za.co.paulscott.antifitness.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * DeviceListAdapter class is list adapter for showing scanned Devices name,
 * address and RSSI image based on RSSI values.
 */
public class DeviceListAdapter extends BaseAdapter {
  private static final int TYPE_TITLE = 0;
  private static final int TYPE_ITEM = 1;
  private static final int TYPE_EMPTY = 2;

  private final ArrayList<ExtendedBluetoothDevice> mListBondedValues = new ArrayList<ExtendedBluetoothDevice>();
  private final ArrayList<ExtendedBluetoothDevice> mListValues = new ArrayList<ExtendedBluetoothDevice>();
  private final Context mContext;
  private final ExtendedBluetoothDevice.AddressComparator comparator = new ExtendedBluetoothDevice.AddressComparator();

  public DeviceListAdapter(Context context) {
    mContext = context;
  }

  public void addBondedDevice(ExtendedBluetoothDevice device) {
    mListBondedValues.add(device);
    notifyDataSetChanged();
  }

  /**
   * Looks for the device with the same address as given one in the list of
   * bonded devices. If the device has been found it updates its RSSI value.
   * 
   * @param address
   *            the device address
   * @param rssi
   *            the RSSI of the scanned device
   */
  public void updateRssiOfBondedDevice(String address, int rssi) {
    comparator.address = address;
    final int indexInBonded = mListBondedValues.indexOf(comparator);
    if (indexInBonded >= 0) {
      ExtendedBluetoothDevice previousDevice = mListBondedValues
          .get(indexInBonded);
      previousDevice.rssi = rssi;
      notifyDataSetChanged();
    }
  }

  /**
   * If such device exists on the bonded device list, this method does
   * nothing. If not then the device is updated (rssi value) or added.
   * 
   * @param device
   *            the device to be added or updated
   */
  public void addOrUpdateDevice(ExtendedBluetoothDevice device) {
    final boolean indexInBonded = mListBondedValues.contains(device);
    if (indexInBonded) {
      return;
    }

    final int indexInNotBonded = mListValues.indexOf(device);
    if (indexInNotBonded >= 0) {
      ExtendedBluetoothDevice previousDevice = mListValues
          .get(indexInNotBonded);
      previousDevice.rssi = device.rssi;
      notifyDataSetChanged();
      return;
    }
    mListValues.add(device);
    notifyDataSetChanged();
  }

  public void clearDevices() {
    mListValues.clear();
    notifyDataSetChanged();
  }

  @Override
  public int getCount() {
    final int bondedCount = mListBondedValues.size() + 1; // 1 for the title
    final int availableCount = mListValues.isEmpty() ? 2 : mListValues
        .size() + 1; // 1 for title, 1 for empty text
    if (bondedCount == 1)
      return availableCount;
    return bondedCount + availableCount;
  }

  @Override
  public Object getItem(int position) {
    final int bondedCount = mListBondedValues.size() + 1; // 1 for the title
    if (mListBondedValues.isEmpty()) {
      if (position == 0)
        return R.string.scanner_subtitle__not_bonded;
      else
        return mListValues.get(position - 1);
    } else {
      if (position == 0)
        return R.string.scanner_subtitle_bonded;
      if (position < bondedCount)
        return mListBondedValues.get(position - 1);
      if (position == bondedCount)
        return R.string.scanner_subtitle__not_bonded;
      return mListValues.get(position - bondedCount - 1);
    }
  }

  @Override
  public int getViewTypeCount() {
    return 3;
  }

  @Override
  public boolean areAllItemsEnabled() {
    return false;
  }

  @Override
  public boolean isEnabled(int position) {
    return getItemViewType(position) == TYPE_ITEM;
  }

  @Override
  public int getItemViewType(int position) {
    if (position == 0)
      return TYPE_TITLE;

    if (!mListBondedValues.isEmpty()
        && position == mListBondedValues.size() + 1)
      return TYPE_TITLE;

    if (position == getCount() - 1 && mListValues.isEmpty())
      return TYPE_EMPTY;

    return TYPE_ITEM;
  }

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

  @Override
  public View getView(int position, View oldView, ViewGroup parent) {
    final LayoutInflater inflater = LayoutInflater.from(mContext);
    final int type = getItemViewType(position);

    View view = oldView;
    switch (type) {
    case TYPE_EMPTY:
      if (view == null) {
        view = inflater.inflate(R.layout.device_list_empty, parent,
            false);
      }
      break;
    case TYPE_TITLE:
      if (view == null) {
        view = inflater.inflate(R.layout.device_list_title, parent,
            false);
      }
      final TextView title = (TextView) view;
      title.setText((Integer) getItem(position));
      break;
    default:
      if (view == null) {
        view = inflater
            .inflate(R.layout.device_list_row, parent, false);
        final ViewHolder holder = new ViewHolder();
        holder.name = (TextView) view.findViewById(R.id.name);
        holder.address = (TextView) view.findViewById(R.id.address);
        holder.rssi = (ImageView) view.findViewById(R.id.rssi);
        view.setTag(holder);
      }

      final ExtendedBluetoothDevice device = (ExtendedBluetoothDevice) getItem(position);
      final ViewHolder holder = (ViewHolder) view.getTag();
      final String name = device.name;
      holder.name.setText(name != null ? name : mContext
          .getString(R.string.not_available));
      holder.address.setText(device.device.getAddress());
      if (!device.isBonded || device.rssi != ScannerFragment.NO_RSSI) {
        final int rssiPercent = (int) (100.0f * (127.0f + device.rssi) / (127.0f + 20.0f));
        holder.rssi.setImageLevel(rssiPercent);
        holder.rssi.setVisibility(View.VISIBLE);
      } else {
        holder.rssi.setVisibility(View.GONE);
      }
      break;
    }

    return view;
  }

  private class ViewHolder {
    private TextView name;
    private TextView address;
    private ImageView rssi;
  }
}




Java Source Code List

no.nordicsemi.android.nrftoolbox.AppHelpFragment.java
no.nordicsemi.android.nrftoolbox.dfu.DeviceDisconnectedException.java
no.nordicsemi.android.nrftoolbox.dfu.DfuActivity.java
no.nordicsemi.android.nrftoolbox.dfu.DfuException.java
no.nordicsemi.android.nrftoolbox.dfu.DfuInitiatorActivity.java
no.nordicsemi.android.nrftoolbox.dfu.DfuService.java
no.nordicsemi.android.nrftoolbox.dfu.HexFileValidationException.java
no.nordicsemi.android.nrftoolbox.dfu.HexInputStream.java
no.nordicsemi.android.nrftoolbox.dfu.NotificationActivity.java
no.nordicsemi.android.nrftoolbox.dfu.RemoteDfuException.java
no.nordicsemi.android.nrftoolbox.dfu.UnknownResponseException.java
no.nordicsemi.android.nrftoolbox.dfu.UploadAbortedException.java
no.nordicsemi.android.nrftoolbox.dfu.adapter.FileBrowserAppsAdapter.java
no.nordicsemi.android.nrftoolbox.dfu.fragment.UploadCancelFragment.java
no.nordicsemi.android.nrftoolbox.dfu.settings.AboutDfuPreference.java
no.nordicsemi.android.nrftoolbox.dfu.settings.SettingsActivity.java
no.nordicsemi.android.nrftoolbox.dfu.settings.SettingsFragment.java
no.nordicsemi.android.nrftoolbox.scanner.DeviceListAdapter.java
no.nordicsemi.android.nrftoolbox.scanner.ExtendedBluetoothDevice.java
no.nordicsemi.android.nrftoolbox.scanner.ScannerFragment.java
no.nordicsemi.android.nrftoolbox.scanner.ScannerServiceParser.java
no.nordicsemi.android.nrftoolbox.utility.DebugLogger.java
no.nordicsemi.android.nrftoolbox.utility.GattError.java
za.co.paulscott.adapters.InteractiveArrayAdapter.java
za.co.paulscott.antifitness.DeviceInfoFragment.java
za.co.paulscott.antifitness.MainActivity.java
za.co.paulscott.antifitness.MetawearReceiver.java
za.co.paulscott.antifitness.ModuleActivity.java
za.co.paulscott.antifitness.ModuleFragment.java
za.co.paulscott.models.DStvHighlights.java
za.co.paulscott.networkhelpers.URLFetcherAsync.java