Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 54
ADFA-1966: Move file operations to background thread#688
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b00f0c7
fix(ADFA-1966): Move plugin installation file I/Os to background
dara-abijo-adfa e9a0e68
fix(ADFA-1966): Move screenshot file I/Os to background
dara-abijo-adfa fed8a77
fix(ADFA-1966): Clean up resources
dara-abijo-adfa 38863ec
fix(ADFA-1966): Clean up temp file in case of exception
dara-abijo-adfa a8e0f73
Merge branch 'stage' into ADFA-1966
dara-abijo-adfa 1f32632
Merge branch 'stage' into ADFA-1966
dara-abijo-adfa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
51 changes: 31 additions & 20 deletions
51 app/src/main/java/com/itsaky/androidide/viewmodels/PluginManagerViewModel.kt
This file contains hidden or 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
54 changes: 34 additions & 20 deletions
54 common/src/main/java/com/itsaky/androidide/utils/FeedbackManager.kt
This file contains hidden or 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 |
|---|---|---|
| @@ -20,7 +20,9 @@ import androidx.core.text.HtmlCompat | ||
| import androidx.lifecycle.lifecycleScope | ||
| import com.itsaky.androidide.buildinfo.BuildInfo | ||
| import com.itsaky.androidide.resources.R | ||
| import kotlinx.coroutines.Dispatchers | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.withContext | ||
| import org.slf4j.LoggerFactory | ||
| import java.io.File | ||
| import java.io.FileOutputStream | ||
| @@ -181,7 +183,7 @@ object FeedbackManager { | ||
| fun captureScreenshot(context: Context, callback: (File?) -> Unit) { | ||
| val activity = context as? Activity | ||
| val activity = context as? AppCompatActivity | ||
| if (activity == null) { | ||
| logger.warn("Cannot capture screenshot: Context is not an Activity") | ||
| callback(null) | ||
| @@ -210,14 +212,16 @@ object FeedbackManager { | ||
| } | ||
| private fun captureWithPixelCopy( | ||
| activity: Activity, | ||
| activity: AppCompatActivity, | ||
| rootView: View, | ||
| screenshotFile: File, | ||
| callback: (File?) -> Unit | ||
| ) { | ||
| runCatching { | ||
| val bitmap = createBitmap(rootView.width, rootView.height) | ||
| var bitmap: Bitmap? = null | ||
| try { | ||
| bitmap = createBitmap(rootView.width, rootView.height) | ||
| val locationOfViewInWindow = IntArray(2) | ||
| rootView.getLocationInWindow(locationOfViewInWindow) | ||
| @@ -232,29 +236,39 @@ object FeedbackManager { | ||
| bitmap, | ||
| { result -> | ||
| if (result == PixelCopy.SUCCESS) { | ||
| saveScreenshot(bitmap, screenshotFile, callback) | ||
| } | ||
| activity.lifecycleScope.launch { | ||
| saveScreenshot(bitmap, screenshotFile, callback) | ||
| } | ||
| } else { | ||
| logger.error("PixelCopy failed with result code: $result") | ||
| bitmap.recycle() | ||
| callback(null) | ||
| } | ||
| }, | ||
| Handler(Looper.getMainLooper()) | ||
| ) | ||
| }.onFailure { | ||
| logger.error("PixelCopy exception, falling back to Canvas", it) | ||
| } catch (e: Exception) { | ||
| logger.error("PixelCopy exception, falling back to Canvas", e) | ||
| bitmap?.recycle() | ||
| callback(null) | ||
| } | ||
| } | ||
| private fun saveScreenshot(bitmap: Bitmap, file: File, callback: (File?) -> Unit) { | ||
| runCatching { | ||
| FileOutputStream(file).use { out -> | ||
| bitmap.compress(Bitmap.CompressFormat.PNG, 90, out) | ||
| } | ||
| bitmap.recycle() | ||
| callback(file) | ||
| }.onFailure { | ||
| logger.error("Failed to save screenshot", it) | ||
| callback(null) | ||
| } | ||
| } | ||
| private suspend fun saveScreenshot(bitmap: Bitmap, file: File, callback: (File?) -> Unit) { | ||
| val result = withContext(Dispatchers.IO) { | ||
| runCatching { | ||
dara-abijo-adfa marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| FileOutputStream(file).use { out -> | ||
| bitmap.compress(Bitmap.CompressFormat.PNG, 90, out) | ||
| } | ||
| file | ||
| }.onFailure { | ||
| logger.error("Failed to save screenshot", it) | ||
| }.getOrNull() | ||
| } | ||
| bitmap.recycle() | ||
| callback(result) | ||
| } | ||
| private fun launchIntentChooser( | ||
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.