diff --git a/app/src/main/assets/planning_example.json b/app/src/main/assets/planning_example.json new file mode 100644 index 0000000..a4a193a --- /dev/null +++ b/app/src/main/assets/planning_example.json @@ -0,0 +1,22 @@ +{ + "planning": [ + { + "title": "Anime Matin", + "periodicity": "1111100", + "hour_begin": "9:00", + "hour_end": "10:30" + }, + { + "title": "Jeux Vidéos Matin", + "periodicity": "1111100", + "hour_begin": "09:00", + "hour_end": "10:30" + }, + { + "title": "Programmation nocturne", + "periodicity": "1111111", + "hour_begin": "23:00", + "hour_end": "5:30" + } + ] +} \ No newline at end of file diff --git a/app/src/main/java/fr/forum_thalie/tsumugi/MainActivity.kt b/app/src/main/java/fr/forum_thalie/tsumugi/MainActivity.kt index d66911a..5e7cf5e 100644 --- a/app/src/main/java/fr/forum_thalie/tsumugi/MainActivity.kt +++ b/app/src/main/java/fr/forum_thalie/tsumugi/MainActivity.kt @@ -16,6 +16,7 @@ import fr.forum_thalie.tsumugi.playerstore.PlayerStore import java.util.Timer import android.view.MenuItem import fr.forum_thalie.tsumugi.alarm.RadioAlarm +import fr.forum_thalie.tsumugi.planning.PlanningParser /* Log to file import @@ -155,6 +156,9 @@ class MainActivity : BaseActivity() { isTimerStarted = true } + // fetch program + PlanningParser.instance.parseUrl(/* getString(R.string.planning_url) */ context = this) + // initialize the UI setTheme(R.style.AppTheme) setContentView(R.layout.activity_main) diff --git a/app/src/main/java/fr/forum_thalie/tsumugi/planning/PlanningParser.kt b/app/src/main/java/fr/forum_thalie/tsumugi/planning/PlanningParser.kt new file mode 100644 index 0000000..665793e --- /dev/null +++ b/app/src/main/java/fr/forum_thalie/tsumugi/planning/PlanningParser.kt @@ -0,0 +1,61 @@ +package fr.forum_thalie.tsumugi.planning + +import android.content.Context +import fr.forum_thalie.tsumugi.Async +import org.json.JSONObject +import java.io.IOException +import java.net.URL + +class PlanningParser { + + val programs: ArrayList = ArrayList() + + fun parseUrl(url: String? = null, context: Context? = null) + { + val scrape : (Any?) -> String = { + if (url.isNullOrEmpty() && context != null) + { + val json: String + try { + val inputStream = context.assets.open("planning_example.json") + val size = inputStream.available() + val buffer = ByteArray(size) + inputStream.use { it.read(buffer) } + json = String(buffer) + json + } catch (ioException: IOException) { + ioException.printStackTrace() + "" + } + } + else + URL(url).readText() + + } + val post : (parameter: Any?) -> Unit = { + val result = JSONObject(it as String) + if (result.has("planning")) + { + val programList = result.getJSONArray("planning") + for (i in 0 until programList.length()) + { + val item = programList[i] as JSONObject + val periodicity = item.getInt("periodicity") + val hourBeginS = item.getString("hour_begin").split(":") + val hourBegin = hourBeginS.first().toInt()*60 + hourBeginS.last().toInt() + val hourEndS = item.getString("hour_end").split(":") + val hourEnd = hourEndS.first().toInt()* 60 + hourEndS.last().toInt() + val title = item.getString("title") + programs.add(Program(title, periodicity, hourBegin, hourEnd)) + } + } + } + Async(scrape, post) + } + + companion object { + val instance by lazy { + PlanningParser() + } + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/forum_thalie/tsumugi/planning/Program.kt b/app/src/main/java/fr/forum_thalie/tsumugi/planning/Program.kt new file mode 100644 index 0000000..4e8a13e --- /dev/null +++ b/app/src/main/java/fr/forum_thalie/tsumugi/planning/Program.kt @@ -0,0 +1,41 @@ +package fr.forum_thalie.tsumugi.planning + +import android.util.Log +import fr.forum_thalie.tsumugi.tag +import org.json.JSONObject +import java.util.* + +class Program (val title: String, private val periodicity: Int, private val hourBegin: Int, private val hourEnd: Int) { + fun isCurrent(): Boolean + { + val now = Calendar.getInstance() + val currentDay = if (now.get(Calendar.DAY_OF_WEEK) - 1 == 0) 6 else now.get(Calendar.DAY_OF_WEEK) - 2 + // 0 (Monday) to 5 (Saturday) + 6 (Sunday) + + // this translates to "true" when: + // - the currentDay is flagged in the "periodicity" bit array + // OR + // - Yesterday is flagged in the "periodicity" bit array AND the program does span over 2 days (night programs typically). + // We'll check a after this whether the current hour is within the span. + val isNow: Boolean = ((((1000000 shr currentDay) and (periodicity)) == 1)) || (((1000000 shr ((currentDay-1)%7) and (periodicity)) == 1) && hourEnd < hourBegin) + + // shr = shift-right. It's a binary mask. + if (isNow) + { + val hasBegun = (now.get(Calendar.HOUR_OF_DAY)*60 + now.get(Calendar.MINUTE) >= hourBegin ) + val hasNotEnded = (now.get(Calendar.HOUR_OF_DAY)*60 + now.get(Calendar.MINUTE) <= hourEnd ) + if (hasBegun && hasNotEnded) + return true + } + + return false + } + + override fun toString(): String { + return "Title: $title, time info (periodicity, begin, end): $periodicity, $hourBegin, $hourEnd" + } + + init { + Log.d(tag, this.toString()) + } +} \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8326a5f..d141255 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -43,5 +43,6 @@ Disable https://tsumugi.forum-thalie.fr/ https://tsumugi.forum-thalie.fr/?feed=rss2 + ADD SOME URL HERE...