aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/kotlin/com/pitchedapps/frost/facebook/CookieMap.kt
blob: 96b1f2de7178c021ad9c00ccdc792ac0d8342242 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package com.pitchedapps.frost.facebook

import android.webkit.CookieManager
import com.pitchedapps.frost.utils.Prefs

/**
 * Created by Allan Wang on 2017-05-30.
 */
object CookieMap {

    var userId: Int = -1
    private val userMatcher = "c_user=([0-9]*);"
    private val map = HashMap<String, String>()

    operator fun get(key: String) = map[key]

    operator fun set(key: String, value: String) {
        map[key] = value
    }

    fun put(url: String, cookie: String) {
        map.put(url, cookie)
        checkUserId(url, cookie)
    }

    fun checkUserId(url: String, cookie: String) {
        if (userId != -1) return
        if (!url.contains("facebook") || !cookie.contains(userMatcher)) return
        val id = Regex(userMatcher).find(cookie)?.value
        if (id != null) {
            userId = id.toInt()
            save()
        }
    }

    fun save() {
        Prefs.userId = userId
        CookieManager.getInstance().flush()

    }

    fun reset() {

    }
}