3636import org .wordpress .android .util .AppLog .T ;
3737import org .wordpress .android .util .JSONUtils ;
3838import org .wordpress .android .util .ProfilingUtils ;
39+ import org .wordpress .android .util .ShortcodeUtils ;
3940import org .wordpress .android .util .StringUtils ;
4041import org .wordpress .android .util .ToastUtils ;
4142import org .wordpress .android .util .UrlUtils ;
@@ -93,7 +94,7 @@ public class EditorFragment extends EditorFragmentAbstract implements View.OnCli
9394
9495 private ConcurrentHashMap <String , MediaFile > mWaitingMediaFiles ;
9596 private Set <MediaGallery > mWaitingGalleries ;
96- private Set <String > mUploadingMediaIds ;
97+ private Map <String , MediaType > mUploadingMedia ;
9798 private Set <String > mFailedMediaIds ;
9899 private MediaGallery mUploadingMediaGallery ;
99100
@@ -137,7 +138,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa
137138
138139 mWaitingMediaFiles = new ConcurrentHashMap <>();
139140 mWaitingGalleries = Collections .newSetFromMap (new ConcurrentHashMap <MediaGallery , Boolean >());
140- mUploadingMediaIds = new HashSet <>();
141+ mUploadingMedia = new HashMap <>();
141142 mFailedMediaIds = new HashSet <>();
142143
143144 // -- WebView configuration
@@ -234,7 +235,7 @@ public void onResume() {
234235 @ Override
235236 public void onDetach () {
236237 // Soft cancel (delete flag off) all media uploads currently in progress
237- for (String mediaId : mUploadingMediaIds ) {
238+ for (String mediaId : mUploadingMedia . keySet () ) {
238239 mEditorFragmentListener .onMediaUploadCancelClicked (mediaId , false );
239240 }
240241 super .onDetach ();
@@ -411,7 +412,7 @@ public void onClick(View v) {
411412 mEditorFragmentListener .onTrackableEvent (TrackableEvent .HTML_BUTTON_TAPPED );
412413
413414 // Don't switch to HTML mode if currently uploading media
414- if (!mUploadingMediaIds .isEmpty ()) {
415+ if (!mUploadingMedia .isEmpty ()) {
415416 ((ToggleButton ) v ).setChecked (false );
416417
417418 if (isAdded ()) {
@@ -753,17 +754,38 @@ public void appendMediaFile(final MediaFile mediaFile, final String mediaUrl, Im
753754 return ;
754755 }
755756
757+ final String safeMediaUrl = Utils .escapeQuotes (mediaUrl );
758+
756759 mWebView .post (new Runnable () {
757760 @ Override
758761 public void run () {
759762 if (URLUtil .isNetworkUrl (mediaUrl )) {
760763 String mediaId = mediaFile .getMediaId ();
761- mWebView .execJavaScriptFromString ("ZSSEditor.insertImage('" + mediaUrl + "', '" + mediaId + "');" );
764+ if (mediaFile .isVideo ()) {
765+ String posterUrl = Utils .escapeQuotes (StringUtils .notNullStr (mediaFile .getThumbnailURL ()));
766+ String videoPressId = ShortcodeUtils .getVideoPressIdFromShortCode (
767+ mediaFile .getVideoPressShortCode ());
768+
769+ mWebView .execJavaScriptFromString ("ZSSEditor.insertVideo('" + safeMediaUrl + "', '" +
770+ posterUrl + "', '" + videoPressId + "');" );
771+ } else {
772+ mWebView .execJavaScriptFromString ("ZSSEditor.insertImage('" + safeMediaUrl + "', '" + mediaId +
773+ "');" );
774+ }
762775 } else {
763776 String id = mediaFile .getMediaId ();
764- mWebView .execJavaScriptFromString ("ZSSEditor.insertLocalImage(" + id + ", '" + mediaUrl + "');" );
765- mWebView .execJavaScriptFromString ("ZSSEditor.setProgressOnImage(" + id + ", " + 0 + ");" );
766- mUploadingMediaIds .add (id );
777+ if (mediaFile .isVideo ()) {
778+ String posterUrl = Utils .escapeQuotes (StringUtils .notNullStr (mediaFile .getThumbnailURL ()));
779+ mWebView .execJavaScriptFromString ("ZSSEditor.insertLocalVideo(" + id + ", '" + posterUrl +
780+ "');" );
781+ mWebView .execJavaScriptFromString ("ZSSEditor.setProgressOnVideo(" + id + ", " + 0 + ");" );
782+ mUploadingMedia .put (id , MediaType .VIDEO );
783+ } else {
784+ mWebView .execJavaScriptFromString ("ZSSEditor.insertLocalImage(" + id + ", '" + safeMediaUrl +
785+ "');" );
786+ mWebView .execJavaScriptFromString ("ZSSEditor.setProgressOnImage(" + id + ", " + 0 + ");" );
787+ mUploadingMedia .put (id , MediaType .IMAGE );
788+ }
767789 }
768790 }
769791 });
@@ -796,8 +818,8 @@ public void setUrlForVideoPressId(final String videoId, final String videoUrl, f
796818 mWebView .post (new Runnable () {
797819 @ Override
798820 public void run () {
799- mWebView .execJavaScriptFromString ("ZSSEditor.setVideoPressLinks('" + videoId + "', '" +
800- videoUrl + "', '" + posterUrl + "');" );
821+ mWebView .execJavaScriptFromString ("ZSSEditor.setVideoPressLinks('" + videoId + "', '" +
822+ Utils . escapeQuotes ( videoUrl ) + "', '" + Utils . escapeQuotes ( posterUrl ) + "');" );
801823 }
802824 });
803825 }
@@ -823,38 +845,67 @@ public void setContentPlaceholder(CharSequence placeholderText) {
823845 }
824846
825847 @ Override
826- public void onMediaUploadSucceeded (final String mediaId , final String remoteId , final String remoteUrl ) {
827- mWebView .post (new Runnable () {
828- @ Override
829- public void run () {
830- mWebView .execJavaScriptFromString ("ZSSEditor.replaceLocalImageWithRemoteImage(" + mediaId + ", '" +
831- remoteId + "', '" + remoteUrl + "');" );
832- mUploadingMediaIds .remove (mediaId );
833- }
834- });
848+ public void onMediaUploadSucceeded (final String localMediaId , final MediaFile mediaFile ) {
849+ final MediaType mediaType = mUploadingMedia .get (localMediaId );
850+ if (mediaType != null ) {
851+ mWebView .post (new Runnable () {
852+ @ Override
853+ public void run () {
854+ String remoteUrl = Utils .escapeQuotes (mediaFile .getFileURL ());
855+ if (mediaType .equals (MediaType .IMAGE )) {
856+ String remoteMediaId = mediaFile .getMediaId ();
857+ mWebView .execJavaScriptFromString ("ZSSEditor.replaceLocalImageWithRemoteImage(" + localMediaId +
858+ ", '" + remoteMediaId + "', '" + remoteUrl + "');" );
859+ } else if (mediaType .equals (MediaType .VIDEO )) {
860+ String posterUrl = Utils .escapeQuotes (StringUtils .notNullStr (mediaFile .getThumbnailURL ()));
861+ String videoPressId = ShortcodeUtils .getVideoPressIdFromShortCode (
862+ mediaFile .getVideoPressShortCode ());
863+ mWebView .execJavaScriptFromString ("ZSSEditor.replaceLocalVideoWithRemoteVideo(" + localMediaId +
864+ ", '" + remoteUrl + "', '" + posterUrl + "', '" + videoPressId + "');" );
865+ }
866+ mUploadingMedia .remove (localMediaId );
867+ }
868+ });
869+ }
835870 }
836871
837872 @ Override
838873 public void onMediaUploadProgress (final String mediaId , final float progress ) {
839- mWebView .post (new Runnable () {
840- @ Override
841- public void run () {
842- String progressString = String .format (Locale .US , "%.1f" , progress );
843- mWebView .execJavaScriptFromString ("ZSSEditor.setProgressOnImage(" + mediaId + ", " +
844- progressString + ");" );
845- }
846- });
874+ final MediaType mediaType = mUploadingMedia .get (mediaId );
875+ if (mediaType != null ) {
876+ mWebView .post (new Runnable () {
877+ @ Override
878+ public void run () {
879+ String progressString = String .format (Locale .US , "%.1f" , progress );
880+ if (mediaType .equals (MediaType .IMAGE )) {
881+ mWebView .execJavaScriptFromString ("ZSSEditor.setProgressOnImage(" + mediaId + ", " +
882+ progressString + ");" );
883+ } else if (mediaType .equals (MediaType .VIDEO )) {
884+ mWebView .execJavaScriptFromString ("ZSSEditor.setProgressOnVideo(" + mediaId + ", " +
885+ progressString + ");" );
886+ }
887+ }
888+ });
889+ }
847890 }
848891
849892 @ Override
850893 public void onMediaUploadFailed (final String mediaId , final String errorMessage ) {
851894 mWebView .post (new Runnable () {
852895 @ Override
853896 public void run () {
854- mWebView .execJavaScriptFromString ("ZSSEditor.markImageUploadFailed(" + mediaId + ", '"
855- + errorMessage .replace ("'" , "\\ '" ).replace ("\" " , "\\ \" " ) + "');" );
897+ MediaType mediaType = mUploadingMedia .get (mediaId );
898+ switch (mediaType ) {
899+ case IMAGE :
900+ mWebView .execJavaScriptFromString ("ZSSEditor.markImageUploadFailed(" + mediaId + ", '"
901+ + Utils .escapeQuotes (errorMessage ) + "');" );
902+ break ;
903+ case VIDEO :
904+ mWebView .execJavaScriptFromString ("ZSSEditor.markVideoUploadFailed(" + mediaId + ", '"
905+ + Utils .escapeQuotes (errorMessage ) + "');" );
906+ }
856907 mFailedMediaIds .add (mediaId );
857- mUploadingMediaIds .remove (mediaId );
908+ mUploadingMedia .remove (mediaId );
858909 }
859910 });
860911 }
@@ -900,11 +951,11 @@ public void run() {
900951
901952 // If there are images that are still in progress (because the editor exited before they completed),
902953 // set them to failed, so the user can restart them (otherwise they will stay stuck in 'uploading' mode)
903- mWebView .execJavaScriptFromString ("ZSSEditor.markAllUploadingImagesAsFailed ('"
954+ mWebView .execJavaScriptFromString ("ZSSEditor.markAllUploadingMediaAsFailed ('"
904955 + getString (R .string .tap_to_try_again ) + "');" );
905956
906957 // Update the list of failed media uploads
907- mWebView .execJavaScriptFromString ("ZSSEditor.getFailedImages ();" );
958+ mWebView .execJavaScriptFromString ("ZSSEditor.getFailedMedia ();" );
908959
909960 hideActionBarIfNeeded ();
910961
@@ -980,7 +1031,11 @@ public void run() {
9801031 });
9811032 }
9821033
983- public void onMediaTapped (final String mediaId , String url , final JSONObject meta , String uploadStatus ) {
1034+ public void onMediaTapped (final String mediaId , final MediaType mediaType , final JSONObject meta , String uploadStatus ) {
1035+ if (mediaType == null ) {
1036+ return ;
1037+ }
1038+
9841039 switch (uploadStatus ) {
9851040 case "uploading" :
9861041 // Display 'cancel upload' dialog
@@ -993,8 +1048,14 @@ public void onClick(DialogInterface dialog, int id) {
9931048 mWebView .post (new Runnable () {
9941049 @ Override
9951050 public void run () {
996- mWebView .execJavaScriptFromString ("ZSSEditor.removeImage(" + mediaId + ");" );
997- mUploadingMediaIds .remove (mediaId );
1051+ switch (mediaType ) {
1052+ case IMAGE :
1053+ mWebView .execJavaScriptFromString ("ZSSEditor.removeImage(" + mediaId + ");" );
1054+ break ;
1055+ case VIDEO :
1056+ mWebView .execJavaScriptFromString ("ZSSEditor.removeVideo(" + mediaId + ");" );
1057+ }
1058+ mUploadingMedia .remove (mediaId );
9981059 }
9991060 });
10001061 dialog .dismiss ();
@@ -1017,15 +1078,30 @@ public void onClick(DialogInterface dialog, int id) {
10171078 mWebView .post (new Runnable () {
10181079 @ Override
10191080 public void run () {
1020- mWebView .execJavaScriptFromString ("ZSSEditor.unmarkImageUploadFailed(" + mediaId + ");" );
1021- mWebView .execJavaScriptFromString ("ZSSEditor.setProgressOnImage(" + mediaId + ", " + 0 + ");" );
1081+ switch (mediaType ) {
1082+ case IMAGE :
1083+ mWebView .execJavaScriptFromString ("ZSSEditor.unmarkImageUploadFailed(" + mediaId
1084+ + ");" );
1085+ mWebView .execJavaScriptFromString ("ZSSEditor.setProgressOnImage(" + mediaId + ", "
1086+ + 0 + ");" );
1087+ break ;
1088+ case VIDEO :
1089+ mWebView .execJavaScriptFromString ("ZSSEditor.unmarkVideoUploadFailed(" + mediaId
1090+ + ");" );
1091+ mWebView .execJavaScriptFromString ("ZSSEditor.setProgressOnVideo(" + mediaId + ", "
1092+ + 0 + ");" );
1093+ }
10221094 mFailedMediaIds .remove (mediaId );
1023- mUploadingMediaIds . add (mediaId );
1095+ mUploadingMedia . put (mediaId , mediaType );
10241096 }
10251097 });
10261098 break ;
10271099 default :
1028- // Show media options fragment
1100+ if (!mediaType .equals (MediaType .IMAGE )) {
1101+ return ;
1102+ }
1103+
1104+ // Only show image options fragment for image taps
10291105 FragmentManager fragmentManager = getFragmentManager ();
10301106
10311107 if (fragmentManager .findFragmentByTag (ImageSettingsDialogFragment .IMAGE_SETTINGS_DIALOG_TAG ) != null ) {
@@ -1125,7 +1201,7 @@ public void onGetHtmlResponse(Map<String, String> inputArgs) {
11251201 mJavaScriptResult = inputArgs .get ("result" );
11261202 mGetSelectedTextCountDownLatch .countDown ();
11271203 break ;
1128- case "getFailedImages " :
1204+ case "getFailedMedia " :
11291205 String [] mediaIds = inputArgs .get ("ids" ).split ("," );
11301206 for (String mediaId : mediaIds ) {
11311207 if (!mediaId .equals ("" )) {
0 commit comments