From 66457c9f2ec61953d831b132206e8e06e14313bd Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 24 Jan 2017 20:17:36 +0100 Subject: [PATCH] transcode videos before sharing. change storage location --- build.gradle | 1 + src/main/AndroidManifest.xml | 2 + .../persistance/FileBackend.java | 32 +++----- .../services/XmppConnectionService.java | 79 ++++++++++++++++++- .../ui/ConversationActivity.java | 19 ++++- .../conversations/ui/ShareWithActivity.java | 15 +++- .../ui/UiInformableCallback.java | 5 ++ src/main/res/values/strings.xml | 1 + src/main/res/xml/file_paths.xml | 5 +- 9 files changed, 131 insertions(+), 28 deletions(-) create mode 100644 src/main/java/eu/siacs/conversations/ui/UiInformableCallback.java diff --git a/build.gradle b/build.gradle index befefcff6..556a902c2 100644 --- a/build.gradle +++ b/build.gradle @@ -40,6 +40,7 @@ dependencies { compile 'org.whispersystems:axolotl-android:1.3.4' compile 'com.makeramen:roundedimageview:2.2.0' compile "com.wefika:flowlayout:0.4.1" + compile 'net.ypresto.androidtranscoder:android-transcoder:0.2.0' } ext { diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index d6991f880..c43c0429f 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -19,6 +19,8 @@ android:name="android.permission.READ_PHONE_STATE" tools:node="remove" /> + + progressTracker = new ArrayList<>(); + final UiInformableCallback informableCallback; + if (callback instanceof UiInformableCallback) { + informableCallback = (UiInformableCallback) callback; + } else { + informableCallback = null; + } + MediaTranscoder.Listener listener = new MediaTranscoder.Listener() { + @Override + public void onTranscodeProgress(double progress) { + int p = ((int) Math.round(progress * 100) / 20) * 20; + if (!progressTracker.contains(p) && p != 100 && p != 0) { + progressTracker.add(p); + if (informableCallback != null) { + + informableCallback.inform(getString(R.string.transcoding_video_progress, p)); + } + } + } + + @Override + public void onTranscodeCompleted() { + if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) { + getPgpEngine().encrypt(message, callback); + } else { + callback.success(message); + } + } + + @Override + public void onTranscodeCanceled() { + processAsFile(); + } + + @Override + public void onTranscodeFailed(Exception e) { + Log.d(Config.LOGTAG,"video transcoding failed "+e.getMessage()); + processAsFile(); + } + }; + MediaTranscoder.getInstance().transcodeVideo(fileDescriptor, file.getAbsolutePath(), + MediaFormatStrategyPresets.createAndroid720pStrategy(), listener); + } + + @Override + public void run() { + final String mimeType = MimeUtils.guessMimeTypeFromUri(XmppConnectionService.this, uri); + if (mimeType != null && mimeType.startsWith("video/") && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { + try { + processAsVideo(); + } catch (Throwable e) { + processAsFile(); + } + } else { + processAsFile(); + } + + } }); } diff --git a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java index 8a7a27856..65b817322 100644 --- a/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ConversationActivity.java @@ -1543,9 +1543,26 @@ public class ConversationActivity extends XmppActivity } final Toast prepareFileToast = Toast.makeText(getApplicationContext(),getText(R.string.preparing_file), Toast.LENGTH_LONG); prepareFileToast.show(); - xmppConnectionService.attachFileToConversation(conversation, uri, new UiCallback() { + xmppConnectionService.attachFileToConversation(conversation, uri, new UiInformableCallback() { + @Override + public void inform(final String text) { + hidePrepareFileToast(prepareFileToast); + runOnUiThread(new Runnable() { + @Override + public void run() { + replaceToast(text); + } + }); + } + @Override public void success(Message message) { + runOnUiThread(new Runnable() { + @Override + public void run() { + hideToast(); + } + }); hidePrepareFileToast(prepareFileToast); xmppConnectionService.sendMessage(message); } diff --git a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java index f5724fc69..9cb7c87ee 100644 --- a/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/ShareWithActivity.java @@ -59,7 +59,17 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer private Toast mToast; private AtomicInteger attachmentCounter = new AtomicInteger(0); - private UiCallback attachFileCallback = new UiCallback() { + private UiInformableCallback attachFileCallback = new UiInformableCallback() { + + @Override + public void inform(final String text) { + runOnUiThread(new Runnable() { + @Override + public void run() { + replaceToast(text); + } + }); + } @Override public void userInputRequried(PendingIntent pi, Message object) { @@ -293,8 +303,7 @@ public class ShareWithActivity extends XmppActivity implements XmppConnectionSer } else { replaceToast(getString(R.string.preparing_file)); ShareWithActivity.this.xmppConnectionService - .attachFileToConversation(conversation, share.uris.get(0), - attachFileCallback); + .attachFileToConversation(conversation, share.uris.get(0), attachFileCallback); } } }; diff --git a/src/main/java/eu/siacs/conversations/ui/UiInformableCallback.java b/src/main/java/eu/siacs/conversations/ui/UiInformableCallback.java new file mode 100644 index 000000000..9021f9885 --- /dev/null +++ b/src/main/java/eu/siacs/conversations/ui/UiInformableCallback.java @@ -0,0 +1,5 @@ +package eu.siacs.conversations.ui; + +public interface UiInformableCallback extends UiCallback { + void inform(String text); +} diff --git a/src/main/res/values/strings.xml b/src/main/res/values/strings.xml index b56ec72cc..68e08ba49 100644 --- a/src/main/res/values/strings.xml +++ b/src/main/res/values/strings.xml @@ -732,4 +732,5 @@ Automatically delete messages from this device that are older than the configured time frame. Encrypting message Overstepping local retention period. + Compressing video (%s%% completed) diff --git a/src/main/res/xml/file_paths.xml b/src/main/res/xml/file_paths.xml index 6a5e2609c..4147940e8 100644 --- a/src/main/res/xml/file_paths.xml +++ b/src/main/res/xml/file_paths.xml @@ -1,7 +1,8 @@ - + + - + \ No newline at end of file