Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ android {

defaultConfig {
applicationId 'org.openintents.convertcsv'
versionCode 13
versionName '2.0.0-beta1'
versionCode 15
versionName '2.0.0-beta3'
minSdkVersion 19
targetSdkVersion rootProject.ext.targetSdkVersion
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<application
android:icon="@drawable/ic_menu_convert_csv"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat"
tools:replace="android:label">
<!-- aTrackDog metadata -->
<meta-data
Expand Down Expand Up @@ -106,8 +107,9 @@
<activity
android:name=".blockstack.AccountActivity"
android:label="@string/activity_account_title"
android:launchMode="singleTask"
android:parentActivityName=".InfoActivity"
android:theme="@style/AppTheme.NoActionBar">
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.VIEW"/>

Expand Down Expand Up @@ -138,4 +140,4 @@
<uses-permission android:name="org.openintents.shopping.READ_PERMISSION"/>
<uses-permission android:name="org.openintents.shopping.WRITE_PERMISSION"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package org.openintents.convertcsv.blockstack
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.DocumentsContract
import android.support.v4.app.NavUtils
import android.support.v7.app.AppCompatActivity
import android.util.Log
Expand All @@ -12,13 +13,10 @@ import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_account.*
import kotlinx.android.synthetic.main.content_account.*
import kotlinx.coroutines.experimental.CommonPool
import kotlinx.coroutines.experimental.*
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.async
import kotlinx.coroutines.experimental.launch
import org.blockstack.android.sdk.BlockstackSession
import org.blockstack.android.sdk.Executor
import org.blockstack.android.sdk.PutFileOptions
import org.jetbrains.anko.coroutines.experimental.Ref
import org.jetbrains.anko.coroutines.experimental.asReference
import org.openintents.convertcsv.R
Expand All @@ -35,14 +33,13 @@ class AccountActivity : AppCompatActivity() {
setSupportActionBar(toolbar)
supportActionBar!!.setDisplayHomeAsUpEnabled(true)

signInButton.isEnabled = false
signOutButton.isEnabled = false
setLoadingUI(true)

val ref: Ref<AccountActivity> = this.asReference()

launch(UI) {
async(CommonPool) {
_blockstackSession = BlockstackSession(ref(), defaultConfig, executor = object: Executor {
async(v8Context) {
_blockstackSession = BlockstackSession(ref(), defaultConfig, executor = object : Executor {
override fun onMainThread(function: (Context) -> Unit) {
runOnUiThread {
function(this@AccountActivity)
Expand All @@ -54,18 +51,18 @@ class AccountActivity : AppCompatActivity() {
async(CommonPool) {
function()
}
} catch (e:Exception) {
} catch (e: Exception) {
Log.d(TAG, "error in network thread", e)
}
}

override fun onV8Thread(function: () -> Unit) {
async(CommonPool) {
runOnV8Thread {
function()
}
}

})
}, sessionStore = getSessionStore(this@AccountActivity))
if (intent?.action == Intent.ACTION_VIEW) {
handleAuthResponse(intent)
}
Expand All @@ -74,39 +71,34 @@ class AccountActivity : AppCompatActivity() {
}

signInButton.setOnClickListener { _ ->
blockstackSession().redirectUserToSignIn { _ ->
Log.d(TAG, "signed in error!")
launch(UI) {
runOnV8Thread {
blockstackSession().signUserOut()
blockstackSession().redirectUserToSignIn { _ ->
Log.d(TAG, "signed in redirect")
}
}
}
}

signOutButton.setOnClickListener { _ ->
launch(UI) {
async(CommonPool) {
runOnV8Thread {
blockstackSession().signUserOut()
}.await()
notifyDocumentUI()
Log.d(TAG, "signed out!")
finish()
}
}

putFileButton.setOnClickListener { _ ->
launch(UI) {
async(CommonPool) {
blockstackSession().putFile("test.csv", "no data", PutFileOptions(false)) {
Log.d(TAG, "put done " + it.value + " " + it.error)
}
}
}
}
}

private fun onLoaded() {
signInButton.isEnabled = true
signOutButton.isEnabled = true
launch(UI) {
val signedIn = async(CommonPool) {
val signedIn = runOnV8Thread {
blockstackSession().isUserSignedIn()
}.await()
setLoadingUI(false)

if (signedIn) {
signInButton.visibility = View.GONE
Expand All @@ -115,12 +107,23 @@ class AccountActivity : AppCompatActivity() {
signInButton.visibility = View.VISIBLE
signOutButton.visibility = View.GONE
}
notifyDocumentUI()
}
}

private fun onSignIn() {
blockstackSession().loadUserData()
finish()
launch(UI) {
runOnV8Thread {
blockstackSession().loadUserData()
}.await()
notifyDocumentUI()
finish()
}
}

private fun notifyDocumentUI() {
val rootsUri = DocumentsContract.buildRootsUri("org.openintents.convertcsv.documents")
this@AccountActivity.getContentResolver().notifyChange(rootsUri, null)
}

override fun onNewIntent(intent: Intent?) {
Expand All @@ -137,16 +140,37 @@ class AccountActivity : AppCompatActivity() {
val authResponse = intent?.data?.getQueryParameter("authResponse")
if (authResponse != null) {
Log.d(TAG, "authResponse: ${authResponse}")
blockstackSession().handlePendingSignIn(authResponse, {
if (it.hasErrors) {
Toast.makeText(this, it.error, Toast.LENGTH_SHORT).show()
} else {
Log.d(TAG, "signed in!")
runOnUiThread {
onSignIn()
}
setLoadingUI(true)
runOnV8Thread {
try {
Log.d(TAG, "before signed in!")
blockstackSession().handlePendingSignIn(authResponse, {
Log.d(TAG, "signed in result " + it.error + " " + it.value)
if (it.hasErrors) {
Toast.makeText(this@AccountActivity, it.error, Toast.LENGTH_SHORT).show()
} else {
Log.d(TAG, "signed in!")
runOnUiThread {
onSignIn()
}
}
})
} catch (e: Exception) {
Log.d(TAG, "signed in error", e)
}
})
}
}
}

private fun setLoadingUI(loading: Boolean) {
if (loading) {
signInButton.isEnabled = false
signOutButton.isEnabled = false
accountDescription.visibility = View.VISIBLE
} else {
signInButton.isEnabled = true
signOutButton.isEnabled = true
accountDescription.visibility = View.INVISIBLE
}
}

Expand All @@ -158,14 +182,24 @@ class AccountActivity : AppCompatActivity() {
return super.onOptionsItemSelected(item)
}

fun <T> runOnV8Thread(runnable: () -> T): Deferred<T> {
return async(v8Context) {
runnable()
}

}

fun blockstackSession(): BlockstackSession {
val session = _blockstackSession
if (session != null) {
return session
} else {
Log.d(TAG, "too early")
throw IllegalStateException("No session.")
}
}
}

val v8Context = newFixedThreadPoolContext(1, "v8Thread")


Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class ConvertCSVProvider : DocumentsProvider() {
function.invoke()
}
}
})
}, sessionStore = getSessionStore(context))
}
mBaseDir = GaiaFile("", true)

Expand All @@ -113,6 +113,7 @@ class ConvertCSVProvider : DocumentsProvider() {
var title: String? = null
runOnV8Thread {
isUserLoggedIn = mSession?.isUserSignedIn() ?: false
Log.d(TAG, "signed in " + isUserLoggedIn + " " + mSession)
if (isUserLoggedIn) {
title = mSession?.loadUserData()?.profile?.name
if (title == null) {
Expand Down Expand Up @@ -270,17 +271,6 @@ class ConvertCSVProvider : DocumentsProvider() {
}
// END_INCLUDE(query_search_documents)

// BEGIN_INCLUDE(open_document_thumbnail)
@Throws(FileNotFoundException::class)
override fun openDocumentThumbnail(documentId: String, sizeHint: Point,
signal: CancellationSignal): AssetFileDescriptor {
Log.v(TAG, "openDocumentThumbnail")

val file = getFileForDocId(documentId)
val pfd = ParcelFileDescriptor.open(File("/"), ParcelFileDescriptor.MODE_READ_ONLY)
return AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH)
}
// END_INCLUDE(open_document_thumbnail)

// BEGIN_INCLUDE(query_document)
@Throws(FileNotFoundException::class)
Expand Down Expand Up @@ -358,7 +348,7 @@ class ConvertCSVProvider : DocumentsProvider() {
return ParcelFileDescriptor.open(outputFile, accessMode, mainHandler) {
Log.d(TAG, "closing file " + it?.toString())

val options = PutFileOptions(encrypt = false)
val options = PutFileOptions()
var putFileDone = false
val inputStream: InputStream = File(outputFile.path).inputStream()
val inputString = inputStream.bufferedReader().use { it.readText() }
Expand All @@ -383,18 +373,20 @@ class ConvertCSVProvider : DocumentsProvider() {
}
}
} else {
val options = GetFileOptions(decrypt = false)
val options = GetFileOptions()
var getFileDone = false

var fileNotFound = false
runOnV8Thread {
mSession?.getFile(file.path, options) {
Log.d(TAG, "getFile " + it.value)
if (it.hasValue) {
it.value
if (it.value is String) {
outputFile.writeText(it.value as String)
} else {
outputFile.writeBytes(it.value as ByteArray)
}
} else {
fileNotFound = true
}
getFileDone = true
}
Expand All @@ -404,6 +396,10 @@ class ConvertCSVProvider : DocumentsProvider() {
Thread.sleep(500)
}

if (fileNotFound) {
throw FileNotFoundException(file.path)
}

return ParcelFileDescriptor.open(outputFile, accessMode)
}
}
Expand All @@ -415,8 +411,7 @@ class ConvertCSVProvider : DocumentsProvider() {
override fun createDocument(documentId: String, mimeType: String, displayName: String): String {
Log.v(TAG, "createDocument")

val parent = getFileForDocId(documentId)
val file = GaiaFile(parent!!.path + "/" + displayName, false)
val file = GaiaFile(displayName, false)
try {
// TODO create file
} catch (e: IOException) {
Expand Down Expand Up @@ -527,7 +522,7 @@ class ConvertCSVProvider : DocumentsProvider() {

if (mimeType.startsWith("image/")) {
// Allow the image to be represented by a thumbnail rather than an icon
flags = flags or Document.FLAG_SUPPORTS_THUMBNAIL
flags = flags // TODO support Document.FLAG_SUPPORTS_THUMBNAIL
}

val row = result.newRow()
Expand All @@ -552,12 +547,11 @@ class ConvertCSVProvider : DocumentsProvider() {
*/
@Throws(FileNotFoundException::class)
private fun getFileForDocId(docId: String): GaiaFile? {
var target = mBaseDir
if (docId == ROOT) {
return target
return mBaseDir
}

target = GaiaFile(docId, false)
val target = GaiaFile(docId, false)
return target

}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package org.openintents.convertcsv.blockstack

import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import org.blockstack.android.sdk.Scope
import org.blockstack.android.sdk.SessionStore
import org.blockstack.android.sdk.toBlockstackConfig

val defaultConfig = "https://convertcsv.openintents.org".toBlockstackConfig(
kotlin.arrayOf(Scope.StoreWrite))

private var sessionStore: SessionStore? = null
fun getSessionStore(context:Context):SessionStore {
if (sessionStore == null) {
sessionStore = SessionStore(PreferenceManager.getDefaultSharedPreferences(context))
}
return sessionStore!!
}

Loading