2020-05-14 20:14:41 +02:00
|
|
|
package com.h.pixeldroid.utils
|
|
|
|
|
|
|
|
import android.content.Context
|
2020-12-26 12:10:54 +01:00
|
|
|
import android.content.SharedPreferences
|
|
|
|
import android.content.res.Resources
|
2020-05-14 20:14:41 +02:00
|
|
|
import android.net.ConnectivityManager
|
2020-12-26 12:10:54 +01:00
|
|
|
import android.os.Build
|
|
|
|
import androidx.appcompat.app.AppCompatDelegate
|
2020-09-10 20:20:23 +02:00
|
|
|
import com.h.pixeldroid.R
|
2020-05-14 20:14:41 +02:00
|
|
|
|
2020-12-26 12:10:54 +01:00
|
|
|
fun hasInternet(context: Context): Boolean {
|
|
|
|
val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
|
|
|
return cm.activeNetwork != null
|
|
|
|
}
|
|
|
|
|
|
|
|
fun normalizeDomain(domain: String): String {
|
|
|
|
return "https://" + domain
|
|
|
|
.replace("http://", "")
|
|
|
|
.replace("https://", "")
|
|
|
|
.trim(Char::isWhitespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Updates the application's theme depending on the given preferences and resources
|
|
|
|
*/
|
|
|
|
fun setThemeFromPreferences(preferences: SharedPreferences, resources : Resources) {
|
|
|
|
val themes = resources.getStringArray(R.array.theme_values)
|
|
|
|
//Set the theme
|
|
|
|
when(preferences.getString("theme", "")) {
|
|
|
|
//Light
|
|
|
|
themes[1] -> {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
|
2020-05-14 20:14:41 +02:00
|
|
|
}
|
2020-12-26 12:10:54 +01:00
|
|
|
//Dark
|
|
|
|
themes[2] -> {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
|
2020-05-19 09:49:34 +02:00
|
|
|
}
|
2020-12-26 12:10:54 +01:00
|
|
|
else -> {
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
|
|
|
|
} else {
|
|
|
|
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY)
|
2020-09-10 20:20:23 +02:00
|
|
|
}
|
|
|
|
}
|
2020-05-14 20:14:41 +02:00
|
|
|
}
|
2020-12-26 12:10:54 +01:00
|
|
|
}
|