1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package cn.flightfeather.thirdapp.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.thirdapp.R
import cn.flightfeather.thirdapp.module.base.BaseFragment
import cn.flightfeather.thirdapp.util.SystemServiceUtils
import cn.flightfeather.thirdapp.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)
            }
        }
    }
}