-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add link to send logcat messages via email (#45)
* Add link to send logcat messages via email * Clean up * Error handling * Make file provider depending on applicationId
- Loading branch information
Showing
11 changed files
with
331 additions
and
17 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<paths> | ||
<external-files-path name="documents" path="." /> | ||
</paths> |
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
67 changes: 67 additions & 0 deletions
67
...le/src/main/java/exchange/dydx/trading/feature/profile/reportissue/DydxReportIssueView.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,67 @@ | ||
package exchange.dydx.trading.feature.profile.reportissue | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.hilt.navigation.compose.hiltViewModel | ||
import exchange.dydx.abacus.protocols.LocalizerProtocol | ||
import exchange.dydx.platformui.designSystem.theme.ThemeFont | ||
import exchange.dydx.platformui.designSystem.theme.dydxDefault | ||
import exchange.dydx.platformui.designSystem.theme.themeFont | ||
import exchange.dydx.trading.common.component.DydxComponent | ||
import exchange.dydx.trading.common.compose.collectAsStateWithLifecycle | ||
import exchange.dydx.trading.common.theme.DydxThemedPreviewSurface | ||
import exchange.dydx.trading.common.theme.MockLocalizer | ||
|
||
@Preview | ||
@Composable | ||
fun Preview_DydxReportIssueView() { | ||
DydxThemedPreviewSurface { | ||
DydxReportIssueView.Content(Modifier, DydxReportIssueView.ViewState.preview) | ||
} | ||
} | ||
|
||
object DydxReportIssueView : DydxComponent { | ||
data class ViewState( | ||
val localizer: LocalizerProtocol, | ||
val text: String?, | ||
) { | ||
companion object { | ||
val preview = ViewState( | ||
localizer = MockLocalizer(), | ||
text = "1.0M", | ||
) | ||
} | ||
} | ||
|
||
@Composable | ||
override fun Content(modifier: Modifier) { | ||
val viewModel: DydxReportIssueViewModel = hiltViewModel() | ||
|
||
val state = viewModel.state.collectAsStateWithLifecycle(initialValue = null).value | ||
Content(modifier, state) | ||
} | ||
|
||
@Composable | ||
fun Content(modifier: Modifier, state: ViewState?) { | ||
if (state == null) { | ||
return | ||
} | ||
Column( | ||
modifier = modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally, | ||
) { | ||
Text( | ||
style = TextStyle.dydxDefault.themeFont(fontSize = ThemeFont.FontSize.extra), | ||
text = state?.text ?: "", | ||
) | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...c/main/java/exchange/dydx/trading/feature/profile/reportissue/DydxReportIssueViewModel.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,106 @@ | ||
package exchange.dydx.trading.feature.profile.reportissue | ||
|
||
import android.content.Context | ||
import android.net.Uri | ||
import android.os.Environment | ||
import androidx.core.content.FileProvider | ||
import androidx.lifecycle.ViewModel | ||
import androidx.lifecycle.viewModelScope | ||
import dagger.hilt.android.lifecycle.HiltViewModel | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import exchange.dydx.abacus.protocols.LocalizerProtocol | ||
import exchange.dydx.platformui.components.PlatformInfo | ||
import exchange.dydx.trading.common.DydxViewModel | ||
import exchange.dydx.trading.common.navigation.DydxRouter | ||
import exchange.dydx.utilities.utils.EmailUtils | ||
import exchange.dydx.utilities.utils.FileUtils | ||
import exchange.dydx.utilities.utils.LogCatReader | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.launch | ||
import kotlinx.coroutines.plus | ||
import kotlinx.coroutines.withContext | ||
import java.io.File | ||
import javax.inject.Inject | ||
|
||
@HiltViewModel | ||
class DydxReportIssueViewModel @Inject constructor( | ||
private val localizer: LocalizerProtocol, | ||
private val router: DydxRouter, | ||
@ApplicationContext private val context: Context, | ||
val platformInfo: PlatformInfo, | ||
) : ViewModel(), DydxViewModel { | ||
|
||
private val textFlow = MutableStateFlow("") | ||
|
||
val state: Flow<DydxReportIssueView.ViewState?> = textFlow.map { text -> | ||
createViewState(text) | ||
} | ||
|
||
init { | ||
textFlow.value = localizer.localize("APP.ISSUE_REPORT.LOADING_TITLE") | ||
|
||
viewModelScope.launch { | ||
var logUri: Uri? = null | ||
withContext(Dispatchers.IO) { | ||
// add a delay to show the loading text | ||
kotlinx.coroutines.delay(500) | ||
logUri = createLog() | ||
} | ||
if (logUri != null) { | ||
textFlow.value = localizer.localize("APP.ISSUE_REPORT.LOADING_COMPLETED_TITLE") | ||
EmailUtils.sendEmailWithAttachment( | ||
context = context, | ||
fileUri = logUri, | ||
email = "", | ||
subject = localizer.localize("APP.ISSUE_REPORT.EMAIL_SUBJECT"), | ||
body = localizer.localize("APP.ISSUE_REPORT.EMAIL_BODY"), | ||
mimeType = "application/x-zip", | ||
chooserTitle = localizer.localize("APP.ISSUE_REPORT.CHOOSER_TITLE"), | ||
) | ||
} else { | ||
val error = localizer.localize("APP.ISSUE_REPORT.LOADING_ERROR_TITLE") | ||
textFlow.value = error | ||
platformInfo.show(message = error) | ||
} | ||
|
||
router.navigateBack() | ||
} | ||
} | ||
|
||
private fun createViewState(text: String): DydxReportIssueView.ViewState { | ||
return DydxReportIssueView.ViewState( | ||
localizer = localizer, | ||
text = text, | ||
) | ||
} | ||
|
||
private fun createLog(): Uri? { | ||
val file = | ||
File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), "dydx_app.log") | ||
if (!LogCatReader.saveLogCatToFile(file)) { | ||
return null | ||
} | ||
val zipFile = createZipFile(context, file) | ||
file.delete() | ||
return if (zipFile != null) { | ||
FileProvider.getUriForFile(context, context.packageName, zipFile) | ||
} else { | ||
null | ||
} | ||
} | ||
|
||
private fun createZipFile(context: Context, file: File): File? { | ||
val fileName = file.name | ||
val zipFileName = "$fileName.zip" | ||
val zipFilePath = | ||
File(context.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS), zipFileName) | ||
return if (FileUtils.compressFile(context, file.absolutePath, zipFilePath.absolutePath)) { | ||
zipFilePath | ||
} else { | ||
null | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
v4/utilities/src/main/java/exchange/dydx/utilities/utils/EmailUtils.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,29 @@ | ||
package exchange.dydx.utilities.utils | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.net.Uri | ||
|
||
object EmailUtils { | ||
fun sendEmailWithAttachment( | ||
context: Context, | ||
fileUri: Uri?, | ||
email: String, | ||
subject: String?, | ||
body: String?, | ||
mimeType: String?, | ||
chooserTitle: String? | ||
) { | ||
val emailIntent = Intent(Intent.ACTION_SEND) | ||
emailIntent.setType(mimeType) | ||
emailIntent.putExtra(Intent.EXTRA_EMAIL, arrayOf(email)) | ||
emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject) | ||
emailIntent.putExtra(Intent.EXTRA_TEXT, body) | ||
emailIntent.putExtra(Intent.EXTRA_STREAM, fileUri) | ||
val chooser = Intent.createChooser(emailIntent, chooserTitle) | ||
if (chooser.resolveActivity(context.packageManager) != null) { | ||
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | ||
context.startActivity(chooser) | ||
} | ||
} | ||
} |
Oops, something went wrong.