package cn.flightfeather.thirdappmodule.module.common
|
|
import android.app.Activity
|
import android.content.Intent
|
import android.graphics.drawable.Drawable
|
import android.os.Bundle
|
import android.os.Environment
|
import android.support.v4.content.ContextCompat
|
import android.view.View
|
import android.view.ViewGroup
|
import cn.flightfeather.thirdappmodule.R
|
import cn.flightfeather.thirdappmodule.module.base.BaseFragment
|
import cn.flightfeather.thirdappmodule.util.SystemServiceUtils
|
import cn.flightfeather.thirdappmodule.util.file.FileUtils
|
import com.tencent.smtt.sdk.QbSdk
|
import com.tencent.smtt.sdk.TbsReaderView
|
import kotlinx.android.synthetic.main.fragment_office_file.*
|
import org.json.JSONException
|
import org.json.JSONObject
|
import java.io.File
|
|
/**
|
* @author riku
|
* Date: 2019/12/31
|
* office文件浏览
|
*/
|
class OfficeFileReadFragment : BaseFragment() {
|
|
companion object {
|
fun newInstance(filePos: Int, filePath: String?, fileName: String?): OfficeFileReadFragment {
|
val bundle = Bundle().apply {
|
putInt("filePos", filePos)
|
putString("filePath", filePath)
|
putString("fileName", fileName)
|
}
|
return OfficeFileReadFragment().apply { arguments = bundle }
|
}
|
}
|
|
override fun toolbarEnabled(): Boolean = true
|
|
override fun getMenuVisibility(): List<Int?> = listOf(View.VISIBLE)
|
|
override fun getMenuRes(): List<Drawable?> = listOf(ContextCompat.getDrawable(context!!, R.drawable.icon_share_blue))
|
|
override fun getMenuClickListener(): List<View.OnClickListener?> = listOf(View.OnClickListener {
|
SystemServiceUtils.shareFile(activity, filePath)
|
})
|
|
override fun getTitleText(): String? = fileName
|
|
private var filePos = 0
|
|
private var filePath: String? = ""
|
|
private var fileName: String? = ""
|
|
private lateinit var tbsReaderView: TbsReaderView
|
|
override fun getLayoutId(): Int = R.layout.fragment_office_file
|
|
override fun onCreateView() {
|
arguments?.let {
|
filePos = it.getInt("filePos")
|
filePath = it.getString("filePath")
|
fileName = it.getString("fileName")
|
}
|
super.onCreateView()
|
}
|
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
super.onViewCreated(view, savedInstanceState)
|
|
// init()
|
openLocalFile()
|
|
val intent = Intent()
|
intent.putExtra(OfficeFileManageActivity.FILE_PATH, filePath)
|
intent.putExtra(OfficeFileManageActivity.FILE_POS, filePos)
|
activity?.setResult(Activity.RESULT_OK, intent)
|
}
|
|
override fun onDetach() {
|
super.onDetach()
|
tbsReaderView.onStop()
|
}
|
|
override fun onDestroy() {
|
super.onDestroy()
|
tbsReaderView.onStop()
|
}
|
|
private fun init() {
|
val params = hashMapOf<String, String>()
|
params["local"] = "true"
|
val obj = JSONObject()
|
try {
|
obj.put("pkgName", context?.applicationContext?.packageName)
|
} catch (e: JSONException) {
|
e.printStackTrace()
|
}
|
params["menuData"] = obj.toString()
|
QbSdk.getMiniQBVersion(context)
|
QbSdk.openFileReader(context, filePath, params) {
|
activity?.finish()
|
}
|
}
|
|
private fun openLocalFile() {
|
tbsReaderView = TbsReaderView(context, null)
|
tbs_reader_container.addView(tbsReaderView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
|
if (!filePath.isNullOrEmpty()) {
|
val tempFile = File("/storage/emulated/0/TbsReaderTemp")
|
if (!tempFile.exists()) {
|
tempFile.mkdirs()
|
}
|
|
//加载文件
|
val localBundle = Bundle()
|
localBundle.putString("filePath", filePath)
|
localBundle.putString(
|
"tempPath",
|
Environment.getExternalStorageDirectory().path + File.separator + "TbsReaderTemp"
|
)
|
val result = tbsReaderView.preOpen(FileUtils.getExtensionName(filePath), false)
|
if (result) {
|
tbsReaderView.openFile(localBundle)
|
}
|
}
|
}
|
}
|