Skip to content

Commit

Permalink
Merge pull request #554 from jorgeblacio/sign_in_form_fix
Browse files Browse the repository at this point in the history
Now you can upload images in sign in form, also couple of fixes.
  • Loading branch information
danieltigse authored Oct 14, 2019
2 parents a076e45 + a6be3e6 commit 2d0147e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 92
versionName "0.21.15"
versionCode 93
versionName "0.21.16"
applicationId "com.criptext.mail"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
multiDexEnabled true
Expand Down
53 changes: 46 additions & 7 deletions src/main/kotlin/com/criptext/mail/scenes/WebViewActivity.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,38 @@
package com.criptext.mail.scenes

import android.Manifest
import android.annotation.TargetApi
import android.app.Activity
import android.content.ActivityNotFoundException
import android.content.Intent
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.webkit.WebChromeClient
import android.webkit.WebResourceRequest
import android.webkit.WebView
import android.webkit.WebViewClient
import android.webkit.*
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityCompat
import com.criptext.mail.BaseActivity
import com.criptext.mail.R
import com.criptext.mail.utils.DownloadHelper
import com.criptext.mail.utils.UIMessage
import com.criptext.mail.utils.WebViewUtils
import com.criptext.mail.utils.compat.PermissionUtilsCompat
import com.criptext.mail.utils.file.DownloadBlobInterface
import com.criptext.mail.utils.getLocalizedUIMessage
import droidninja.filepicker.FilePickerConst


class WebViewActivity : AppCompatActivity() {
val userAgent = "Mozilla/5.0 (Linux; Android 4.4.4; Nexus 5 Build/LMY48B) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36"

val webViewCriptext: WebView by lazy { findViewById<WebView>(R.id.webViewCriptext) }
var mUrl: String? = null
var browserName: String? = null
private var mUploadMessage: ValueCallback<Array<Uri>>? = null

private val client = object : WebViewClient() {
override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
Expand All @@ -50,6 +54,33 @@ class WebViewActivity : AppCompatActivity() {
}
}

private val chromeClient = object : WebChromeClient() {
override fun onShowFileChooser(webView: WebView?, filePathCallback: ValueCallback<Array<Uri>>?, fileChooserParams: FileChooserParams?): Boolean {
// make sure there is no existing message
if (mUploadMessage != null) {
mUploadMessage!!.onReceiveValue(null)
mUploadMessage = null
}

mUploadMessage = filePathCallback

val intent = Intent(Intent.ACTION_OPEN_DOCUMENT).apply {
addCategory(Intent.CATEGORY_OPENABLE)
putExtra(Intent.EXTRA_ALLOW_MULTIPLE, false)
type = "image/*"
}
try {
this@WebViewActivity.startActivityForResult(intent, FilePickerConst.REQUEST_CODE_PHOTO)
} catch (e: ActivityNotFoundException) {
mUploadMessage = null
Toast.makeText(this@WebViewActivity, "Cannot open file chooser", Toast.LENGTH_LONG).show()
return false
}

return true
}
}

private val downloadListener = { url: String, _: String, contentDisposition: String,
_: String, _: Long ->

Expand Down Expand Up @@ -87,7 +118,7 @@ class WebViewActivity : AppCompatActivity() {
}

private fun setupWebView() {
webViewCriptext.webChromeClient = WebChromeClient()
webViewCriptext.webChromeClient = chromeClient
webViewCriptext.settings.javaScriptEnabled = true
webViewCriptext.settings.userAgentString = userAgent
webViewCriptext.webViewClient = client
Expand Down Expand Up @@ -180,4 +211,12 @@ class WebViewActivity : AppCompatActivity() {
overridePendingTransition(R.anim.stay, R.anim.slide_out_down)
}

override fun onActivityResult(requestCode: Int, resultCode: Int,
intent: Intent?) {
if (requestCode == FilePickerConst.REQUEST_CODE_PHOTO) {
if (mUploadMessage == null) return
mUploadMessage!!.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent))
mUploadMessage = null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,8 @@ class MailboxSceneController(private val scene: MailboxScene,
WebSocketSingleton.getInstance(activeAccount.jwt)

websocketEvents.setListener(webSocketEventListener)
generalDataSource.listener = null
dataSource.listener = null
host.exitToScene(MailboxParams(),
ActivityMessage.ShowUIMessage(UIMessage(R.string.snack_bar_active_account, arrayOf(activeAccount.userEmail))),
true, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ object AccountDataValidator {

return when {
sanitizedValue.length < 3 -> FormData.Error(UIMessage(R.string.username_length_error))
!validCriptextUserPattern.matcher(sanitizedValue).matches() ||
validEmailAddressPattern.matcher(sanitizedValue).matches() -> FormData.Error(UIMessage(R.string.username_invalid_error))
!validCriptextUserPattern.matcher(sanitizedValue).matches() &&
!validEmailAddressPattern.matcher(sanitizedValue).matches() -> FormData.Error(UIMessage(R.string.username_invalid_error))
else -> FormData.Valid(sanitizedValue)
}
}
Expand Down

0 comments on commit 2d0147e

Please sign in to comment.