-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix integer overflow and parsing issues (#100)
- Loading branch information
Showing
8 changed files
with
152 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
.../src/main/java/info/varden/hauk/system/preferences/ui/listener/CascadeChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package info.varden.hauk.system.preferences.ui.listener; | ||
|
||
import androidx.preference.Preference; | ||
|
||
/** | ||
* Preference change listener that cascades the change event to several | ||
* {@link androidx.preference.Preference.OnPreferenceChangeListener}s. | ||
* | ||
* @author Marius Lindvall | ||
*/ | ||
public final class CascadeChangeListener implements Preference.OnPreferenceChangeListener { | ||
private final Preference.OnPreferenceChangeListener[] listeners; | ||
|
||
public CascadeChangeListener(Preference.OnPreferenceChangeListener[] listeners) { | ||
this.listeners = listeners.clone(); | ||
} | ||
|
||
@Override | ||
public boolean onPreferenceChange(Preference preference, Object newValue) { | ||
for (Preference.OnPreferenceChangeListener listener : this.listeners) { | ||
if (!listener.onPreferenceChange(preference, newValue)) return false; | ||
} | ||
return true; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...c/main/java/info/varden/hauk/system/preferences/ui/listener/FloatBoundChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package info.varden.hauk.system.preferences.ui.listener; | ||
|
||
import androidx.preference.Preference; | ||
|
||
import info.varden.hauk.utils.Log; | ||
|
||
/** | ||
* Bounds checking preference change listener that ensures the given value is between two floating | ||
* point values (inclusive). | ||
* | ||
* @author Marius Lindvall | ||
*/ | ||
public final class FloatBoundChangeListener implements Preference.OnPreferenceChangeListener { | ||
private final float min; | ||
private final float max; | ||
|
||
public FloatBoundChangeListener(float min, float max) { | ||
this.min = min; | ||
this.max = max; | ||
} | ||
|
||
@Override | ||
public boolean onPreferenceChange(Preference preference, Object newValue) { | ||
try { | ||
float value = Float.parseFloat((String) newValue); | ||
return value >= this.min && value <= this.max; | ||
} catch (NumberFormatException ex) { | ||
Log.e("Number %s is not a valid float", ex, newValue); //NON-NLS | ||
} | ||
return false; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...main/java/info/varden/hauk/system/preferences/ui/listener/IntegerBoundChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package info.varden.hauk.system.preferences.ui.listener; | ||
|
||
import androidx.preference.Preference; | ||
|
||
import info.varden.hauk.utils.Log; | ||
|
||
/** | ||
* Bounds checking preference change listener that ensures the given value is between two integer | ||
* values (inclusive). | ||
* | ||
* @author Marius Lindvall | ||
*/ | ||
public final class IntegerBoundChangeListener implements Preference.OnPreferenceChangeListener { | ||
private final int min; | ||
private final int max; | ||
|
||
public IntegerBoundChangeListener(int min, int max) { | ||
this.min = min; | ||
this.max = max; | ||
} | ||
|
||
@Override | ||
public boolean onPreferenceChange(Preference preference, Object newValue) { | ||
try { | ||
int value = Integer.parseInt((String) newValue); | ||
return value >= this.min && value <= this.max; | ||
} catch (NumberFormatException ex) { | ||
Log.e("Number %s is not a valid integer", ex, newValue); //NON-NLS | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters