package org.meshbay.client.shell import android.app.Activity import android.content.Intent import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.CountDownLatch import java.util.concurrent.atomic.AtomicInteger /** * A system picker (folder, save-as, open) started from a bridge worker and * waited on there — the bridge never blocks the UI thread, and the page gets * its answer as the reply to the call that asked. */ class Pickers(private val activity: Activity) { private class Waiting { val done = CountDownLatch(1); @Volatile var result: Intent? = null } private val waiting = ConcurrentHashMap() private val codes = AtomicInteger(4000) /** The result intent, or null when the person dismissed the picker. */ fun run(intent: Intent): Intent? { val code = codes.incrementAndGet() val w = Waiting() waiting[code] = w activity.runOnUiThread { try { activity.startActivityForResult(intent, code) } catch (e: android.content.ActivityNotFoundException) { waiting.remove(code); w.done.countDown() } } w.done.await() return w.result } /** From Activity.onActivityResult; true when the code was one of ours. */ fun deliver(requestCode: Int, resultCode: Int, data: Intent?): Boolean { val w = waiting.remove(requestCode) ?: return false w.result = if (resultCode == Activity.RESULT_OK) data ?: Intent() else null w.done.countDown() return true } }