mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Merge pull request #2571 from deltachat/adb/webxdc-export
add webxdc.sendToChat() API
This commit is contained in:
@@ -39,5 +39,28 @@ window.webxdc = (() => {
|
||||
sendUpdate: (payload, descr) => {
|
||||
InternalJSApi.sendStatusUpdate(JSON.stringify(payload), descr);
|
||||
},
|
||||
|
||||
sendToChat: async (message) => {
|
||||
const data = {};
|
||||
if (!message.text && !message.file) {
|
||||
return Promise.reject("Invalid empty message, at least one of text or file should be provided");
|
||||
}
|
||||
if (message.text) {
|
||||
data.text = message.text;
|
||||
}
|
||||
if (message.file) {
|
||||
if (!message.file.name || typeof message.file.base64 !== 'string') {
|
||||
return Promise.reject("provided file is invalid, you need to set both name and base64 content");
|
||||
}
|
||||
data.base64 = message.file.base64;
|
||||
data.name = message.file.name;
|
||||
}
|
||||
|
||||
|
||||
const errorMsg = InternalJSApi.sendToChat(JSON.stringify(data));
|
||||
if (errorMsg) {
|
||||
return Promise.reject(errorMsg);
|
||||
}
|
||||
},
|
||||
};
|
||||
})();
|
||||
|
||||
@@ -10,6 +10,7 @@ import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Base64;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
@@ -337,5 +338,34 @@ public class WebxdcActivity extends WebViewActivity implements DcEventCenter.DcE
|
||||
Log.i(TAG, "getStatusUpdates");
|
||||
return WebxdcActivity.this.dcContext.getWebxdcStatusUpdates(WebxdcActivity.this.dcAppMsg.getId(), lastKnownSerial );
|
||||
}
|
||||
|
||||
@JavascriptInterface
|
||||
public String sendToChat(String message) {
|
||||
Log.i(TAG, "sendToChat");
|
||||
try {
|
||||
JSONObject jsonObject = new JSONObject(message);
|
||||
|
||||
String text = null;
|
||||
byte[] data = null;
|
||||
String name = null;
|
||||
if (jsonObject.has("base64")) {
|
||||
data = Base64.decode(jsonObject.getString("base64"), Base64.NO_WRAP | Base64.NO_PADDING);
|
||||
name = jsonObject.getString("name");
|
||||
}
|
||||
if (jsonObject.has("text")) {
|
||||
text = jsonObject.getString("text");
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(text) && TextUtils.isEmpty(name)) {
|
||||
return "provided file is invalid, you need to set both name and base64 content";
|
||||
}
|
||||
|
||||
DcHelper.share(WebxdcActivity.this, data, "application/octet-stream", name, text);
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return e.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,10 @@ import com.b44t.messenger.rpc.Rpc;
|
||||
import org.thoughtcrime.securesms.ApplicationContext;
|
||||
import org.thoughtcrime.securesms.BuildConfig;
|
||||
import org.thoughtcrime.securesms.R;
|
||||
import org.thoughtcrime.securesms.ShareActivity;
|
||||
import org.thoughtcrime.securesms.database.model.ThreadRecord;
|
||||
import org.thoughtcrime.securesms.notifications.NotificationCenter;
|
||||
import org.thoughtcrime.securesms.providers.PersistentBlobProvider;
|
||||
import org.thoughtcrime.securesms.recipients.Recipient;
|
||||
import org.thoughtcrime.securesms.util.MediaUtil;
|
||||
|
||||
@@ -311,6 +313,23 @@ public class DcHelper {
|
||||
}
|
||||
}
|
||||
|
||||
public static void share(Context activity, byte[] data, String mimeType, String fileName, String text) {
|
||||
Intent intent = new Intent(activity, ShareActivity.class);
|
||||
intent.setAction(Intent.ACTION_SEND);
|
||||
|
||||
if (data != null) {
|
||||
Uri uri = PersistentBlobProvider.getInstance().create(activity, data, mimeType, fileName);
|
||||
intent.setType(mimeType);
|
||||
intent.putExtra(Intent.EXTRA_STREAM, uri);
|
||||
}
|
||||
|
||||
if (text != null) {
|
||||
intent.putExtra(Intent.EXTRA_TEXT, text);
|
||||
}
|
||||
|
||||
activity.startActivity(intent);
|
||||
}
|
||||
|
||||
private static void startActivity(Activity activity, Intent intent) {
|
||||
// request for permission to install apks on API 26+ if intent mimetype is an apk
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
|
||||
|
||||
Reference in New Issue
Block a user