Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class LogsActivity : ToolbarActivity() {
android.R.id.home -> finish()
R.id.action_delete_logs -> vm.deleteAll()
R.id.action_send_logs -> vm.send()
R.id.action_export_logs -> vm.export()
R.id.action_refresh_logs -> vm.load()
else -> retval = super.onOptionsItemSelected(item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,27 @@
*/
package com.nextcloud.client.logger.ui

import android.app.DownloadManager
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
import android.net.Uri
import android.os.Build
import android.widget.Toast
import androidx.core.app.NotificationCompat
import androidx.core.content.FileProvider
import com.nextcloud.client.core.AsyncRunner
import com.nextcloud.client.core.Cancellable
import com.nextcloud.client.core.Clock
import com.nextcloud.client.logger.LogEntry
import com.owncloud.android.R
import com.owncloud.android.ui.notifications.NotificationUtils
import com.owncloud.android.utils.FileExportUtils
import java.io.File
import java.io.FileWriter
import java.security.SecureRandom
import java.util.TimeZone

class LogsEmailSender(private val context: Context, private val clock: Clock, private val runner: AsyncRunner) {
Expand All @@ -36,13 +43,17 @@ class LogsEmailSender(private val context: Context, private val clock: Clock, pr
) : Function0<Uri?> {

override fun invoke(): Uri? {
file.parentFile.mkdirs()
val fo = FileWriter(file, false)
logs.forEach {
fo.write(it.toString(tz))
fo.write("\n")
file.parentFile?.mkdirs()

file.outputStream().use { outputStream ->
outputStream.writer(Charsets.UTF_8).buffered().use { writer ->
logs.forEach {
writer.write(it.toString(tz))
writer.newLine()
}
}
}
fo.close()

return FileProvider.getUriForFile(context, context.getString(R.string.file_provider_authority), file)
}
}
Expand All @@ -59,13 +70,78 @@ class LogsEmailSender(private val context: Context, private val clock: Clock, pr
}
}

fun export(logs: List<LogEntry>) {
if (task == null) {
val outFile = File(context.cacheDir, "attachments/logs.txt")
task = runner.postQuickTask(Task(context, logs, outFile, clock.tz), onResult = {
task = null
export(outFile)
})
}
}

fun stop() {
if (task != null) {
task?.cancel()
task = null
}
}

private fun export(file: File) {
FileExportUtils().exportFile(
"Nextcloud Android Files Logs",
"text/plain",
context.contentResolver,
null,
file
)
showSuccessNotification(1)
}

fun showSuccessNotification(successfulExports: Int) {
showNotification(
context.resources.getQuantityString(
R.plurals.export_successful,
successfulExports,
successfulExports
)
)
}

private fun showNotification(message: String) {
val notificationId = SecureRandom().nextInt()

val notificationBuilder = NotificationCompat.Builder(
context,
NotificationUtils.NOTIFICATION_CHANNEL_DOWNLOAD
)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle(message)
.setAutoCancel(true)

val actionIntent = Intent(DownloadManager.ACTION_VIEW_DOWNLOADS).apply {
flags = FLAG_ACTIVITY_NEW_TASK
}
val actionPendingIntent = PendingIntent.getActivity(
context,
notificationId,
actionIntent,
PendingIntent.FLAG_CANCEL_CURRENT or
PendingIntent.FLAG_IMMUTABLE
)
notificationBuilder.addAction(
NotificationCompat.Action(
null,
context.getString(R.string.locate_folder),
actionPendingIntent
)
)

val notificationManager = context
.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(notificationId, notificationBuilder.build())
}

private fun send(uri: Uri?) {
task = null
val intent = Intent(Intent.ACTION_SEND_MULTIPLE)
Expand All @@ -76,12 +152,12 @@ class LogsEmailSender(private val context: Context, private val clock: Clock, pr

intent.putExtra(Intent.EXTRA_TEXT, getPhoneInfo())

intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
intent.flags = FLAG_ACTIVITY_NEW_TASK
intent.type = LOGS_MIME_TYPE
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayListOf(uri))
try {
context.startActivity(intent)
} catch (ex: ActivityNotFoundException) {
} catch (_: ActivityNotFoundException) {
Toast.makeText(context, R.string.log_send_no_mail_app, Toast.LENGTH_SHORT).show()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ class LogsViewModel @Inject constructor(
}
}

fun export() {
entries.value?.let {
sender.export(it)
}
}

fun load() {
if (isLoading.value != true) {
logsRepository.load(this::onLoaded)
Expand Down
9 changes: 8 additions & 1 deletion app/src/main/res/menu/activity_logs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@
android:title="@string/logs_menu_send"
app:showAsAction="never"
android:orderInCategory="200"
android:icon="@drawable/ic_send"/>
android:icon="@drawable/ic_send" />

<item
android:id="@+id/action_export_logs"
android:title="@string/logs_menu_export"
app:showAsAction="never"
android:orderInCategory="200"
android:icon="@drawable/ic_export" />

<item
android:id="@+id/action_delete_logs"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,7 @@
<string name="logs_title">Logs</string>
<string name="logs_menu_refresh">Refresh</string>
<string name="logs_menu_send">Send logs by email</string>
<string name="logs_menu_export">Export logs</string>
<string name="logs_menu_delete">Delete logs</string>
<string name="log_send_no_mail_app">No app for sending logs found. Please install an email client.</string>
<string name="log_send_mail_subject">%1$s Android app logs</string>
Expand Down
Loading