diff options
author | Iván Ávalos <avalos@disroot.org> | 2022-01-20 01:26:29 -0600 |
---|---|---|
committer | Iván Ávalos <avalos@disroot.org> | 2022-01-20 01:26:29 -0600 |
commit | 5b49e2eb2db829b2b386a707d79a52f72a2ef0e3 (patch) | |
tree | 556f1c62fb22ad0bc85720985a0fcc1137989e96 /androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt | |
parent | 035f1290a1f094c26ee8acaec7ea7090d3f1628b (diff) | |
download | etbsa-trackermap-mobile-5b49e2eb2db829b2b386a707d79a52f72a2ef0e3.tar.gz etbsa-trackermap-mobile-5b49e2eb2db829b2b386a707d79a52f72a2ef0e3.tar.bz2 etbsa-trackermap-mobile-5b49e2eb2db829b2b386a707d79a52f72a2ef0e3.zip |
Properly implemented save and share report actions
Diffstat (limited to 'androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt')
-rw-r--r-- | androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt index 56a9167..fe18997 100644 --- a/androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt +++ b/androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt @@ -1,10 +1,22 @@ package mx.trackermap.TrackerMap.android.shared +import android.app.NotificationManager +import android.app.PendingIntent import android.content.Context +import android.content.Intent +import android.util.Log import android.view.View import androidx.appcompat.widget.PopupMenu +import androidx.core.app.NotificationCompat +import androidx.core.app.ShareCompat +import androidx.core.content.FileProvider +import com.google.firebase.messaging.FirebaseMessagingService import mx.trackermap.TrackerMap.android.R import mx.trackermap.TrackerMap.client.models.MapLayer +import java.io.BufferedOutputStream +import java.io.File +import java.io.FileOutputStream +import java.io.IOException class Utils { companion object { @@ -22,5 +34,64 @@ class Utils { } popOver.show() } + + fun openFileIntent(context: Context, file: File, mime: String): Intent { + Log.d("Utils", "Filename is ${file.name}") + val uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file) + val intent = Intent() + intent.action = Intent.ACTION_VIEW + intent.setDataAndType(uri, mime) + intent.putExtra(Intent.EXTRA_STREAM, uri) + return intent + } + + fun shareFile(context: Context, file: File, mime: String) { + Log.d("Utils", "Filename is ${file.name}") + val uri = FileProvider.getUriForFile(context, "${context.packageName}.fileprovider", file) + ShareCompat.IntentBuilder(context) + .setType(mime) + .setStream(uri) + .setChooserTitle("Share this with your friends!") + .startChooser() + } + + fun saveReportToCache( + context: Context, + data: ByteArray, + filename: String + ): File { + val reportsDir = File(context.cacheDir, "reports_tmp") + reportsDir.deleteRecursively() + val cacheFile = File(reportsDir, filename) + if (!cacheFile.exists()) { + cacheFile.parentFile?.mkdirs() + } else { + cacheFile.delete() + } + + val output = BufferedOutputStream(FileOutputStream(cacheFile)) + + try { + output.write(data) + output.flush() + output.close() + } catch (e: IOException) { + e.printStackTrace() + } + + return cacheFile + } + + fun showNotification(context: Context, id: Int, body: String?, pendingIntent: PendingIntent) { + val builder = NotificationCompat.Builder(context, context.getString(R.string.notification_channel_id)) + .setSmallIcon(R.drawable.icon_notify) + .setContentTitle(context.getString(R.string.app_name)) + .setContentText(body) + .setAutoCancel(true) + .setContentIntent(pendingIntent) + (context.getSystemService(FirebaseMessagingService.NOTIFICATION_SERVICE) + as NotificationManager) + .notify(id, builder.build()) + } } }
\ No newline at end of file |