2016-10-21 13:01:30 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.adapter;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.ArrayAdapter;
|
|
|
|
import android.widget.ImageView;
|
|
|
|
import android.widget.LinearLayout;
|
|
|
|
import android.widget.ListView;
|
|
|
|
import android.widget.ProgressBar;
|
|
|
|
import android.widget.RelativeLayout;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.ConfigureAlarms;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.activities.charts.ChartsActivity;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.BatteryState;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adapter for displaying GBDevice instances.
|
|
|
|
*/
|
|
|
|
public class GBDeviceAdapterv2 extends ArrayAdapter<GBDevice> {
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
private DeviceCoordinator coordinator;
|
|
|
|
|
|
|
|
public GBDeviceAdapterv2(Context context, List<GBDevice> deviceList) {
|
|
|
|
super(context, 0, deviceList);
|
|
|
|
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public View getView(int position, View view, ViewGroup parent) {
|
|
|
|
final GBDevice device = getItem(position);
|
|
|
|
coordinator = DeviceHelper.getInstance().getCoordinator(device);
|
|
|
|
|
|
|
|
if (view == null) {
|
|
|
|
LayoutInflater inflater = (LayoutInflater) context
|
|
|
|
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
|
|
|
|
|
|
|
view = inflater.inflate(R.layout.device_itemv2, parent, false);
|
|
|
|
}
|
|
|
|
TextView deviceStatusLabel = (TextView) view.findViewById(R.id.device_status);
|
|
|
|
TextView deviceNameLabel = (TextView) view.findViewById(R.id.device_name);
|
|
|
|
|
|
|
|
TextView batteryStatusLabel = (TextView) view.findViewById(R.id.battery_status);
|
|
|
|
final ImageView deviceImageView = (ImageView) view.findViewById(R.id.device_image);
|
2016-10-21 17:44:36 +02:00
|
|
|
deviceImageView.setOnLongClickListener(new View.OnLongClickListener(){
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onLongClick(View v) {
|
|
|
|
//TODO: move somewhere else
|
|
|
|
GBApplication.deviceService().disconnect();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
2016-10-21 13:01:30 +02:00
|
|
|
|
|
|
|
ProgressBar busyIndicator = (ProgressBar) view.findViewById(R.id.device_busy_indicator);
|
|
|
|
|
|
|
|
deviceNameLabel.setText(getUniqueDeviceName(device));
|
|
|
|
|
|
|
|
if (device.isBusy()) {
|
|
|
|
deviceStatusLabel.setText(device.getBusyTask());
|
|
|
|
busyIndicator.setVisibility(View.VISIBLE);
|
|
|
|
} else {
|
|
|
|
deviceStatusLabel.setText(device.getStateString());
|
|
|
|
busyIndicator.setVisibility(View.INVISIBLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
//begin of action row
|
|
|
|
//battery
|
|
|
|
LinearLayout batteryStatusBox = (LinearLayout) view.findViewById(R.id.device_battery_status_box);
|
|
|
|
batteryStatusBox.setVisibility(View.GONE);
|
|
|
|
|
|
|
|
ImageView batteryIcon = (ImageView) view.findViewById(R.id.device_battery_status);
|
|
|
|
|
|
|
|
short batteryLevel = device.getBatteryLevel();
|
|
|
|
if (batteryLevel != GBDevice.BATTERY_UNKNOWN) {
|
|
|
|
batteryStatusBox.setVisibility(View.VISIBLE);
|
|
|
|
batteryStatusLabel.setText(device.getBatteryLevel() + "%");
|
|
|
|
BatteryState batteryState = device.getBatteryState();
|
2016-10-21 17:44:36 +02:00
|
|
|
if (BatteryState.BATTERY_CHARGING.equals(batteryState) ||
|
|
|
|
BatteryState.BATTERY_CHARGING_FULL.equals(batteryState)) {
|
|
|
|
batteryIcon.setImageLevel(device.getBatteryLevel() + 100);
|
2016-10-21 13:01:30 +02:00
|
|
|
} else {
|
2016-10-21 17:44:36 +02:00
|
|
|
batteryIcon.setImageLevel(device.getBatteryLevel());
|
2016-10-21 13:01:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//fetch activity data
|
|
|
|
ImageView fetchActivityData = (ImageView) view.findViewById(R.id.device_action_fetch_activity);
|
|
|
|
LinearLayout fetchActivityDataBox = (LinearLayout) view.findViewById(R.id.device_action_fetch_activity_box);
|
|
|
|
|
2016-10-21 17:44:36 +02:00
|
|
|
fetchActivityDataBox.setVisibility((device.isInitialized() && coordinator.supportsActivityDataFetching()) ? View.VISIBLE : View.GONE);
|
|
|
|
fetchActivityData.setOnClickListener(new View.OnClickListener()
|
|
|
|
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
GBApplication.deviceService().onFetchActivityData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 13:01:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
//take screenshot
|
|
|
|
ImageView takeScreenshotView = (ImageView) view.findViewById(R.id.device_action_take_screenshot);
|
2016-10-21 17:44:36 +02:00
|
|
|
takeScreenshotView.setVisibility((device.isInitialized() && coordinator.supportsScreenshots()) ? View.VISIBLE : View.GONE);
|
|
|
|
takeScreenshotView.setOnClickListener(new View.OnClickListener()
|
|
|
|
|
|
|
|
{
|
2016-10-21 13:01:30 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
GBApplication.deviceService().onScreenshotReq();
|
|
|
|
}
|
|
|
|
}
|
2016-10-21 17:44:36 +02:00
|
|
|
|
2016-10-21 13:01:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
//set alarms
|
|
|
|
ImageView setAlarmsView = (ImageView) view.findViewById(R.id.device_action_set_alarms);
|
|
|
|
setAlarmsView.setVisibility(coordinator.supportsAlarmConfiguration() ? View.VISIBLE : View.GONE);
|
2016-10-21 17:44:36 +02:00
|
|
|
setAlarmsView.setOnClickListener(new View.OnClickListener()
|
|
|
|
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent startIntent;
|
|
|
|
startIntent = new Intent(context, ConfigureAlarms.class);
|
|
|
|
context.startActivity(startIntent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 13:01:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
//show graphs
|
|
|
|
ImageView showActivityGraphs = (ImageView) view.findViewById(R.id.device_action_show_activity_graphs);
|
|
|
|
showActivityGraphs.setVisibility(coordinator.supportsActivityTracking() ? View.VISIBLE : View.GONE);
|
2016-10-21 17:44:36 +02:00
|
|
|
showActivityGraphs.setOnClickListener(new View.OnClickListener()
|
|
|
|
|
|
|
|
{
|
2016-10-21 13:01:30 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
Intent startIntent;
|
|
|
|
startIntent = new Intent(context, ChartsActivity.class);
|
|
|
|
startIntent.putExtra(GBDevice.EXTRA_DEVICE, device);
|
2016-10-21 17:44:36 +02:00
|
|
|
context.startActivity(startIntent);
|
|
|
|
}
|
2016-10-21 13:01:30 +02:00
|
|
|
}
|
2016-10-21 17:44:36 +02:00
|
|
|
|
2016-10-21 13:01:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
//Info icon is last in the row
|
|
|
|
ImageView deviceInfoView = (ImageView) view.findViewById(R.id.device_info_image);
|
|
|
|
final RelativeLayout deviceInfoBox = (RelativeLayout) view.findViewById(R.id.device_item_infos_box);
|
2016-10-21 17:44:36 +02:00
|
|
|
ListView deviceInfoList = (ListView) view.findViewById(R.id.device_item_infos);
|
2016-10-21 13:01:30 +02:00
|
|
|
ItemWithDetailsAdapter infoAdapter = new ItemWithDetailsAdapter(context, device.getDeviceInfos());
|
|
|
|
infoAdapter.setHorizontalAlignment(true);
|
|
|
|
deviceInfoList.setAdapter(infoAdapter);
|
2016-10-21 17:44:36 +02:00
|
|
|
justifyListViewHeightBasedOnChildren(deviceInfoList);
|
|
|
|
deviceInfoList.setFocusable(false);
|
|
|
|
|
2016-10-21 13:01:30 +02:00
|
|
|
boolean showInfoIcon = device.hasDeviceInfos() && !device.isBusy();
|
|
|
|
deviceInfoView.setVisibility(showInfoIcon ? View.VISIBLE : View.GONE);
|
|
|
|
deviceInfoView.setOnClickListener(new View.OnClickListener() {
|
2016-10-21 17:44:36 +02:00
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
if (deviceInfoBox.getVisibility() == View.VISIBLE) {
|
|
|
|
deviceInfoBox.setVisibility(View.GONE);
|
|
|
|
} else {
|
|
|
|
deviceInfoBox.setVisibility(View.VISIBLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
);
|
2016-10-21 13:01:30 +02:00
|
|
|
|
|
|
|
//remove device, hidden under details
|
|
|
|
ImageView removeDevice = (ImageView) view.findViewById(R.id.device_action_remove);
|
2016-10-21 17:44:36 +02:00
|
|
|
removeDevice.setOnClickListener(new View.OnClickListener()
|
|
|
|
|
|
|
|
{
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
//TODO: the logic is bolted to controlcenter, but I don't think it belongs here
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-21 13:01:30 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
|
2016-10-21 17:44:36 +02:00
|
|
|
switch (device.getType())
|
|
|
|
|
|
|
|
{
|
2016-10-21 13:01:30 +02:00
|
|
|
case PEBBLE:
|
|
|
|
if (device.isConnected()) {
|
|
|
|
deviceImageView.setImageResource(R.drawable.ic_device_pebble);
|
|
|
|
} else {
|
|
|
|
deviceImageView.setImageResource(R.drawable.ic_device_pebble_disabled);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case MIBAND:
|
|
|
|
case MIBAND2:
|
|
|
|
if (device.isConnected()) {
|
|
|
|
deviceImageView.setImageResource(R.drawable.ic_device_miband);
|
|
|
|
} else {
|
|
|
|
deviceImageView.setImageResource(R.drawable.ic_device_miband_disabled);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case VIBRATISSIMO:
|
|
|
|
if (device.isConnected()) {
|
|
|
|
deviceImageView.setImageResource(R.drawable.ic_device_lovetoy);
|
|
|
|
} else {
|
|
|
|
deviceImageView.setImageResource(R.drawable.ic_device_lovetoy_disabled);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (device.isConnected()) {
|
|
|
|
deviceImageView.setImageResource(R.drawable.ic_launcher);
|
|
|
|
} else {
|
|
|
|
deviceImageView.setImageResource(R.drawable.ic_device_default_disabled);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void justifyListViewHeightBasedOnChildren(ListView listView) {
|
|
|
|
ArrayAdapter adapter = (ArrayAdapter) listView.getAdapter();
|
|
|
|
|
|
|
|
if (adapter == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ViewGroup vg = listView;
|
|
|
|
int totalHeight = 0;
|
|
|
|
for (int i = 0; i < adapter.getCount(); i++) {
|
|
|
|
View listItem = adapter.getView(i, null, vg);
|
|
|
|
listItem.measure(0, 0);
|
|
|
|
totalHeight += listItem.getMeasuredHeight();
|
|
|
|
}
|
|
|
|
|
|
|
|
ViewGroup.LayoutParams par = listView.getLayoutParams();
|
|
|
|
par.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
|
|
|
|
listView.setLayoutParams(par);
|
|
|
|
listView.requestLayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
private String getUniqueDeviceName(GBDevice device) {
|
|
|
|
String deviceName = device.getName();
|
|
|
|
if (!isUniqueDeviceName(device, deviceName)) {
|
|
|
|
if (device.getModel() != null) {
|
|
|
|
deviceName = deviceName + " " + device.getModel();
|
|
|
|
if (!isUniqueDeviceName(device, deviceName)) {
|
|
|
|
deviceName = deviceName + " " + device.getShortAddress();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
deviceName = deviceName + " " + device.getShortAddress();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return deviceName;
|
|
|
|
}
|
|
|
|
|
|
|
|
private boolean isUniqueDeviceName(GBDevice device, String deviceName) {
|
|
|
|
for (int i = 0; i < getCount(); i++) {
|
|
|
|
GBDevice item = getItem(i);
|
|
|
|
if (item == device) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (deviceName.equals(item.getName())) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|