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

Add configuration toggles for cards and widgets

This commit is contained in:
Arjan Schrijver 2024-01-08 16:41:54 +01:00
parent db8d354a8d
commit 8d7641b24e
7 changed files with 227 additions and 110 deletions

View File

@ -848,6 +848,12 @@
android:name=".externalevents.opentracks.OpenTracksController"
android:label="OpenTracks controller and intent receiver"
android:exported="true"/>
<activity
android:name=".activities.dashboard.DashboardSettingsActivity"
android:parentActivityName=".activities.MainActivity"
android:label="@string/dashboard_settings"
android:excludeFromRecents="true" />
</application>
</manifest>

View File

@ -16,33 +16,47 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.activities;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentContainerView;
import androidx.gridlayout.widget.GridLayout;
import com.google.android.material.card.MaterialCardView;
import java.util.Calendar;
import java.util.GregorianCalendar;
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.AbstractDashboardWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardActiveTimeWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardDistanceWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardSettingsActivity;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardSleepWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardStepsWidget;
import nodomain.freeyourgadget.gadgetbridge.activities.dashboard.DashboardTodayWidget;
import nodomain.freeyourgadget.gadgetbridge.util.DateTimeUtils;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
public class DashboardFragment extends Fragment {
private Calendar day = GregorianCalendar.getInstance();
private TextView textViewDate;
private TextView arrowLeft;
private TextView arrowRight;
private GridLayout gridLayout;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
@ -50,6 +64,7 @@ public class DashboardFragment extends Fragment {
View dashboardView = inflater.inflate(R.layout.fragment_dashboard, container, false);
setHasOptionsMenu(true);
textViewDate = dashboardView.findViewById(R.id.dashboard_date);
gridLayout = dashboardView.findViewById(R.id.dashboard_gridlayout);
arrowLeft = dashboardView.findViewById(R.id.arrow_left);
arrowLeft.setOnClickListener(v -> {
@ -72,9 +87,31 @@ public class DashboardFragment extends Fragment {
@Override
public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.dashboard_menu, menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.dashboard_show_calendar:
// TODO: implement calendar activity
GB.toast("The calendar view is not implemented yet", Toast.LENGTH_SHORT, GB.INFO);
return false;
case R.id.dashboard_settings:
Intent intent = new Intent(requireActivity(), DashboardSettingsActivity.class);
startActivityForResult(intent, 0);
return false;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
refresh();
}
private void refresh() {
day.set(Calendar.HOUR_OF_DAY, 23);
day.set(Calendar.MINUTE, 59);
@ -91,25 +128,56 @@ public class DashboardFragment extends Fragment {
arrowRight.setAlpha(1);
}
gridLayout.removeAllViews();
Prefs prefs = GBApplication.getPrefs();
boolean cardsEnabled = prefs.getBoolean("dashboard_cards_enabled", true);
if (prefs.getBoolean("dashboard_widget_today_enabled", true)) {
createWidget(DashboardTodayWidget.newInstance(timeFrom, timeTo), cardsEnabled, 2);
}
if (prefs.getBoolean("dashboard_widget_steps_enabled", true)) {
createWidget(DashboardStepsWidget.newInstance(timeFrom, timeTo), cardsEnabled, 1);
}
if (prefs.getBoolean("dashboard_widget_distance_enabled", true)) {
createWidget(DashboardDistanceWidget.newInstance(timeFrom, timeTo), cardsEnabled, 1);
}
if (prefs.getBoolean("dashboard_widget_active_time_enabled", true)) {
createWidget(DashboardActiveTimeWidget.newInstance(timeFrom, timeTo), cardsEnabled, 1);
}
if (prefs.getBoolean("dashboard_widget_sleep_enabled", true)) {
createWidget(DashboardSleepWidget.newInstance(timeFrom, timeTo), cardsEnabled, 1);
}
}
private void createWidget(AbstractDashboardWidget widgetObj, boolean cardsEnabled, int columnSpan) {
final float scale = getContext().getResources().getDisplayMetrics().density;
FragmentContainerView fragment = new FragmentContainerView(getActivity());
int fragmentId = View.generateViewId();
fragment.setId(fragmentId);
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_today, DashboardTodayWidget.newInstance(timeFrom, timeTo))
.commit();
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_steps, DashboardStepsWidget.newInstance(timeFrom, timeTo))
.commit();
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_distance, DashboardDistanceWidget.newInstance(timeFrom, timeTo))
.commit();
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_active_time, DashboardActiveTimeWidget.newInstance(timeFrom, timeTo))
.commit();
getActivity().getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_sleep, DashboardSleepWidget.newInstance(timeFrom, timeTo))
.replace(fragmentId, widgetObj)
.commit();
GridLayout.LayoutParams layoutParams = new GridLayout.LayoutParams(
GridLayout.spec(GridLayout.UNDEFINED, GridLayout.FILL,1f),
GridLayout.spec(GridLayout.UNDEFINED, columnSpan, GridLayout.FILL,1f)
);
int pixels_8dp = (int) (8 * scale + 0.5f);
layoutParams.setMargins(pixels_8dp, pixels_8dp, pixels_8dp, pixels_8dp);
if (cardsEnabled) {
MaterialCardView card = new MaterialCardView(getActivity());
int pixels_4dp = (int) (4 * scale + 0.5f);
card.setRadius(pixels_4dp);
card.setCardElevation(pixels_4dp);
card.setContentPadding(pixels_4dp, pixels_4dp, pixels_4dp, pixels_4dp);
card.setLayoutParams(layoutParams);
card.addView(fragment);
gridLayout.addView(card);
} else {
fragment.setLayoutParams(layoutParams);
gridLayout.addView(fragment);
}
}
}

View File

@ -0,0 +1,52 @@
/* Copyright (C) 2019-2020 Andreas Shimokawa
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;
import android.os.Bundle;
import androidx.preference.PreferenceFragmentCompat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import nodomain.freeyourgadget.gadgetbridge.R;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractPreferenceFragment;
import nodomain.freeyourgadget.gadgetbridge.activities.AbstractSettingsActivityV2;
public class DashboardSettingsActivity extends AbstractSettingsActivityV2 {
@Override
protected String fragmentTag() {
return SettingsFragment.FRAGMENT_TAG;
}
@Override
protected PreferenceFragmentCompat newFragment() {
return new SettingsFragment();
}
public static class SettingsFragment extends AbstractPreferenceFragment {
private static final Logger LOG = LoggerFactory.getLogger(DashboardSettingsActivity.class);
static final String FRAGMENT_TAG = "DASHBOARD_SETTINGS_FRAGMENT";
@Override
public void onCreatePreferences(final Bundle savedInstanceState, final String rootKey) {
setPreferencesFromResource(R.xml.dashboard_settings, rootKey);
}
}
}

View File

@ -8,14 +8,6 @@
android:layout_height="match_parent"
tools:context=".activities.DashboardFragment">
<ImageView
android:id="@+id/no_items_bg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:alpha="0.1"
app:srcCompat="@drawable/gadgetbridge_img" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -57,93 +49,12 @@
android:layout_alignParentEnd="true"/>
</RelativeLayout>
<com.google.android.material.card.MaterialCardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:contentPadding="4dp">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_today"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.card.MaterialCardView>
<androidx.gridlayout.widget.GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/dashboard_gridlayout"
app:columnCount="2">
<com.google.android.material.card.MaterialCardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:contentPadding="4dp"
app:layout_columnWeight="1"
app:layout_gravity="fill">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_steps"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:contentPadding="4dp"
app:layout_columnWeight="1"
app:layout_gravity="fill">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_distance"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:contentPadding="4dp"
app:layout_columnWeight="1"
app:layout_gravity="fill">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_active_time"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="8dp"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="4dp"
card_view:contentPadding="4dp"
app:layout_columnWeight="1"
app:layout_gravity="fill">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_sleep"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</com.google.android.material.card.MaterialCardView>
</androidx.gridlayout.widget.GridLayout>
</LinearLayout>
</ScrollView>

View File

@ -4,9 +4,15 @@
tools:context=".activities.DashboardFragment">
<item
android:id="@+id/dashboard_choose_devices"
android:icon="@drawable/ic_devices_other"
android:title="Choose devices"
android:id="@+id/dashboard_show_calendar"
android:icon="@drawable/ic_calendar_from"
android:title="@string/menuitem_calendar"
app:iconTint="?attr/actionmenu_icon_color"
app:showAsAction="ifRoom" />
<item
android:id="@+id/dashboard_settings"
android:icon="@drawable/ic_settings"
android:title="@string/dashboard_settings"
app:iconTint="?attr/actionmenu_icon_color"
app:showAsAction="ifRoom" />
</menu>

View File

@ -2725,6 +2725,7 @@
<string name="warning_missing_notification_permission">Could not post ongoing notification due to missing permission</string>
<string name="pref_test_features_title">Features</string>
<string name="pref_test_features_summary">Enabled features for this test device</string>
<string name="dashboard_settings">Dashboard settings</string>
<string name="device_state_waiting_scan">Waiting for device scan</string>
<string name="auto_reconnect_ble_scan_title">Reconnect by BLE scan</string>
<string name="auto_reconnect_ble_scan_summary">Wait for device scan instead of blind connection attempts</string>

View File

@ -0,0 +1,73 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:iconSpaceReserved="false">
<PreferenceCategory
android:key="pref_key_dashboard_widgets"
android:title="@string/pref_header_general"
app:iconSpaceReserved="false">
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="dashboard_cards_enabled"
android:layout="@layout/preference_checkbox"
android:title="Show widgets on cards"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_key_dashboard_widgets"
android:title="Widget settings"
app:iconSpaceReserved="false">
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="dashboard_widget_today_enabled"
android:layout="@layout/preference_checkbox"
android:title="Activity chart"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="dashboard_widget_goals_enabled"
android:layout="@layout/preference_checkbox"
android:title="Goals chart"
android:enabled="false"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="dashboard_widget_steps_enabled"
android:layout="@layout/preference_checkbox"
android:title="@string/steps"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="dashboard_widget_distance_enabled"
android:layout="@layout/preference_checkbox"
android:title="@string/distance"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="dashboard_widget_active_time_enabled"
android:layout="@layout/preference_checkbox"
android:title="@string/active_time"
app:iconSpaceReserved="false" />
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="dashboard_widget_sleep_enabled"
android:layout="@layout/preference_checkbox"
android:title="@string/menuitem_sleep"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory
android:key="pref_key_dashboard_devices"
android:title="Devices to include"
app:iconSpaceReserved="false">
<SwitchPreferenceCompat
android:defaultValue="true"
android:key="dashboard_devices_all"
android:layout="@layout/preference_checkbox"
android:title="All devices"
android:enabled="false"
app:iconSpaceReserved="false" />
</PreferenceCategory>
</PreferenceScreen>