Skip to content
This repository was archived by the owner on Jan 29, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
910be28
imported requestly-android-okhttp in requestly-android
Nov 2, 2022
3e7257d
created fragment, adaptor and ui
Nov 4, 2022
8b15ebd
shared pref logic completed
Nov 9, 2022
9469cf4
minor improvement in Host Switcher
Nov 9, 2022
eba9462
hide requestly shared pref files
Nov 9, 2022
1479d61
comments added
Nov 9, 2022
98c5e78
removed unused code
Nov 9, 2022
6d66262
snapshot testing
Nov 21, 2022
a8c2c17
merge conflicts removed
Dec 1, 2022
4fbf657
unused code removed
Dec 1, 2022
5bbf737
sub menus added to HOST-SWITCH fragment
Dec 1, 2022
b3cacfa
dialog ui for adding new mock rule created
Dec 5, 2022
1d71a6f
schema changed as per contract. Corresponding typesafe classes create…
Dec 5, 2022
085bc07
support for requestMethod added
Dec 5, 2022
4a3399e
CRUD operation for Replace rule added. UI pending, Adaptor modified t…
Dec 5, 2022
fe4b559
minor code refactoring. Split schema with utililty functions
Dec 13, 2022
f91b1f4
inputType should be a URI
Dec 13, 2022
d1ee166
name change to req/resp modifier
Dec 13, 2022
6065500
file name change, UI design fixes for consistency, minor refactoring …
Dec 13, 2022
83b8f6a
toString not needed
Dec 13, 2022
6571a1c
UI layer created for both kind of rules. Single UI supports both now
Dec 13, 2022
4f553f4
completed functionality for mocking with host switching using Rule & …
Dec 28, 2022
94e9203
refactoring to change name from host_switcher to api_modifler
Dec 28, 2022
4cc63f7
code refactoring
Dec 28, 2022
760d7aa
reverted to non-snapshot version
Dec 28, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,64 @@ package io.requestly.android.core
import android.content.Context
import android.content.SharedPreferences
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.reflect.TypeToken
import io.requestly.android.core.modules.apiModifier.dao.Rule
import io.requestly.android.core.modules.apiModifier.dao.RuleJsonDeserializer
import java.io.File
import java.lang.ref.WeakReference

data class SharedPrefFileData(
val key: String,
val value: Any?,
val dataType: SharedPrefType?,
val fileName: String,
)

enum class SharedPrefType {
STRING {
override fun toString() = "String"
},
INTEGER {
override fun toString() = "Integer"
},
DOUBLE {
override fun toString() = "Double"
},
LONG {
override fun toString() = "Long"
},
BOOLEAN {
override fun toString() = "Boolean"
},
STRING_SET {
override fun toString() = "StringSet"
};

abstract override fun toString(): String
}

object KeyValueStorageManager {

private const val FILE_NAME = "io.requestly.requestly_pref_file"
private const val REQUESTLY_SHARED_PREF_FILE_PREFIX = "io.requestly."
private const val FILE_NAME = "${REQUESTLY_SHARED_PREF_FILE_PREFIX}requestly_pref_file"

/**
* Full path to the default directory assigned to the package for its
* persistent data.
*/
private lateinit var mContext: WeakReference<Context>
private lateinit var mSharedPref: SharedPreferences
private lateinit var gson: Gson
private var changeListeners: MutableList<SharedPreferences.OnSharedPreferenceChangeListener> = mutableListOf()
private var changeListeners: MutableList<SharedPreferences.OnSharedPreferenceChangeListener> =
mutableListOf()

fun initialize(context: Context) {
mContext = WeakReference(context)
mSharedPref = context.getSharedPreferences(FILE_NAME, 0)
gson = Gson()
val builder = GsonBuilder()
builder.registerTypeAdapter(Rule::class.java, RuleJsonDeserializer())
gson = builder.create()
}

fun getString(key: String, defaultValue: String? = null): String? {
Expand Down Expand Up @@ -50,6 +95,63 @@ object KeyValueStorageManager {
return gson.fromJson(json, typeToken.type)
}

fun deleteKeyFromFile(keyName: String, fileName: String) {
val pref = mContext.get()?.getSharedPreferences(fileName, 0) ?: return

with(pref.edit()) {
this.remove(keyName)
this.commit()
}
}

fun putStringInfile(value: String, keyName: String, fileName: String) {
val pref = mContext.get()?.getSharedPreferences(fileName, 0) ?: return
with(pref.edit()) {
this.putString(keyName, value)
this.commit()
}
}

fun putStringSetInfile(value: Set<String>, keyName: String, fileName: String) {
val pref = mContext.get()?.getSharedPreferences(fileName, 0) ?: return
with(pref.edit()) {
this.putStringSet(keyName, value)
this.commit()
}
}

fun putIntegerInfile(value: Int, keyName: String, fileName: String) {
val pref = mContext.get()?.getSharedPreferences(fileName, 0) ?: return
with(pref.edit()) {
this.putInt(keyName, value)
this.commit()
}
}

fun putDoubleInfile(value: Double, keyName: String, fileName: String) {
val pref = mContext.get()?.getSharedPreferences(fileName, 0) ?: return
with(pref.edit()) {
this.putFloat(keyName, value.toFloat())
this.commit()
}
}

fun putLongInfile(value: Long, keyName: String, fileName: String) {
val pref = mContext.get()?.getSharedPreferences(fileName, 0) ?: return
with(pref.edit()) {
this.putLong(keyName, value)
this.commit()
}
}

fun putBooleanInfile(value: Boolean, keyName: String, fileName: String) {
val pref = mContext.get()?.getSharedPreferences(fileName, 0) ?: return
with(pref.edit()) {
this.putBoolean(keyName, value)
this.commit()
}
}

fun registerChangeListener(forKey: String, changeListener: () -> Unit) {
val listener = SharedPreferences.OnSharedPreferenceChangeListener { _, key ->
if (key === forKey) {
Expand All @@ -59,4 +161,45 @@ object KeyValueStorageManager {
changeListeners.add(listener)
mSharedPref.registerOnSharedPreferenceChangeListener(listener)
}

@Suppress("CANDIDATE_CHOSEN_USING_OVERLOAD_RESOLUTION_BY_LAMBDA_ANNOTATION")
fun fetchDataFromAllSharedPrefFiles(): List<SharedPrefFileData> {
val context = mContext.get() ?: return emptyList()

val prefsDir = File(context.applicationInfo.dataDir, "shared_prefs")

if (!prefsDir.exists() || !prefsDir.isDirectory) return emptyList()

return prefsDir
.list()
?.filter { !it.startsWith(REQUESTLY_SHARED_PREF_FILE_PREFIX) }
?.flatMap { filename ->
val fileNameWithOutExtension = filename.removeSuffix(".xml")
val thisPref = context.getSharedPreferences(fileNameWithOutExtension, 0)
return@flatMap thisPref.all.map { entry ->
SharedPrefFileData(
key = entry.key,
value = entry.value,
dataType = detectType(entry.value),
fileName = fileNameWithOutExtension,
)
}
} ?: emptyList()
}

private fun detectType(value: Any?): SharedPrefType? {
if (value == null) return null

return when (value::class.simpleName) {
"Int" -> SharedPrefType.INTEGER
"Float" -> SharedPrefType.DOUBLE
"Long" -> SharedPrefType.LONG
"HashSet" -> SharedPrefType.STRING_SET
"Boolean" -> SharedPrefType.BOOLEAN
"String" -> SharedPrefType.STRING
else -> null
}
}
}


Loading