Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
make toast vibration length configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
lfuelling committed Sep 25, 2019
1 parent 822f5f2 commit 4f62b6d
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 21 deletions.
4 changes: 4 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ android {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

lintOptions {
abortOnError false // fuck lint
}

productFlavors {
}

packagingOptions {
exclude 'META-INF/LICENSE'
exclude 'META-INF/DEPENDENCIES'
Expand All @@ -61,6 +64,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.browser:browser:1.0.0'
implementation 'androidx.preference:preference:1.1.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation 'com.github.BoD:aLibGlitch:1.0.0'
implementation 'org.jsoup:jsoup:1.12.1'
Expand Down
10 changes: 7 additions & 3 deletions app/src/main/java/io/lerk/lrkFM/Pref.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.NonNull;

import android.util.Log;

import java.util.HashSet;
Expand All @@ -16,15 +18,17 @@
* To read settings you can replace
* <pre>PreferenceManager.getDefaultSharedPreferences(context).getBoolean(MY_PREFERENCE.getKey(), false);</pre>
* with <pre>new Pref&lt;Boolean&gt;(MY_PREFERENCE).getValue();</pre>.
*
* <p>
* To write settings you can replace
* <pre>PreferenceManager.getDefaultSharedPreferences(context).edit().putBoolean(MY_PREFERENCE.getKey(), false).apply();</pre>
* with <pre>new Pref&lt;Boolean;&gt;(MY_PREFERENCE).setValue(false);</pre>
* <p>
* Apparently androidx/Android Q introduces Integers as valid preference type.
* Since {@link SharedPreferences} (afaik) don't support that, they should be handled as Strings.
*
* @param <T> The type of preference to use. Currently {@link Boolean}, {@link String}, {@link HashSet}.
* @author Lukas Fülling ([email protected])
* @see PreferenceEntity
*
* @param <T> The type of preference to use. Currently {@link Boolean}, {@link String}, {@link HashSet}.
*/
public class Pref<T> {

Expand Down
19 changes: 16 additions & 3 deletions app/src/main/java/io/lerk/lrkFM/VibratingToast.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package io.lerk.lrkFM;

import android.content.Context;
import android.content.res.Resources;
import android.os.Vibrator;
import android.widget.Toast;

import androidx.preference.PreferenceManager;

import io.lerk.lrkFM.consts.PreferenceEntity;

/**
Expand All @@ -15,14 +18,24 @@
*/
public class VibratingToast extends Toast {

public VibratingToast(Context context, CharSequence text, int duration) {
/**
* Constructor. Calling this will also show the toast.
*
* @param context the context
* @param text the text to show
* @param toastDuration toast duration. Must be a constant from {@link Toast} class
* @see Toast#LENGTH_LONG
* @see Toast#LENGTH_SHORT
*/
public VibratingToast(Context context, CharSequence text, int toastDuration) {
super(context);
if (new Pref<Boolean>(PreferenceEntity.VIBRATING_TOASTS).getValue()) {
Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null) {
vibrator.vibrate(120);
int duration = Integer.parseInt(new Pref<String>(PreferenceEntity.VIBRATION_LENGTH).getValue());
vibrator.vibrate(duration);
}
}
makeText(context, text, duration).show();
makeText(context, text, toastDuration).show();
}
}
34 changes: 26 additions & 8 deletions app/src/main/java/io/lerk/lrkFM/activities/SettingsActivity.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package io.lerk.lrkFM.activities;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceGroup;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.widget.Toolbar;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
Expand All @@ -26,10 +30,12 @@
import io.lerk.lrkFM.R;
import io.lerk.lrkFM.activities.themed.ThemedAppCompatPreferenceActivity;
import io.lerk.lrkFM.consts.PreferenceEntity;
import io.lerk.lrkFM.consts.PreferenceStore;

/**
* Settings.
*/
@SuppressWarnings("deprecation") // :c
public class SettingsActivity extends ThemedAppCompatPreferenceActivity {

@Override
Expand Down Expand Up @@ -144,6 +150,10 @@ public void onCreate(Bundle savedInstanceState) {
addPreferencesFromResource(R.xml.preferences_notifications);
setHasOptionsMenu(true);
addOnPreferenceChangeListeners(this.getPreferenceScreen());

Preference vibrationLengthPreference = findPreference(PreferenceEntity.VIBRATION_LENGTH.getKey());
vibrationLengthPreference.setSummary(getContext().getResources() // add current value to description
.getString(R.string.pref_vibration_length_desc_init));
}

@Override
Expand All @@ -158,7 +168,8 @@ public boolean onOptionsItemSelected(MenuItem item) {
}

/**
* Adds a generic {@link android.preference.Preference.OnPreferenceChangeListener} to update the preferences_general with {@link Pref}. It does that recursively for every Preference contained in the supplied {@link PreferenceGroup}
* Adds a generic {@link android.preference.Preference.OnPreferenceChangeListener} to update the preferences with {@link Pref}.
* It does that recursively for every Preference contained in the supplied {@link PreferenceGroup}.
* This is what you get, when customizing too much stuff.
*
* @param preferenceGroup the current {@link PreferenceGroup}
Expand All @@ -175,28 +186,35 @@ private static void addOnPreferenceChangeListeners(PreferenceGroup preferenceGro
}

/**
* This actually adds the listener. This is called by {@link #addOnPreferenceChangeListeners(PreferenceGroup)}
* This actually adds the listener.
* This is called by {@link #addOnPreferenceChangeListeners(PreferenceGroup)}.
*
* @param p the {@link Preference}
*/
private static void setOnPreferenceChangeListener(Preference p) {
p.setOnPreferenceChangeListener((preference, newValue) -> {
if (preference.getKey().equals("filename_length")) {
if (Integer.parseInt(String.valueOf(newValue)) >= 4) {
return true;
} else {
if (Integer.parseInt(String.valueOf(newValue)) <= 4) {
Toast.makeText(preference.getContext(), R.string.err_filename_length_lower_than_four, Toast.LENGTH_LONG).show();
((EditTextPreference) preference).setText("4");
return true;
return false;
}
} else if (preference.getKey().equals("vibration_length")) {
preference.setSummary(preference.getContext().getResources()
.getString(R.string.pref_vibration_length_desc) + " " + newValue);
Vibrator vibrator = (Vibrator) preference.getContext().getSystemService(Context.VIBRATOR_SERVICE);
if (vibrator != null) {
vibrator.vibrate((Integer) newValue);
}
}
PreferenceEntity preferenceEntity = PreferenceEntity.determineByKey(preference.getKey());
if (preferenceEntity != null) {
if (newValue instanceof Boolean) {
new Pref<Boolean>(preferenceEntity).setValue((Boolean) newValue);
return true;
} else if (newValue instanceof String) {
new Pref<String>(preferenceEntity).setValue((String) newValue);
} else if (newValue instanceof String || newValue instanceof Integer) {
String value = newValue.toString();
new Pref<String>(preferenceEntity).setValue(value);
return true;
} else if (newValue instanceof HashSet) {
new Pref<HashSet>(preferenceEntity).setValue((HashSet) newValue);
Expand Down
18 changes: 15 additions & 3 deletions app/src/main/java/io/lerk/lrkFM/consts/PreferenceEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import io.lerk.lrkFM.LrkFMApp;
import io.lerk.lrkFM.R;
import io.lerk.lrkFM.VibratingToast;

import static io.lerk.lrkFM.consts.PreferenceStore.CLOUD_BACKED;
import static io.lerk.lrkFM.consts.PreferenceStore.LOCAL;
Expand Down Expand Up @@ -116,6 +117,11 @@ public enum PreferenceEntity {
*/
VIBRATING_TOASTS(CLOUD_BACKED, "context_toasts_vibrating", true),

/**
* Vibration length of vibrating Toasts.
*/
VIBRATION_LENGTH(CLOUD_BACKED, "vibration_length", 120),

/**
* Replace checkboxes with menu buttons.
*/
Expand All @@ -128,6 +134,7 @@ public enum PreferenceEntity {

/**
* The preference's storage type.
*
* @see PreferenceStore
*/
private final PreferenceStore store;
Expand All @@ -144,8 +151,9 @@ public enum PreferenceEntity {

/**
* Constructor.
* @param store teh storage type
* @param key the key
*
* @param store teh storage type
* @param key the key
* @param defaultValue the default value
*/
PreferenceEntity(PreferenceStore store, String key, Object defaultValue) {
Expand All @@ -157,6 +165,7 @@ public enum PreferenceEntity {

/**
* Getter.
*
* @return the key.
*/
public String getKey() {
Expand All @@ -165,6 +174,7 @@ public String getKey() {

/**
* Getter.
*
* @return the storage type.
* @see PreferenceStore
*/
Expand All @@ -174,6 +184,7 @@ public PreferenceStore getStore() {

/**
* Getter.
*
* @return the type.
*/
public Class getType() {
Expand All @@ -182,6 +193,7 @@ public Class getType() {

/**
* Getter.
*
* @return the default value.
*/
public Object getDefaultValue() {
Expand All @@ -191,7 +203,7 @@ public Object getDefaultValue() {
@Nullable
public static PreferenceEntity determineByKey(String key) {
for (PreferenceEntity preference : PreferenceEntity.values()) {
if(preference.getKey().equals(key)) {
if (preference.getKey().equals(key)) {
return preference;
}
}
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,20 @@
<string name="restart_required_title">Neustart Erforderlich</string>
<string name="restart_required_message">Eine Änderung dieser Einstellung erfordert einen Neustart von lrkFM.</string>
<string name="a11y_desc_logo">App Logo</string>
<string name="pref_vibration_length_desc">Legt fest, wie lange die Vibration im Falle eines Toasts dauert (in ms). Momentan:</string>
<string name="pref_vibration_length_desc_init">Legt fest, wie lange die Vibration im Falle eines Toasts dauert (in ms).</string>
<string name="action_copy">Kopieren…</string>
<string name="action_delete">Löschen…</string>
<string name="action_move">Verschieben…</string>
<string name="enter_new_file_name">Neuen Dateinamen eingeben</string>
<string name="err_deleting_element_mult">Fehler beim löschen:</string>
<string name="file_already_selected">Datei ist bereits ausgewählt!</string>
<string name="nav_bug_report">Fehler Melden</string>
<string name="new_file">Neue Datei</string>
<string name="pref_header_notifications_toasts"><![CDATA[Benachrichtigungen & Toasts]]></string>
<string name="pref_show_menu_button">Optionsbutton anzeigen</string>
<string name="pref_show_menu_button_desc">Wenn aktiv, werden die Checkboxen durch den Optionsbutton aus vorheringen Versionen ersetzt.</string>
<string name="pref_vibration_length">Dauer der Vibration</string>
<string name="warn_delete_mult_msg_start">Sicher, dass</string>
<string name="warn_delete_mult_msg_end">Dateien gelöscht werden sollen?</string>
</resources>
12 changes: 8 additions & 4 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<string name="cancel">@android:string/cancel</string>
<string name="invalid_path">Invalid path! Only absolute paths pointing to a directory please.</string>
<string name="add_bookmark">Add Bookmark</string>
<string name="nav_bug_report" translatable="false">Report Bug</string>
<string name="nav_bug_report">Report Bug</string>
<string name="share_file">Share File</string>
<string name="pref_show_toast_cd">Show Toast on directory change</string>
<string name="pref_show_toast_cd_desc">When loading a new directory, a toast will be shown.</string>
Expand Down Expand Up @@ -153,8 +153,8 @@
<string name="only_one_app_to_handle_file">Only one app to handle this file found!</string>
<string name="pref_themes_text_default">Default</string>
<string name="pref_themes_text_dark">Dark</string>
<string translatable="false" name="pref_themes_value_default">default</string>
<string translatable="false" name="pref_themes_value_dark">dark</string>
<string name="pref_themes_value_default" translatable="false">default</string>
<string name="pref_themes_value_dark" translatable="false">dark</string>
<string name="pref_themes_desc">Set the app\'s color scheme.</string>
<string name="pref_themes_title">Theme</string>
<string name="dialog_icon_desc">Dialog icon.</string>
Expand All @@ -177,11 +177,15 @@
<string name="action_copy">Copy…</string>
<string name="action_move">Move…</string>
<string name="action_delete">Delete…</string>
<string name="empty" />
<string name="empty" translatable="false" />
<string name="warn_delete_mult_msg_start">Are you sure that you want to delete</string>
<string name="warn_delete_mult_msg_end">file(s)?</string>
<string name="err_deleting_element_mult">Error deleting:</string>
<string name="pref_header_notifications_toasts"><![CDATA[Notifications & Toasts]]></string>
<string name="pref_show_menu_button">Show menu button</string>
<string name="pref_show_menu_button_desc">When active, the checkboxes will be replaced with menu buttons.</string>
<string name="pref_vibration_length_default" translatable="false">120</string>
<string name="pref_vibration_length">Vibration Length</string>
<string name="pref_vibration_length_desc">Configure the length of vibration when toasts appear. Currently:</string>
<string name="pref_vibration_length_desc_init">Configure the length of vibration when toasts appear.</string>
</resources>
7 changes: 7 additions & 0 deletions app/src/main/res/xml/preferences_notifications.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,12 @@
android:key="context_toasts_vibrating"
android:summary="@string/pref_context_toasts_vibrating_desc"
android:title="@string/pref_context_toasts_vibrating" />
<SeekBarPreference
android:key="vibration_length"
android:defaultValue="@string/pref_vibration_length_default"
android:dependency="context_toasts_vibrating"
android:summary="@string/pref_vibration_length_desc"
android:max="250"
android:title="@string/pref_vibration_length"/>
</PreferenceCategory>
</PreferenceScreen>

0 comments on commit 4f62b6d

Please sign in to comment.