/** * TrackerMap * Copyright (C) 2021-2022 Iván Ávalos , Henoch Ojeda * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ 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 { fun showLayersPopUp(context: Context, view: View, callback: (layer: MapLayer.Type) -> Unit) { val popOver = PopupMenu(context, view) popOver.menuInflater.inflate(R.menu.map_layers, popOver.menu) popOver.setOnMenuItemClickListener { item -> val layer = when (item.itemId) { R.id.layerStreets -> MapLayer.Type.STREETS R.id.layerGmapsStreets -> MapLayer.Type.GMAPS_STREETS R.id.layerGmapsSatellite -> MapLayer.Type.GMAPS_SATELLITE else -> MapLayer.Type.STREETS } callback(layer) true } 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()) } } }