From a80d007e0c9d4b63b99a7c9a03ac73e7c5a54203 Mon Sep 17 00:00:00 2001 From: Donovan Preston Date: Fri, 27 Jul 2018 09:11:46 -0400 Subject: [PATCH] Hook up the android kotlin code to the send js code (#860) --- .gitignore | 3 + android/android.iml | 2 +- android/android.js | 159 +++++++++++++++ android/app/app.iml | 3 +- android/app/build.gradle | 9 + android/app/src/main/AndroidManifest.xml | 2 +- android/app/src/main/assets/background_1.jpg | Bin 0 -> 312252 bytes android/app/src/main/assets/index.css | 84 ++++++++ android/app/src/main/assets/index.html | 185 +----------------- .../app/src/main/assets/intent-target.html | 20 -- .../mozilla/send/sendandroid/MainActivity.kt | 23 ++- android/generateAndLinkBundle.js | 21 ++ app/api.js | 7 +- app/fileManager.js | 12 +- build/version_plugin.js | 6 +- webpack.config.js | 3 +- 16 files changed, 317 insertions(+), 222 deletions(-) create mode 100644 android/android.js create mode 100644 android/app/src/main/assets/background_1.jpg create mode 100644 android/app/src/main/assets/index.css delete mode 100644 android/app/src/main/assets/intent-target.html create mode 100644 android/generateAndLinkBundle.js diff --git a/.gitignore b/.gitignore index 1c5a2fc6..f12a06e0 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,6 @@ dist .nyc_output .tox .pytest_cache +android/app/src/main/assets/vendor.js +android/app/src/main/assets/android.js + diff --git a/android/android.iml b/android/android.iml index 686bd2aa..8dac7322 100644 --- a/android/android.iml +++ b/android/android.iml @@ -13,7 +13,7 @@ - + \ No newline at end of file diff --git a/android/android.js b/android/android.js new file mode 100644 index 00000000..b0ca7557 --- /dev/null +++ b/android/android.js @@ -0,0 +1,159 @@ +/* global window, document, fetch */ + +const MAXFILESIZE = 1024 * 1024 * 1024 * 2; + +const EventEmitter = require('events'); +const emitter = new EventEmitter(); + +function dom(tagName, attributes, children = []) { + const node = document.createElement(tagName); + for (const name in attributes) { + if (name.indexOf('on') === 0) { + node[name] = attributes[name]; + } else if (name === 'htmlFor') { + node.htmlFor = attributes.htmlFor; + } else if (name === 'className') { + node.className = attributes.className; + } else { + node.setAttribute(name, attributes[name]); + } + } + if (!(children instanceof Array)) { + children = [children]; + } + for (let child of children) { + if (typeof child === 'string') { + child = document.createTextNode(child); + } + node.appendChild(child); + } + return node; +} + +function uploadComplete(file) { + document.body.innerHTML = ''; + const input = dom('input', { id: 'url', value: file.url }); + const copy = dom( + 'button', + { + id: 'copy-button', + className: 'button', + onclick: () => { + input.select(); + document.execCommand('copy'); + input.blur(); + copy.textContent = 'Copied!'; + setTimeout(function() { + copy.textContent = 'Copy to clipboard'; + }, 2000); + } + }, + 'Copy to clipboard' + ); + const node = dom( + 'div', + { id: 'striped' }, + dom('div', { id: 'white' }, [ + input, + copy, + dom( + 'button', + { id: 'send-another', className: 'button', onclick: render }, + 'Send another file' + ) + ]) + ); + document.body.appendChild(node); +} + +const state = { + storage: { + files: [], + remove: function(fileId) { + console.log('REMOVE FILEID', fileId); + }, + writeFile: function(file) { + console.log('WRITEFILE', file); + }, + addFile: uploadComplete, + totalUploads: 0 + }, + transfer: null, + uploading: false, + settingPassword: false, + passwordSetError: null, + route: '/' +}; + +function upload(event) { + event.preventDefault(); + const target = event.target; + const file = target.files[0]; + if (file.size === 0) { + return; + } + if (file.size > MAXFILESIZE) { + console.log('file too big (no bigger than ' + MAXFILESIZE + ')'); + return; + } + + emitter.emit('upload', { file: file, type: 'click' }); +} + +function render() { + document.body.innerHTML = ''; + const striped = dom( + 'div', + { id: 'striped' }, + dom('div', { id: 'white' }, [ + dom('label', { id: 'label', htmlFor: 'input' }, 'Choose file'), + dom('input', { + id: 'input', + type: 'file', + name: 'input', + onchange: upload + }) + ]) + ); + document.body.appendChild(striped); +} + +emitter.on('render', function() { + document.body.innerHTML = ''; + const percent = + (state.transfer.progress[0] / state.transfer.progress[1]) * 100; + const node = dom( + 'div', + { style: 'background-color: white; width: 100%' }, + dom('span', { + style: `display: inline-block; width: ${percent}%; background-color: blue` + }) + ); + document.body.appendChild(node); +}); + +emitter.on('pushState', function(path) { + console.log('pushState ' + path + ' ' + JSON.stringify(state)); +}); + +const fileManager = require('../app/fileManager').default; +try { + fileManager(state, emitter); +} catch (e) { + console.error('error' + e); + console.error(e); +} + +window.addEventListener( + 'message', + event => { + fetch(event.data) + .then(res => res.blob()) + .then(blob => { + emitter.emit('upload', { file: blob, type: 'share' }); + }); + }, + false +); + +render(); diff --git a/android/app/app.iml b/android/app/app.iml index 418506f8..5f5dbac2 100644 --- a/android/app/app.iml +++ b/android/app/app.iml @@ -22,13 +22,14 @@ - +