From b5d7e99ba5d8627c21cc0c45044fd9d0c6ef8c4f Mon Sep 17 00:00:00 2001 From: Donovan Preston Date: Fri, 22 Jun 2018 16:48:21 -0400 Subject: [PATCH] Serve ui out of local html files; pass text/plain and image/* data to the webview using a data url --- android/app/src/main/AndroidManifest.xml | 4 +- android/app/src/main/assets/index.html | 188 ++++++++++++++++++ .../app/src/main/assets/intent-target.html | 20 ++ .../send/sendandroid/IntentActivity.kt | 28 --- .../mozilla/send/sendandroid/MainActivity.kt | 42 +++- 5 files changed, 250 insertions(+), 32 deletions(-) create mode 100644 android/app/src/main/assets/index.html create mode 100644 android/app/src/main/assets/intent-target.html delete mode 100644 android/app/src/main/java/com/mozilla/send/sendandroid/IntentActivity.kt diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 838b119d..992e1612 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -3,6 +3,7 @@ package="com.mozilla.send.sendandroid"> + - - @@ -30,7 +29,6 @@ - \ No newline at end of file diff --git a/android/app/src/main/assets/index.html b/android/app/src/main/assets/index.html new file mode 100644 index 00000000..73434fe9 --- /dev/null +++ b/android/app/src/main/assets/index.html @@ -0,0 +1,188 @@ + + + + + + + + + + + + + + + + + + + + Firefox Send + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + +
+
+ + +
+
Private, Encrypted File Sharing
+
+
Send files through a safe, private, and encrypted link that automatically expires to ensure your stuff does not remain online forever.
+ + Learn more + +
+
+ +
+ Drop your file here to start uploading +
+ + For the most reliable operation, it’s best to keep your file under 1GB + + + +
+ +
+ +
+ + + + \ No newline at end of file diff --git a/android/app/src/main/assets/intent-target.html b/android/app/src/main/assets/intent-target.html new file mode 100644 index 00000000..17a43f44 --- /dev/null +++ b/android/app/src/main/assets/intent-target.html @@ -0,0 +1,20 @@ + + + + + + Firefox Send + + + +
output
+ + + + \ No newline at end of file diff --git a/android/app/src/main/java/com/mozilla/send/sendandroid/IntentActivity.kt b/android/app/src/main/java/com/mozilla/send/sendandroid/IntentActivity.kt deleted file mode 100644 index f518da3b..00000000 --- a/android/app/src/main/java/com/mozilla/send/sendandroid/IntentActivity.kt +++ /dev/null @@ -1,28 +0,0 @@ -package com.mozilla.send.sendandroid - - -import android.support.v7.app.AppCompatActivity -import android.os.Bundle -import android.content.Intent -import android.util.Log -import android.net.Uri - -class IntentActivity : AppCompatActivity() { - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - val intent = getIntent() - val action = intent.getAction() - val type = intent.getType() - if (Intent.ACTION_SEND.equals(action) && type != null) { - if ("text/plain".equals(type)) { - val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT) - Log.w("INTENT", "text/plain " + sharedText); - } else if (type.startsWith("image/")) { - val imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM) as Uri - - Log.w("INTENT", "image/ " + imageUri); - } - } - } -} diff --git a/android/app/src/main/java/com/mozilla/send/sendandroid/MainActivity.kt b/android/app/src/main/java/com/mozilla/send/sendandroid/MainActivity.kt index e362501e..fc123456 100644 --- a/android/app/src/main/java/com/mozilla/send/sendandroid/MainActivity.kt +++ b/android/app/src/main/java/com/mozilla/send/sendandroid/MainActivity.kt @@ -7,11 +7,23 @@ import im.delight.android.webview.AdvancedWebView import android.graphics.Bitmap import android.content.Intent import android.annotation.SuppressLint +import android.net.Uri import android.webkit.WebView +import android.webkit.WebMessage import android.util.Log +import android.util.Base64 +import android.provider.MediaStore +import android.R.attr.data + + + + + + class MainActivity : AppCompatActivity(), AdvancedWebView.Listener { private var mWebView: AdvancedWebView? = null + private var mToShare: String? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -23,7 +35,28 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener { val webSettings = mWebView!!.getSettings() webSettings.setJavaScriptEnabled(true) - mWebView!!.loadUrl("https://send.firefox.com") + val intent = getIntent() + val action = intent.getAction() + val type = intent.getType() + if (Intent.ACTION_SEND.equals(action) && type != null) { + if (type.equals("text/plain")) { + val sharedText = intent.getStringExtra(Intent.EXTRA_TEXT) + Log.w("INTENT", "text/plain " + sharedText) + mToShare = "data:text/plain;base64," + Base64.encodeToString(sharedText.toByteArray(), 16).trim() + } else if (type.startsWith("image/")) { + val imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM) as Uri + Log.w("INTENT", "image/ " + imageUri) + mToShare = "data:text/plain;base64," + Base64.encodeToString(imageUri.path.toByteArray(), 16).trim() + + // TODO Currently this causes a Permission Denied error + // val stream = contentResolver.openInputStream(imageUri) + } + mWebView!!.loadUrl("file:///android_asset/intent-target.html") + + } else { + mWebView!!.loadUrl("file:///android_asset/index.html") + } + } @SuppressLint("NewApi") @@ -66,6 +99,13 @@ class MainActivity : AppCompatActivity(), AdvancedWebView.Listener { override fun onPageFinished(url: String) { Log.w("MAIN", "onPageFinished") + if (mToShare != null) { + Log.w("INTENT", mToShare) + + val webView = findViewById(R.id.webview) as AdvancedWebView + webView.postWebMessage(WebMessage(mToShare), Uri.EMPTY) + } + } override fun onPageError(errorCode: Int, description: String, failingUrl: String) {