| | |
| | | package com.runt.open.mvi.base |
| | | |
| | | import android.Manifest |
| | | import android.annotation.SuppressLint |
| | | import android.app.ActivityManager |
| | | import android.app.UiModeManager |
| | | import android.content.ClipData |
| | |
| | | import com.runt.open.mvi.R |
| | | import com.runt.open.mvi.base.model.BaseViewModel |
| | | import com.runt.open.mvi.base.model.ViewModelFactory |
| | | import com.runt.open.mvi.utils.FileUtils |
| | | import com.runt.open.mvi.utils.PreferencesUtils |
| | | import java.io.File |
| | | import java.lang.reflect.ParameterizedType |
| | |
| | | |
| | | val PARAMS_TITLE = "title" |
| | | |
| | | private var requestFileResult:Observer<List<Uri>>? = null; |
| | | private var requestFileResult:Observer<List<String>>? = null; |
| | | |
| | | private var fileLauncher : ActivityResultLauncher<Intent>? = null //选择文件 |
| | | private var multipleFileLauncher : ActivityResultLauncher<Array<String>>? = null //选择文件 |
| | |
| | | |
| | | //选择文件 |
| | | fileLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult() , object : ActivityResultCallback<ActivityResult?> { |
| | | @SuppressLint("WrongConstant") |
| | | override fun onActivityResult(result : ActivityResult?) { |
| | | Log.i(TAG,"fileLauncher ${result!!.data?.data}") |
| | | Log.i(TAG,"fileLauncher clipData= ${result!!.data?.clipData}") |
| | | fileUriList.clear(); |
| | | result!!.data?.data?.let{ |
| | | val flags = result!!.data!!.flags and |
| | | (Intent.FLAG_GRANT_READ_URI_PERMISSION or |
| | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
| | | |
| | | // 🔐 持久化权限(核心) |
| | | contentResolver.takePersistableUriPermission(it, flags) |
| | | Log.i(TAG , "onRequestFileResult: ${it}") |
| | | fileUriList.add(it); |
| | | } |
| | | result!!.data?.clipData?.let{ |
| | | val count = it.itemCount |
| | | for (i in 0 until count) { |
| | | val flags = result!!.data!!.flags and |
| | | (Intent.FLAG_GRANT_READ_URI_PERMISSION or |
| | | Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
| | | |
| | | // 🔐 持久化权限(核心) |
| | | contentResolver.takePersistableUriPermission(it.getItemAt(i).uri, flags) |
| | | Log.i(TAG , "onRequestFileResult: ${it.getItemAt(i).uri}") |
| | | fileUriList.add(it.getItemAt(i).uri) |
| | | } |
| | | } |
| | |
| | | |
| | | val cR : ContentResolver = getContentResolver() |
| | | val mime = MimeTypeMap.getSingleton() |
| | | var filePath = FileUtils.getFilePathFromUri(fileUriList.get(0)!!); |
| | | var filePath = fileUriList.get(0)!!.toString() |
| | | //文件不存在或读写权限受限 |
| | | if(filePath == null){ |
| | | //android 11需要申请权限 //是否有所有问读写权限 |
| | |
| | | } |
| | | requestFileResult?.onChanged(ArrayList()) |
| | | }else{ |
| | | requestFileResult?.onChanged(fileUriList) |
| | | var filePathes = ArrayList<String>() |
| | | for (i in 0 until fileUriList.size) { |
| | | filePathes.add(fileUriList.get(i)!!.toString()) |
| | | } |
| | | requestFileResult?.onChanged(filePathes) |
| | | } |
| | | } |
| | | |
| | |
| | | @StringDef(Manifest.permission.READ_MEDIA_VIDEO, Manifest.permission.READ_MEDIA_AUDIO, Manifest.permission.READ_MEDIA_IMAGES,Manifest.permission.READ_EXTERNAL_STORAGE) |
| | | annotation class ManifestType |
| | | |
| | | fun requestFile(@ManifestType fileType :String,maxCount:Int = 1,fileResult:Observer<List<Uri>>){ |
| | | fun requestFile(@ManifestType fileType :String,maxCount:Int = 1,fileResult:Observer<List<String>>){ |
| | | requestFileResult = fileResult; |
| | | var permissions = ""; |
| | | //android 13 权限申请细化类型 |
| | |
| | | requestPermissions(permissions , object :Observer<String>{ |
| | | override fun onChanged(value : String) { |
| | | if(!value.isEmpty()){ |
| | | var intent = Intent() |
| | | when(fileType){ |
| | | Manifest.permission.READ_MEDIA_VIDEO,Manifest.permission.READ_MEDIA_IMAGES ->{ |
| | | var intent = Intent() |
| | | intent.action = MediaStore.ACTION_PICK_IMAGES |
| | | var type = if(fileType == Manifest.permission.READ_MEDIA_VIDEO) "video/*" else "image/*" |
| | | intent.setType(type) // 设置文件类型,可以更具体如"application/pdf" |
| | |
| | | //设置最大数量 |
| | | intent.putExtra(MediaStore.EXTRA_PICK_IMAGES_MAX, maxCount); |
| | | } |
| | | fileLauncher!!.launch(intent) |
| | | } |
| | | Manifest.permission.READ_MEDIA_AUDIO ->{ |
| | | var intent = Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI) |
| | | fileLauncher!!.launch(intent) |
| | | intent = Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI) |
| | | //multipleFileLauncher!!.launch(arrayOf("audio/*" |
| | | // "audio/mp3", // MP3 |
| | | // "audio/m4a", // M4A |
| | |
| | | } |
| | | else -> {} |
| | | } |
| | | intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) |
| | | intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION) |
| | | intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION) |
| | | fileLauncher!!.launch(intent) |
| | | }else{ |
| | | requestFileResult?.onChanged(ArrayList()) |
| | | } |