Commit c13b5df5 authored by wanglei's avatar wanglei

...

parent 390e2591
......@@ -32,6 +32,7 @@ import com.base.pdfreaderallpdfreader.ui.view.DialogCallBack
import com.base.pdfreaderallpdfreader.ui.view.DocumentDialog.showDocumentHomeMoreDialog
import com.base.pdfreaderallpdfreader.ui.view.PdfDialog.showPdfHomeMoreDialog
import com.base.pdfreaderallpdfreader.ui.word.WordActivity
import com.base.pdfreaderallpdfreader.utils.LogEx
import com.base.pdfreaderallpdfreader.utils.PdfBoxUtils.checkPdfEncryption
import com.base.pdfreaderallpdfreader.utils.SpStringUtils
import com.base.pdfreaderallpdfreader.utils.SpStringUtils.BOOKMARK_KEY
......@@ -44,6 +45,7 @@ class DocumentFragment() : Fragment(), DialogCallBack {
private var type = TYPE_PDF
private val TAG = "DocumentFragment"
private lateinit var binding: FragmentDocumentBinding
private var adapter: DocumentAdapter? = null
......@@ -99,8 +101,9 @@ class DocumentFragment() : Fragment(), DialogCallBack {
binding.ivTop.isVisible = false
}
binding.swipeRefreshLayout.setOnRefreshListener {
initData()
initData("swipeRefreshLayout")
}
binding.swipeRefreshLayout.isRefreshing = true
}
override fun onResume() {
......@@ -108,19 +111,17 @@ class DocumentFragment() : Fragment(), DialogCallBack {
if (type == TYPE_PDF) {
if (pdfNeedRefresh) {
pdfNeedRefresh = false
initData()
initData("onResume")
}
} else {
initData()
initData("onResume")
}
}
private fun initData() {
binding.swipeRefreshLayout.isRefreshing = true
private fun initData(where: String = "") {
lifecycleScope.launch(Dispatchers.IO) {
requireContext().updateMediaStore()
LogEx.logDebug(TAG, "initData $type where=$where")
val list: List<DocumentBean>
when (type) {
......@@ -187,7 +188,8 @@ class DocumentFragment() : Fragment(), DialogCallBack {
val newFile = File(file.parentFile, newName)
val result = file.renameTo(newFile)
if (result) {
initData()
requireContext().updateMediaStore()
initData("renameDocumentBean")
}
} catch (e: Exception) {
e.printStackTrace()
......
......@@ -4,6 +4,7 @@ import android.content.Context
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Environment
import com.base.pdfreaderallpdfreader.R
import com.base.pdfreaderallpdfreader.bean.ConstObject
import com.base.pdfreaderallpdfreader.bean.ConstObject.MIME_TYPE_DOC
import com.base.pdfreaderallpdfreader.bean.ConstObject.MIME_TYPE_DOCX
......@@ -25,20 +26,18 @@ import com.base.pdfreaderallpdfreader.utils.PdfBoxUtils.checkPdfEncryption
import com.base.pdfreaderallpdfreader.utils.PermissionUtils.checkStorePermission
import com.base.pdfreaderallpdfreader.utils.SpStringUtils
import com.base.pdfreaderallpdfreader.utils.SpStringUtils.BOOKMARK_KEY
import com.base.pdfreaderallpdfreader.utils.UriUtils.isUriValid
import com.base.pdfreaderallpdfreader.utils.getMediaFile
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
fun Context.upDateDemoStore() {
val demoFile = File(filesDir, "demo")
MediaScannerConnection.scanFile(
this, arrayOf(demoFile.absolutePath), null
) { path: String?, uri: Uri? -> }
}
fun getDocumentAppDir(): File {
val appDir = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), context.packageName)
val appDir = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS),
context.getString(R.string.app_name)
)
if (!appDir.exists())
appDir.exists()
return appDir
......@@ -109,7 +108,6 @@ fun getPPtDemo(): DocumentBean {
}
fun getAllDocument(context: Context): MutableList<DocumentBean> {
context.upDateDemoStore()
if (!ConstObject.haveSaveDemo) {
context.saveAssetsFile()
......@@ -128,7 +126,7 @@ fun getAllDocument(context: Context): MutableList<DocumentBean> {
val list = context.getMediaFile(selectionArgs = selectionArgs).filter {
if (context.checkStorePermission()) {
true
isUriValid(context, it.uri)
} else {
!Uri.EMPTY.equals(it.uri)
}
......@@ -140,11 +138,6 @@ fun getAllDocument(context: Context): MutableList<DocumentBean> {
}
documentBean
}
LogEx.logDebug("getAllDocument", "documentList size=${documentList.size}")
documentList.forEach { document ->
LogEx.logDebug("getAllDocument", "document=$document")
}
val new = documentList.toMutableList()
val pptDemo = getPPtDemo()
if (File(pptDemo.path).exists()) {
......@@ -194,14 +187,19 @@ fun getPdfDocument(context: Context): MutableList<DocumentBean> {
val selectionArgs = arrayOf(
MIME_TYPE_PDF
)
val list = context.getMediaFile(selectionArgs = selectionArgs)
val list = context.getMediaFile(selectionArgs = selectionArgs).filter {
if (context.checkStorePermission()) {
isUriValid(context, it.uri)
} else {
!Uri.EMPTY.equals(it.uri)
}
}
val documentList = list.map {
val demoDocumentBean = DocumentBean(it.path, uri = it.uri, type = TYPE_PDF)
context.pdfCheck(demoDocumentBean)
demoDocumentBean
}
var new = if (context.checkStorePermission()) documentList else documentList.filter { it.uri != null }
new = new.toMutableList()
val new = documentList.toMutableList()
if (context.checkStorePermission()) {
val pdfDemo = getPdfDemo(context)
if (File(pdfDemo.path).exists()) {
......@@ -220,12 +218,18 @@ fun getWordDocument(context: Context): MutableList<DocumentBean> {
MIME_TYPE_DOC,
MIME_TYPE_DOCX,
)
val list = context.getMediaFile(selectionArgs = selectionArgs)
val list = context.getMediaFile(selectionArgs = selectionArgs).filter {
if (context.checkStorePermission()) {
isUriValid(context, it.uri)
} else {
!Uri.EMPTY.equals(it.uri)
}
}
val documentList = list.map {
DocumentBean(it.path, uri = it.uri, type = TYPE_WORD)
}
var new = if (context.checkStorePermission()) documentList else documentList.filter { it.uri == null }
new = new.toMutableList()
val new = documentList.toMutableList()
if (context.checkStorePermission()) {
val wordDemo = getWordDemo()
if (File(wordDemo.path).exists()) {
......@@ -244,12 +248,17 @@ fun getExcelDocument(context: Context): MutableList<DocumentBean> {
MIME_TYPE_XLS,
MIME_TYPE_XLSX,
)
val list = context.getMediaFile(selectionArgs = selectionArgs)
val list = context.getMediaFile(selectionArgs = selectionArgs).filter {
if (context.checkStorePermission()) {
isUriValid(context, it.uri)
} else {
!Uri.EMPTY.equals(it.uri)
}
}
val documentList = list.map {
DocumentBean(it.path, uri = it.uri, type = TYPE_EXCEL)
}
var new = if (context.checkStorePermission()) documentList else documentList.filter { it.uri == null }
new = new.toMutableList()
val new = documentList.toMutableList()
if (context.checkStorePermission()) {
val excelDemo = getExcelDemo()
if (File(excelDemo.path).exists()) {
......@@ -268,12 +277,17 @@ fun getPptDocument(context: Context): MutableList<DocumentBean> {
MIME_TYPE_PPT,
MIME_TYPE_PPTX,
)
val list = context.getMediaFile(selectionArgs = selectionArgs)
val list = context.getMediaFile(selectionArgs = selectionArgs).filter {
if (context.checkStorePermission()) {
isUriValid(context, it.uri)
} else {
!Uri.EMPTY.equals(it.uri)
}
}
val documentList = list.map {
DocumentBean(it.path, uri = it.uri, type = TYPE_PPT)
}
var new = if (context.checkStorePermission()) documentList else documentList.filter { it.uri == null }
new = new.toMutableList()
val new = documentList.toMutableList()
if (context.checkStorePermission()) {
val pptDemo = getPPtDemo()
if (File(pptDemo.path).exists()) {
......
......@@ -110,6 +110,8 @@ class MainActivity : BaseActivity<ActivityMainBinding>() {
launcher.launch(arrayOf(Manifest.permission.POST_NOTIFICATIONS)) {}
}
}
updateMediaStore()
}
addDeskShortCut()
......
......@@ -21,6 +21,7 @@ import com.base.pdfreaderallpdfreader.utils.LogEx
import com.base.pdfreaderallpdfreader.utils.PdfBoxUtils.checkPdfEncryption
import com.base.pdfreaderallpdfreader.utils.SpStringUtils
import com.base.pdfreaderallpdfreader.utils.SpStringUtils.BOOKMARK_KEY
import com.base.pdfreaderallpdfreader.utils.updateMediaStore
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
......@@ -90,6 +91,7 @@ class RecentFragment() : Fragment(), DialogCallBack {
override fun deleteDocument(item: DocumentBean) {
val flag = File(item.path).delete()
if (flag) {
requireContext().updateMediaStore()
adapter?.remove(item)
}
}
......@@ -107,6 +109,7 @@ class RecentFragment() : Fragment(), DialogCallBack {
val newFile = File(file.parentFile, newName)
val result = file.renameTo(newFile)
if (result) {
requireContext().updateMediaStore()
initData()
}
} catch (e: Exception) {
......
......@@ -482,14 +482,14 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>() {
private fun createPdfUI() {
if (muPDFCore == null) return
val countPages = muPDFCore?.countPages()
val countPages: Int? = muPDFCore?.countPages()
binding.tvPageCount.text = "1/${countPages}"
binding.mupdfReaderView.setListener(object : MuPDFReaderViewListener {
@SuppressLint("SetTextI18n")
override fun onMoveToChild(i: Int) {
binding.tvPageCount.text = "${i + 1}/$countPages}"
binding.tvPageCount.text = "${i + 1}/$countPages"
pdfPageAdapter.changeSelectPager(i)
binding.rvPager.scrollToPosition(i)
setVerticalSeekbar(i + 1, countPages ?: 0)
......@@ -646,7 +646,9 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>() {
if (item.state == 0) {
activity.startActivity(Intent(activity, PdfActivity::class.java).apply {
putExtra("path", item.path)
if (item.path.isEmpty()) {//处理uri错误的情况
putExtra("uri", item.uri.toString())
}
})
}
if (item.state == 1) {
......@@ -664,7 +666,9 @@ class PdfActivity : BaseActivity<ActivityPdfBinding>() {
fun jumpSplit(activity: Activity, path: String, uri: String? = null, pwd: String? = null) {
activity.startActivity(Intent(activity, PdfSplitActivity::class.java).apply {
putExtra("path", path)
if (path.isEmpty()) {
putExtra("uri", uri)
}
putExtra("pwd", pwd)
})
}
......
......@@ -45,13 +45,6 @@ fun Context.updateMediaStore(
) { path: String?, uri: Uri? -> }
}
fun Context.upDateDemoStore() {
val demoFile = File(filesDir, "demo")
MediaScannerConnection.scanFile(
this, arrayOf(demoFile.absolutePath) , null
) { path: String?, uri: Uri? -> }
}
//图片视频音频不用这个查
fun Context.getMediaFile(selectionArgs: Array<String>? = null): ArrayList<MediaBean> {
......
......@@ -5,6 +5,7 @@ import android.net.Uri
import com.tom_roush.pdfbox.io.IOUtils
import java.io.InputStream
object UriUtils {
fun readFileToByteArray(context: Context, uri: Uri): ByteArray? {
......@@ -26,4 +27,18 @@ object UriUtils {
}
}
}
fun isUriValid(context: Context, uri: Uri?): Boolean {
if (uri == null) {
return false
}
val contentResolver = context.contentResolver
val cursor = contentResolver.query(uri, null, null, null, null)
if (cursor != null) {
val hasData = cursor.moveToFirst()
cursor.close()
return hasData
}
return false
}
}
\ No newline at end of file
......@@ -170,9 +170,11 @@
android:layout_marginStart="28dp"
android:layout_marginTop="28dp"
android:background="@drawable/bg_54585b_5"
android:gravity="center"
android:includeFontPadding="false"
android:paddingHorizontal="2dp"
android:paddingVertical="2dp"
android:minWidth="40dp"
android:paddingHorizontal="5dp"
android:paddingVertical="3dp"
android:textColor="@color/white"
android:textSize="12sp"
app:layout_constraintStart_toStartOf="parent"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment