Skip to content

Commit

Permalink
chore: minSdk bump to 24
Browse files Browse the repository at this point in the history
this is a big, boring move from 23 to 24
- drop any >= 24 checks
- inline anything that is now always available from compat
- keep `Shortcut[Group]` for another commit, to reduce line noise
  - unusually means that we keep `CompatV24` in this commit
  • Loading branch information
mikehardy committed Nov 11, 2024
1 parent 61fb493 commit 58e6a9d
Show file tree
Hide file tree
Showing 31 changed files with 167 additions and 456 deletions.
5 changes: 0 additions & 5 deletions AnkiDroid/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,6 @@ android {
versionName="2.20alpha1"
minSdk libs.versions.minSdk.get().toInteger()

// Stays until this is in a release: https://github.com/google/desugar_jdk_libs/commit/c01a5446ca13586b801dbba4d83c6821337b3cc2
if (testReleaseBuild && minSdk < 24) {
minSdk 24
}

// After #13695: change .tests_emulator.yml
targetSdk libs.versions.targetSdk.get().toInteger()
testApplicationId "com.ichi2.anki.tests"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,22 @@
* this program. If not, see <http://www.gnu.org/licenses/>.
*/

package com.ichi2.anki.compat
package com.ichi2.anki

import androidx.test.ext.junit.runners.AndroidJUnit4
import com.ichi2.anki.tests.InstrumentedTest
import com.ichi2.compat.CompatHelper
import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import java.util.Locale

@RunWith(AndroidJUnit4::class)
class CompatNormalizeTest : InstrumentedTest() {
class TtsVoicesTest : InstrumentedTest() {
@Test
fun normalize() {
fun assertEqual(l: Locale, str: String) {
val normalized = CompatHelper.compat.normalize(l)
val normalized = AndroidTtsVoice.normalize(l)
assertThat(normalized.toLanguageTag(), equalTo(str))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package com.ichi2.anki.dialogs
import androidx.core.os.bundleOf
import androidx.fragment.app.testing.launchFragment
import androidx.test.espresso.Espresso.onView
import androidx.test.espresso.assertion.ViewAssertions.doesNotExist
import androidx.test.espresso.assertion.ViewAssertions.matches
import androidx.test.espresso.matcher.ViewMatchers.isDisplayed
import androidx.test.espresso.matcher.ViewMatchers.withText
Expand All @@ -36,39 +35,14 @@ class ModelEditorContextMenuTest : InstrumentedTest() {

@Test
@Ignore("flaky")
fun showsAllOptionsIfAboveN() {
fun showsAllOptions() {
launchFragment(
fragmentArgs = bundleOf(ModelEditorContextMenu.KEY_LABEL to testDialogTitle),
themeResId = R.style.Theme_Light
) { MockModelEditorContextMenu(isAtLeastAtN = true) }
) { ModelEditorContextMenu() }
onView(withText(testDialogTitle)).check(matches(isDisplayed()))
ModelEditorContextMenuAction.entries.forEach {
onView(withText(it.actionTextId)).check(matches(isDisplayed()))
}
}

@Test
@Ignore("flaky")
fun doesNotShowLanguageHintOptionIfBelowN() {
launchFragment(
fragmentArgs = bundleOf(ModelEditorContextMenu.KEY_LABEL to testDialogTitle),
themeResId = R.style.Theme_Light
) { MockModelEditorContextMenu(isAtLeastAtN = false) }
onView(withText(testDialogTitle)).check(matches(isDisplayed()))
// ModelEditorContextMenuAction.AddLanguageHint shouldn't be available
onView(withText(ModelEditorContextMenuAction.AddLanguageHint.actionTextId)).check(
doesNotExist()
)
// make sure we aren't losing other items besides ModelEditorContextMenuAction.AddLanguageHint
ModelEditorContextMenuAction.entries
.filterNot { it == ModelEditorContextMenuAction.AddLanguageHint }.forEach {
onView(withText(it.actionTextId)).check(matches(isDisplayed()))
}
}

class MockModelEditorContextMenu(
private val isAtLeastAtN: Boolean
) : ModelEditorContextMenu() {
override fun isAtLeastAtN(): Boolean = isAtLeastAtN
}
}
34 changes: 31 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/AnkiActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ import com.ichi2.anki.receiver.SdCardReceiver
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.workarounds.AppLoadedFromBackupWorkaround.showedActivityFailedScreen
import com.ichi2.async.CollectionLoader
import com.ichi2.compat.CompatHelper
import com.ichi2.compat.CompatHelper.Companion.registerReceiverCompat
import com.ichi2.compat.CompatV24
import com.ichi2.compat.CompatV24.ShortcutGroup
import com.ichi2.compat.ShortcutGroupProvider
import com.ichi2.compat.customtabs.CustomTabActivityHelper
import com.ichi2.compat.customtabs.CustomTabsFallback
import com.ichi2.compat.customtabs.CustomTabsHelper
import com.ichi2.compat.shortcut
import com.ichi2.libanki.Collection
import com.ichi2.themes.Themes
import com.ichi2.utils.AdaptionUtil
Expand Down Expand Up @@ -637,14 +638,41 @@ open class AnkiActivity : AppCompatActivity, SimpleMessageDialogListener, Shortc
menu: Menu?,
deviceId: Int
) {
val shortcutGroups = CompatHelper.compat.getShortcuts(this)
val shortcutGroups = getShortcuts()
data.addAll(shortcutGroups)
super.onProvideKeyboardShortcuts(data, menu, deviceId)
}

/**
* Shows keyboard shortcuts dialog
*/
fun showKeyboardShortcutsDialog() {
val shortcutsGroup = getShortcuts()
// Don't show keyboard shortcuts dialog if there is no available shortcuts and also
// if there's 1 item because shortcutsGroup always includes generalShortcutGroup.
if (shortcutsGroup.size <= 1) return
Timber.i("displaying keyboard shortcut screen")
requestShowKeyboardShortcuts()
}

/**
* Get current activity keyboard shortcuts
*/
fun getShortcuts(): List<KeyboardShortcutGroup> {
val generalShortcutGroup = ShortcutGroup(
listOf(
shortcut("Alt+K", R.string.show_keyboard_shortcuts_dialog),
shortcut("Ctrl+Z", R.string.undo)
),
R.string.pref_cat_general
).toShortcutGroup(this)

return listOfNotNull(shortcuts?.toShortcutGroup(this), generalShortcutGroup)
}

override fun onKeyUp(keyCode: Int, event: KeyEvent): Boolean {
if (event.isAltPressed && keyCode == KeyEvent.KEYCODE_K) {
CompatHelper.compat.showKeyboardShortcutsDialog(this)
showKeyboardShortcutsDialog()
return true
}

Expand Down
3 changes: 1 addition & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardBrowser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ import com.ichi2.anki.utils.roundedTimeSpanUnformatted
import com.ichi2.anki.widgets.DeckDropDownAdapter.SubtitleListener
import com.ichi2.annotations.NeedsTest
import com.ichi2.async.renderBrowserQA
import com.ichi2.compat.CompatHelper
import com.ichi2.compat.CompatV24
import com.ichi2.compat.shortcut
import com.ichi2.libanki.Card
Expand Down Expand Up @@ -678,7 +677,7 @@ open class CardBrowser :
return true
} else if (event.isAltPressed) {
Timber.i("Alt+K: Show keyboard shortcuts dialog")
CompatHelper.compat.showKeyboardShortcutsDialog(this)
showKeyboardShortcutsDialog()
return true
}
}
Expand Down
7 changes: 3 additions & 4 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardTemplateEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package com.ichi2.anki

import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
Expand Down Expand Up @@ -560,12 +559,12 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener {
}

override fun onPrepareActionMode(mode: ActionMode, menu: Menu): Boolean {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && menu.findItem(insertFieldId) != null) {
if (menu.findItem(insertFieldId) != null) {
return false
}
val initialSize = menu.size()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && currentEditorViewId != R.id.styling_edit) {
if (currentEditorViewId != R.id.styling_edit) {
// 10644: Do not pass in a R.string as the final parameter as MIUI on Android 12 crashes.
menu.add(Menu.FIRST, insertFieldId, 0, getString(R.string.card_template_editor_insert_field))
}
Expand All @@ -575,7 +574,7 @@ open class CardTemplateEditor : AnkiActivity(), DeckSelectionListener {

override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean {
val itemId = item.itemId
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && itemId == insertFieldId) {
return if (itemId == insertFieldId) {
showInsertFieldDialog()
mode.finish()
true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@

package com.ichi2.anki

import android.os.Build
import android.view.ActionMode
import android.view.Menu
import android.view.MenuItem
import android.view.View
import androidx.annotation.RequiresApi

/**
* Custom ActionMode.Callback implementation for adding and handling cloze deletion action
Expand All @@ -34,7 +32,6 @@ class CustomActionModeCallback(
private val clozeMenuId: Int,
private val onActionItemSelected: (mode: ActionMode, item: MenuItem) -> Boolean
) : ActionMode.Callback {
@RequiresApi(Build.VERSION_CODES.N)
private val setLanguageId = View.generateViewId()

override fun onCreateActionMode(mode: ActionMode, menu: Menu): Boolean {
Expand All @@ -46,7 +43,7 @@ class CustomActionModeCallback(
if (menu.findItem(clozeMenuId) != null) {
return false
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && menu.findItem(setLanguageId) != null) {
if (menu.findItem(setLanguageId) != null) {
return false
}

Expand Down
5 changes: 2 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ import com.ichi2.anki.worker.SyncWorker
import com.ichi2.anki.worker.UniqueWorkNames
import com.ichi2.annotations.NeedsTest
import com.ichi2.async.deleteMedia
import com.ichi2.compat.CompatHelper
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
import com.ichi2.compat.CompatHelper.Companion.sdkVersion
import com.ichi2.compat.CompatV24
Expand Down Expand Up @@ -182,6 +181,7 @@ import com.ichi2.utils.SyncStatus
import com.ichi2.utils.VersionUtils
import com.ichi2.utils.cancelable
import com.ichi2.utils.checkBoxPrompt
import com.ichi2.utils.configureView
import com.ichi2.utils.customView
import com.ichi2.utils.message
import com.ichi2.utils.negativeButton
Expand Down Expand Up @@ -584,9 +584,8 @@ open class DeckPicker :
}
}

CompatHelper.compat.configureView(
pullToSyncWrapper.configureView(
this,
pullToSyncWrapper,
IMPORT_MIME_TYPES,
DropHelper.Options.Builder()
.setHighlightColor(R.color.material_lime_green_A700)
Expand Down
2 changes: 1 addition & 1 deletion AnkiDroid/src/main/java/com/ichi2/anki/FieldEditLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ class FieldEditLine : FrameLayout {
}

fun setHintLocale(hintLocale: Locale?) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && hintLocale != null) {
if (hintLocale != null) {
editText.setHintLocale(hintLocale)
}
}
Expand Down
3 changes: 0 additions & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/FieldEditText.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,12 @@ import android.content.ClipboardManager
import android.content.Context
import android.graphics.drawable.Drawable
import android.net.Uri
import android.os.Build
import android.os.LocaleList
import android.os.Parcelable
import android.text.InputType
import android.util.AttributeSet
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import androidx.annotation.RequiresApi
import androidx.annotation.VisibleForTesting
import com.google.android.material.color.MaterialColors
import com.ichi2.anki.preferences.sharedPrefs
Expand Down Expand Up @@ -106,7 +104,6 @@ class FieldEditText : FixedEditText, NoteService.NoteField {
super.onSelectionChanged(selStart, selEnd)
}

@RequiresApi(api = Build.VERSION_CODES.N)
fun setHintLocale(locale: Locale) {
Timber.d("Setting hint locale to '%s'", locale)
imeHintLocales = LocaleList(locale)
Expand Down
17 changes: 2 additions & 15 deletions AnkiDroid/src/main/java/com/ichi2/anki/ModelFieldEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
****************************************************************************************/
package com.ichi2.anki

import android.os.Build
import android.os.Bundle
import android.text.InputType
import android.view.Menu
Expand All @@ -25,7 +24,6 @@ import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.EditText
import android.widget.ListView
import androidx.annotation.RequiresApi
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
Expand Down Expand Up @@ -469,20 +467,12 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler {
ModelEditorContextMenuAction.Delete -> deleteFieldDialog()
ModelEditorContextMenuAction.Rename -> renameFieldDialog()
ModelEditorContextMenuAction.ToggleSticky -> toggleStickyField()
ModelEditorContextMenuAction.AddLanguageHint -> {
Timber.i("displaying locale hint dialog")
// localeHintDialog() is safe to be called here without the check but we can't
// suppress @RequiresApi just for the method call, we would have to do it on
// handleAction() which is not ok
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
localeHintDialog()
}
}
ModelEditorContextMenuAction.AddLanguageHint -> localeHintDialog()
}
}

@RequiresApi(api = Build.VERSION_CODES.N)
private fun localeHintDialog() {
Timber.i("displaying locale hint dialog")
// We don't currently show the current value, but we may want to in the future
val dialogFragment: DialogFragment = LocaleSelectionDialog.newInstance(this)
showDialogFragment(dialogFragment)
Expand All @@ -492,20 +482,17 @@ class ModelFieldEditor : AnkiActivity(), LocaleSelectionDialogHandler {
* Sets the Locale Hint of the field to the provided value.
* This allows some keyboard (GBoard) to change language
*/
@RequiresApi(api = Build.VERSION_CODES.N)
private fun addFieldLocaleHint(selectedLocale: Locale) {
setLanguageHintForField(getColUnsafe.notetypes, notetype, currentPos, selectedLocale)
val format = getString(R.string.model_field_editor_language_hint_dialog_success_result, selectedLocale.displayName)
showSnackbar(format, Snackbar.LENGTH_SHORT)
}

@RequiresApi(api = Build.VERSION_CODES.N)
override fun onSelectedLocale(selectedLocale: Locale) {
addFieldLocaleHint(selectedLocale)
dismissAllDialogFragments()
}

@RequiresApi(api = Build.VERSION_CODES.N)
override fun onLocaleSelectionCancelled() {
dismissAllDialogFragments()
}
Expand Down
7 changes: 2 additions & 5 deletions AnkiDroid/src/main/java/com/ichi2/anki/NoteEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.AppCompatButton
import androidx.appcompat.widget.PopupMenu
import androidx.core.content.ContextCompat.registerReceiver
import androidx.core.content.FileProvider
import androidx.core.content.IntentCompat
import androidx.core.content.edit
Expand Down Expand Up @@ -130,7 +129,6 @@ import com.ichi2.anki.utils.ext.isImageOcclusion
import com.ichi2.anki.utils.ext.sharedPrefs
import com.ichi2.anki.widgets.DeckDropDownAdapter.SubtitleListener
import com.ichi2.annotations.NeedsTest
import com.ichi2.compat.CompatHelper
import com.ichi2.compat.CompatHelper.Companion.getSerializableCompat
import com.ichi2.compat.CompatV24
import com.ichi2.compat.ShortcutGroupProvider
Expand Down Expand Up @@ -164,6 +162,7 @@ import com.ichi2.utils.KotlinCleanup
import com.ichi2.utils.MapUtil
import com.ichi2.utils.NoteFieldDecorator
import com.ichi2.utils.TextViewUtil
import com.ichi2.utils.configureView
import com.ichi2.utils.message
import com.ichi2.utils.negativeButton
import com.ichi2.utils.neutralButton
Expand All @@ -183,7 +182,6 @@ import java.util.function.Consumer
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
import androidx.appcompat.widget.Toolbar as MainToolbar

/**
* Allows the user to edit a note, for instance if there is a typo. A card is a presentation of a note, and has two
Expand Down Expand Up @@ -1637,9 +1635,8 @@ class NoteEditor : AnkiFragment(R.layout.note_editor), DeckSelectionListener, Su
description!!
)
}
CompatHelper.compat.configureView(
editLineView.configureView(
requireActivity(),
editLineView,
MEDIA_MIME_TYPES,
DropHelper.Options.Builder()
.setHighlightColor(R.color.material_lime_green_A700)
Expand Down
Loading

0 comments on commit 58e6a9d

Please sign in to comment.