Common.kt 4.7 KB
Newer Older
1
/*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 * Copyright (C) 2019-2021  E FOUNDATION
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
16
17
 */

18
package foundation.e.apps.utils
19

20
import android.content.Context
21
22
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
jo's avatar
jo committed
23
import android.net.ConnectivityManager
Dayona Joseph's avatar
Dayona Joseph committed
24
25
26
27
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.core.os.LocaleListCompat
import com.fasterxml.jackson.databind.DeserializationContext
Nihar Thakkar's avatar
Nihar Thakkar committed
28
import com.fasterxml.jackson.databind.DeserializationFeature
Dayona Joseph's avatar
Dayona Joseph committed
29
import com.fasterxml.jackson.databind.KeyDeserializer
Nihar Thakkar's avatar
Nihar Thakkar committed
30
import com.fasterxml.jackson.databind.ObjectMapper
Dayona Joseph's avatar
Dayona Joseph committed
31
32
33
import com.fasterxml.jackson.databind.module.SimpleModule
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import foundation.e.apps.categories.model.Category
jo's avatar
jo committed
34
import java.net.URL
Dayona Joseph's avatar
Dayona Joseph committed
35
36
import java.nio.file.Paths
import java.util.*
37
import java.util.concurrent.Executors
jo's avatar
jo committed
38
import javax.net.ssl.HttpsURLConnection
39
import kotlin.math.roundToInt
40

41
object Common {
42

jo's avatar
jo committed
43
    val EXECUTOR = Executors.newCachedThreadPool()!!
44

45
46
47
48
49
50
    fun isNetworkAvailable(context: Context): Boolean {
        val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetworkInfo = connectivityManager.activeNetworkInfo
        return activeNetworkInfo != null && activeNetworkInfo.isConnected
    }

51
52
    fun toMiB(bytes: Int): Double {
        val inMiB = bytes.div(1048576.0)
53
        return inMiB.times(10.0).roundToInt().div(10.0)
54
    }
55

Nihar Thakkar's avatar
Nihar Thakkar committed
56
    fun createConnection(url: String, requestMethod: String): HttpsURLConnection {
Dayona Joseph's avatar
Dayona Joseph committed
57
        val preferredLanguage =getAcceptedLanguageHeaderValue()
jo's avatar
jo committed
58
        val connection = URL(url).openConnection() as HttpsURLConnection
Dayona Joseph's avatar
Dayona Joseph committed
59
        connection.setRequestProperty("Accept-Language", preferredLanguage)
Nihar Thakkar's avatar
Nihar Thakkar committed
60
        connection.requestMethod = requestMethod
jo's avatar
jo committed
61
62
63
64
65
        connection.connectTimeout = Constants.CONNECT_TIMEOUT
        connection.readTimeout = Constants.READ_TIMEOUT
        return connection
    }

66
67
68
69
70
71
    /*
     * Checks if the given [packageName] is a system app or not
     * @param packageManager current PackageManager
     * @param packageName package to verify
     * @return true if the app is system app. false otherwise
     */
Arnau Vàzquez's avatar
Arnau Vàzquez committed
72
    fun isSystemApp(packageManager: PackageManager, packageName: String?): Boolean {
73
74
75
76
77
78
        if (packageName != null) {
            return try {
                val info = packageManager.getPackageInfo(packageName, 0)
                (info.applicationInfo.flags and ApplicationInfo.FLAG_SYSTEM) != 0
            } catch (exception: Exception) {
                false
79
80
81
82
            }
        }
        return false
    }
83

Arnau Vàzquez's avatar
Arnau Vàzquez committed
84
    fun appHasLaunchActivity(context: Context, packageName: String?): Boolean {
85
        return (packageName?.let { context.packageManager.getLaunchIntentForPackage(it) } != null)
86
    }
Nihar Thakkar's avatar
Nihar Thakkar committed
87
88
89

    fun getObjectMapper(): ObjectMapper {
        val objectMapper = ObjectMapper()
Dayona Joseph's avatar
Dayona Joseph committed
90
91
92
93
        var simpleModule =  SimpleModule()
        simpleModule.addKeyDeserializer(Category::class.java,keyDeserializer())
        objectMapper.registerModule(simpleModule);
        objectMapper.registerKotlinModule()
Nihar Thakkar's avatar
Nihar Thakkar committed
94
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
95
        objectMapper.registerKotlinModule()
Nihar Thakkar's avatar
Nihar Thakkar committed
96
97
        return objectMapper
    }
Dayona Joseph's avatar
Dayona Joseph committed
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

    fun getAcceptedLanguageHeaderValue(): String {
        var weight = 1.0F
        return getPreferredLocaleList()
                .map { it.toLanguageTag() }
                .reduce { accumulator, languageTag ->
                    weight -= 0.1F
                    "$accumulator,$languageTag;q=$weight"
                }
    }

    fun getPreferredLocaleList(): List<Locale> {
        val adjustedLocaleListCompat = LocaleListCompat.getAdjustedDefault()
        val preferredLocaleList = mutableListOf<Locale>()
        for (index in 0 until adjustedLocaleListCompat.size()) {
            preferredLocaleList.add(adjustedLocaleListCompat.get(index))
        }
        return preferredLocaleList
    }
}

class keyDeserializer : KeyDeserializer() {
    @RequiresApi(Build.VERSION_CODES.O)
    override fun deserializeKey(p0: String?, p1: DeserializationContext?): Any? {
        return Paths.get(p0)
    }


126
}