2019-09-01 11:54:12 +02:00
|
|
|
package com.denizd.substitutionplan.data
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-09-03 18:18:21 +02:00
|
|
|
import android.Manifest
|
2019-08-31 16:12:36 +02:00
|
|
|
import android.annotation.TargetApi
|
|
|
|
|
import android.app.NotificationChannel
|
|
|
|
|
import android.app.NotificationManager
|
|
|
|
|
import android.content.Context
|
2019-08-09 21:18:27 +02:00
|
|
|
import android.content.SharedPreferences
|
2019-09-03 18:18:21 +02:00
|
|
|
import android.content.pm.PackageManager
|
2019-08-31 16:12:36 +02:00
|
|
|
import android.graphics.Color
|
2019-09-03 18:18:21 +02:00
|
|
|
import android.os.Environment
|
|
|
|
|
import android.widget.Toast
|
|
|
|
|
import androidx.core.app.ActivityCompat.requestPermissions
|
|
|
|
|
import androidx.core.content.ContextCompat.checkSelfPermission
|
|
|
|
|
import androidx.fragment.app.FragmentActivity
|
2019-09-01 11:54:12 +02:00
|
|
|
import com.denizd.substitutionplan.R
|
|
|
|
|
import com.denizd.substitutionplan.models.Subst
|
2019-08-18 17:53:22 +02:00
|
|
|
import java.util.*
|
2019-09-03 18:18:21 +02:00
|
|
|
import java.io.File
|
|
|
|
|
import java.io.FileOutputStream
|
|
|
|
|
import java.io.OutputStreamWriter
|
|
|
|
|
import javax.xml.parsers.DocumentBuilderFactory
|
2019-08-09 21:18:27 +02:00
|
|
|
|
2019-09-03 18:18:21 +02:00
|
|
|
internal object HelperFunctions {
|
2019-05-21 18:55:29 +02:00
|
|
|
|
2019-08-18 17:53:22 +02:00
|
|
|
val languageIndependentCourses = arrayOf("German", "English", "French", "Spanish", "Latin", "Turkish", "Chinese", "Arts", "Music",
|
|
|
|
|
"Theatre", "Geography", "History", "Politics", "Philosophy", "Religion", "Maths", "Biology", "Chemistry",
|
|
|
|
|
"Physics", "CompSci", "PhysEd", "GLL", "WAT", "Forder", "WP")
|
|
|
|
|
val colourNames = arrayOf("default", "red", "orange", "yellow", "green", "teal", "cyan", "blue", "purple", "pink",
|
|
|
|
|
"brown", "grey", "pureWhite", "salmon", "tangerine", "banana", "flora", "spindrift", "sky", "orchid",
|
|
|
|
|
"lavender", "carnation", "brown2", "pureBlack")
|
2019-09-11 20:59:55 +02:00
|
|
|
private val colourIntegers = intArrayOf(
|
|
|
|
|
0,
|
2019-09-01 11:54:12 +02:00
|
|
|
R.color.bgRed,
|
|
|
|
|
R.color.bgOrange,
|
|
|
|
|
R.color.bgYellow,
|
|
|
|
|
R.color.bgGreen,
|
|
|
|
|
R.color.bgTeal,
|
|
|
|
|
R.color.bgCyan,
|
|
|
|
|
R.color.bgBlue,
|
|
|
|
|
R.color.bgPurple,
|
|
|
|
|
R.color.bgPink,
|
|
|
|
|
R.color.bgBrown,
|
|
|
|
|
R.color.bgGrey,
|
|
|
|
|
R.color.bgPureWhite,
|
|
|
|
|
R.color.bgSalmon,
|
|
|
|
|
R.color.bgTangerine,
|
|
|
|
|
R.color.bgBanana,
|
|
|
|
|
R.color.bgFlora,
|
|
|
|
|
R.color.bgSpindrift,
|
|
|
|
|
R.color.bgSky,
|
|
|
|
|
R.color.bgOrchid,
|
|
|
|
|
R.color.bgLavender,
|
|
|
|
|
R.color.bgCarnation,
|
|
|
|
|
R.color.bgBrown2,
|
|
|
|
|
R.color.bgPureBlack
|
|
|
|
|
)
|
2019-08-31 16:12:36 +02:00
|
|
|
const val notificationChannelId = "general"
|
2019-05-23 18:08:04 +02:00
|
|
|
|
2019-08-09 21:18:27 +02:00
|
|
|
fun getColourForString(name: String): Int {
|
2019-08-31 16:12:36 +02:00
|
|
|
return when (name) {
|
2019-08-09 21:18:27 +02:00
|
|
|
"red" -> R.color.bgRed
|
|
|
|
|
"orange" -> R.color.bgOrange
|
|
|
|
|
"yellow" -> R.color.bgYellow
|
|
|
|
|
"green" -> R.color.bgGreen
|
|
|
|
|
"teal" -> R.color.bgTeal
|
|
|
|
|
"cyan" -> R.color.bgCyan
|
|
|
|
|
"blue" -> R.color.bgBlue
|
|
|
|
|
"purple" -> R.color.bgPurple
|
|
|
|
|
"pink" -> R.color.bgPink
|
|
|
|
|
"brown" -> R.color.bgBrown
|
|
|
|
|
"grey" -> R.color.bgGrey
|
|
|
|
|
"pureWhite" -> R.color.bgPureWhite
|
|
|
|
|
"salmon" -> R.color.bgSalmon
|
|
|
|
|
"tangerine" -> R.color.bgTangerine
|
|
|
|
|
"banana" -> R.color.bgBanana
|
|
|
|
|
"flora" -> R.color.bgFlora
|
|
|
|
|
"spindrift" -> R.color.bgSpindrift
|
|
|
|
|
"sky" -> R.color.bgSky
|
|
|
|
|
"orchid" -> R.color.bgOrchid
|
|
|
|
|
"lavender" -> R.color.bgLavender
|
|
|
|
|
"carnation" -> R.color.bgCarnation
|
|
|
|
|
"brown2" -> R.color.bgBrown2
|
|
|
|
|
"pureBlack" -> R.color.bgPureBlack
|
|
|
|
|
else -> R.color.colorBackgroundLight
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-18 17:53:22 +02:00
|
|
|
fun getIconForCourse(course: String): Int {
|
|
|
|
|
return with (course.toLowerCase(Locale.ROOT)) {
|
2019-07-31 11:27:46 +02:00
|
|
|
when {
|
|
|
|
|
contains("deu") || contains("dep") || contains("daz") -> R.drawable.ic_german
|
|
|
|
|
contains("mat") || contains("map") -> R.drawable.ic_maths
|
|
|
|
|
contains("eng") || contains("enp") || contains("ena") -> R.drawable.ic_english
|
|
|
|
|
contains("spo") || contains("spp") || contains("spth") -> R.drawable.ic_pe
|
|
|
|
|
contains("pol") || contains("pop") -> R.drawable.ic_politics
|
|
|
|
|
contains("dar") || contains("dap") -> R.drawable.ic_drama
|
|
|
|
|
contains("phy") || contains("php") -> R.drawable.ic_physics
|
|
|
|
|
contains("bio") || contains("bip") || contains("nw") -> R.drawable.ic_biology
|
|
|
|
|
contains("che") || contains("chp") -> R.drawable.ic_chemistry
|
|
|
|
|
contains("phi") || contains("psp") -> R.drawable.ic_philosophy
|
|
|
|
|
contains("laa") || contains("laf") || contains("lat") -> R.drawable.ic_latin
|
|
|
|
|
contains("spa") || contains("spf") -> R.drawable.ic_spanish
|
|
|
|
|
contains("fra") || contains("frf") || contains("frz") -> R.drawable.ic_french
|
|
|
|
|
contains("inf") -> R.drawable.ic_compsci
|
|
|
|
|
contains("ges") -> R.drawable.ic_history
|
|
|
|
|
contains("rel") -> R.drawable.ic_religion
|
|
|
|
|
contains("geg") || contains("wuk") -> R.drawable.ic_geography
|
|
|
|
|
contains("kun") -> R.drawable.ic_arts
|
|
|
|
|
contains("mus") -> R.drawable.ic_music
|
|
|
|
|
contains("tue") -> R.drawable.ic_turkish
|
|
|
|
|
contains("chi") -> R.drawable.ic_chinese
|
|
|
|
|
contains("gll") -> R.drawable.ic_gll
|
|
|
|
|
contains("wat") -> R.drawable.ic_wat
|
|
|
|
|
contains("för") -> R.drawable.ic_help
|
|
|
|
|
contains("wp") || contains("met") -> R.drawable.ic_pencil
|
|
|
|
|
else -> R.drawable.ic_empty
|
|
|
|
|
}
|
2019-05-21 18:55:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
2019-08-09 21:18:27 +02:00
|
|
|
|
|
|
|
|
fun transferOldColourIntsToString(prefs: SharedPreferences) {
|
|
|
|
|
var colour = ""
|
|
|
|
|
val edit = prefs.edit()
|
2019-08-18 17:53:22 +02:00
|
|
|
for (course in languageIndependentCourses) {
|
2019-09-05 18:20:30 +02:00
|
|
|
for (i in colourIntegers.indices) {
|
2019-08-09 21:18:27 +02:00
|
|
|
if (prefs.getInt("bg$course", 0) == colourIntegers[i]) {
|
2019-08-18 17:53:22 +02:00
|
|
|
colour = colourNames[i]
|
2019-08-09 21:18:27 +02:00
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
colour = ""
|
|
|
|
|
}
|
|
|
|
|
edit.putString("card$course", colour)
|
|
|
|
|
}
|
|
|
|
|
edit.apply()
|
|
|
|
|
}
|
2019-08-31 16:12:36 +02:00
|
|
|
|
|
|
|
|
fun checkPersonalSubstitutions(subst: Subst, coursePreference: String, classPreference: String, psa: Boolean): Boolean {
|
|
|
|
|
val group = subst.group
|
|
|
|
|
val course = subst.course
|
|
|
|
|
if (psa && subst.date.isNotEmpty() && subst.date.substring(0, 3) == "psa") {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
if (coursePreference.isEmpty() && classPreference.isNotEmpty()) {
|
2019-09-18 16:24:12 +02:00
|
|
|
if (group.isNotEmpty()) {
|
2019-08-31 16:12:36 +02:00
|
|
|
if (classPreference.contains(group) || group.contains(classPreference)) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else if (classPreference.isNotEmpty() && coursePreference.isNotEmpty()) {
|
|
|
|
|
if (group != "" && course != "") {
|
2019-09-18 16:24:12 +02:00
|
|
|
if ((classPreference.contains(group) || group.contains(classPreference)) && coursePreference.contains(course)) {
|
|
|
|
|
return true
|
2019-08-31 16:12:36 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@TargetApi(26)
|
|
|
|
|
fun getNotificationChannel(context: Context, prefs: SharedPreferences): NotificationChannel {
|
|
|
|
|
val manager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
|
return if (!prefs.getBoolean("notifChannelCreated", false)) {
|
2019-09-01 11:54:12 +02:00
|
|
|
val channel = NotificationChannel(
|
|
|
|
|
notificationChannelId, context.getString(
|
|
|
|
|
R.string.general
|
|
|
|
|
), NotificationManager.IMPORTANCE_DEFAULT)
|
2019-08-31 16:12:36 +02:00
|
|
|
channel.enableLights(true)
|
|
|
|
|
channel.lightColor = Color.BLUE
|
|
|
|
|
manager.createNotificationChannel(channel)
|
|
|
|
|
prefs.edit().putBoolean("notifChannelCreated", true).apply()
|
|
|
|
|
channel
|
|
|
|
|
} else {
|
|
|
|
|
manager.getNotificationChannel(notificationChannelId)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-03 18:18:21 +02:00
|
|
|
|
|
|
|
|
private val prefKeys = arrayOf("username", "classes", "courses", "greeting", "themeInt", "notif",
|
|
|
|
|
"defaultPersonalised", "autoRefresh", "firstTimeDev", "launchDev", "pingFB",
|
|
|
|
|
"subscribedToFBDebugChannel", "subscribedToiOSChannel")
|
|
|
|
|
private val prefTypes = arrayOf("string", "string", "string", "bool", "int", "bool", "bool", "bool",
|
|
|
|
|
"string", "int", "int", "bool", "bool")
|
|
|
|
|
|
|
|
|
|
private fun getPrefValue(prefs: SharedPreferences, type: String, key: String): Any {
|
|
|
|
|
return when (type) {
|
|
|
|
|
"string" -> prefs.getString(key, "")
|
|
|
|
|
"int" -> prefs.getInt(key, 0)
|
|
|
|
|
"bool" -> prefs.getBoolean(key, false)
|
|
|
|
|
else -> ""
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private fun setPrefValue(prefs: SharedPreferences, key: String, value: String, type: String) {
|
|
|
|
|
when (type) {
|
|
|
|
|
"int" -> prefs.edit().putInt(key, value.toInt()).apply()
|
|
|
|
|
"string" -> prefs.edit().putString(key, value).apply()
|
|
|
|
|
"bool" -> prefs.edit().putBoolean(key, value.toBoolean()).apply()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun writePrefsToXml(prefs: SharedPreferences, context: Context, activity: FragmentActivity) {
|
|
|
|
|
if (checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
val localPrefKeys = ArrayList<String>()
|
|
|
|
|
val localPrefTypes = ArrayList<String>()
|
|
|
|
|
for (i in prefKeys.indices) {
|
|
|
|
|
localPrefKeys.add(prefKeys[i])
|
|
|
|
|
localPrefTypes.add(prefTypes[i])
|
|
|
|
|
}
|
|
|
|
|
for (i in languageIndependentCourses.indices) {
|
|
|
|
|
localPrefKeys.add("card${languageIndependentCourses[i]}")
|
|
|
|
|
localPrefTypes.add("string")
|
|
|
|
|
}
|
|
|
|
|
val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
|
|
|
|
|
val file = File(dir, "avh_plan_data.xml")
|
|
|
|
|
val out = OutputStreamWriter(FileOutputStream(file, false))
|
|
|
|
|
|
|
|
|
|
var input = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<items>"
|
|
|
|
|
for (i in localPrefKeys.indices) {
|
|
|
|
|
input += "\n\t<key>${localPrefKeys[i]}</key>" +
|
|
|
|
|
"\n\t<value>${getPrefValue(prefs, localPrefTypes[i], localPrefKeys[i])}</value>"
|
|
|
|
|
}
|
|
|
|
|
input += "\n</items>"
|
|
|
|
|
out.write(input)
|
|
|
|
|
out.flush()
|
|
|
|
|
out.close()
|
|
|
|
|
Toast.makeText(context, context.getString(R.string.success), Toast.LENGTH_LONG).show()
|
|
|
|
|
} else {
|
2019-09-22 13:39:20 +02:00
|
|
|
Toast.makeText(context, context.getString(R.string.permission_denied), Toast.LENGTH_LONG).show()
|
2019-09-03 18:18:21 +02:00
|
|
|
requestPermissions(activity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 42)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fun readPrefsFromXml(prefs: SharedPreferences, context: Context, activity: FragmentActivity) {
|
|
|
|
|
if (checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
|
|
|
|
|
val localPrefTypes = ArrayList<String>()
|
|
|
|
|
for (i in prefKeys.indices) {
|
|
|
|
|
localPrefTypes.add(prefTypes[i])
|
|
|
|
|
}
|
|
|
|
|
for (i in languageIndependentCourses.indices) {
|
|
|
|
|
localPrefTypes.add("string")
|
|
|
|
|
}
|
|
|
|
|
val dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)
|
|
|
|
|
val file = File(dir, "avh_plan_data.xml")
|
|
|
|
|
val documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder()
|
|
|
|
|
val document = documentBuilder.parse(file)
|
|
|
|
|
|
|
|
|
|
for (i in localPrefTypes.indices) {
|
|
|
|
|
val key = document.getElementsByTagName("key").item(i).textContent
|
|
|
|
|
val value = document.getElementsByTagName("value").item(i).textContent
|
|
|
|
|
setPrefValue(prefs, key, value, localPrefTypes[i])
|
|
|
|
|
}
|
|
|
|
|
Toast.makeText(context, context.getString(R.string.success), Toast.LENGTH_LONG).show()
|
|
|
|
|
} else {
|
2019-09-22 13:39:20 +02:00
|
|
|
Toast.makeText(context, context.getString(R.string.permission_denied), Toast.LENGTH_LONG).show()
|
2019-09-03 18:18:21 +02:00
|
|
|
requestPermissions(activity, arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), 42)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-09-13 12:03:14 +02:00
|
|
|
|
|
|
|
|
fun checkStringForArray(s: String, checking: Array<String>): Boolean {
|
|
|
|
|
checking.forEach { check ->
|
|
|
|
|
if (s.contains(check)) return true
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2019-05-21 18:55:29 +02:00
|
|
|
}
|