Skip to content

Commit

Permalink
doing a bigger re-work of how share-to works in turtl. instead of mak…
Browse files Browse the repository at this point in the history
…ing decisions about what gets passed back in the java portion, we just gather all the info we can in java and pass it ALL back. then we can pick it apart and make decisions in the javascript code. this lets us get a bit more nuanced in the share code and centralize the logic into one place. so, this fixes sharing from chrome, and it also now detects shares of image files which saves the note as an "image" type instead of "file". fixes turtl/tracker#271
  • Loading branch information
orthecreedence committed Apr 15, 2019
1 parent 311f5d3 commit e2a87ce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,23 @@ public static JSONObject toJSONObject(
final ContentResolver contentResolver,
final Intent intent)
throws JSONException {
JSONArray items = null;
if (items == null || items.length() == 0) {
items = itemsFromText(contentResolver, intent);
}
JSONArray items_clip = null;
JSONArray items_text = null;
JSONArray items_extra = null;
JSONArray items_data = null;
items_text = itemsFromText(contentResolver, intent);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
items = itemsFromClipData(contentResolver, intent.getClipData());
}
if (items == null || items.length() == 0) {
items = itemsFromExtras(contentResolver, intent.getExtras());
}
if (items == null || items.length() == 0) {
items = itemsFromData(contentResolver, intent.getData());
}
if (items == null) {
return null;
items_clip = itemsFromClipData(contentResolver, intent.getClipData());
}
items_extra = itemsFromExtras(contentResolver, intent.getExtras());
items_data = itemsFromData(contentResolver, intent.getData());
final JSONObject action = new JSONObject();
action.put("action", translateAction(intent.getAction()));
action.put("exit", readExitOnSent(intent.getExtras()));
action.put("items", items);
action.put("items_text", items_text);
action.put("items_clip", items_clip);
action.put("items_extra", items_extra);
action.put("items_data", items_data);
return action;
}

Expand Down Expand Up @@ -91,9 +88,12 @@ public static JSONArray itemsFromClipData(
JSONObject item = null;
if(uri != null) {
item = toJSONObject(contentResolver, uri);
item.put("subtype", "clip-uri");
item.put("data", getDataFromURI(contentResolver, uri));
} else if(text != null) {
item = new JSONObject();
item.put("type", "text");
item.put("subtype", "clip");
item.put("data", text.toString());
}
if(item == null) {
Expand Down Expand Up @@ -127,6 +127,7 @@ public static JSONArray itemsFromExtras(
return null;
}
final JSONObject[] items = new JSONObject[1];
item.put("subtype", "extra");
items[0] = item;
return new JSONArray(items);
}
Expand All @@ -147,6 +148,7 @@ public static JSONArray itemsFromText(
return null;
}
item.put("type", "text");
item.put("subtype", "text");
item.put("data", text);
if (item == null) {
return null;
Expand All @@ -173,6 +175,7 @@ public static JSONArray itemsFromData(
return null;
}
final JSONObject[] items = new JSONObject[1];
item.put("subtype", "data");
items[0] = item;
return new JSONArray(items);
}
Expand Down
2 changes: 1 addition & 1 deletion bundle/cordova-plugin-turtl-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"main": "www/core.js",
"repository": {
"type": "git",
"url": "git+https://github.com/turtl/mobile.git"
"url": "git+https://github.com/turtl/android.git"
},
"author": "Andrew Lyon <andrew@lyonbros.com>",
"license": "GPLv3"
Expand Down
20 changes: 18 additions & 2 deletions www/share-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,23 @@ function setup_share_to() {
};
const handle_intent = function(intent) {
if(intent.action != 'SEND') return;
var shared = intent.items[0];

// we prioritize the items sent in in this order:
// - text
// - clip
// - extra
// - data
// otherwise things can get screwy
const item_text = (intent.items_text || [])[0];
const item_clip = (intent.items_clip || [])[0];
const item_extra = (intent.items_extra || [])[0];
const item_data = (intent.items_data || [])[0];
var shared = null;
if(!shared && item_text) shared = item_text;
if(!shared && item_clip) shared = item_clip;
if(!shared && item_extra) shared = item_extra;
if(!shared && item_data) shared = item_data;

var note = new Note();
var file = null;
var type = null;
Expand Down Expand Up @@ -39,7 +55,7 @@ function setup_share_to() {
}
loader_promise = new Promise(function(resolve) {
cordova.openwith.load(shared, function(base64_data, _item) {
type = 'file';
type = shared.type.indexOf('image/') === 0 ? 'image' : 'file';
note.get('file').unset('cleared').set({
set: true,
name: filename,
Expand Down

0 comments on commit e2a87ce

Please sign in to comment.