fix #24, fix #25, fix OutOfMemoryError

This commit is contained in:
Kieran BW 2021-11-15 19:53:46 +00:00
parent 88fa7c0157
commit ca19a877dd
3 changed files with 32 additions and 15 deletions

View File

@ -3,6 +3,13 @@
All major and minor version changes will be documented in this file. Details of All major and minor version changes will be documented in this file. Details of
patch-level version changes can be found in [commit messages](../../commits/master). patch-level version changes can be found in [commit messages](../../commits/master).
## Next_Ver - 2021/11/xx
- Attempt to resolve issue reported where the app crashed when importing stickers (suspected cause
of a `java.lang.OutOfMemoryError`)
- Improve sticker layout https://github.com/FredHappyface/Android.EweSticker/issues/24
- Improve large sticker preview https://github.com/FredHappyface/Android.EweSticker/issues/25
## 20211114 - 2021/11/14 ## 20211114 - 2021/11/14
- Reopen last used pack https://github.com/FredHappyface/Android.EweSticker/issues/14 - Reopen last used pack https://github.com/FredHappyface/Android.EweSticker/issues/14

View File

@ -94,14 +94,14 @@ class ImageKeyboard : InputMethodService() {
// Constants // Constants
val scale = applicationContext.resources.displayMetrics.density val scale = applicationContext.resources.displayMetrics.density
mInternalDir = File(filesDir, "stickers") mInternalDir = File(filesDir, "stickers")
mTotalIconPadding =
(resources.getDimension(R.dimen.sticker_padding) * 2 * (mIconsPerX + 1)).toInt()
// Shared Preferences // Shared Preferences
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(baseContext) mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(baseContext)
mVertical = mSharedPreferences.getBoolean("vertical", false) mVertical = mSharedPreferences.getBoolean("vertical", false)
mIconsPerX = mSharedPreferences.getInt("iconsPerX", 3) mIconsPerX = mSharedPreferences.getInt("iconsPerX", 3)
mTotalIconPadding =
(resources.getDimension(R.dimen.sticker_padding) * 2 * (mIconsPerX + 1)).toInt()
mIconSize = (if (mVertical) { mIconSize = (if (mVertical) {
(resources.displayMetrics.widthPixels - mTotalIconPadding) / mIconsPerX (resources.displayMetrics.widthPixels - mTotalIconPadding) / mIconsPerX.toFloat()
} else { } else {
(mSharedPreferences.getInt("iconSize", 80) * scale) (mSharedPreferences.getInt("iconSize", 80) * scale)
}).toInt() }).toInt()
@ -142,7 +142,10 @@ class ImageKeyboard : InputMethodService() {
mIconSize * mIconsPerX + mTotalIconPadding mIconSize * mIconsPerX + mTotalIconPadding
} }
mPackContent.layoutParams?.height = mKeyboardHeight mPackContent.layoutParams?.height = mKeyboardHeight
mFullIconSize = (min(resources.displayMetrics.widthPixels, mKeyboardHeight) * 0.8).toInt() mFullIconSize = (min(
resources.displayMetrics.widthPixels,
mKeyboardHeight - resources.getDimensionPixelOffset(R.dimen.text_size_body)
) * 0.95).toInt()
createPackIcons() createPackIcons()
return keyboardLayout return keyboardLayout
} }

View File

@ -28,12 +28,17 @@ import java.util.concurrent.Executors
*/ */
class MainActivity : AppCompatActivity() { class MainActivity : AppCompatActivity() {
// init // init
private val gMaxFiles = 4096
private val mSupportedMimes = Utils.getSupportedMimes() private val mSupportedMimes = Utils.getSupportedMimes()
// onCreate // onCreate
private lateinit var mSharedPreferences: SharedPreferences private lateinit var mSharedPreferences: SharedPreferences
private lateinit var mContextView: View private lateinit var mContextView: View
// importSticker(s)
private var mFilesLeft = gMaxFiles
private var mTotalStickers = 0
/** /**
* Sets up content view, shared prefs, etc. * Sets up content view, shared prefs, etc.
* *
@ -91,6 +96,8 @@ class MainActivity : AppCompatActivity() {
* @param view: View * @param view: View
*/ */
fun chooseDir(view: View) { fun chooseDir(view: View) {
mFilesLeft = gMaxFiles
mTotalStickers = 0
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
@ -104,9 +111,9 @@ class MainActivity : AppCompatActivity() {
* *
* @return 1 if sticker imported successfully else 0 * @return 1 if sticker imported successfully else 0
*/ */
private fun importSticker(sticker: DocumentFile): Int { private fun importSticker(sticker: DocumentFile) {
if (sticker.isDirectory || sticker.type !in mSupportedMimes) { if (sticker.isDirectory || sticker.type !in mSupportedMimes) {
return 0 return
} }
val destSticker = File(filesDir, "stickers/${sticker.parentFile?.name}/${sticker.name}") val destSticker = File(filesDir, "stickers/${sticker.parentFile?.name}/${sticker.name}")
destSticker.parentFile?.mkdirs() destSticker.parentFile?.mkdirs()
@ -115,9 +122,8 @@ class MainActivity : AppCompatActivity() {
Files.copy(inputStream, destSticker.toPath()) Files.copy(inputStream, destSticker.toPath())
inputStream?.close() inputStream?.close()
} catch (e: java.lang.Exception) { } catch (e: java.lang.Exception) {
return 0
} }
return 1 mTotalStickers++
} }
/** /**
@ -140,20 +146,19 @@ class MainActivity : AppCompatActivity() {
"stickerDirPath", "stickerDirPath",
resources.getString(R.string.update_sticker_pack_info_path) resources.getString(R.string.update_sticker_pack_info_path)
) )
var totalStickers = 0
val leafNodes = val leafNodes =
fileWalk(DocumentFile.fromTreeUri(applicationContext, Uri.parse(stickerDirPath))) fileWalk(DocumentFile.fromTreeUri(applicationContext, Uri.parse(stickerDirPath)))
for (file in leafNodes) { for (file in leafNodes.take(gMaxFiles)) {
totalStickers += importSticker(file) importSticker(file)
} }
handler.post { handler.post {
Snackbar.make( Snackbar.make(
mContextView, mContextView,
"Imported $totalStickers stickers. You may need to reload the keyboard for new stickers to show up.", "Imported $mTotalStickers stickers. You may need to reload the keyboard for new stickers to show up.",
Snackbar.LENGTH_LONG Snackbar.LENGTH_LONG
).show() ).show()
val editor = mSharedPreferences.edit() val editor = mSharedPreferences.edit()
editor.putInt("numStickersImported", totalStickers) editor.putInt("numStickersImported", mTotalStickers)
editor.apply() editor.apply()
refreshStickerDirPath() refreshStickerDirPath()
button.isEnabled = true button.isEnabled = true
@ -169,13 +174,15 @@ class MainActivity : AppCompatActivity() {
*/ */
private fun fileWalk(rootNode: DocumentFile?): MutableSet<DocumentFile> { private fun fileWalk(rootNode: DocumentFile?): MutableSet<DocumentFile> {
val leafNodes = mutableSetOf<DocumentFile>() val leafNodes = mutableSetOf<DocumentFile>()
if (rootNode == null) { if (rootNode == null || mFilesLeft < 0) {
return leafNodes return leafNodes
} }
for (file in rootNode.listFiles()) { val files = rootNode.listFiles()
for (file in files) {
if (file.isFile) leafNodes.add(file) if (file.isFile) leafNodes.add(file)
if (file.isDirectory) leafNodes.addAll(fileWalk(file)) if (file.isDirectory) leafNodes.addAll(fileWalk(file))
} }
mFilesLeft -= files.size
return leafNodes return leafNodes
} }