aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/moe/yuuta/workmode/suspend/data/SuspendedStorage.kt
blob: 74c9b67bcf4acc3005a86de79b7b902296aea5cb (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package moe.yuuta.workmode.suspend.data

import android.content.Context
import android.content.SharedPreferences
import com.crashlytics.android.Crashlytics
import com.crashlytics.android.answers.Answers
import com.crashlytics.android.answers.CustomEvent
import com.elvishew.xlog.XLog
import moe.yuuta.workmode.Setup
import moe.yuuta.workmode.utils.Utils
import java.util.stream.Collectors

/**
 * An independent storage of suspended status
 */
class SuspendedStorage(mContext: Context) {
    private val logger = XLog.tag("Storage").build()

    companion object {
        private lateinit var instance: SuspendedStorage

        fun get(context: Context): SuspendedStorage {
            if (!::instance.isInitialized) {
                instance = SuspendedStorage(context)
            }
            return instance
        }
    }

    private val storage: SharedPreferences = mContext.getSharedPreferences("suspended", Context.MODE_PRIVATE)

    fun getStorage(): SharedPreferences = storage

    fun getList(): List<String> = (getStorage().getStringSet("list", setOf()) ?: listOf<String>()).toList()

    fun setList(set: Set<String>) {
        logger.d("s() $set")
        getStorage().edit().putStringSet("list", set).apply()
    }

    fun setStatus(status: Status) {
        getStorage().edit().putInt("status",
                when (status) {
                    Status.OFF -> 0
                    Status.ON -> 1
                }).apply()
    }

    fun getStatus(): Status =
            when (getStorage().getInt("status", 0)) {
                0 -> Status.OFF
                1 -> Status.ON
                else -> Status.OFF
            }

    fun setListMode(listMode: ListMode) {
        getStorage().edit().putInt("list_mode",
                when (listMode) {
                    ListMode.BLACKLIST -> 0
                    ListMode.WHITELIST -> 1
                }).apply()
    }

    fun getListMode(): ListMode =
            when (getStorage().getInt("list_mode", 0)) {
                0 -> ListMode.BLACKLIST
                1 -> ListMode.WHITELIST
                else -> ListMode.BLACKLIST
            }

    fun cleanList(context: Context) {
        val installed = context.packageManager.getInstalledApplications(0)
                .stream()
                .filter(Utils.buildGeneralApplicationInfoFilter(context))
                .map {
                    return@map it.packageName
                }
                .collect(Collectors.toList())
        setList(getList().stream()
                .filter {
                    return@filter installed.contains(it)
                }
                .collect(Collectors.toSet()))
    }

    // #Anti-Crack
    fun reportCrack(id: String, reason: String) {
        if (getList().isEmpty()) {
            // Only report when user had set at least one app.
            logger.d("Skipping for registering...")
            return
        }
        val reportTimes = getStorage().getInt("c_$id", 0)
        getStorage().edit()
                .putInt("c_$id", reportTimes + 1)
                .apply()
        if (reportTimes <= 0) {
            Runnable {
                Runnable {
                    if (Setup.FABRIC_ENABLE)
                        Runnable {
                            Answers.getInstance().logCustom(CustomEvent("St.rf.pa.")
                                    .putCustomAttribute("rn", reason)
                                    .putCustomAttribute("ic", id))
                            Crashlytics.log("Sf. $reason $id")
                        }.run()
                }.run()
            }.run()
        }
    }

    // #Anti-Crack
    fun removeCrack(id: String) {
        getStorage().edit()
                .remove("c_$id")
                .apply()
        if (Setup.FABRIC_ENABLE)
            Runnable {
                Answers.getInstance().logCustom(CustomEvent("St.rf.rm.")
                        .putCustomAttribute("ic", id))
                Crashlytics.log("Sf.rm. $id")
            }.run()
    }
}