2020-01-09 10:44:32 +01:00
|
|
|
/* Copyright (C) 2017-2020 Carsten Pfeiffer, Daniele Gobbetti
|
2018-06-25 18:35:46 +02: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/>. */
|
2017-10-19 21:52:38 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.adapter;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.List;
|
2020-08-07 09:21:14 +02:00
|
|
|
import java.util.concurrent.TimeUnit;
|
2017-10-19 21:52:38 +02:00
|
|
|
|
2018-04-07 10:16:36 +02:00
|
|
|
import de.greenrobot.dao.query.QueryBuilder;
|
2017-10-19 21:52:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
|
2018-04-07 10:16:36 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.database.DBHelper;
|
2017-10-19 21:52:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummaryDao;
|
2018-04-07 10:16:36 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.entities.Device;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
|
2017-10-19 21:52:38 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
|
|
|
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
|
|
|
|
|
|
|
public class ActivitySummariesAdapter extends AbstractItemAdapter<BaseActivitySummary> {
|
2018-04-13 20:27:31 +02:00
|
|
|
private final GBDevice device;
|
2020-08-11 21:54:02 +02:00
|
|
|
private int activityKindFilter;
|
2020-08-18 11:16:36 +02:00
|
|
|
long dateFromFilter=0;
|
|
|
|
long dateToFilter=0;
|
2020-08-24 00:09:10 +02:00
|
|
|
String nameContainsFilter;
|
2018-04-13 20:27:31 +02:00
|
|
|
|
2020-08-24 00:09:10 +02:00
|
|
|
public ActivitySummariesAdapter(Context context, GBDevice device, int activityKindFilter, long dateFromFilter, long dateToFilter, String nameContainsFilter) {
|
2017-10-19 21:52:38 +02:00
|
|
|
super(context);
|
2018-04-13 20:27:31 +02:00
|
|
|
this.device = device;
|
2020-08-11 21:54:02 +02:00
|
|
|
this.activityKindFilter = activityKindFilter;
|
2020-08-18 11:16:36 +02:00
|
|
|
this.dateFromFilter=dateFromFilter;
|
|
|
|
this.dateToFilter=dateToFilter;
|
2020-08-24 00:09:10 +02:00
|
|
|
this.nameContainsFilter=nameContainsFilter;
|
2018-04-13 20:27:31 +02:00
|
|
|
loadItems();
|
2017-10-19 21:52:38 +02:00
|
|
|
}
|
|
|
|
|
2018-04-13 20:27:31 +02:00
|
|
|
@Override
|
|
|
|
public void loadItems() {
|
2017-10-19 21:52:38 +02:00
|
|
|
try (DBHandler handler = GBApplication.acquireDB()) {
|
|
|
|
BaseActivitySummaryDao summaryDao = handler.getDaoSession().getBaseActivitySummaryDao();
|
2018-04-07 10:16:36 +02:00
|
|
|
Device dbDevice = DBHelper.findDevice(device, handler.getDaoSession());
|
|
|
|
|
|
|
|
QueryBuilder<BaseActivitySummary> qb = summaryDao.queryBuilder();
|
2020-08-18 11:16:36 +02:00
|
|
|
qb.where(
|
|
|
|
BaseActivitySummaryDao.Properties.DeviceId.eq(
|
|
|
|
dbDevice.getId())).orderDesc(BaseActivitySummaryDao.Properties.StartTime);
|
|
|
|
|
|
|
|
if (activityKindFilter !=0) {
|
|
|
|
qb.where(
|
|
|
|
BaseActivitySummaryDao.Properties.ActivityKind.eq(activityKindFilter));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dateFromFilter !=0) {
|
2020-08-11 21:54:02 +02:00
|
|
|
qb.where(
|
2020-08-18 11:16:36 +02:00
|
|
|
BaseActivitySummaryDao.Properties.StartTime.gt(new Date(dateFromFilter)));
|
|
|
|
}
|
|
|
|
if (dateToFilter !=0) {
|
2020-08-11 21:54:02 +02:00
|
|
|
qb.where(
|
2020-08-18 11:16:36 +02:00
|
|
|
BaseActivitySummaryDao.Properties.EndTime.lt(new Date(dateToFilter)));
|
2020-08-11 21:54:02 +02:00
|
|
|
}
|
2020-08-24 00:09:10 +02:00
|
|
|
if (nameContainsFilter !=null && nameContainsFilter.length() > 0) {
|
|
|
|
qb.where(
|
|
|
|
BaseActivitySummaryDao.Properties.Name.like("%" + nameContainsFilter + "%"));
|
|
|
|
}
|
2020-08-11 21:54:02 +02:00
|
|
|
|
2018-04-07 10:16:36 +02:00
|
|
|
List<BaseActivitySummary> allSummaries = qb.build().list();
|
2017-10-19 21:52:38 +02:00
|
|
|
setItems(allSummaries, true);
|
|
|
|
} catch (Exception e) {
|
|
|
|
GB.toast("Error loading activity summaries.", Toast.LENGTH_SHORT, GB.ERROR, e);
|
|
|
|
}
|
|
|
|
}
|
2020-08-11 21:54:02 +02:00
|
|
|
|
|
|
|
public void setActivityKindFilter(int filter){
|
|
|
|
this.activityKindFilter=filter;
|
|
|
|
}
|
2020-08-18 11:16:36 +02:00
|
|
|
public void setDateFromFilter(long date){
|
|
|
|
this.dateFromFilter=date;
|
|
|
|
}
|
|
|
|
public void setDateToFilter(long date){
|
|
|
|
this.dateToFilter=date;
|
|
|
|
}
|
2020-08-24 00:09:10 +02:00
|
|
|
public void setNameContainsFilter(String name){
|
|
|
|
this.nameContainsFilter=name;
|
|
|
|
}
|
2020-08-18 11:16:36 +02:00
|
|
|
|
2017-10-19 21:52:38 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String getName(BaseActivitySummary item) {
|
|
|
|
String name = item.getName();
|
2020-08-24 00:09:10 +02:00
|
|
|
if (name == null) name="";
|
|
|
|
String gpxTrack = item.getGpxTrack();
|
|
|
|
String hasGps = " ";
|
|
|
|
if (gpxTrack != null) {
|
|
|
|
hasGps=" 🛰️ ";
|
2017-10-19 21:52:38 +02:00
|
|
|
}
|
2020-08-24 00:09:10 +02:00
|
|
|
return ActivityKind.asString(item.getActivityKind(), getContext())+ hasGps + name;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected String getDetails(BaseActivitySummary item) {
|
|
|
|
|
2017-10-19 21:52:38 +02:00
|
|
|
|
|
|
|
Date startTime = item.getStartTime();
|
2020-08-07 09:21:14 +02:00
|
|
|
Long duration = (item.getEndTime().getTime() - item.getStartTime().getTime());
|
|
|
|
|
2017-10-19 21:52:38 +02:00
|
|
|
if (startTime != null) {
|
2020-08-09 13:11:10 +02:00
|
|
|
return DateTimeUtils.formatDateTime(startTime) + " (" + DateTimeUtils.formatDurationHoursMinutes(duration, TimeUnit.MILLISECONDS) + ")";
|
2017-10-19 21:52:38 +02:00
|
|
|
}
|
2020-08-07 09:21:14 +02:00
|
|
|
|
2017-10-19 21:52:38 +02:00
|
|
|
return "Unknown activity";
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected int getIcon(BaseActivitySummary item) {
|
|
|
|
return ActivityKind.getIconId(item.getActivityKind());
|
|
|
|
}
|
|
|
|
}
|