|
|
@@ -0,0 +1,146 @@
|
|
|
+package com.yingyangfly.home.dialog
|
|
|
+
|
|
|
+import android.annotation.SuppressLint
|
|
|
+import android.content.Context
|
|
|
+import android.os.Bundle
|
|
|
+import android.view.LayoutInflater
|
|
|
+import android.view.MotionEvent
|
|
|
+import android.view.View
|
|
|
+import android.view.ViewGroup
|
|
|
+import android.widget.ProgressBar
|
|
|
+import androidx.appcompat.widget.AppCompatButton
|
|
|
+import androidx.appcompat.widget.AppCompatTextView
|
|
|
+import androidx.fragment.app.DialogFragment
|
|
|
+import com.gyf.immersionbar.BarHide
|
|
|
+import com.gyf.immersionbar.ktx.immersionBar
|
|
|
+import com.yingyang.home.R
|
|
|
+import com.yingyangfly.baselib.ext.setOnSingleClickListener
|
|
|
+import com.yingyangfly.baselib.ext.show
|
|
|
+import com.yingyangfly.baselib.ext.toast
|
|
|
+import com.yingyangfly.baselib.utils.ViewTool
|
|
|
+import com.yingyangfly.home.updater.AppUpdater
|
|
|
+import com.yingyangfly.home.updater.UpdateConfig
|
|
|
+import com.yingyangfly.home.updater.callback.UpdateCallback
|
|
|
+import com.yingyangfly.home.updater.http.OkHttpManager
|
|
|
+import java.io.File
|
|
|
+
|
|
|
+/**
|
|
|
+ * App升级弹窗
|
|
|
+ */
|
|
|
+class DownlaodAppFragment : DialogFragment(), View.OnTouchListener {
|
|
|
+
|
|
|
+ private var upgradeBtn: AppCompatButton? = null
|
|
|
+ private var progressBar: ProgressBar? = null
|
|
|
+ private var tvProgress: AppCompatTextView? = null
|
|
|
+
|
|
|
+ private var content: Context? = null
|
|
|
+ private var url: String = ""
|
|
|
+
|
|
|
+ fun setDownloadUrl(content: Context, url: String) {
|
|
|
+ this.content = content
|
|
|
+ this.url = url
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
+ super.onCreate(savedInstanceState)
|
|
|
+ isCancelable = false
|
|
|
+ dialog?.setCanceledOnTouchOutside(true)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onStart() {
|
|
|
+ super.onStart()
|
|
|
+ dialog?.window?.setLayout(
|
|
|
+ ViewGroup.LayoutParams.MATCH_PARENT,
|
|
|
+ ViewGroup.LayoutParams.MATCH_PARENT
|
|
|
+ )
|
|
|
+ dialog?.window?.setBackgroundDrawableResource(R.color.transparent)
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onCreateView(
|
|
|
+ inflater: LayoutInflater,
|
|
|
+ container: ViewGroup?,
|
|
|
+ savedInstanceState: Bundle?
|
|
|
+ ): View? {
|
|
|
+ immersionBar {
|
|
|
+ hideBar(BarHide.FLAG_HIDE_BAR)
|
|
|
+ navigationBarColor(R.color.transparent)
|
|
|
+ }
|
|
|
+ val rootView = ViewTool.inflateFragmentPixels(
|
|
|
+ activity, R.layout.fragment_down_load_app, container, 1194, 834
|
|
|
+ )
|
|
|
+ findId(rootView)
|
|
|
+ init()
|
|
|
+ return rootView
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun findId(rootView: View) {
|
|
|
+ upgradeBtn = rootView.findViewById(R.id.upgradeBtn)
|
|
|
+ tvProgress = rootView.findViewById(R.id.tvProgress)
|
|
|
+ progressBar = rootView.findViewById(R.id.progressBar)
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun init() {
|
|
|
+ upgradeBtn?.setOnSingleClickListener {
|
|
|
+ if (content != null) {
|
|
|
+ val config = UpdateConfig()
|
|
|
+ config.url = url
|
|
|
+ val appUpdater = AppUpdater(content!!, config)
|
|
|
+ .setHttpManager(OkHttpManager.getInstance())
|
|
|
+ .setUpdateCallback(object : UpdateCallback {
|
|
|
+ override fun onDownloading(isDownloading: Boolean) {
|
|
|
+ if (isDownloading) {
|
|
|
+ "已经在下载中,请勿重复下载".toast()
|
|
|
+ } else {
|
|
|
+ upgradeBtn?.show(false)
|
|
|
+ tvProgress?.show(true)
|
|
|
+ progressBar?.show(true)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onStart(url: String?) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @SuppressLint("SetTextI18n")
|
|
|
+ override fun onProgress(progress: Long, total: Long, isChanged: Boolean) {
|
|
|
+ if (isChanged) {
|
|
|
+ if (progress > 0) {
|
|
|
+ val currProgress = (progress * 1.0f / total * 100.0f).toInt()
|
|
|
+ tvProgress?.text =
|
|
|
+ getString(R.string.app_updater_progress_notification_content) + currProgress + "%"
|
|
|
+ progressBar!!.progress = currProgress
|
|
|
+ } else {
|
|
|
+ tvProgress?.text =
|
|
|
+ getString(R.string.app_updater_start_notification_content)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onFinish(file: File?) {
|
|
|
+ dismiss()
|
|
|
+ "下载完成".toast()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onError(e: Exception?) {
|
|
|
+ dismiss()
|
|
|
+ "下载失败".toast()
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onCancel() {
|
|
|
+ dismiss()
|
|
|
+ "取消下载".toast()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ appUpdater.start()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onTouch(v: View?, event: MotionEvent?): Boolean {
|
|
|
+ if (isCancelable && dialog?.isShowing == true) {
|
|
|
+ dismiss()
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ }
|
|
|
+}
|