1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-06-28 07:50:11 +02:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/adapter/DeviceCandidateAdapter.java
José Rebelo 8085eb6ec3 Discover bonded devices by default
A lof of devices will simply work anyway even if already paired in
Android bluetooth settings. Discover them by default, but warn the user
if the device is not known to pair correctly if already paired in Android
settings. Allows this warning to be disabled to known working devices.
2024-03-25 20:31:33 +00:00

101 lines
4.0 KiB
Java

/* Copyright (C) 2015-2024 Andreas Shimokawa, Carsten Pfeiffer, Daniel
Dakhno, José Rebelo, Petr Vaněk, Taavi Eomäe
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 <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.adapter;
import android.content.Context;
import android.text.TextUtils;
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.ArrayList;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceCandidate;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
/**
* Adapter for displaying GBDeviceCandate instances.
*/
public class DeviceCandidateAdapter extends ArrayAdapter<GBDeviceCandidate> {
private final Context context;
public DeviceCandidateAdapter(Context context, List<GBDeviceCandidate> deviceCandidates) {
super(context, 0, deviceCandidates);
this.context = context;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
GBDeviceCandidate device = getItem(position);
if (view == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.item_with_details, parent, false);
}
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);
DeviceType deviceType = DeviceHelper.getInstance().resolveDeviceType(device);
DeviceCoordinator coordinator = deviceType.getDeviceCoordinator();
String name = formatDeviceCandidate(device);
deviceNameLabel.setText(name);
deviceAddressLabel.setText(device.getMacAddress());
deviceImageView.setImageResource(coordinator.getDefaultIconResource());
final List<String> statusLines = new ArrayList<>();
if (device.isBonded()) {
statusLines.add(getContext().getString(R.string.device_is_currently_bonded));
}
if (!deviceType.isSupported()) {
statusLines.add(getContext().getString(R.string.device_unsupported));
}
if (coordinator.isExperimental()) {
statusLines.add(getContext().getString(R.string.device_experimental));
}
if (coordinator.getBondingStyle() == DeviceCoordinator.BONDING_STYLE_REQUIRE_KEY) {
statusLines.add(getContext().getString(R.string.device_requires_key));
}
deviceStatus.setText(TextUtils.join("\n", statusLines));
return view;
}
private String formatDeviceCandidate(GBDeviceCandidate device) {
if (device.getRssi() > GBDevice.RSSI_UNKNOWN) {
return context.getString(R.string.device_with_rssi, device.getName(), GB.formatRssi(device.getRssi()));
}
return device.getName();
}
}