1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-19 00:19:25 +01:00

Display gauge on all small widgets

This commit is contained in:
Arjan Schrijver 2024-01-10 22:51:34 +01:00
parent d9f0c4764a
commit 8a6267c076
12 changed files with 194 additions and 132 deletions

View File

@ -85,6 +85,12 @@ public class DashboardFragment extends Fragment {
return dashboardView;
}
@Override
public void onResume() {
super.onResume();
refresh();
}
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
@ -163,6 +169,7 @@ public class DashboardFragment extends Fragment {
GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL,1f),
GridLayout.spec(GridLayout.UNDEFINED, columnSpan, GridLayout.FILL,1f)
);
layoutParams.width = 0;
int pixels_8dp = (int) (8 * scale + 0.5f);
layoutParams.setMargins(pixels_8dp, pixels_8dp, pixels_8dp, pixels_8dp);

View File

@ -16,8 +16,13 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.dashboard;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import androidx.annotation.ColorInt;
import androidx.fragment.app.Fragment;
import org.slf4j.Logger;
@ -96,4 +101,29 @@ public abstract class AbstractDashboardWidget extends Fragment {
DeviceCoordinator coordinator = device.getDeviceCoordinator();
return coordinator.getSampleProvider(device, db.getDaoSession());
}
/**
* @param width Bitmap width in pixels
* @param barWidth Gauge bar width in pixels
* @param filledColor Color of the filled part of the gauge
* @param filledFactor Factor between 0 and 1 that determines the amount of the gauge that should be filled
* @return Bitmap containing the gauge
*/
Bitmap drawGauge(int width, int barWidth, @ColorInt int filledColor, float filledFactor) {
int height = width / 2;
int barMargin = (int) Math.ceil(barWidth / 2f);
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(barWidth);
paint.setColor(filledColor);
canvas.drawArc(barMargin, barMargin, width - barMargin, width - barMargin, 180, 180 * filledFactor, false, paint);
paint.setColor(Color.argb(150, 150, 150, 150));
canvas.drawArc(barMargin, barMargin, width - barMargin, width - barMargin, 180 + 180 * filledFactor, 180 - 180 * filledFactor, false, paint);
return bitmap;
}
}

View File

@ -16,23 +16,24 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.dashboard;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.concurrent.TimeUnit;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
/**
* A simple {@link AbstractDashboardWidget} subclass.
@ -64,9 +65,12 @@ public class DashboardActiveTimeWidget extends AbstractDashboardWidget {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.dashboard_widget_active_time, container, false);
TextView activeTime = fragmentView.findViewById(R.id.activetime_text);
ImageView activeTimeGauge = fragmentView.findViewById(R.id.activetime_gauge);
// Update text representation
List<GBDevice> devices = GBApplication.app().getDeviceManager().getDevices();
long totalActiveMinutes = 0;
try (DBHandler dbHandler = GBApplication.acquireDB()) {
@ -78,8 +82,17 @@ public class DashboardActiveTimeWidget extends AbstractDashboardWidget {
} catch (Exception e) {
LOG.warn("Could not calculate total amount of sleep: ", e);
}
TextView activeTime = fragmentView.findViewById(R.id.active_time);
activeTime.setText(DateTimeUtils.formatDurationHoursMinutes(totalActiveMinutes, TimeUnit.MINUTES));
String activeHours = String.format("%d", (int) Math.floor(totalActiveMinutes / 60f));
String activeMinutes = String.format("%02d", (int) (totalActiveMinutes % 60f));
activeTime.setText(activeHours + ":" + activeMinutes);
// Draw gauge
ActivityUser activityUser = new ActivityUser();
int activeTimeGoal = activityUser.getActiveTimeGoalMinutes();
float goalFactor = (float) totalActiveMinutes / activeTimeGoal;
if (goalFactor > 1) goalFactor = 1;
activeTimeGauge.setImageBitmap(drawGauge(200, 15, Color.rgb(170, 0, 255), goalFactor));
return fragmentView;
}
}

View File

@ -16,10 +16,12 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.dashboard;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.slf4j.Logger;
@ -64,9 +66,12 @@ public class DashboardDistanceWidget extends AbstractDashboardWidget {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.dashboard_widget_distance, container, false);
TextView distance = fragmentView.findViewById(R.id.distance_text);
ImageView distanceGauge = fragmentView.findViewById(R.id.distance_gauge);
// Update text representation
List<GBDevice> devices = GBApplication.app().getDeviceManager().getDevices();
long totalSteps = 0;
try (DBHandler dbHandler = GBApplication.acquireDB()) {
@ -80,10 +85,16 @@ public class DashboardDistanceWidget extends AbstractDashboardWidget {
}
ActivityUser activityUser = new ActivityUser();
int stepLength = activityUser.getStepLengthCm();
double distanceMeters = totalSteps * stepLength * 0.01;
float distanceMeters = totalSteps * stepLength * 0.01f;
String distanceFormatted = FormatUtils.getFormattedDistanceLabel(distanceMeters);
TextView distance = fragmentView.findViewById(R.id.distance);
distance.setText(distanceFormatted);
// Draw gauge
int distanceGoal = activityUser.getDistanceGoalMeters();
float goalFactor = distanceMeters / distanceGoal;
if (goalFactor > 1) goalFactor = 1;
distanceGauge.setImageBitmap(drawGauge(200, 15, Color.GREEN, goalFactor));
return fragmentView;
}
}

View File

@ -16,23 +16,24 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.dashboard;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;
import java.util.concurrent.TimeUnit;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
/**
* A simple {@link AbstractDashboardWidget} subclass.
@ -64,9 +65,12 @@ public class DashboardSleepWidget extends AbstractDashboardWidget {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.dashboard_widget_sleep, container, false);
TextView sleepAmount = fragmentView.findViewById(R.id.sleep_text);
ImageView sleepGauge = fragmentView.findViewById(R.id.sleep_gauge);
// Update text representation
List<GBDevice> devices = GBApplication.app().getDeviceManager().getDevices();
long totalSleepMinutes = 0;
try (DBHandler dbHandler = GBApplication.acquireDB()) {
@ -78,8 +82,17 @@ public class DashboardSleepWidget extends AbstractDashboardWidget {
} catch (Exception e) {
LOG.warn("Could not calculate total amount of sleep: ", e);
}
TextView sleepAmount = fragmentView.findViewById(R.id.sleep_amount);
sleepAmount.setText(DateTimeUtils.formatDurationHoursMinutes(totalSleepMinutes, TimeUnit.MINUTES));
String sleepHours = String.format("%d", (int) Math.floor(totalSleepMinutes / 60f));
String sleepMinutes = String.format("%02d", (int) (totalSleepMinutes % 60f));
sleepAmount.setText(sleepHours + ":" + sleepMinutes);
// Draw gauge
ActivityUser activityUser = new ActivityUser();
int sleepMinutesGoal = activityUser.getSleepDurationGoal() * 60;
float goalFactor = (float) totalSleepMinutes / sleepMinutesGoal;
if (goalFactor > 1) goalFactor = 1;
sleepGauge.setImageBitmap(drawGauge(200, 15, Color.rgb(170, 0, 255), goalFactor));
return fragmentView;
}
}

View File

@ -16,10 +16,12 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities.dashboard;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import org.slf4j.Logger;
@ -31,6 +33,7 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.database.DBHandler;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityUser;
/**
* A simple {@link AbstractDashboardWidget} subclass.
@ -62,11 +65,14 @@ public class DashboardStepsWidget extends AbstractDashboardWidget {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.dashboard_widget_steps, container, false);
TextView stepsCount = fragmentView.findViewById(R.id.steps_count);
ImageView stepsGauge = fragmentView.findViewById(R.id.steps_gauge);
// Update text representation
List<GBDevice> devices = GBApplication.app().getDeviceManager().getDevices();
long totalSteps = 0;
int totalSteps = 0;
try (DBHandler dbHandler = GBApplication.acquireDB()) {
for (GBDevice dev : devices) {
if (dev.getDeviceCoordinator().supportsActivityTracking()) {
@ -76,8 +82,15 @@ public class DashboardStepsWidget extends AbstractDashboardWidget {
} catch (Exception e) {
LOG.warn("Could not calculate total amount of steps: ", e);
}
TextView stepsCount = fragmentView.findViewById(R.id.steps_count);
stepsCount.setText(String.valueOf(totalSteps));
// 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));
return fragmentView;
}
}

View File

@ -80,8 +80,7 @@ public class DashboardTodayWidget extends AbstractDashboardWidget {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View todayView = inflater.inflate(R.layout.dashboard_widget_today, container, false);
// Initialize chart

View File

@ -18,7 +18,6 @@
package nodomain.freeyourgadget.gadgetbridge.model;
import android.content.Context;
import android.widget.Toast;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -33,8 +32,6 @@ import nodomain.freeyourgadget.gadgetbridge.devices.DeviceCoordinator;
import nodomain.freeyourgadget.gadgetbridge.devices.SampleProvider;
import nodomain.freeyourgadget.gadgetbridge.entities.AbstractActivitySample;
import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
public class DailyTotals {
@ -78,17 +75,17 @@ public class DailyTotals {
}
public long[] getDailyTotalsForDevice(GBDevice device, Calendar day, DBHandler handler) {
ActivityAnalysis analysis = new ActivityAnalysis();
ActivityAmounts amountsSteps;
ActivityAmounts amountsSleep;
ActivityAnalysis analysis = new ActivityAnalysis();
ActivityAmounts amountsSteps;
ActivityAmounts amountsSleep;
amountsSteps = analysis.calculateActivityAmounts(getSamplesOfDay(handler, day, 0, device));
amountsSleep = analysis.calculateActivityAmounts(getSamplesOfDay(handler, day, -12, device));
amountsSteps = analysis.calculateActivityAmounts(getSamplesOfDay(handler, day, 0, device));
amountsSleep = analysis.calculateActivityAmounts(getSamplesOfDay(handler, day, -12, device));
long[] sleep = getTotalsSleepForActivityAmounts(amountsSleep);
long steps = getTotalsStepsForActivityAmounts(amountsSteps);
long[] sleep = getTotalsSleepForActivityAmounts(amountsSleep);
long steps = getTotalsStepsForActivityAmounts(amountsSteps);
return new long[]{steps, sleep[0] + sleep[1] + sleep[2]};
return new long[]{steps, sleep[0] + sleep[1] + sleep[2]};
}
private long[] getTotalsSleepForActivityAmounts(ActivityAmounts activityAmounts) {

View File

@ -2,7 +2,6 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.dashboard.DashboardActiveTimeWidget">
@ -11,35 +10,31 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
android:orientation="vertical"
android:id="@+id/card_layout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/card_activetime_title">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:src="@drawable/ic_timer"
app:tint="#aa00ff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:text="@string/activity_list_summary_active_time"
android:textColor="#aa00ff" />
</LinearLayout>
<ImageView
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:scaleType="fitStart"
android:id="@+id/activetime_gauge" />
<TextView
android:id="@+id/active_time"
android:id="@+id/activetime_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:textSize="30dp"
android:layout_below="@+id/card_activetime_title"/>
android:layout_marginTop="28dp"
android:layout_centerHorizontal="true"
android:text="1:55"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/activetime_text"
android:layout_centerHorizontal="true"
android:text="@string/activity_list_summary_active_time" />
</RelativeLayout>

View File

@ -2,7 +2,6 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.dashboard.DashboardDistanceWidget">
@ -11,35 +10,31 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
android:orientation="vertical"
android:id="@+id/card_layout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/card_distance_title">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:src="@drawable/ic_distance"
app:tint="#00ff00" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:text="@string/distance"
android:textColor="#00ff00" />
</LinearLayout>
<ImageView
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:scaleType="fitStart"
android:id="@+id/distance_gauge" />
<TextView
android:id="@+id/distance"
android:id="@+id/distance_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:textSize="30dp"
android:layout_below="@+id/card_distance_title"/>
android:layout_marginTop="28dp"
android:layout_centerHorizontal="true"
android:text="2.5km"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/distance_text"
android:layout_centerHorizontal="true"
android:text="@string/distance" />
</RelativeLayout>

View File

@ -2,7 +2,6 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.dashboard.DashboardSleepWidget">
@ -11,35 +10,31 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
android:orientation="vertical"
android:id="@+id/card_layout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/card_sleep_title">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:src="@drawable/ic_activity_sleep"
app:tint="#ffaa00" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:text="@string/menuitem_sleep"
android:textColor="#ffaa00" />
</LinearLayout>
<ImageView
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:scaleType="fitStart"
android:id="@+id/sleep_gauge" />
<TextView
android:id="@+id/sleep_amount"
android:id="@+id/sleep_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:textSize="30dp"
android:layout_below="@+id/card_sleep_title"/>
android:layout_marginTop="28dp"
android:layout_centerHorizontal="true"
android:text="7:45"
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/sleep_text"
android:layout_centerHorizontal="true"
android:text="@string/menuitem_sleep" />
</RelativeLayout>

View File

@ -2,7 +2,6 @@
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.dashboard.DashboardStepsWidget">
@ -11,36 +10,31 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
android:orientation="vertical"
android:id="@+id/card_layout">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/card_steps_title">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:src="@drawable/ic_activity_walking"
app:tint="#0000ff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:text="@string/steps"
android:textColor="#0000ff"/>
</LinearLayout>
<ImageView
android:layout_width="150dp"
android:layout_height="75dp"
android:layout_centerHorizontal="true"
android:scaleType="fitStart"
android:id="@+id/steps_gauge" />
<TextView
android:id="@+id/steps_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="2dp"
android:layout_marginTop="28dp"
android:layout_centerHorizontal="true"
android:text="5000"
android:textSize="30dp"
android:layout_below="@+id/card_steps_title"/>
android:textSize="30dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/steps_count"
android:layout_centerHorizontal="true"
android:text="@string/steps" />
</RelativeLayout>