@@ -8,8 +8,10 @@ import android.content.pm.PackageManager
88import android.content.res.Configuration
99import android.graphics.Bitmap
1010import android.graphics.BitmapFactory
11+ import android.graphics.Canvas
1112import android.graphics.drawable.BitmapDrawable
1213import android.graphics.drawable.ColorDrawable
14+ import android.graphics.drawable.Drawable
1315import android.net.Uri
1416import android.os.Build
1517import android.os.Bundle
@@ -31,16 +33,20 @@ import org.wordpress.android.util.ToastUtils
3133import org.wordpress.aztec.AztecAttributes
3234import org.wordpress.aztec.AztecText
3335import org.wordpress.aztec.HistoryListener
36+ import org.wordpress.aztec.Html
37+ import org.wordpress.aztec.glideloader.GlideVideoThumbnailLoader
3438import org.wordpress.aztec.picassoloader.PicassoImageLoader
3539import org.wordpress.aztec.source.SourceViewEditText
40+ import org.wordpress.aztec.spans.AztecMediaSpan
3641import org.wordpress.aztec.toolbar.AztecToolbar
3742import org.wordpress.aztec.toolbar.AztecToolbarClickListener
3843import org.xml.sax.Attributes
3944import java.io.File
4045
4146class MainActivity : AppCompatActivity (),
4247 AztecText .OnImeBackListener ,
43- AztecText .OnMediaTappedListener ,
48+ AztecText .OnImageTappedListener ,
49+ AztecText .OnVideoTappedListener ,
4450 AztecToolbarClickListener ,
4551 HistoryListener ,
4652 OnRequestPermissionsResultCallback ,
@@ -98,6 +104,8 @@ class MainActivity : AppCompatActivity(),
98104
99105 private val LONG_TEXT = " <br><br>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. "
100106
107+ private val VIDEO = " <video src=\" https://www.w3schools.com/html/mov_bbb.mp4\" />"
108+
101109 private val EXAMPLE =
102110 IMG +
103111 HEADING +
@@ -118,7 +126,8 @@ class MainActivity : AppCompatActivity(),
118126 CODE +
119127 UNKNOWN +
120128 EMOJI +
121- LONG_TEXT
129+ LONG_TEXT +
130+ VIDEO
122131
123132 private val isRunningTest : Boolean by lazy {
124133 try {
@@ -167,8 +176,8 @@ class MainActivity : AppCompatActivity(),
167176 val options = BitmapFactory .Options ()
168177 options.inDensity = DisplayMetrics .DENSITY_DEFAULT
169178 bitmap = BitmapFactory .decodeFile(mediaPath, options)
170- }
171- REQUEST_MEDIA_CAMERA_VIDEO -> {
179+
180+ insertImageAndSimulateUpload(bitmap, mediaPath)
172181 }
173182 REQUEST_MEDIA_PHOTO -> {
174183 mediaPath = data?.data.toString()
@@ -178,27 +187,68 @@ class MainActivity : AppCompatActivity(),
178187 val options = BitmapFactory .Options ()
179188 options.inDensity = DisplayMetrics .DENSITY_DEFAULT
180189 bitmap = BitmapFactory .decodeStream(stream, null , options)
190+
191+ insertImageAndSimulateUpload(bitmap, mediaPath)
192+ }
193+ REQUEST_MEDIA_CAMERA_VIDEO -> {
194+ mediaPath = data?.data.toString()
181195 }
182196 REQUEST_MEDIA_VIDEO -> {
197+ mediaPath = data?.data.toString()
198+
199+ aztec.videoThumbnailGetter?.loadVideoThumbnail(mediaPath, object : Html .VideoThumbnailGetter .Callbacks {
200+ override fun onThumbnailFailed () {
201+ }
202+
203+ override fun onThumbnailLoaded (drawable : Drawable ? ) {
204+ val conf = Bitmap .Config .ARGB_8888 // see other conf types
205+ bitmap = Bitmap .createBitmap(drawable!! .intrinsicWidth, drawable.intrinsicHeight, conf)
206+ val canvas = Canvas (bitmap)
207+ drawable.setBounds(0 , 0 , canvas.width, canvas.height)
208+ drawable.draw(canvas)
209+
210+ insertVideoAndSimulateUpload(bitmap, mediaPath)
211+ }
212+
213+ override fun onThumbnailLoading (drawable : Drawable ? ) {
214+ }
215+
216+ }, this .resources.displayMetrics.widthPixels)
183217 }
184218 }
185-
186- insertMediaAndSimulateUpload(bitmap, mediaPath)
187219 }
188220
189221 super .onActivityResult(requestCode, resultCode, data)
190222 }
191223
192- fun insertMediaAndSimulateUpload (bitmap : Bitmap ? , mediaPath : String ) {
224+ fun insertImageAndSimulateUpload (bitmap : Bitmap ? , mediaPath : String ) {
225+ val (id, attrs) = generateAttributesForMedia(mediaPath, isVideo = false )
226+ val mediaSpan = aztec.insertImage(BitmapDrawable (resources, bitmap), attrs)
227+ insertMediaAndSimulateUpload(id, attrs, mediaSpan)
228+ }
229+
230+ fun insertVideoAndSimulateUpload (bitmap : Bitmap ? , mediaPath : String ) {
231+ val (id, attrs) = generateAttributesForMedia(mediaPath, isVideo = true )
232+ val mediaSpan = aztec.insertVideo(BitmapDrawable (resources, bitmap), attrs)
233+ insertMediaAndSimulateUpload(id, attrs, mediaSpan)
234+ }
235+
236+ private fun generateAttributesForMedia (mediaPath : String , isVideo : Boolean ): Pair <String , AztecAttributes > {
193237 val id = (Math .random() * Int .MAX_VALUE ).toString()
194238
195239 val attrs = AztecAttributes ()
196240 attrs.setValue(" src" , mediaPath) // Temporary source value. Replace with URL after uploaded.
197241 attrs.setValue(" id" , id)
198242 attrs.setValue(" uploading" , " true" )
199243
200- val mediaSpan = aztec.insertMedia(BitmapDrawable (resources, bitmap), attrs)
244+ if (isVideo) {
245+ attrs.setValue(" video" , " true" )
246+ }
247+
248+ return Pair (id, attrs)
249+ }
201250
251+ private fun insertMediaAndSimulateUpload (id : String , attrs : AztecAttributes , mediaSpan : AztecMediaSpan ) {
202252 val predicate = object : AztecText .AttributePredicate {
203253 override fun matches (attrs : Attributes ): Boolean {
204254 return attrs.getValue(" id" ) == id
@@ -227,6 +277,12 @@ class MainActivity : AppCompatActivity(),
227277 if (progress >= 10000 ) {
228278 attrs.removeAttribute(attrs.getIndex(" uploading" ))
229279 aztec.clearOverlays(predicate)
280+
281+ if (attrs.hasAttribute(" video" )) {
282+ attrs.removeAttribute(attrs.getIndex(" video" ))
283+ aztec.setOverlay(predicate, 0 , ContextCompat .getDrawable(this , android.R .drawable.ic_media_play), Gravity .CENTER )
284+ }
285+
230286 aztec.updateElementAttributes(predicate, attrs)
231287 }
232288 }
@@ -253,9 +309,10 @@ class MainActivity : AppCompatActivity(),
253309 aztec = findViewById(R .id.aztec) as AztecText
254310
255311 aztec.imageGetter = PicassoImageLoader (this , aztec)
256- // aztec.imageGetter = GlideImageLoader (this)
312+ aztec.videoThumbnailGetter = GlideVideoThumbnailLoader (this )
257313
258- aztec.setOnMediaTappedListener(this )
314+ aztec.setOnImageTappedListener(this )
315+ aztec.setOnVideoTappedListener(this )
259316
260317 source = findViewById(R .id.source) as SourceViewEditText
261318
@@ -628,15 +685,15 @@ class MainActivity : AppCompatActivity(),
628685 item?.isChecked = (item?.isChecked == false )
629686
630687 when (item?.itemId) {
631- org.wordpress.aztec. R .id.gallery -> {
688+ R .id.gallery -> {
632689 onGalleryMediaOptionSelected()
633690 return true
634691 }
635- org.wordpress.aztec. R .id.photo -> {
692+ R .id.photo -> {
636693 showPhotoMediaDialog()
637694 return true
638695 }
639- org.wordpress.aztec. R .id.video -> {
696+ R .id.video -> {
640697 showVideoMediaDialog()
641698 return true
642699 }
@@ -706,7 +763,21 @@ class MainActivity : AppCompatActivity(),
706763 addVideoMediaDialog!! .show()
707764 }
708765
709- override fun mediaTapped (attrs : AztecAttributes , naturalWidth : Int , naturalHeight : Int ) {
710- ToastUtils .showToast(this , " Media tapped!" )
766+ override fun onImageTapped (attrs : AztecAttributes , naturalWidth : Int , naturalHeight : Int ) {
767+ ToastUtils .showToast(this , " Image tapped!" )
768+ }
769+
770+ override fun onVideoTapped (attrs : AztecAttributes ) {
771+ val url = attrs.getValue(" src" )
772+ url?.let {
773+ try {
774+ val intent = Intent (Intent .ACTION_VIEW , Uri .parse(url))
775+ intent.setDataAndType(Uri .parse(url), " video/*" )
776+ intent.addFlags(Intent .FLAG_GRANT_READ_URI_PERMISSION )
777+ startActivity(intent)
778+ } catch (e: Exception ) {
779+ ToastUtils .showToast(this , " Video tapped!" )
780+ }
781+ }
711782 }
712783}
0 commit comments