1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-08-16 04:12:28 +02:00
Gadgetbridge/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/activities/dashboard/DashboardStepsWidget.java

106 lines
4.0 KiB
Java
Raw Normal View History

2023-12-27 22:32:26 +01:00
/* Copyright (C) 2023 Arjan Schrijver
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/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.dashboard;
2024-01-10 22:51:34 +01:00
import android.graphics.Color;
2023-12-27 22:32:26 +01:00
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
2024-01-10 22:51:34 +01:00
import android.widget.ImageView;
2023-12-27 22:32:26 +01:00
import android.widget.TextView;
2023-12-27 23:01:35 +01:00
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
2023-12-27 22:32:26 +01:00
import nodomain.freeyourgadget.gadgetbridge.R;
2023-12-27 23:01:35 +01:00
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
2024-01-10 22:51:34 +01:00
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
2023-12-27 22:32:26 +01:00
/**
* A simple {@link AbstractDashboardWidget} subclass.
2023-12-27 22:32:26 +01:00
* Use the {@link DashboardStepsWidget#newInstance} factory method to
* create an instance of this fragment.
*/
2023-12-27 23:01:35 +01:00
public class DashboardStepsWidget extends AbstractDashboardWidget {
private static final Logger LOG = LoggerFactory.getLogger(DashboardStepsWidget.class);
2024-01-11 13:23:44 +01:00
private TextView stepsCount;
private ImageView stepsGauge;
2023-12-27 22:32:26 +01:00
public DashboardStepsWidget() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param timeFrom Start time in seconds since Unix epoch.
* @param timeTo End time in seconds since Unix epoch.
* @return A new instance of fragment DashboardStepsWidget.
*/
public static DashboardStepsWidget newInstance(int timeFrom, int timeTo) {
DashboardStepsWidget fragment = new DashboardStepsWidget();
Bundle args = new Bundle();
args.putInt(ARG_TIME_FROM, timeFrom);
args.putInt(ARG_TIME_TO, timeTo);
fragment.setArguments(args);
return fragment;
}
2023-12-27 22:32:26 +01:00
@Override
2024-01-10 22:51:34 +01:00
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2023-12-27 22:32:26 +01:00
View fragmentView = inflater.inflate(R.layout.dashboard_widget_steps, container, false);
2024-01-11 13:23:44 +01:00
stepsCount = fragmentView.findViewById(R.id.steps_count);
stepsGauge = fragmentView.findViewById(R.id.steps_gauge);
fillData();
return fragmentView;
}
2024-01-10 22:51:34 +01:00
2024-01-11 13:23:44 +01:00
@Override
public void onResume() {
super.onResume();
if (stepsCount != null && stepsGauge != null) fillData();
}
protected void fillData() {
2024-01-10 22:51:34 +01:00
// Update text representation
2023-12-27 23:01:35 +01:00
List<GBDevice> devices = GBApplication.app().getDeviceManager().getDevices();
2024-01-10 22:51:34 +01:00
int totalSteps = 0;
2023-12-27 23:01:35 +01:00
try (DBHandler dbHandler = GBApplication.acquireDB()) {
for (GBDevice dev : devices) {
if (dev.getDeviceCoordinator().supportsActivityTracking()) {
totalSteps += getSteps(dev, dbHandler);
}
}
} catch (Exception e) {
LOG.warn("Could not calculate total amount of steps: ", e);
}
stepsCount.setText(String.valueOf(totalSteps));
2024-01-10 22:51:34 +01:00
// Draw gauge
ActivityUser activityUser = new ActivityUser();
float stepsGoal = activityUser.getStepsGoal();
float goalFactor = totalSteps / stepsGoal;
if (goalFactor > 1) goalFactor = 1;
stepsGauge.setImageBitmap(drawGauge(200, 15, Color.BLUE, goalFactor));
2023-12-27 22:32:26 +01:00
}
}