From f16e2eeabba55ae30e79336bc692dcec9103410b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Rebelo?= Date: Thu, 14 Nov 2024 23:22:18 +0000 Subject: [PATCH] Test device: Add dummy activities --- .../devices/test/TestDeviceCoordinator.java | 24 +++--- .../TestDeviceSpecificSettingsCustomizer.java | 74 ++++++++++++++++--- app/src/main/res/values/strings.xml | 2 + ...settings_developer_add_test_activities.xml | 8 ++ 4 files changed, 85 insertions(+), 23 deletions(-) create mode 100644 app/src/main/res/xml/devicesettings_developer_add_test_activities.xml diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/TestDeviceCoordinator.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/TestDeviceCoordinator.java index 94a6df1ff..9e4459fa8 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/TestDeviceCoordinator.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/TestDeviceCoordinator.java @@ -38,7 +38,9 @@ import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.GBException; import nodomain.freeyourgadget.gadgetbridge.R; import nodomain.freeyourgadget.gadgetbridge.activities.appmanager.AppManagerActivity; +import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettings; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsCustomizer; +import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsScreen; import nodomain.freeyourgadget.gadgetbridge.capabilities.HeartRateCapability; import nodomain.freeyourgadget.gadgetbridge.capabilities.password.PasswordCapabilityImpl; import nodomain.freeyourgadget.gadgetbridge.capabilities.widgets.WidgetManager; @@ -502,26 +504,26 @@ public class TestDeviceCoordinator extends AbstractDeviceCoordinator { } @Override - public int[] getSupportedDeviceSpecificSettings(final GBDevice device) { - final List settings = new ArrayList<>(); + public DeviceSpecificSettings getDeviceSpecificSettings(final GBDevice device) { + final DeviceSpecificSettings deviceSpecificSettings = new DeviceSpecificSettings(); - settings.add(R.xml.devicesettings_header_apps); - settings.add(R.xml.devicesettings_loyalty_cards); + deviceSpecificSettings.addRootScreen(R.xml.devicesettings_loyalty_cards); if (getWorldClocksSlotCount() > 0) { - settings.add(R.xml.devicesettings_header_time); - settings.add(R.xml.devicesettings_world_clocks); + final List dateTime = deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DATE_TIME); + dateTime.add(R.xml.devicesettings_world_clocks); } if (getContactsSlotCount(device) > 0) { - settings.add(R.xml.devicesettings_header_other); - settings.add(R.xml.devicesettings_contacts); + deviceSpecificSettings.addRootScreen(R.xml.devicesettings_contacts); } - settings.add(R.xml.devicesettings_header_developer); - settings.add(R.xml.devicesettings_test_features); + deviceSpecificSettings.addRootScreen(R.xml.devicesettings_test_features); - return ArrayUtils.toPrimitive(settings.toArray(new Integer[0])); + final List developer = deviceSpecificSettings.addRootScreen(DeviceSpecificSettingsScreen.DEVELOPER); + developer.add(R.xml.devicesettings_developer_add_test_activities); + + return deviceSpecificSettings; } @Override diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/TestDeviceSpecificSettingsCustomizer.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/TestDeviceSpecificSettingsCustomizer.java index d6335013d..30547cb9d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/TestDeviceSpecificSettingsCustomizer.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/test/TestDeviceSpecificSettingsCustomizer.java @@ -17,15 +17,30 @@ package nodomain.freeyourgadget.gadgetbridge.devices.test; import android.os.Parcel; +import android.widget.Toast; +import androidx.annotation.NonNull; import androidx.preference.MultiSelectListPreference; import androidx.preference.Preference; +import java.util.ArrayList; import java.util.Collections; +import java.util.Date; +import java.util.List; +import java.util.Random; import java.util.Set; +import nodomain.freeyourgadget.gadgetbridge.GBApplication; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsCustomizer; import nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSpecificSettingsHandler; +import nodomain.freeyourgadget.gadgetbridge.database.DBHandler; +import nodomain.freeyourgadget.gadgetbridge.database.DBHelper; +import nodomain.freeyourgadget.gadgetbridge.entities.BaseActivitySummary; +import nodomain.freeyourgadget.gadgetbridge.entities.DaoSession; +import nodomain.freeyourgadget.gadgetbridge.entities.Device; +import nodomain.freeyourgadget.gadgetbridge.entities.User; +import nodomain.freeyourgadget.gadgetbridge.model.ActivityKind; +import nodomain.freeyourgadget.gadgetbridge.util.GB; import nodomain.freeyourgadget.gadgetbridge.util.Prefs; public class TestDeviceSpecificSettingsCustomizer implements DeviceSpecificSettingsCustomizer { @@ -39,20 +54,55 @@ public class TestDeviceSpecificSettingsCustomizer implements DeviceSpecificSetti @Override public void customizeSettings(final DeviceSpecificSettingsHandler handler, final Prefs prefs, final String rootKey) { final Preference pref = handler.findPreference(TestDeviceConst.PREF_TEST_FEATURES); - if (pref == null) { - return; + if (pref != null) { + // Populate the preference directly from the enum + final CharSequence[] entries = new CharSequence[TestFeature.values().length]; + final CharSequence[] values = new CharSequence[TestFeature.values().length]; + for (int i = 0; i < TestFeature.values().length; i++) { + entries[i] = TestFeature.values()[i].name(); + values[i] = TestFeature.values()[i].name(); + } + if (pref instanceof MultiSelectListPreference) { + ((MultiSelectListPreference) pref).setEntries(entries); + ((MultiSelectListPreference) pref).setEntryValues(values); + } } - // Populate the preference directly from the enum - final CharSequence[] entries = new CharSequence[TestFeature.values().length]; - final CharSequence[] values = new CharSequence[TestFeature.values().length]; - for (int i = 0; i < TestFeature.values().length; i++) { - entries[i] = TestFeature.values()[i].name(); - values[i] = TestFeature.values()[i].name(); - } - if (pref instanceof MultiSelectListPreference) { - ((MultiSelectListPreference) pref).setEntries(entries); - ((MultiSelectListPreference) pref).setEntryValues(values); + final Preference addTestActivities = handler.findPreference("pref_developer_add_test_activities"); + if (addTestActivities != null) { + addTestActivities.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { + @Override + public boolean onPreferenceClick(@NonNull final Preference preference) { + try (DBHandler dbHandler = GBApplication.acquireDB()) { + final DaoSession session = dbHandler.getDaoSession(); + final Device device = DBHelper.getDevice(handler.getDevice(), session); + final User user = DBHelper.getUser(session); + + //final QueryBuilder qb = session.getBaseActivitySummaryDao().queryBuilder(); + //qb.where(BaseActivitySummaryDao.Properties.DeviceId.eq(device.getId())).buildDelete().executeDeleteWithoutDetachingEntities(); + + final List summaries = new ArrayList<>(); + + for (final ActivityKind activityKind : ActivityKind.values()) { + final BaseActivitySummary summary = new BaseActivitySummary(); + summary.setStartTime(new Date(System.currentTimeMillis() - new Random().nextInt(31 * 24 * 60 * 60) * 1000L)); + summary.setEndTime(new Date(summary.getStartTime().getTime() + new Random().nextInt(60 * 60 * 2) * 1000L)); + summary.setDevice(device); + summary.setUser(user); + summary.setActivityKind(activityKind.getCode()); + // TODO data + summaries.add(summary); + } + + session.getBaseActivitySummaryDao().insertOrReplaceInTx(summaries); + } catch (final Exception e) { + GB.toast(handler.getContext(), "Error saving activity summary", Toast.LENGTH_LONG, GB.ERROR, e); + return false; + } + + return true; + } + }); } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7b87e5633..f8f910042 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -3192,6 +3192,8 @@ Could not post ongoing notification due to missing permission Features Enabled features for this test device + Add test activities + Populate the database with dummy test activities Waiting for device scan Reconnect by BLE scan Wait for device scan instead of blind connection attempts diff --git a/app/src/main/res/xml/devicesettings_developer_add_test_activities.xml b/app/src/main/res/xml/devicesettings_developer_add_test_activities.xml new file mode 100644 index 000000000..f53f4abf9 --- /dev/null +++ b/app/src/main/res/xml/devicesettings_developer_add_test_activities.xml @@ -0,0 +1,8 @@ + + + +