2017-10-19 21:52:38 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.activities;
|
|
|
|
|
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2018-03-31 16:21:25 +02:00
|
|
|
import android.support.v4.widget.SwipeRefreshLayout;
|
2017-10-19 21:52:38 +02:00
|
|
|
import android.view.ContextMenu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
import android.view.View;
|
|
|
|
import android.widget.AdapterView;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2018-03-31 16:21:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.R;
|
2017-10-19 21:52:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.adapter.ActivitySummariesAdapter;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary;
|
2018-03-31 16:21:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2017-10-19 21:52:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivitySummary;
|
2018-03-31 16:21:25 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.RecordedDataTypes;
|
2017-10-19 21:52:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.AndroidUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
|
|
|
|
public class ActivitySummariesActivity extends AbstractListActivity<BaseActivitySummary> {
|
|
|
|
|
2017-11-05 00:20:26 +01:00
|
|
|
private int selectedIndex;
|
|
|
|
|
2018-03-31 16:21:25 +02:00
|
|
|
private GBDevice mGBDevice;
|
|
|
|
private SwipeRefreshLayout swipeLayout;
|
|
|
|
|
2017-10-19 21:52:38 +02:00
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
2018-03-31 16:21:25 +02:00
|
|
|
Bundle extras = getIntent().getExtras();
|
|
|
|
if (extras != null) {
|
|
|
|
mGBDevice = extras.getParcelable(GBDevice.EXTRA_DEVICE);
|
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Must provide a device when invoking this activity");
|
|
|
|
}
|
|
|
|
|
2017-10-19 21:52:38 +02:00
|
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setItemAdapter(new ActivitySummariesAdapter(this));
|
|
|
|
|
|
|
|
getItemListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
2017-11-03 22:04:01 +01:00
|
|
|
Object item = parent.getItemAtPosition(position);
|
2017-10-19 21:52:38 +02:00
|
|
|
if (item != null) {
|
|
|
|
ActivitySummary summary = (ActivitySummary) item;
|
|
|
|
String gpxTrack = summary.getGpxTrack();
|
|
|
|
if (gpxTrack != null) {
|
|
|
|
showTrack(gpxTrack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
getItemListView().setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
|
|
|
|
@Override
|
|
|
|
public void onCreateContextMenu(ContextMenu menu, View v, final ContextMenu.ContextMenuInfo menuInfo) {
|
|
|
|
MenuItem delete = menu.add("Delete");
|
|
|
|
delete.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onMenuItemClick(MenuItem item) {
|
2017-11-05 00:20:26 +01:00
|
|
|
deleteItemAt(selectedIndex);
|
2017-10-19 21:52:38 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
getItemListView().setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
|
|
|
@Override
|
|
|
|
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
|
2017-11-05 00:20:26 +01:00
|
|
|
selectedIndex = position;
|
2017-10-19 21:52:38 +02:00
|
|
|
return getItemListView().showContextMenu();
|
|
|
|
}
|
|
|
|
});
|
2018-03-31 16:21:25 +02:00
|
|
|
swipeLayout = findViewById(R.id.list_activity_swipe_layout);
|
|
|
|
swipeLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
|
|
|
|
@Override
|
|
|
|
public void onRefresh() {
|
|
|
|
fetchTrackData();
|
|
|
|
}
|
|
|
|
});
|
2017-10-19 21:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private void deleteItemAt(int position) {
|
|
|
|
BaseActivitySummary item = getItemAdapter().getItem(position);
|
|
|
|
if (item != null) {
|
|
|
|
item.delete();
|
|
|
|
getItemAdapter().remove(item);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void showTrack(String gpxTrack) {
|
|
|
|
try {
|
|
|
|
AndroidUtils.viewFile(gpxTrack, Intent.ACTION_VIEW, this);
|
|
|
|
} catch (IOException e) {
|
|
|
|
GB.toast(this, "Unable to display GPX track: " + e.getMessage(), Toast.LENGTH_LONG, GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
2018-03-31 16:21:25 +02:00
|
|
|
private void fetchTrackData() {
|
|
|
|
if (mGBDevice.isInitialized()) {
|
|
|
|
GBApplication.deviceService().onFetchRecordedData(RecordedDataTypes.TYPE_GPS_TRACKS);
|
|
|
|
} else {
|
|
|
|
swipeLayout.setRefreshing(false);
|
|
|
|
GB.toast(this, getString(R.string.device_not_connected), Toast.LENGTH_SHORT, GB.ERROR);
|
|
|
|
}
|
|
|
|
}
|
2017-10-19 21:52:38 +02:00
|
|
|
}
|