2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2015-2024 Andreas Shimokawa, Carsten Pfeiffer, Daniel
|
|
|
|
Dakhno, José Rebelo, Petr Vaněk, Taavi Eomäe
|
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
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://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;
|
2022-01-09 14:45:21 +01:00
|
|
|
import android.text.TextUtils;
|
2015-05-05 00:48:02 +02:00
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2022-01-09 14:45:21 +01:00
|
|
|
import java.util.ArrayList;
|
2015-05-05 00:48:02 +02:00
|
|
|
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;
|
2023-10-28 10:49:16 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
|
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
|
|
|
|
2023-10-28 10:49:16 +02:00
|
|
|
DeviceType deviceType = DeviceHelper.getInstance().resolveDeviceType(device);
|
|
|
|
|
|
|
|
DeviceCoordinator coordinator = deviceType.getDeviceCoordinator();
|
2023-09-27 23:11:02 +02:00
|
|
|
|
2015-05-05 00:48:02 +02:00
|
|
|
String name = formatDeviceCandidate(device);
|
|
|
|
deviceNameLabel.setText(name);
|
|
|
|
deviceAddressLabel.setText(device.getMacAddress());
|
2023-09-27 23:11:02 +02:00
|
|
|
deviceImageView.setImageResource(coordinator.getDefaultIconResource());
|
2015-05-05 00:48:02 +02:00
|
|
|
|
2022-01-09 14:45:21 +01:00
|
|
|
final List<String> statusLines = new ArrayList<>();
|
2023-08-31 23:23:10 +02:00
|
|
|
if (device.isBonded()) {
|
2022-01-09 14:45:21 +01:00
|
|
|
statusLines.add(getContext().getString(R.string.device_is_currently_bonded));
|
2020-08-02 02:31:28 +02:00
|
|
|
if (!GBApplication.getPrefs().getBoolean("ignore_bonded_devices", true)) { // This could be passed to the constructor instead
|
2023-09-27 23:11:02 +02:00
|
|
|
deviceImageView.setImageResource(coordinator.getDisabledIconResource());
|
2020-08-02 02:31:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-28 10:49:16 +02:00
|
|
|
if (!deviceType.isSupported()) {
|
2022-01-09 14:45:21 +01:00
|
|
|
statusLines.add(getContext().getString(R.string.device_unsupported));
|
2021-12-23 18:28:02 +01:00
|
|
|
}
|
|
|
|
|
2023-08-18 00:14:40 +02:00
|
|
|
if (coordinator.isExperimental()) {
|
|
|
|
statusLines.add(getContext().getString(R.string.device_experimental));
|
|
|
|
}
|
2020-08-02 02:31:28 +02:00
|
|
|
if (coordinator.getBondingStyle() == DeviceCoordinator.BONDING_STYLE_REQUIRE_KEY) {
|
2022-01-09 14:45:21 +01:00
|
|
|
statusLines.add(getContext().getString(R.string.device_requires_key));
|
2020-08-02 02:31:28 +02:00
|
|
|
}
|
|
|
|
|
2022-01-09 14:45:21 +01:00
|
|
|
deviceStatus.setText(TextUtils.join("\n", statusLines));
|
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();
|
|
|
|
}
|
|
|
|
}
|