-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #87 from Parsely/extract_queuemanager_local_storage
- Loading branch information
Showing
6 changed files
with
360 additions
and
110 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
parsely/src/main/java/com/parsely/parselyandroid/LocalStorageRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.parsely.parselyandroid | ||
|
||
import android.content.Context | ||
import java.io.EOFException | ||
import java.io.FileNotFoundException | ||
import java.io.ObjectInputStream | ||
import java.io.ObjectOutputStream | ||
|
||
internal open class LocalStorageRepository(private val context: Context) { | ||
/** | ||
* Persist an object to storage. | ||
* | ||
* @param o Object to store. | ||
*/ | ||
private fun persistObject(o: Any) { | ||
try { | ||
val fos = context.applicationContext.openFileOutput( | ||
STORAGE_KEY, | ||
Context.MODE_PRIVATE | ||
) | ||
val oos = ObjectOutputStream(fos) | ||
oos.writeObject(o) | ||
oos.close() | ||
} catch (ex: Exception) { | ||
ParselyTracker.PLog("Exception thrown during queue serialization: %s", ex.toString()) | ||
} | ||
} | ||
|
||
/** | ||
* Delete the stored queue from persistent storage. | ||
*/ | ||
fun purgeStoredQueue() { | ||
persistObject(ArrayList<Map<String, Any>>()) | ||
} | ||
|
||
/** | ||
* Get the stored event queue from persistent storage. | ||
* | ||
* @return The stored queue of events. | ||
*/ | ||
open fun getStoredQueue(): ArrayList<Map<String, Any?>?> { | ||
var storedQueue: ArrayList<Map<String, Any?>?> = ArrayList() | ||
try { | ||
val fis = context.applicationContext.openFileInput(STORAGE_KEY) | ||
val ois = ObjectInputStream(fis) | ||
@Suppress("UNCHECKED_CAST") | ||
storedQueue = ois.readObject() as ArrayList<Map<String, Any?>?> | ||
ois.close() | ||
} catch (ex: EOFException) { | ||
// Nothing to do here. | ||
} catch (ex: FileNotFoundException) { | ||
// Nothing to do here. Means there was no saved queue. | ||
} catch (ex: Exception) { | ||
ParselyTracker.PLog( | ||
"Exception thrown during queue deserialization: %s", | ||
ex.toString() | ||
) | ||
} | ||
return storedQueue | ||
} | ||
|
||
/** | ||
* Delete an event from the stored queue. | ||
*/ | ||
open fun expelStoredEvent() { | ||
val storedQueue = getStoredQueue() | ||
storedQueue.removeAt(0) | ||
} | ||
|
||
/** | ||
* Save the event queue to persistent storage. | ||
*/ | ||
@Synchronized | ||
open fun persistQueue(inMemoryQueue: List<Map<String, Any?>?>) { | ||
ParselyTracker.PLog("Persisting event queue") | ||
persistObject((inMemoryQueue + getStoredQueue()).distinct()) | ||
} | ||
|
||
companion object { | ||
private const val STORAGE_KEY = "parsely-events.ser" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
parsely/src/main/java/com/parsely/parselyandroid/QueueManager.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.parsely.parselyandroid | ||
|
||
import android.os.AsyncTask | ||
|
||
@Suppress("DEPRECATION") | ||
internal class QueueManager( | ||
private val parselyTracker: ParselyTracker, | ||
private val localStorageRepository: LocalStorageRepository | ||
) : AsyncTask<Void?, Void?, Void?>() { | ||
|
||
@Deprecated("Deprecated in Java") | ||
override fun doInBackground(vararg params: Void?): Void? { | ||
// if event queue is too big, push to persisted storage | ||
if (parselyTracker.inMemoryQueue.size > QUEUE_SIZE_LIMIT) { | ||
ParselyTracker.PLog("Queue size exceeded, expelling oldest event to persistent memory") | ||
localStorageRepository.persistQueue(parselyTracker.inMemoryQueue) | ||
parselyTracker.inMemoryQueue.removeAt(0) | ||
// if persisted storage is too big, expel one | ||
if (parselyTracker.storedEventsCount() > STORAGE_SIZE_LIMIT) { | ||
localStorageRepository.expelStoredEvent() | ||
} | ||
} | ||
return null | ||
} | ||
|
||
companion object { | ||
const val QUEUE_SIZE_LIMIT = 50 | ||
const val STORAGE_SIZE_LIMIT = 100 | ||
} | ||
} |
Oops, something went wrong.