2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2015-2020 Andreas Shimokawa, Carsten Pfeiffer
|
2017-03-10 14:53:19 +01:00
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge 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 Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
2015-05-05 00:48:02 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.adapter;
|
|
|
|
|
2020-08-02 02:31:28 +02:00
|
|
|
import android.bluetooth.BluetoothDevice;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.content.Context;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
2020-08-02 02:31:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2015-05-05 00:48:02 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2020-08-02 02:31:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2015-08-03 23:09:49 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
|
2020-08-02 02:31:28 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
2015-09-24 14:45:21 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2015-10-26 23:32:03 +01:00
|
|
|
/**
|
|
|
|
* Adapter for displaying GBDeviceCandate instances.
|
|
|
|
*/
|
2015-08-03 23:09:49 +02:00
|
|
|
public class DeviceCandidateAdapter extends ArrayAdapter<GBDeviceCandidate> {
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
public DeviceCandidateAdapter(Context context, List<GBDeviceCandidate> deviceCandidates) {
|
2015-05-05 00:48:02 +02:00
|
|
|
super(context, 0, deviceCandidates);
|
|
|
|
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View view, ViewGroup parent) {
|
2015-08-03 23:09:49 +02:00
|
|
|
GBDeviceCandidate device = getItem(position);
|
2015-05-05 00:48:02 +02:00
|
|
|
if (view == null) {
|
2020-08-02 02:31:28 +02:00
|
|
|
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
2015-08-30 00:21:51 +02:00
|
|
|
view = inflater.inflate(R.layout.item_with_details, parent, false);
|
2015-05-05 00:48:02 +02:00
|
|
|
}
|
2020-08-02 02:31:28 +02:00
|
|
|
ImageView deviceImageView = view.findViewById(R.id.item_image);
|
|
|
|
TextView deviceNameLabel = view.findViewById(R.id.item_name);
|
|
|
|
TextView deviceAddressLabel = view.findViewById(R.id.item_details);
|
|
|
|
TextView deviceStatus = view.findViewById(R.id.item_status);
|
2015-05-05 00:48:02 +02:00
|
|
|
|
|
|
|
String name = formatDeviceCandidate(device);
|
|
|
|
deviceNameLabel.setText(name);
|
|
|
|
deviceAddressLabel.setText(device.getMacAddress());
|
2017-02-26 22:46:49 +01:00
|
|
|
deviceImageView.setImageResource(device.getDeviceType().getIcon());
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2020-08-02 02:31:28 +02:00
|
|
|
String status = "";
|
|
|
|
if (device.getDevice().getBondState() == BluetoothDevice.BOND_BONDED) {
|
|
|
|
status += getContext().getString(R.string.device_is_currently_bonded);
|
|
|
|
if (!GBApplication.getPrefs().getBoolean("ignore_bonded_devices", true)) { // This could be passed to the constructor instead
|
|
|
|
deviceImageView.setImageResource(device.getDeviceType().getDisabledIcon());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-23 18:28:02 +01:00
|
|
|
if (!device.getDeviceType().isSupported()) {
|
|
|
|
status += " UNSUPPORTED";
|
|
|
|
}
|
|
|
|
|
2020-08-02 02:31:28 +02:00
|
|
|
DeviceCoordinator coordinator = DeviceHelper.getInstance().getCoordinator(device);
|
|
|
|
if (coordinator.getBondingStyle() == DeviceCoordinator.BONDING_STYLE_REQUIRE_KEY) {
|
|
|
|
if (device.getDevice().getBondState() == BluetoothDevice.BOND_BONDED) {
|
|
|
|
status += "\n";
|
|
|
|
}
|
|
|
|
status += getContext().getString(R.string.device_requires_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceStatus.setText(status);
|
2015-05-05 00:48:02 +02:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2015-08-03 23:09:49 +02:00
|
|
|
private String formatDeviceCandidate(GBDeviceCandidate device) {
|
2015-05-05 00:48:02 +02:00
|
|
|
if (device.getRssi() > GBDevice.RSSI_UNKNOWN) {
|
|
|
|
return context.getString(R.string.device_with_rssi, device.getName(), GB.formatRssi(device.getRssi()));
|
|
|
|
}
|
|
|
|
return device.getName();
|
|
|
|
}
|
|
|
|
}
|