66 */
77package com.nextcloud.client.logger.ui
88
9+ import android.app.DownloadManager
10+ import android.app.NotificationManager
11+ import android.app.PendingIntent
912import android.content.ActivityNotFoundException
1013import android.content.Context
1114import android.content.Intent
15+ import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
1216import android.net.Uri
1317import android.os.Build
1418import android.widget.Toast
19+ import androidx.core.app.NotificationCompat
1520import androidx.core.content.FileProvider
1621import com.nextcloud.client.core.AsyncRunner
1722import com.nextcloud.client.core.Cancellable
1823import com.nextcloud.client.core.Clock
1924import com.nextcloud.client.logger.LogEntry
2025import com.owncloud.android.R
26+ import com.owncloud.android.ui.notifications.NotificationUtils
27+ import com.owncloud.android.utils.FileExportUtils
2128import java.io.File
22- import java.io.FileWriter
29+ import java.security.SecureRandom
2330import java.util.TimeZone
2431
2532class LogsEmailSender (private val context : Context , private val clock : Clock , private val runner : AsyncRunner ) {
@@ -36,13 +43,17 @@ class LogsEmailSender(private val context: Context, private val clock: Clock, pr
3643 ) : Function0<Uri?> {
3744
3845 override fun invoke (): Uri ? {
39- file.parentFile.mkdirs()
40- val fo = FileWriter (file, false )
41- logs.forEach {
42- fo.write(it.toString(tz))
43- fo.write(" \n " )
46+ file.parentFile?.mkdirs()
47+
48+ file.outputStream().use { outputStream ->
49+ outputStream.writer(Charsets .UTF_8 ).buffered().use { writer ->
50+ logs.forEach {
51+ writer.write(it.toString(tz))
52+ writer.newLine()
53+ }
54+ }
4455 }
45- fo.close()
56+
4657 return FileProvider .getUriForFile(context, context.getString(R .string.file_provider_authority), file)
4758 }
4859 }
@@ -59,13 +70,78 @@ class LogsEmailSender(private val context: Context, private val clock: Clock, pr
5970 }
6071 }
6172
73+ fun export (logs : List <LogEntry >) {
74+ if (task == null ) {
75+ val outFile = File (context.cacheDir, " attachments/logs.txt" )
76+ task = runner.postQuickTask(Task (context, logs, outFile, clock.tz), onResult = {
77+ task = null
78+ export(outFile)
79+ })
80+ }
81+ }
82+
6283 fun stop () {
6384 if (task != null ) {
6485 task?.cancel()
6586 task = null
6687 }
6788 }
6889
90+ private fun export (file : File ) {
91+ FileExportUtils ().exportFile(
92+ " Nextcloud Android Files Logs" ,
93+ " text/plain" ,
94+ context.contentResolver,
95+ null ,
96+ file
97+ )
98+ showSuccessNotification(1 )
99+ }
100+
101+ fun showSuccessNotification (successfulExports : Int ) {
102+ showNotification(
103+ context.resources.getQuantityString(
104+ R .plurals.export_successful,
105+ successfulExports,
106+ successfulExports
107+ )
108+ )
109+ }
110+
111+ private fun showNotification (message : String ) {
112+ val notificationId = SecureRandom ().nextInt()
113+
114+ val notificationBuilder = NotificationCompat .Builder (
115+ context,
116+ NotificationUtils .NOTIFICATION_CHANNEL_DOWNLOAD
117+ )
118+ .setSmallIcon(R .drawable.notification_icon)
119+ .setContentTitle(message)
120+ .setAutoCancel(true )
121+
122+ val actionIntent = Intent (DownloadManager .ACTION_VIEW_DOWNLOADS ).apply {
123+ flags = FLAG_ACTIVITY_NEW_TASK
124+ }
125+ val actionPendingIntent = PendingIntent .getActivity(
126+ context,
127+ notificationId,
128+ actionIntent,
129+ PendingIntent .FLAG_CANCEL_CURRENT or
130+ PendingIntent .FLAG_IMMUTABLE
131+ )
132+ notificationBuilder.addAction(
133+ NotificationCompat .Action (
134+ null ,
135+ context.getString(R .string.locate_folder),
136+ actionPendingIntent
137+ )
138+ )
139+
140+ val notificationManager = context
141+ .getSystemService(Context .NOTIFICATION_SERVICE ) as NotificationManager
142+ notificationManager.notify(notificationId, notificationBuilder.build())
143+ }
144+
69145 private fun send (uri : Uri ? ) {
70146 task = null
71147 val intent = Intent (Intent .ACTION_SEND_MULTIPLE )
@@ -76,12 +152,12 @@ class LogsEmailSender(private val context: Context, private val clock: Clock, pr
76152
77153 intent.putExtra(Intent .EXTRA_TEXT , getPhoneInfo())
78154
79- intent.flags = Intent . FLAG_ACTIVITY_NEW_TASK
155+ intent.flags = FLAG_ACTIVITY_NEW_TASK
80156 intent.type = LOGS_MIME_TYPE
81157 intent.putParcelableArrayListExtra(Intent .EXTRA_STREAM , arrayListOf (uri))
82158 try {
83159 context.startActivity(intent)
84- } catch (ex : ActivityNotFoundException ) {
160+ } catch (_ : ActivityNotFoundException ) {
85161 Toast .makeText(context, R .string.log_send_no_mail_app, Toast .LENGTH_SHORT ).show()
86162 }
87163 }
0 commit comments