Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/AnkiDroidApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,6 @@ open class AnkiDroidApp :
/** Running under instrumentation. a "/androidTest" directory will be created which contains a test collection */
@Suppress("ktlint:standard:property-naming")
var INSTRUMENTATION_TESTING = false
const val XML_CUSTOM_NAMESPACE = "http://arbitrary.app.namespace/com.ichi2.anki"
const val ANDROID_NAMESPACE = "http://schemas.android.com/apk/res/android"

// Tag for logging messages.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import android.text.InputFilter.LengthFilter
import android.text.InputType
import android.util.AttributeSet
import android.view.View
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.R
import timber.log.Timber

@Suppress(
Expand Down Expand Up @@ -115,7 +115,9 @@ open class NumberRangePreference :
* This method should only be called once from the constructor.
*/
private fun getMinFromAttributes(attrs: AttributeSet?): Int =
attrs?.getAttributeIntValue(AnkiDroidApp.XML_CUSTOM_NAMESPACE, "min", 0) ?: 0
context.obtainStyledAttributes(attrs, R.styleable.NumberRangePreference).use {
it.getInt(R.styleable.NumberRangePreference_min, 0)
}

/**
* Returns the value of the max attribute, or its default value if not specified
Expand All @@ -124,8 +126,9 @@ open class NumberRangePreference :
* This method should only be called once from the constructor.
*/
private fun getMaxFromAttributes(attrs: AttributeSet?): Int =
attrs?.getAttributeIntValue(AnkiDroidApp.XML_CUSTOM_NAMESPACE, "max", Int.MAX_VALUE)
?: Int.MAX_VALUE
context.obtainStyledAttributes(attrs, R.styleable.NumberRangePreference).use {
it.getInt(R.styleable.NumberRangePreference_max, Int.MAX_VALUE)
}

/**
* Update settings to only allow integer input and set the maximum number of digits allowed in the text field based
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import android.widget.EditText
import androidx.core.content.withStyledAttributes
import androidx.preference.EditTextPreference
import androidx.preference.EditTextPreferenceDialogFragmentCompat
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.R
import com.ichi2.anki.common.annotations.NeedsTest
import com.ichi2.anki.utils.getFormattedStringOrPlurals
Expand All @@ -49,8 +48,10 @@ open class NumberRangePreferenceCompat
private set

init {
min = attrs?.getAttributeIntValue(AnkiDroidApp.XML_CUSTOM_NAMESPACE, "min", 0) ?: 0
max = attrs?.getAttributeIntValue(AnkiDroidApp.XML_CUSTOM_NAMESPACE, "max", Int.MAX_VALUE) ?: Int.MAX_VALUE
context.withStyledAttributes(attrs, R.styleable.NumberRangePreferenceCompat) {
min = getInt(R.styleable.NumberRangePreferenceCompat_min, 0)
max = getInt(R.styleable.NumberRangePreferenceCompat_max, Int.MAX_VALUE)
}
defaultValue = attrs?.getAttributeValue("http://schemas.android.com/apk/res/android", "defaultValue")

context.withStyledAttributes(attrs, R.styleable.CustomPreference) {
Expand Down Expand Up @@ -124,7 +125,7 @@ open class NumberRangePreferenceCompat
private fun getDefaultValue(): Int {
return try {
return defaultValue?.toInt() ?: min
} catch (e: Exception) {
} catch (_: Exception) {
min
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import android.content.Context
import android.text.InputType
import android.util.AttributeSet
import android.view.View
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.R
import com.ichi2.anki.common.utils.ext.stringIterable
import com.ichi2.anki.showThemedToast
Expand Down Expand Up @@ -105,8 +104,9 @@ class StepsPreference :
}

private fun getAllowEmptyFromAttributes(attrs: AttributeSet?): Boolean =
attrs?.getAttributeBooleanValue(AnkiDroidApp.XML_CUSTOM_NAMESPACE, "allowEmpty", true)
?: true
context.obtainStyledAttributes(attrs, R.styleable.StepsPreference).use {
it.getBoolean(R.styleable.StepsPreference_allowEmpty, true)
}

companion object {
/**
Expand Down
17 changes: 16 additions & 1 deletion AnkiDroid/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@

<!-- Custom preferences -->
<attr name="allowEmpty" format="boolean" />

<attr name="min" format="integer" />
<attr name="max" format="integer" />

<declare-styleable name="CustomPreference">
<!-- Whether the preference should automatically set its summary to the value saved for the
Expand All @@ -37,6 +38,20 @@
<attr name="summaryFormat" format="string"/>
</declare-styleable>

<declare-styleable name="NumberRangePreference">
<attr name="min"/>
<attr name="max"/>
</declare-styleable>

<declare-styleable name="StepsPreference">
<attr name="allowEmpty"/>
</declare-styleable>

<declare-styleable name="NumberRangePreferenceCompat">
Copy link
Member

@david-allison david-allison Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This appears to be unused. I would have expected a declare-styleable for each preference, rather than using the hardcoded namespace.

Why did you choose the namespace option?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is unused because I'm not reading min/max via R.styleable in preference, I used http://schemas.android.com/apk/res-auto as a shortcut.
Should I define a declare-styleable for each preference(NumberRangePreference, NumberRangePreferenceCompat & StepsPreference) and read attributes via R.styleable, instead of using the namespace approach?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get why you chose the hardcoded namespace option. It's not something I'm too familiar with.

declare-styleable seems better intuitively, but I want to understand your reasoning.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I did it this way because the preference was using XML_CUSTOM_NAMESPACE so I thought to use RES_AUTO_NAMESPACE the similar way. But now I will use declare-styleable which is better and update the code accordingly.

<attr name="min"/>
<attr name="max"/>
</declare-styleable>

<declare-styleable name="HtmlHelpPreference">
<attr name="substitution1" format="string" />
<attr name="substitution2" format="string" />
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/res/xml/cram_deck_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
-->

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://arbitrary.app.namespace/com.ichi2.anki" >
xmlns:app="http://schemas.android.com/apk/res-auto" >

<PreferenceCategory android:title="@string/deck_conf_cram_filter" >
<com.ichi2.preferences.AutoFocusEditTextPreference
Expand Down
4 changes: 2 additions & 2 deletions AnkiDroid/src/main/res/xml/preferences_advanced.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@

<!-- Advanced Preferences -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app1="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/pref_cat_advanced"
android:key="@string/pref_advanced_screen_key">
<EditTextPreference
android:defaultValue="/sdcard/AnkiDroid"
android:key="@string/pref_ankidroid_directory_key"
android:title="@string/col_path"
app1:useSimpleSummaryProvider="true"/>
app:useSimpleSummaryProvider="true"/>
<Preference
android:summary="@string/reset_languages_summ"
android:title="@string/reset_languages"
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/res/xml/preferences_backup_limits.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://arbitrary.app.namespace/com.ichi2.anki"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/button_backup"
android:key="@string/pref_backups_screen_key">
<!-- Includes modified text from `TR.preferencesBackupExplanation()`
Expand Down
9 changes: 4 additions & 5 deletions AnkiDroid/src/main/res/xml/preferences_dev_options.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
~ at developers, who are assumed to speak English.
-->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://arbitrary.app.namespace/com.ichi2.anki"
xmlns:app1="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:title="@string/pref_cat_dev_options"
android:key="@string/pref_dev_options_screen_key">
<SwitchPreferenceCompat
Expand Down Expand Up @@ -66,7 +65,7 @@
<com.ichi2.preferences.IncrementerNumberRangePreferenceCompat
android:key="@string/pref_fill_default_deck_number_key"
android:title="Number of notes to generate"
app1:useSimpleSummaryProvider="true"
app:useSimpleSummaryProvider="true"
android:defaultValue="200"
app:min="1"
/>
Expand All @@ -81,14 +80,14 @@
<com.ichi2.preferences.IncrementerNumberRangePreferenceCompat
android:key="@string/pref_fill_collection_number_file_key"
android:title="Number of files to generate"
app1:useSimpleSummaryProvider="true"
app:useSimpleSummaryProvider="true"
android:defaultValue="20"
app:min="1"
/>
<com.ichi2.preferences.IncrementerNumberRangePreferenceCompat
android:key="@string/pref_fill_collection_size_file_key"
android:title="Size of the files to generate (in byte)"
app1:useSimpleSummaryProvider="true"
app:useSimpleSummaryProvider="true"
android:defaultValue="1000000"
app:min="1"
/>
Expand Down
11 changes: 5 additions & 6 deletions AnkiDroid/src/main/res/xml/preferences_reviewing.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@

<!-- Reviewing Preferences -->
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://arbitrary.app.namespace/com.ichi2.anki"
xmlns:app1="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:title="@string/pref_cat_reviewing"
android:key="@string/pref_reviewing_screen_key"
Expand All @@ -34,20 +33,20 @@
android:valueFrom="0"
android:valueTo="23"
android:title="@string/day_offset_with_description"
app1:persistent="false"
app1:summaryFormat="@plurals/day_offset_summ"/>
app:persistent="false"
app:summaryFormat="@plurals/day_offset_summ"/>
<com.ichi2.preferences.NumberRangePreferenceCompat
android:key="@string/learn_cutoff_preference"
android:title="@string/learn_cutoff"
app:max="999"
app:min="0"
app1:summaryFormat="@plurals/pref_summ_minutes"/>
app:summaryFormat="@plurals/pref_summ_minutes"/>
<com.ichi2.preferences.NumberRangePreferenceCompat
android:key="@string/time_limit_preference"
android:title="@string/time_limit"
app:max="9999"
app:min="0"
app1:summaryFormat="@plurals/pref_summ_minutes"/>
app:summaryFormat="@plurals/pref_summ_minutes"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_cat_advanced">
<SwitchPreferenceCompat
Expand Down