Forráskód Böngészése

Merge branch 'upload-video-to-server'

Wayne 6 éve
szülő
commit
c8c5d82138

+ 8 - 0
build.gradle

@@ -18,7 +18,15 @@ android {
     }
 
     buildTypes {
+        debug {
+            resValue "string", "bomo_cloud_client_id", "7c1b7650fca271fde85b13a36cc4c618901660b4a4e7c0a27951fc63c12b4e1a"
+            resValue "string", "bomo_cloud_client_secret", "6d8a8ea958794a00d2265b8ca35556d328ef9576a22cb5f89dfcb1fdc9ce3535"
+            manifestPlaceholders = [isDebug: true]
+        }
         release {
+            resValue "string", "bomo_cloud_client_id", "e645162f19352e0041f0a0486723f75fe8d44a979f312214986aab22f89a0c3c"
+            resValue "string", "bomo_cloud_client_secret", "3cea632fccc3fc8927a3d92d50bf100d3ca79ab136bc3c56177067b6cf78bc2a"
+            manifestPlaceholders = [isDebug: false]
             minifyEnabled false
             proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
         }

+ 4 - 3
src/main/java/com/bomostory/sceneeditmodule/SceneDrawer.kt

@@ -10,7 +10,6 @@ import com.bomostory.sceneeditmodule.basicdata.Scene
 import kotlin.math.pow
 
 object SceneDrawer {
-    lateinit var actorBitmap: Bitmap
     fun drawScene(context: Context, scene: Scene, trackX: Int, scaleWidth: Int, scaleHeight: Int): Bitmap? {
         var sceneBitmap: Bitmap? = null
 
@@ -22,7 +21,7 @@ object SceneDrawer {
             for (layer in layers) {
                 for (actor in layer.actors) {
                     if (!actor.isDialogue) {
-                        actorBitmap = BitmapFactory.decodeFile(actor.resourcePath)
+                        var actorBitmap = BitmapFactory.decodeFile(actor.resourcePath)
                         actorBitmap = Bitmap.createScaledBitmap(actorBitmap, actor.sideLength, actor.sideHeight, true)
 
                         canvas.save()
@@ -30,7 +29,9 @@ object SceneDrawer {
                         canvas.drawBitmap(actorBitmap, actor.positionX.toFloat(), actor.positionY.toFloat(), Paint())
                         canvas.restore()
 
-                        actorBitmap.recycle()
+                        if (!actorBitmap.isRecycled) {
+                            actorBitmap.recycle()
+                        }
                     } else {
                         canvas.save()
                         canvas.translate(trackX / 2f.pow(layers.indexOf(layer)), 0f)

+ 51 - 19
src/main/java/com/bomostory/sceneeditmodule/screen/movie/MovieEditActivity.kt

@@ -11,6 +11,7 @@ import android.os.Bundle
 import android.os.Environment
 import android.os.Handler
 import android.support.v4.app.DialogFragment
+import android.support.v4.content.FileProvider
 import android.support.v7.app.AppCompatActivity
 import android.view.View
 import android.widget.CompoundButton
@@ -19,14 +20,16 @@ import android.widget.Toast
 import com.bomostory.sceneeditmodule.Config
 import com.bomostory.sceneeditmodule.PdfMaker
 import com.bomostory.sceneeditmodule.SuperMovieMaker
-import com.bomostory.sceneeditmodule.basicdata.*
+import com.bomostory.sceneeditmodule.basicdata.Music
+import com.bomostory.sceneeditmodule.basicdata.Project
+import com.bomostory.sceneeditmodule.basicdata.Scene
 import com.bomostory.sceneeditmodule.screen.movie.music.MusicEditDialog
 import com.bomostory.sceneeditmodule.screen.movie.music.MusicSelectDialog
 import com.bomostory.sceneeditmodule.screen.movie.music.MusicSelectFragment
-import com.bomostory.sceneeditmodule.share.ShareDialog
 import com.bomostory.sceneeditmodule.screen.view.AudioTrackGroupView
 import com.bomostory.sceneeditmodule.screen.view.MovieSelectView
 import com.bomostory.sceneeditmodule.share.ExportPdfDialog
+import com.bomostory.sceneeditmodule.share.ShareDialog
 import com.bomostory.sceneeditmodule.utils.MoviePlayerV2
 import com.bomostory.sceneeditmodule.utils.MusicPlayerV2
 import com.example.exportmedia.MediaHelper
@@ -375,7 +378,46 @@ class MovieEditActivity : AppCompatActivity(),
                 override fun onStopTrackingTouch(seekBar: SeekBar?) {
                 }
             }
-            onClickBomo = Runnable { saveMovie() }
+            onClickBomo = Runnable {
+                saveMovie(object : OnMovieSavedListener {
+                    override fun onSaved(file: File) {
+                        val pd = ProgressDialog(this@MovieEditActivity).apply {
+                            isCancelable = false
+                        }
+                        viewModel.uploadVideo(this@MovieEditActivity, file, project)
+                                .subscribeOn(Schedulers.io())
+                                .observeOn(AndroidSchedulers.mainThread())
+                                .doOnSubscribe { pd.show() }
+                                .doFinally { pd.dismiss() }
+                                .subscribe({
+                                    val context = this@MovieEditActivity
+                                    when (it.errorCode) {
+                                        4013 -> Toast.makeText(context, R.string.share_dialog_upload_failed, Toast.LENGTH_SHORT).show()
+                                        else -> {
+                                            Toast.makeText(context, R.string.share_dialog_upload_suc, Toast.LENGTH_SHORT).show()
+                                        }
+                                    }
+                                }, {
+                                    Toast.makeText(this@MovieEditActivity, R.string.share_dialog_upload_failed, Toast.LENGTH_SHORT).show()
+                                })
+                    }
+                })
+            }
+            onClickMore = Runnable {
+                saveMovie(object : OnMovieSavedListener {
+                    override fun onSaved(file: File) {
+                        val authority = "$packageName"
+                        val context = this@MovieEditActivity
+                        val uri = FileProvider.getUriForFile(context, authority, file)
+
+                        val intent = Intent(Intent.ACTION_SEND)
+                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET)
+                        intent.type = "video/mp4"
+                        intent.putExtra(Intent.EXTRA_STREAM, uri)
+                        startActivity(Intent.createChooser(intent, ""))
+                    }
+                })
+            }
             onClickExportPdf = Runnable { onClickExportPdf() }
             onClickPlay = Runnable {
                 moviePlayer.moviePlayListener = dialogMoviePlayListener
@@ -422,7 +464,11 @@ class MovieEditActivity : AppCompatActivity(),
         }.show(supportFragmentManager)
     }
 
-    private fun saveMovie() {
+    interface OnMovieSavedListener {
+        fun onSaved(file: File)
+    }
+
+    private fun saveMovie(onMovieSavedListener: OnMovieSavedListener) {
         val scaledWidth = resources.getDimensionPixelSize(R.dimen.movie_width)
         val scaleHeight = resources.getDimensionPixelSize(R.dimen.movie_height)
 
@@ -462,21 +508,7 @@ class MovieEditActivity : AppCompatActivity(),
                     AlertDialog.Builder(this).setMessage(msg).show()
                     delayedHide(100)
                 }, {
-                    AlertDialog.Builder(this)
-                            .setMessage("Complete")
-                            .setPositiveButton("Play") { _, _ ->
-                                val uri = Uri.parse(outputFile.absolutePath)
-                                val intent = Intent(Intent.ACTION_VIEW, uri).apply {
-                                    setDataAndType(uri, "video/mp4")
-                                }
-                                if (intent.resolveActivity(packageManager) != null) {
-                                    Intent.createChooser(intent, "").apply {
-                                        startActivity(this)
-                                    }
-                                } else {
-                                    Toast.makeText(this, "No app can play video", Toast.LENGTH_SHORT).show()
-                                }
-                            }.show()
+                    onMovieSavedListener.onSaved(outputFile)
                     delayedHide(100)
                 })
     }

+ 18 - 0
src/main/java/com/bomostory/sceneeditmodule/screen/movie/MovieEditViewModel.kt

@@ -3,12 +3,19 @@ package com.bomostory.sceneeditmodule.screen.movie
 import android.arch.lifecycle.MutableLiveData
 import android.arch.lifecycle.ViewModel
 import android.content.ContentResolver
+import android.content.Context
 import android.media.MediaMetadataRetriever
 import com.bomostory.sceneeditmodule.basicdata.*
 import com.bomostory.sceneeditmodule.utils.TimeUtils
+import com.example.bomocloud.BoMoCloud
+import com.example.bomocloud.video.UploadVideoData
 import com.example.exportmedia.MediaHelper
 import com.example.exportmedia.audio.AudioEditor
+import com.example.tfat.myapplication.R
 import io.reactivex.Observable
+import okhttp3.MediaType
+import okhttp3.MultipartBody
+import okhttp3.RequestBody
 import java.io.File
 
 class MovieEditViewModel : ViewModel() {
@@ -98,4 +105,15 @@ class MovieEditViewModel : ViewModel() {
         return totalPeriod
     }
 
+    fun uploadVideo(context: Context, file: File, project: Project): Observable<UploadVideoData> {
+        val id = context.getString(R.string.bomo_cloud_client_id)
+        val secret = context.getString(R.string.bomo_cloud_client_secret)
+        val author = project.author ?: ""
+        val name = project.name ?: ""
+        val category = 0
+        val filePart = MultipartBody.Part.createFormData("file", file.name, RequestBody.create(MediaType.parse("video/*"), file))
+        return BoMoCloud(id, secret).videoService
+                .uploadVideo(id, secret, author, name, category, filePart)
+    }
+
 }

+ 3 - 1
src/main/res/layout/dialog_share.xml

@@ -124,6 +124,7 @@
 
     <ImageView
         android:id="@+id/iv_shareDialog_youtube"
+        android:visibility="gone"
         android:layout_width="48dp"
         android:layout_height="48dp"
         android:layout_marginLeft="36dp"
@@ -136,6 +137,7 @@
 
     <ImageView
         android:id="@+id/iv_shareDialog_facebook"
+        android:visibility="gone"
         android:layout_width="48dp"
         android:layout_height="48dp"
         android:layout_marginLeft="36dp"
@@ -148,6 +150,7 @@
 
     <ImageView
         android:id="@+id/iv_shareDialog_twitter"
+        android:visibility="gone"
         android:layout_width="48dp"
         android:layout_height="48dp"
         android:layout_marginLeft="36dp"
@@ -166,7 +169,6 @@
         android:layout_marginRight="36dp"
         android:layout_marginTop="16dp"
         android:src="@drawable/ic_btn_more_01"
-        android:tint="@android:color/darker_gray"
         app:layout_constraintLeft_toRightOf="@id/movieView_shareDialog_preview"
         app:layout_constraintTop_toBottomOf="@id/iv_shareDialog_twitter" />
 

+ 3 - 0
src/main/res/values/strings.xml

@@ -22,4 +22,7 @@
     <string name="export_pdf_dialog_radio_foldable_booklet">Foldable Booklet</string>
     <string name="export_pdf_dialog_print">Print</string>
     <string name="export_pdf_dialog_cancel">Cancel</string>
+
+    <string name="share_dialog_upload_suc">Upload success!</string>
+    <string name="share_dialog_upload_failed">Upload failed</string>
 </resources>