forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
will be used in the next commit refactor: move getJavaFieldAsAccessible to main I also changed the method to not throw by default if the field name could not be found
- Loading branch information
Showing
4 changed files
with
69 additions
and
16 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
AnkiDroid/src/main/java/com/ichi2/anki/utils/Reflection.kt
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,34 @@ | ||
/* | ||
* Copyright (c) 2024 Brayan Oliveira <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.ichi2.anki.utils | ||
|
||
import timber.log.Timber | ||
import java.lang.reflect.Field | ||
|
||
/** | ||
* @param clazz Java class to get the field | ||
* @param fieldName name of the field | ||
* @return a [Field] with `isAccessible` set to true, or null if the field could not be found | ||
*/ | ||
fun getJavaFieldAsAccessible(clazz: Class<*>, fieldName: String): Field? { | ||
val field = clazz.declaredFields.firstOrNull { it.name == fieldName }?.apply { | ||
isAccessible = true | ||
} | ||
if (field == null) { | ||
Timber.d("Could not find field $fieldName in class $clazz") | ||
} | ||
return field | ||
} |
30 changes: 30 additions & 0 deletions
30
AnkiDroid/src/main/java/com/ichi2/anki/utils/ext/MenuItemImpl.kt
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,30 @@ | ||
/* | ||
* Copyright (c) 2024 Brayan Oliveira <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify it under | ||
* the terms of the GNU General Public License as published by the Free Software | ||
* Foundation; either version 3 of the License, or (at your option) any later | ||
* version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but WITHOUT ANY | ||
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A | ||
* PARTICULAR PURPOSE. See the GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License along with | ||
* this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
package com.ichi2.anki.utils.ext | ||
|
||
import androidx.appcompat.view.menu.MenuBuilder | ||
import androidx.appcompat.view.menu.MenuItemImpl | ||
import com.ichi2.anki.utils.getJavaFieldAsAccessible | ||
|
||
fun MenuItemImpl.removeSubMenu() { | ||
val subMenuField = getJavaFieldAsAccessible(MenuItemImpl::class.java, "mSubMenu") | ||
subMenuField?.set(this, null) | ||
} | ||
|
||
val MenuItemImpl.menu: MenuBuilder get() { | ||
val menuField = getJavaFieldAsAccessible(MenuItemImpl::class.java, "mMenu") | ||
return menuField?.get(this) as MenuBuilder | ||
} |
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