Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jamorham committed Feb 7, 2024
2 parents 9b8fa94 + df83cd9 commit 8da5e5d
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.eveningoutpost.dexdrip.utils.math;


import com.eveningoutpost.dexdrip.utilitymodels.Pref;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;
Expand Down Expand Up @@ -33,9 +35,14 @@ public String toString() {
}

public Block set(final int top, final int bottom) {
if (top < 0 || bottom < 0) return null;
this.top = top;
this.bottom = bottom;
if (top < 0 || bottom < 0)
{
this.top = 0;
this.bottom = 0;
} else {
this.top = top;
this.bottom = bottom;
}
return this;
}
}
Expand Down Expand Up @@ -69,31 +76,47 @@ public Block addBlockWithMerge(int top, int bottom) {
return b;
}

// TODO this could be a bit smarter
public int findRandomAvailablePositionWithFailSafe(final int height, final int maxHeight) {
val pos = findRandomAvailablePosition(height, maxHeight);
if (pos < 0) {
return new Random().nextInt(maxHeight - height);
} else {
return pos;
boolean useTop, useTopCenter, useCenter, useCenterBottom, useBottom;
final int sectionSize = maxHeight / 5;

try {
useTop = Pref.getBooleanDefaultFalse("aod_use_top");
useTopCenter = Pref.getBooleanDefaultFalse("aod_use_top_center");
useCenter = Pref.getBooleanDefaultFalse("aod_use_center");
useCenterBottom = Pref.getBooleanDefaultFalse("aod_use_center_bottom");
useBottom = Pref.getBooleanDefaultFalse("aod_use_bottom");
} catch (NullPointerException e) {
useTop = useTopCenter = useCenter = useCenterBottom = useBottom = true;
}
}

// TODO this could be a bit smarter
public int findRandomAvailablePosition(final int height, final int maxHeight) {
if (!(useTop || useTopCenter || useCenter || useCenterBottom || useBottom))
{
useTop = useTopCenter = useCenter = useCenterBottom = useBottom = true;
}

final int bound = maxHeight - height;
if (bound < 1) return -2;

int tries = 200;
val random = new Random();

while (tries-- > 0) {
val pos = random.nextInt(bound);
if (findOverlappingBlock(pos, pos + height) == null) {
return pos;
if (bound >= 1) {
int tries = 200;
val random = new Random();

while (tries-- > 0) {
int pos = random.nextInt(bound);
if (findOverlappingBlock(pos, pos + height) == null) {
if ((pos <= sectionSize && useTop)
|| (pos >= sectionSize && pos <= 2 * sectionSize && useTopCenter)
|| (pos >= 2 * sectionSize && pos <= 3 * sectionSize && useCenter)
|| (pos >= 3 * sectionSize && pos <= 4 * sectionSize && useCenterBottom)
|| (pos >= 4 * sectionSize && useBottom)) {
return pos;
}
}
}
}
return -1;
// FailSafe
return new Random().nextInt(bound);
}


Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/values-de/strings-de.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,17 @@
<string name="title_use_number_icon_large">Verwende großes Nummernsymbol</string>
<string name="summary_number_icon_large_arrow">Zeigt den Trendpfeil im großen Symbol an</string>
<string name="title_number_icon_large_arrow">Pfeil in großem Symbol anzeigen</string>
<string name="title_xdrip_plus_accessibility">Always-On-Display Einstellungen</string>
<string name="summary_aod_use_top">Erstes Fünftel des Bildschirms für das AOD-Widget benutzen</string>
<string name="title_aod_use_top">Oberen Rand benutzen</string>
<string name="summary_aod_use_top_center">Zweites Fünftel des Bildschirms für das AOD-Widget benutzen</string>
<string name="title_aod_use_top_center">Obere Mitte benutzen</string>
<string name="summary_aod_use_center">Drittes Fünftel des Bildschirms für das AOD-Widget benutzen</string>
<string name="title_aod_use_center">Mitte benutzen</string>
<string name="summary_aod_use_center_bottom">Viertes Fünftel des Bildschirms für das AOD-Widget benutzen</string>
<string name="title_aod_use_center_bottom">Untere Mitte benutzen</string>
<string name="summary_aod_use_bottom">Letztes Fünftel des Bildschirms für das AOD-Widget benutzen</string>
<string name="title_aod_use_bottom">Unteren Rand benutzen</string>
<string name="summary_show_home_on_boot">Zeige den xDrip-Bildschirm automatisch nach dem Start des Geräts</string>
<string name="title_show_home_on_boot">Zeige xDrip beim Booten</string>
<string name="eula">EULA</string>
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,17 @@
<string name="title_use_number_icon_large">Use Large Number Icon</string>
<string name="summary_number_icon_large_arrow">Shows the trend arrow in the large icon</string>
<string name="title_number_icon_large_arrow">Show Arrow in Large Icon</string>
<string name="title_xdrip_plus_accessibility">Always On Display Settings</string>
<string name="summary_aod_use_top">Use first fifth of screen for AOD Widget</string>
<string name="title_aod_use_top">Use top</string>
<string name="summary_aod_use_top_center">Use second fifth of screen for AOD Widget</string>
<string name="title_aod_use_top_center">Use top center</string>
<string name="summary_aod_use_center">Use third fifth of screen for AOD Widget</string>
<string name="title_aod_use_center">Use center</string>
<string name="summary_aod_use_center_bottom">Use fourth fifth of screen for AOD Widget</string>
<string name="title_aod_use_center_bottom">Use center bottom</string>
<string name="summary_aod_use_bottom">Use last fifth of screen for AOD Widget</string>
<string name="title_aod_use_bottom">Use bottom</string>
<string name="summary_show_home_on_boot">Bring up xDrip screen automatically after device boots</string>
<string name="title_show_home_on_boot">Show xDrip on Boot</string>
<string name="eula">EULA</string>
Expand Down
45 changes: 37 additions & 8 deletions app/src/main/res/xml/xdrip_plus_prefs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -522,14 +522,43 @@
android:key="plus_show_reminders"
android:summary="@string/show_reminder_features"
android:title="@string/enable_reminder_features" />
<Preference
android:key="show_accessibility_settings"
android:summary="@string/summary_show_accessibility_settings"
android:title="@string/title_show_accessibility_settings">
<intent
android:action="android.settings.ACCESSIBILITY_SETTINGS"
/>
</Preference>
<PreferenceScreen
android:key="xdrip_plus_accessibility"
android:title="@string/title_xdrip_plus_accessibility">
<Preference
android:key="show_accessibility_settings"
android:summary="@string/summary_show_accessibility_settings"
android:title="@string/title_show_accessibility_settings">
<intent
android:action="android.settings.ACCESSIBILITY_SETTINGS"
/>
</Preference>
<CheckBoxPreference
android:defaultValue="true"
android:key="aod_use_top"
android:summary="@string/summary_aod_use_top"
android:title="@string/title_aod_use_top" />
<CheckBoxPreference
android:defaultValue="true"
android:key="aod_use_top_center"
android:summary="@string/summary_aod_use_top_center"
android:title="@string/title_aod_use_top_center" />
<CheckBoxPreference
android:defaultValue="true"
android:key="aod_use_center"
android:summary="@string/summary_aod_use_center"
android:title="@string/title_aod_use_center" />
<CheckBoxPreference
android:defaultValue="true"
android:key="aod_use_top_center_bottom"
android:summary="@string/summary_aod_use_center_bottom"
android:title="@string/title_aod_use_center_bottom" />
<CheckBoxPreference
android:defaultValue="true"
android:key="aod_use_bottom"
android:summary="@string/summary_aod_use_bottom"
android:title="@string/title_aod_use_bottom" />
</PreferenceScreen>
<PreferenceScreen
android:key="xdrip_plus_number_wall"
android:title="@string/title_xdrip_plus_number_wall">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,9 @@ public void findRandomAvailablePositionTest() {
val b = new BlockFinder();
b.addBlockWithMerge(10, 100);
b.addBlockWithMerge(50, 150);
assertWithMessage("invalid bound test").that(b.findRandomAvailablePosition(200, 50)).isEqualTo(-2);
assertWithMessage("impossible fit").that(b.findRandomAvailablePosition(100, 200)).isEqualTo(-1);
for (int i = 0; i < 50; i++) {
assertWithMessage("impossible fit failsafe " + i).that(b.findRandomAvailablePositionWithFailSafe(100, 200)).isIn(Range.closed(0, 100));
assertWithMessage("example fit " + i).that(b.findRandomAvailablePosition(50, 2000)).isIn(Range.closed(50, 2000 - 50));
assertWithMessage("example fit " + i).that(b.findRandomAvailablePositionWithFailSafe(50, 2000)).isIn(Range.closed(50, 2000 - 50));
}
}
}

0 comments on commit 8da5e5d

Please sign in to comment.