package moe.yuuta.workmode.suspend.data import android.content.Context import android.content.SharedPreferences import com.elvishew.xlog.XLog import moe.yuuta.workmode.utils.Utils import java.util.stream.Collectors /** * An independent storage of suspended status */ class SuspendedStorage(private val mContext: Context) { private val logger = XLog.tag("Storage").build() private fun getStorage(): SharedPreferences = mContext.getSharedPreferences("suspended", Context.MODE_PRIVATE) fun getList(): List = (getStorage().getStringSet("list", setOf()) ?: listOf()).toList() fun setList(set: Set) { 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 = mContext.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())) } }