Browse Source

added planning parser from simple JSON format (with local example)

yattoz 4 years ago
parent
commit
5920c8cd21

+ 22 - 0
app/src/main/assets/planning_example.json View File

@@ -0,0 +1,22 @@
1
+{
2
+  "planning": [
3
+    {
4
+      "title": "Anime Matin",
5
+      "periodicity": "1111100",
6
+      "hour_begin": "9:00",
7
+      "hour_end": "10:30"
8
+    },
9
+    {
10
+      "title": "Jeux Vidéos Matin",
11
+      "periodicity": "1111100",
12
+      "hour_begin": "09:00",
13
+      "hour_end": "10:30"
14
+    },
15
+    {
16
+      "title": "Programmation nocturne",
17
+      "periodicity": "1111111",
18
+      "hour_begin": "23:00",
19
+      "hour_end": "5:30"
20
+    }
21
+  ]
22
+}

+ 4 - 0
app/src/main/java/fr/forum_thalie/tsumugi/MainActivity.kt View File

@@ -16,6 +16,7 @@ import fr.forum_thalie.tsumugi.playerstore.PlayerStore
16 16
 import java.util.Timer
17 17
 import android.view.MenuItem
18 18
 import fr.forum_thalie.tsumugi.alarm.RadioAlarm
19
+import fr.forum_thalie.tsumugi.planning.PlanningParser
19 20
 
20 21
 
21 22
 /* Log to file import
@@ -155,6 +156,9 @@ class MainActivity : BaseActivity() {
155 156
             isTimerStarted = true
156 157
         }
157 158
 
159
+        // fetch program
160
+        PlanningParser.instance.parseUrl(/* getString(R.string.planning_url) */ context = this)
161
+
158 162
         // initialize the UI
159 163
         setTheme(R.style.AppTheme)
160 164
         setContentView(R.layout.activity_main)

+ 61 - 0
app/src/main/java/fr/forum_thalie/tsumugi/planning/PlanningParser.kt View File

@@ -0,0 +1,61 @@
1
+package fr.forum_thalie.tsumugi.planning
2
+
3
+import android.content.Context
4
+import fr.forum_thalie.tsumugi.Async
5
+import org.json.JSONObject
6
+import java.io.IOException
7
+import java.net.URL
8
+
9
+class PlanningParser {
10
+
11
+    val programs: ArrayList<Program> = ArrayList()
12
+
13
+    fun parseUrl(url: String? = null, context: Context? = null)
14
+    {
15
+        val scrape : (Any?) -> String = {
16
+            if (url.isNullOrEmpty() && context != null)
17
+            {
18
+                val json: String
19
+                try {
20
+                    val inputStream = context.assets.open("planning_example.json")
21
+                    val size = inputStream.available()
22
+                    val buffer = ByteArray(size)
23
+                    inputStream.use { it.read(buffer) }
24
+                    json = String(buffer)
25
+                    json
26
+                } catch (ioException: IOException) {
27
+                    ioException.printStackTrace()
28
+                    ""
29
+                }
30
+            }
31
+            else
32
+                URL(url).readText()
33
+
34
+        }
35
+        val post : (parameter: Any?) -> Unit = {
36
+            val result = JSONObject(it as String)
37
+            if (result.has("planning"))
38
+            {
39
+                val programList = result.getJSONArray("planning")
40
+                for (i in 0 until programList.length())
41
+                {
42
+                    val item = programList[i] as JSONObject
43
+                    val periodicity = item.getInt("periodicity")
44
+                    val hourBeginS = item.getString("hour_begin").split(":")
45
+                    val hourBegin = hourBeginS.first().toInt()*60 + hourBeginS.last().toInt()
46
+                    val hourEndS = item.getString("hour_end").split(":")
47
+                    val hourEnd = hourEndS.first().toInt()* 60 + hourEndS.last().toInt()
48
+                    val title = item.getString("title")
49
+                    programs.add(Program(title, periodicity, hourBegin, hourEnd))
50
+                }
51
+            }
52
+        }
53
+        Async(scrape, post)
54
+    }
55
+
56
+    companion object {
57
+        val instance by lazy {
58
+            PlanningParser()
59
+        }
60
+    }
61
+}

+ 41 - 0
app/src/main/java/fr/forum_thalie/tsumugi/planning/Program.kt View File

@@ -0,0 +1,41 @@
1
+package fr.forum_thalie.tsumugi.planning
2
+
3
+import android.util.Log
4
+import fr.forum_thalie.tsumugi.tag
5
+import org.json.JSONObject
6
+import java.util.*
7
+
8
+class Program (val title: String, private val periodicity: Int, private val hourBegin: Int, private val hourEnd: Int) {
9
+    fun isCurrent(): Boolean
10
+    {
11
+        val now = Calendar.getInstance()
12
+        val currentDay = if (now.get(Calendar.DAY_OF_WEEK) - 1 == 0) 6 else now.get(Calendar.DAY_OF_WEEK) - 2
13
+        // 0 (Monday) to 5 (Saturday) + 6 (Sunday)
14
+
15
+        // this translates to "true" when:
16
+        // - the currentDay is flagged in the "periodicity" bit array
17
+        // OR
18
+        // - Yesterday is flagged in the "periodicity" bit array AND the program does span over 2 days (night programs typically).
19
+        // We'll check a after this whether the current hour is within the span.
20
+        val isNow: Boolean = ((((1000000 shr currentDay) and (periodicity)) == 1)) || (((1000000 shr ((currentDay-1)%7) and (periodicity)) == 1) && hourEnd < hourBegin)
21
+
22
+        // shr = shift-right. It's a binary mask.
23
+        if (isNow)
24
+        {
25
+            val hasBegun = (now.get(Calendar.HOUR_OF_DAY)*60 + now.get(Calendar.MINUTE) >= hourBegin )
26
+            val hasNotEnded = (now.get(Calendar.HOUR_OF_DAY)*60 + now.get(Calendar.MINUTE) <= hourEnd )
27
+            if (hasBegun && hasNotEnded)
28
+                return true
29
+        }
30
+
31
+        return false
32
+    }
33
+
34
+    override fun toString(): String {
35
+        return "Title: $title, time info (periodicity, begin, end): $periodicity, $hourBegin, $hourEnd"
36
+    }
37
+
38
+    init {
39
+        Log.d(tag, this.toString())
40
+    }
41
+}

+ 1 - 0
app/src/main/res/values/strings.xml View File

@@ -43,5 +43,6 @@
43 43
     <string name="disable">Disable</string>
44 44
     <string name="website_url">https://tsumugi.forum-thalie.fr/</string>
45 45
     <string name="rss_url">https://tsumugi.forum-thalie.fr/?feed=rss2</string>
46
+    <string name="planning_url">ADD SOME URL HERE...</string>
46 47
 
47 48
 </resources>