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
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
- Reopen last used pack https://github.com/FredHappyface/Android.EweSticker/issues/14

View File

@ -94,14 +94,14 @@ class ImageKeyboard : InputMethodService() {
// Constants
val scale = applicationContext.resources.displayMetrics.density
mInternalDir = File(filesDir, "stickers")
mTotalIconPadding =
(resources.getDimension(R.dimen.sticker_padding) * 2 * (mIconsPerX + 1)).toInt()
// Shared Preferences
mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(baseContext)
mVertical = mSharedPreferences.getBoolean("vertical", false)
mIconsPerX = mSharedPreferences.getInt("iconsPerX", 3)
mTotalIconPadding =
(resources.getDimension(R.dimen.sticker_padding) * 2 * (mIconsPerX + 1)).toInt()
mIconSize = (if (mVertical) {
(resources.displayMetrics.widthPixels - mTotalIconPadding) / mIconsPerX
(resources.displayMetrics.widthPixels - mTotalIconPadding) / mIconsPerX.toFloat()
} else {
(mSharedPreferences.getInt("iconSize", 80) * scale)
}).toInt()
@ -142,7 +142,10 @@ class ImageKeyboard : InputMethodService() {
mIconSize * mIconsPerX + mTotalIconPadding
}
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()
return keyboardLayout
}

View File

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