honghengqiang 3 年之前
父節點
當前提交
c402316328

+ 0 - 6
.idea/inspectionProfiles/Project_Default.xml

@@ -1,6 +0,0 @@
-<component name="InspectionProjectProfileManager">
-  <profile version="1.0">
-    <option name="myName" value="Project Default" />
-    <inspection_tool class="MarkdownUnresolvedFileReference" enabled="true" level="WARNING" enabled_by_default="true" />
-  </profile>
-</component>

+ 5 - 0
.idea/jarRepositories.xml

@@ -46,5 +46,10 @@
       <option name="name" value="maven3" />
       <option name="url" value="http://lib.gcssloop.com/repository/gcssloop-central/" />
     </remote-repository>
+    <remote-repository>
+      <option name="id" value="maven3" />
+      <option name="name" value="maven3" />
+      <option name="url" value="https://oss.sonatype.org/content/repositories/snapshots/" />
+    </remote-repository>
   </component>
 </project>

+ 2 - 0
baseswago/src/main/java/com/swago/baseswago/baseroom/IRoomInfo.kt

@@ -21,5 +21,7 @@ interface IRoomInfo {
     fun getGameUrl():String
     fun getGameApiBaseUrl():String
     fun getGameType():Int
+    fun getGameVersion():Int
+    fun getGameDownLoadUrl():String
 
 }

+ 5 - 0
baseswago/src/main/java/com/swago/baseswago/constant/UrlConstant.kt

@@ -1,5 +1,8 @@
 package com.swago.baseswago.constant
 
+import com.swago.baseswago.util.AppContext
+import java.io.File
+
 /**
  *@date 2021/8/14 14:06
  *description:
@@ -15,4 +18,6 @@ object UrlConstant {
 
     const val USER_PRIVACY = "http://www.swago.cn/privacyAgreement.html"
 
+    var appGameRootPath = AppContext.getContext().getExternalFilesDir("")?.absolutePath + File.separator + "gameFile"
+
 }

+ 18 - 0
baseswago/src/main/java/com/swago/baseswago/model/live/RoomModel.java

@@ -33,6 +33,8 @@ public class RoomModel {
     private String broadcast_income_coins;
     private int anchor_type;
     private int game_type;//游戏类型(1美女三张)
+    private int game_version;
+    private String game_down_url;
     private String game_icon_url; //游戏图标
     private String game_url;//游戏页面地址
     private String game_api_url;//游戏API请求地址
@@ -149,6 +151,22 @@ public class RoomModel {
         this.game_type = game_type;
     }
 
+    public int getGame_version() {
+        return game_version;
+    }
+
+    public void setGame_version(int game_version) {
+        this.game_version = game_version;
+    }
+
+    public String getGame_down_url() {
+        return game_down_url;
+    }
+
+    public void setGame_down_url(String game_down_url) {
+        this.game_down_url = game_down_url;
+    }
+
     public String getGame_icon_url() {
         return game_icon_url;
     }

+ 102 - 0
baseswago/src/main/java/com/swago/baseswago/util/FileUtil.kt

@@ -0,0 +1,102 @@
+package com.swago.baseswago.util
+
+import java.io.*
+import java.lang.Exception
+import java.util.*
+import java.util.zip.ZipEntry
+import java.util.zip.ZipFile
+
+/**
+ *@date 2022/2/12 11:44
+ *description:
+ */
+object FileUtil {
+
+    fun deleteFile(file: File) {
+        if (file.isDirectory) {
+            file.list()?.forEach {
+                deleteFile(File(file, it))
+            }
+        }
+        file.delete()
+    }
+
+    fun upZipFile(zipFile: String, zipUpPath: String) {
+        if (zipFile.isEmpty()) return
+        if (!zipFile.endsWith(".zip")) {
+            deleteFile(File(zipFile))
+        }
+        val file = File(zipFile)
+        try {
+            val zipFile = ZipFile(file)
+            val zipUpFile = File(zipUpPath)
+            if (!zipUpFile.exists()) {
+                zipUpFile.mkdirs()
+            }
+            val zList: Enumeration<*> = zipFile.entries()
+            var ze: ZipEntry? = null
+            val buf = ByteArray(1024)
+
+            while (zList.hasMoreElements()) {
+                ze = zList.nextElement() as ZipEntry
+                if (ze.isDirectory) {
+                    val tempPath = ze.name
+                    var dirstr = zipUpPath + tempPath
+                    dirstr = String(dirstr.toByteArray(charset("8859_1")))
+                    val f = File(dirstr)
+                    f.mkdir()
+                    continue
+                }
+
+                val tempPath = ze.getName()
+                var readLen = 0
+                val os: OutputStream =
+                    BufferedOutputStream(FileOutputStream(getRealFileName(zipUpPath, tempPath)))
+                val inputStream: InputStream = BufferedInputStream(zipFile.getInputStream(ze))
+                try {
+                    while (inputStream.read(buf, 0, 1024).also { readLen = it } != -1) {
+                        os.write(buf, 0, readLen)
+                    }
+                } catch (e: Exception) {
+                    file.delete()
+                    e.printStackTrace()
+                } finally {
+                    try {
+                        inputStream.close()
+                        os.close()
+                    } catch (e: IOException) {
+                        e.printStackTrace()
+                    }
+                }
+
+            }
+            zipFile.close()
+        }catch (e:Exception){
+            e.printStackTrace()
+        }finally {
+
+        }
+
+
+    }
+
+
+    private fun getRealFileName(baseDir: String, absFileName: String): File {
+        val dirs = absFileName.split("/").toTypedArray()
+        var lastDir = baseDir
+        return if (dirs.size > 1) {
+            for (i in 0 until dirs.size - 1) {
+                lastDir += dirs[i] + "/"
+                val dir = File(lastDir)
+                if (!dir.exists()) {
+                    dir.mkdirs()
+                }
+            }
+            val ret = File(lastDir, dirs[dirs.size - 1])
+            ret
+        } else {
+            File(baseDir, absFileName)
+        }
+    }
+
+}

+ 8 - 0
baseswago/src/main/java/com/swago/baseswago/util/SpUtil.kt

@@ -27,6 +27,14 @@ object SpUtil {
         return getInstance().getString(key,"")
     }
 
+    fun putInt(key: String,value:Int){
+        getInstance().edit().putInt(key,value).apply()
+    }
+
+    fun readInt(key:String):Int{
+        return getInstance().getInt(key,0)
+    }
+
     fun clearSp(){
         getInstance().edit().clear().commit()
     }

+ 2 - 0
room/build.gradle

@@ -52,4 +52,6 @@ dependencies {
     implementation project(':tuikit')
     implementation project(path: ':baseswago')
     kapt "com.alibaba:arouter-compiler:1.2.2"
+    //filedownload
+    api 'com.liulishuo.okdownload:okdownload:1.0.7'
 }

+ 159 - 47
room/src/main/java/com/swago/room/base/BaseComFragment.kt

@@ -1,6 +1,7 @@
 package com.swago.room.base
 
 import android.graphics.Rect
+import android.text.TextUtils
 import android.util.Log
 import android.view.View
 import android.view.ViewGroup
@@ -9,15 +10,18 @@ import androidx.fragment.app.activityViewModels
 import androidx.recyclerview.widget.LinearLayoutManager
 import androidx.recyclerview.widget.RecyclerView
 import com.gyf.immersionbar.ImmersionBar
+import com.liulishuo.okdownload.DownloadListener
+import com.liulishuo.okdownload.DownloadTask
+import com.liulishuo.okdownload.core.breakpoint.BreakpointInfo
+import com.liulishuo.okdownload.core.cause.EndCause
+import com.liulishuo.okdownload.core.cause.ResumeFailedCause
 import com.swago.baseswago.baseroom.IRoomActiveListener
 import com.swago.baseswago.baseroom.RoomTimer
 import com.swago.baseswago.baseroom.SwagoRoomManager
+import com.swago.baseswago.constant.UrlConstant
 import com.swago.baseswago.fragment.BaseXFragment
 import com.swago.baseswago.im.IRoomChat
-import com.swago.baseswago.model.live.gift.IMGiftModel
-import com.swago.baseswago.util.DpPxUtil
-import com.swago.baseswago.util.KeyboardUtils
-import com.swago.baseswago.util.NoDoubleClickListener
+import com.swago.baseswago.util.*
 import com.swago.room.R
 import com.swago.room.adapter.RoomChatAdapter
 import com.swago.room.databinding.FragmentBaseComBinding
@@ -31,24 +35,30 @@ import com.swago.room.inter.IHeader
 import com.swago.room.piaotiao.WaftManager
 import com.swago.room.vm.MsgVm
 import com.swago.room.vm.RoomVm
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.GlobalScope
+import kotlinx.coroutines.launch
+import java.io.File
+import java.lang.Exception
 import java.util.concurrent.CopyOnWriteArrayList
 
 /**
  *@date 2021/10/8 16:43
  *description:
  */
-abstract class BaseComFragment<T:FragmentBaseComBinding> : BaseXFragment<T>(),IRoomActiveListener,
+abstract class BaseComFragment<T : FragmentBaseComBinding> : BaseXFragment<T>(),
+    IRoomActiveListener,
     RoomTimer.TimeTickListener {
 
     val roomVm by activityViewModels<RoomVm>()
     val msgVm by activityViewModels<MsgVm>()
 
-    abstract val iHeader : IHeader
-    abstract val iFooter : IFooter
+    abstract val iHeader: IHeader
+    abstract val iFooter: IFooter
 
-    var dialog:SendMsgDialog? = null
-    var gameDialog:GamePlayDialog? = null
-    var msgListDialog:MessageListDialog? = null
+    var dialog: SendMsgDialog? = null
+    var gameDialog: GamePlayDialog? = null
+    var msgListDialog: MessageListDialog? = null
 
     private val chatAdapter by lazy {
         RoomChatAdapter()
@@ -95,19 +105,19 @@ abstract class BaseComFragment<T:FragmentBaseComBinding> : BaseXFragment<T>(),IR
         binding.rv.adapter = chatAdapter
         chatAdapter.setNewData(dataChatList)
         context?.let {
-            svgPlayerManager.init(it,binding.xSvgaPlayer)
+            svgPlayerManager.init(it, binding.xSvgaPlayer)
         }
 
         activity?.let {
-            KeyboardUtils.registerSoftInputChangedListener(it.window,listener)
+            KeyboardUtils.registerSoftInputChangedListener(it.window, listener)
         }
     }
 
     var listener = KeyboardUtils.OnSoftInputChangedListener {
-        if (it==0){
-            setChatRvPop(false,it)
-        }else{
-            setChatRvPop(true,it)
+        if (it == 0) {
+            setChatRvPop(false, it)
+        } else {
+            setChatRvPop(true, it)
         }
     }
 
@@ -147,31 +157,33 @@ abstract class BaseComFragment<T:FragmentBaseComBinding> : BaseXFragment<T>(),IR
     }
 
 
-    private var keyboardUpRvLayoutParams:ViewGroup.LayoutParams? = null
-    private var keyboardCloseRvLayoutParams:ViewGroup.LayoutParams? = null
-    private fun setChatRvPop(isPopUp:Boolean,height:Int){
-        if (isPopUp){
-            if (keyboardUpRvLayoutParams == null){
-                keyboardUpRvLayoutParams = ConstraintLayout.LayoutParams(0,DpPxUtil.dip2px(170f)).apply {
-                    bottomToBottom = R.id.cl
-                    startToStart = R.id.cl
-                    endToEnd = R.id.cl
-                    marginStart = DpPxUtil.dip2px(10f)
-                    marginEnd = DpPxUtil.dip2px(10f)
-                    bottomMargin = height + DpPxUtil.dip2px(55f)
-                }
+    private var keyboardUpRvLayoutParams: ViewGroup.LayoutParams? = null
+    private var keyboardCloseRvLayoutParams: ViewGroup.LayoutParams? = null
+    private fun setChatRvPop(isPopUp: Boolean, height: Int) {
+        if (isPopUp) {
+            if (keyboardUpRvLayoutParams == null) {
+                keyboardUpRvLayoutParams =
+                    ConstraintLayout.LayoutParams(0, DpPxUtil.dip2px(170f)).apply {
+                        bottomToBottom = R.id.cl
+                        startToStart = R.id.cl
+                        endToEnd = R.id.cl
+                        marginStart = DpPxUtil.dip2px(10f)
+                        marginEnd = DpPxUtil.dip2px(10f)
+                        bottomMargin = height + DpPxUtil.dip2px(55f)
+                    }
             }
             binding.rv.layoutParams = keyboardUpRvLayoutParams
-        }else{
-            if (keyboardCloseRvLayoutParams == null){
-                keyboardCloseRvLayoutParams = ConstraintLayout.LayoutParams(0,DpPxUtil.dip2px(170f)).apply {
-                    bottomToBottom = R.id.cl
-                    startToStart = R.id.cl
-                    endToEnd = R.id.cl
-                    marginStart = DpPxUtil.dip2px(10f)
-                    marginEnd = DpPxUtil.dip2px(10f)
-                    bottomMargin = DpPxUtil.dip2px(70f)
-                }
+        } else {
+            if (keyboardCloseRvLayoutParams == null) {
+                keyboardCloseRvLayoutParams =
+                    ConstraintLayout.LayoutParams(0, DpPxUtil.dip2px(170f)).apply {
+                        bottomToBottom = R.id.cl
+                        startToStart = R.id.cl
+                        endToEnd = R.id.cl
+                        marginStart = DpPxUtil.dip2px(10f)
+                        marginEnd = DpPxUtil.dip2px(10f)
+                        bottomMargin = DpPxUtil.dip2px(70f)
+                    }
             }
             binding.rv.layoutParams = keyboardCloseRvLayoutParams
         }
@@ -187,23 +199,29 @@ abstract class BaseComFragment<T:FragmentBaseComBinding> : BaseXFragment<T>(),IR
         SwagoRoomManager.removeListener(this)
     }
 
-    fun openSendMessageDialog(){
+    fun openSendMessageDialog() {
         dialog = SendMsgDialog()
         dialog?.addSenderMsgToRoomChatList = {
             addChatMsgToRv(it)
         }
-        dialog?.show(childFragmentManager,"SendMsgDialog")
+        dialog?.show(childFragmentManager, "SendMsgDialog")
     }
 
-    fun openMessageListDialog(){
+    fun openMessageListDialog() {
         msgListDialog = MessageListDialog()
-        msgListDialog?.show(childFragmentManager,"MessageListDialog")
+        msgListDialog?.show(childFragmentManager, "MessageListDialog")
     }
 
-    fun openGameDialog(){
+    fun openGameDialog() {
+
         SwagoRoomManager.iRoomInfo?.let {
-            gameDialog = GamePlayDialog.newInstance(it.getGameUrl())
-            gameDialog?.show(childFragmentManager,"GamePlayDialog")
+            val download = checkGameVersion(it.getGameType().toString(),it.getGameDownLoadUrl(),it.getGameVersion().toString())
+            if (download){
+                gameDialog = GamePlayDialog.newInstance("file://" + UrlConstant.appGameRootPath + File.separator + it.getGameType() + File.separator + "index.html")
+            }else{
+                gameDialog = GamePlayDialog.newInstance(it.getGameUrl())
+            }
+            gameDialog?.show(childFragmentManager, "GamePlayDialog")
         }
     }
 
@@ -235,7 +253,7 @@ abstract class BaseComFragment<T:FragmentBaseComBinding> : BaseXFragment<T>(),IR
     }
 
     override fun onTimeCount(time: Int) {
-        if (time%30 == 0){
+        if (time % 30 == 0) {
             SwagoRoomManager.iRoomInfo?.let {
                 roomVm.loadRoomData(it.getRoomId())
             }
@@ -243,4 +261,98 @@ abstract class BaseComFragment<T:FragmentBaseComBinding> : BaseXFragment<T>(),IR
         }
     }
 
+
+    private fun checkGameVersion(gameId: String, gamePackageUrl: String, gameVersionCode: String): Boolean {
+        if (gamePackageUrl.isEmpty() || gameVersionCode.isEmpty()) {
+            return false
+        }
+
+        val zipNames = gamePackageUrl.split("/")
+        val gameName = zipNames.last()
+        if (TextUtils.isEmpty(UrlConstant.appGameRootPath) || !File(UrlConstant.appGameRootPath + File.separator + gameId).exists()) {
+            downLoadGamePackage(gameId, gamePackageUrl, gameVersionCode, gameName)
+            return false
+        } else {
+            return if (!File(UrlConstant.appGameRootPath + File.separator + gameId + File.separator + gameName).exists()) {
+                downLoadGamePackage(gameId, gamePackageUrl, gameVersionCode, gameName)
+                false
+            } else {
+                val localGameVersionCode = SpUtil.readInt(gameId)
+                if (localGameVersionCode == 0 || (gameVersionCode.isEmpty() && localGameVersionCode < gameVersionCode.toInt())) {
+                    downLoadGamePackage(gameId, gamePackageUrl, gameVersionCode, gameName)
+                    false
+                } else {
+                    true
+                }
+            }
+        }
+
+    }
+
+    private fun downLoadGamePackage(
+        gameId: String,
+        gamePackageUrl: String,
+        gameVersionCode: String,
+        name: String
+    ) {
+        val path = UrlConstant.appGameRootPath + File.separator + gameId
+        val file = File(path)
+        if (file.exists()) {
+            file.delete()
+        }
+        file.mkdirs()
+
+        val task = DownloadTask.Builder(gamePackageUrl,path,name).build()
+        task.enqueue(object:DownloadListener{
+            override fun taskStart(task: DownloadTask) {}
+
+            override fun connectTrialStart(
+                task: DownloadTask,
+                requestHeaderFields: MutableMap<String, MutableList<String>>
+            ) {}
+
+            override fun connectTrialEnd(
+                task: DownloadTask,
+                responseCode: Int,
+                responseHeaderFields: MutableMap<String, MutableList<String>>
+            ) {}
+
+            override fun downloadFromBeginning(
+                task: DownloadTask,
+                info: BreakpointInfo,
+                cause: ResumeFailedCause
+            ) {}
+
+            override fun downloadFromBreakpoint(task: DownloadTask, info: BreakpointInfo) {}
+
+            override fun connectStart(
+                task: DownloadTask,
+                blockIndex: Int,
+                requestHeaderFields: MutableMap<String, MutableList<String>>
+            ) {}
+
+            override fun connectEnd(
+                task: DownloadTask,
+                blockIndex: Int,
+                responseCode: Int,
+                responseHeaderFields: MutableMap<String, MutableList<String>>
+            ) {}
+
+            override fun fetchStart(task: DownloadTask, blockIndex: Int, contentLength: Long) {}
+
+            override fun fetchProgress(task: DownloadTask, blockIndex: Int, increaseBytes: Long) {}
+
+            override fun fetchEnd(task: DownloadTask, blockIndex: Int, contentLength: Long) {}
+
+            override fun taskEnd(task: DownloadTask, cause: EndCause, realCause: Exception?) {
+                GlobalScope.launch(Dispatchers.IO) {
+                    val packagePath = path + File.separator + name
+                    FileUtil.upZipFile(packagePath,path + File.separator)
+                    SpUtil.putInt(gameId, gameVersionCode.toInt())
+                }
+            }
+
+        })
+    }
+
 }

+ 8 - 0
room/src/main/java/com/swago/room/bean/UserRoomModel.kt

@@ -91,4 +91,12 @@ class UserRoomModel :  IRoomInfo{
     override fun getGameType(): Int {
         return roomModel?.game_type?:0
     }
+
+    override fun getGameVersion(): Int {
+        return roomModel?.game_version?:0
+    }
+
+    override fun getGameDownLoadUrl(): String {
+        return roomModel?.game_down_url?:""
+    }
 }