[android] keep strong callback reference and use application context during cache path change

This commit is contained in:
Łukasz Paczos
2019-05-09 15:06:43 +02:00
committed by Łukasz Paczos
parent e0f32000df
commit 0f599ce804
4 changed files with 71 additions and 51 deletions

View File

@@ -36,12 +36,12 @@ open class FileSourceMapTest {
fun changeResourcesPathWhileMapVisible() {
val latch = CountDownLatch(1)
rule.activity.runOnUiThread {
FileSource.setResourcesCachePath(rule.activity, fileSourceTestUtils.testPath, object : FileSource.ResourcesCachePathChangeCallback {
override fun onSuccess(path: String?) {
FileSource.setResourcesCachePath(fileSourceTestUtils.testPath, object : FileSource.ResourcesCachePathChangeCallback {
override fun onSuccess(path: String) {
Assert.fail("Requested resources change while the map is running should fail")
}
override fun onError(message: String?) {
override fun onError(message: String) {
Assert.assertEquals("Cannot set path, file source is activated."
+ " Make sure that the map or a resources download is not running.", message)
latch.countDown()

View File

@@ -33,14 +33,13 @@ class FileSourceTestUtils(private val activity: Activity) {
val latch = CountDownLatch(1)
activity.runOnUiThread {
FileSource.setResourcesCachePath(
activity,
path,
object : FileSource.ResourcesCachePathChangeCallback {
override fun onSuccess(path: String?) {
override fun onSuccess(path: String) {
latch.countDown()
}
override fun onError(message: String?) {
override fun onError(message: String) {
Assert.fail("Resource path change failed - path: $path, message: $message")
}
})

View File

@@ -32,6 +32,8 @@ class ChangeResourcesCachePathActivity : AppCompatActivity(),
private lateinit var offlineManager: OfflineManager
private val callback = PathChangeCallback(this)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_change_resources_cache_path)
@@ -50,18 +52,23 @@ class ChangeResourcesCachePathActivity : AppCompatActivity(),
Toast.makeText(this, "Current path: $path", Toast.LENGTH_LONG).show()
}
override fun onDestroy() {
super.onDestroy()
callback.onActivityDestroy()
}
override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
listView.onItemClickListener = null
val path: String = adapter.getItem(position) as String
FileSource.setResourcesCachePath(this, path, this)
FileSource.setResourcesCachePath(path, callback)
}
override fun onError(message: String?) {
override fun onError(message: String) {
listView.onItemClickListener = this
Toast.makeText(this, "Error: $message", Toast.LENGTH_LONG).show()
}
override fun onSuccess(path: String?) {
override fun onSuccess(path: String) {
listView.onItemClickListener = this
Toast.makeText(this, "New path: $path", Toast.LENGTH_LONG).show()
@@ -118,6 +125,21 @@ class ChangeResourcesCachePathActivity : AppCompatActivity(),
return paths
}
private class PathChangeCallback(private var activity: ChangeResourcesCachePathActivity?) : FileSource.ResourcesCachePathChangeCallback {
override fun onSuccess(path: String) {
activity?.onSuccess(path)
}
override fun onError(message: String) {
activity?.onError(message)
}
fun onActivityDestroy() {
activity = null
}
}
class PathAdapter(private val context: Context, private val paths: List<String>) : BaseAdapter() {
override fun getItem(position: Int): Any {