forked from nextcloud/android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogsEmailSender.kt
More file actions
174 lines (151 loc) · 5.61 KB
/
LogsEmailSender.kt
File metadata and controls
174 lines (151 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Nextcloud - Android Client
*
* SPDX-FileCopyrightText: 2019 Chris Narkiewicz <hello@ezaquarii.com>
* SPDX-License-Identifier: AGPL-3.0-or-later OR GPL-2.0-only
*/
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.security.SecureRandom
import java.util.TimeZone
class LogsEmailSender(private val context: Context, private val clock: Clock, private val runner: AsyncRunner) {
private companion object {
const val LOGS_MIME_TYPE = "text/plain"
}
private class Task(
private val context: Context,
private val logs: List<LogEntry>,
private val file: File,
private val tz: TimeZone
) : Function0<Uri?> {
override fun invoke(): Uri? {
file.parentFile?.mkdirs()
file.outputStream().use { outputStream ->
outputStream.writer(Charsets.UTF_8).buffered().use { writer ->
logs.forEach {
writer.write(it.toString(tz))
writer.newLine()
}
}
}
return FileProvider.getUriForFile(context, context.getString(R.string.file_provider_authority), file)
}
}
private var task: Cancellable? = null
fun send(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
send(it)
})
}
}
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)
// NMC Customization
notificationBuilder.color = context.resources.getColor(R.color.primary, null)
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)
intent.putExtra(Intent.EXTRA_EMAIL, context.getString(R.string.mail_logger))
val subject = context.getString(R.string.log_send_mail_subject).format(context.getString(R.string.app_name))
intent.putExtra(Intent.EXTRA_SUBJECT, subject)
intent.putExtra(Intent.EXTRA_TEXT, getPhoneInfo())
intent.flags = FLAG_ACTIVITY_NEW_TASK
intent.type = LOGS_MIME_TYPE
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, arrayListOf(uri))
try {
context.startActivity(intent)
} catch (_: ActivityNotFoundException) {
Toast.makeText(context, R.string.log_send_no_mail_app, Toast.LENGTH_SHORT).show()
}
}
private fun getPhoneInfo(): String = "Model: " + Build.MODEL + "\n" +
"Brand: " + Build.BRAND + "\n" +
"Product: " + Build.PRODUCT + "\n" +
"Device: " + Build.DEVICE + "\n" +
"Version-Codename: " + Build.VERSION.CODENAME + "\n" +
"Version-Release: " + Build.VERSION.RELEASE
}