aboutsummaryrefslogtreecommitdiff
path: root/androidApp/src/main/java/mx/trackermap/TrackerMap/android/shared/Utils.kt
blob: aee7e51daa663d772a24156b20f8c58c4c767f3e (plain)
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
/**
 * TrackerMap
 * Copyright (C) 2021-2022  Iván Ávalos <avalos@disroot.org>, Henoch Ojeda <imhenoch@protonmail.com>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */
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.layerSatellite -> MapLayer.Type.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())
        }
    }
}