preface
The Android ResultsApi package, learning endless, will iterate over time, use the necessary dependence//Kotlin ViewModel
implementation 'androidx. Lifecycle: lifecycle - extensions: 2.2.0'
implementation 'androidx. Lifecycle: lifecycle - viewmodel - KTX: 2.3.0'
implementation 'androidx. Lifecycle: lifecycle - livedata - KTX: 2.3.0'
implementation "Androidx. Activity: activity - KTX: 1.2.0"
implementation "Androidx. Fragments: fragments - KTX: 1.3.0"
Copy the code
The body of the
/** * ResultsApi tool class * Created by Priscilla Cheung on 2021/7/20/11:58:25 * info: Listen via LiveData binding * InitializeApi () is called in BaseActivity and BaseFragment to initialize it@seeStartActivity jumps the Activity and requests a callback@seeStartCamera calls album *@seeStartTakePhotos calls camera *@seeStartVideo Call screen recording *@seeStartPermission Gets a permission *@seeStartPermissions Gets a set of permissions *@seeStartDocument calls file management to get a file *@seeStartDocuments call file management to get multiple files * Example use: * startCamera (ResultsApi. CAMERA, permissionError = {* toast (" does not grant permission ") *}, {* toast (it. The toString ()) *} * startDocuments(arrayOf(ResultsApi.CAMERA)){ * toast(it.toString()) * } */
object ResultsApi {
// As a Camera parameter, meaning filter
val CAMERA = "image/*"
val VIDEO = "video/*"
//-------------------------------------------------------------------------------------------------//
/ * * *@authorPriscilla * initialization * info: It is recommended to call and instantiate in BaseActivity or onCreate, which results in a null pointer */ when not initialized
fun AppCompatActivity.initializeApi(a) {
// Jump the Activity and request a postback
resultActivityLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
activityResultData.postValue(it)
}
/ / photo album
resultCameraLauncher =
registerForActivityResult(ActivityResultContracts.GetContent()) {
imageUrl.postValue(it)
}
/ / camera
resultTakePhotosLauncher =
registerForActivityResult(ActivityResultContracts.TakePicture()) {
takePhotosUrl.postValue(it)
}
/ / screen
resultVideoLauncher =
registerForActivityResult(ActivityResultContracts.TakeVideo()) {
videoBitmap.postValue(it)
}
// Single permission
resultPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
getPermission.postValue(it)
}
// A set of permissions
resultPermissionsLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
getPermissions.postValue(it)
}
// Select a single file
resultDocumentLauncher =
registerForActivityResult(ActivityResultContracts.OpenDocument()) {
documentUrl.postValue(it)
}
// Select multiple files
resultDocumentsLauncher =
registerForActivityResult(ActivityResultContracts.OpenMultipleDocuments()) {
documentsUrl.postValue(it)
}
}
fun Fragment.initializeApi(a) {
// Jump the Activity and request a postback
resultActivityLauncher =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
activityResultData.postValue(it)
}
/ / photo album
resultCameraLauncher =
registerForActivityResult(ActivityResultContracts.GetContent()) {
imageUrl.postValue(it)
}
/ / camera
resultTakePhotosLauncher =
registerForActivityResult(ActivityResultContracts.TakePicture()) {
takePhotosUrl.postValue(it)
}
/ / screen
resultVideoLauncher =
registerForActivityResult(ActivityResultContracts.TakeVideo()) {
videoBitmap.postValue(it)
}
// Single permission
resultPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) {
getPermission.postValue(it)
}
// A set of permissions
resultPermissionsLauncher =
registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {
getPermissions.postValue(it)
}
// Select a single file
resultDocumentLauncher =
registerForActivityResult(ActivityResultContracts.OpenDocument()) {
documentUrl.postValue(it)
}
// Select multiple files
resultDocumentsLauncher =
registerForActivityResult(ActivityResultContracts.OpenMultipleDocuments()) {
documentsUrl.postValue(it)
}
}
//-------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------//
/ * * *@authorPriscilla * jumps the Activity and requests a callback */
private var resultActivityLauncher: ActivityResultLauncher<Intent>? = null
/** * output: returns a Uri path */
val activityResultData: MutableLiveData<ActivityResult> by lazy {
MutableLiveData<ActivityResult>()
}
/**
* input:
* @paramIntent Indicates the incoming intent *@paramResult Returns data */
fun AppCompatActivity.startActivity(intent: Intent,result:(ActivityResult) - >Unit) {
activityResultData.observe(this){ preventRepeated { result.invoke(it) } } resultActivityLauncher!! .launch(intent) }fun Fragment.startActivity(intent: Intent,result:(ActivityResult) - >Unit) {
activityResultData.observe(this){ preventRepeated { result.invoke(it) } } resultActivityLauncher!! .launch(intent) }//-------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------//
/ * * *@authorPriscilla * call album */
private var resultCameraLauncher: ActivityResultLauncher<String>? = null
/** * output: returns a Uri path */
val imageUrl: MutableLiveData<Uri> by lazy {
MutableLiveData<Uri>()
}
/**
* input:
* @paramType is generally passed into CAMERA, and you can customize the filter type *@paramPermissionError Operation performed when a permission is missing *@paramResult Result of the ownership limit */
fun AppCompatActivity.startCamera(type: String,permissionError:(()->Unit)? = null, result: (Uri) - >Unit.) {
startPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE){
if(it) {
imageUrl.observe(this){ preventRepeated { result.invoke(it) } } resultCameraLauncher!! .launch(type) }elsepermissionError? .invoke() } }fun Fragment.startCamera(type: String,permissionError:(()->Unit)? = null, result: (Uri) - >Unit.) {
startPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE){
if(it) {
imageUrl.observe(this){ preventRepeated { result.invoke(it) } } resultCameraLauncher!! .launch(type) }elsepermissionError? .invoke() } }//-------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------//
/ * * *@authorPriscilla * calls camera */
private var resultTakePhotosLauncher: ActivityResultLauncher<Uri>? = null
/** * output: Returns Boolean, True means the save succeeded, False means the save failed */
val takePhotosUrl: MutableLiveData<Boolean> by lazy {
MutableLiveData<Boolean>()
}
/**
* input:
* @paramContext *@paramAuthority Provider authorities field *@paramFile An example of a file path: file (externalCacheDir, "${system.currentTimemillis ()}priscilla.jpg")) *@paramPermissionError Operation performed when a permission is missing *@paramResult Result of the ownership limit */
fun AppCompatActivity.startTakePhotos(
context: Context,
authority: String,
file: File,
permissionError:(()->Unit)? = null,
result: (Uri.Boolean) - >Unit.) {
startPermissions(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
if(it[android.Manifest.permission.WRITE_EXTERNAL_STORAGE]!! && it[android.Manifest.permission.WRITE_EXTERNAL_STORAGE]!!) {val uri = FileProvider.getUriForFile(
context,
authority,
file)
takePhotosUrl.observe(this) { preventRepeated { result.invoke(uri, it) } } resultTakePhotosLauncher!! .launch(uri) }elsepermissionError? .invoke() } }fun Fragment.startTakePhotos(
context: Context,
authority: String,
file: File,
permissionError:(()->Unit)? = null,
result: (Uri.Boolean) - >Unit.) {
startPermissions(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
if(it[android.Manifest.permission.WRITE_EXTERNAL_STORAGE]!! && it[android.Manifest.permission.WRITE_EXTERNAL_STORAGE]!!) {val uri = FileProvider.getUriForFile(
context,
authority,
file)
takePhotosUrl.observe(this) { preventRepeated { result.invoke(uri, it) } } resultTakePhotosLauncher!! .launch(uri) }elsepermissionError? .invoke() } }//-------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------//
/ * * *@authorPriscilla * call recording */
private var resultVideoLauncher: ActivityResultLauncher<Uri>? = null
/** * output:Bitmap, the Bitmap is the video thumbnail, the video saved to the path */
val videoBitmap: MutableLiveData<Bitmap> by lazy {
MutableLiveData<Bitmap>()
}
/**
* input:
* @paramContext *@paramAuthority Provider authorities field *@paramFile An example of a file path: file (externalCacheDir, "${system.currentTimemillis ()}priscilla.mp4")) *@paramVideoUrl returns the file path and video thumbnail */
fun AppCompatActivity.startVideo(
context: Context,
authority: String,
file: File,
videoUrl: (Uri.Bitmap) - >Unit.) {
val uri = FileProvider.getUriForFile(
context,
authority,
file)
videoBitmap.observe(this) { preventRepeated { videoUrl.invoke(uri, it) } } resultVideoLauncher!! .launch(uri) }fun Fragment.startVideo(
context: Context,
authority: String,
file: File,
videoUrl: (Uri.Bitmap) - >Unit.) {
val uri = FileProvider.getUriForFile(
context,
authority,
file)
videoBitmap.observe(this) { preventRepeated { videoUrl.invoke(uri, it) } } resultVideoLauncher!! .launch(uri) }//-------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------//
/ * * *@authorPriscilla * Single permission */
private var resultPermissionLauncher: ActivityResultLauncher<String>? = null
/** * output: returns a read-write delegate, granted successfully if True, failed if False */
val getPermission: MutableLiveData<Boolean> by lazy {
MutableLiveData<Boolean>()
}
/**
* input:
* @paramPermission to request permissions example: android Manifest. Permission. WRITE_EXTERNAL_STORAGE * /
fun AppCompatActivity.startPermission(permission: String, havePermission: (Boolean) - >Unit) {
getPermission.observe(this) {
preventRepeated {
havePermission.invoke(it)
}
}
// Single permission requestresultPermissionLauncher!! .launch(permission) }fun Fragment.startPermission(permission: String, havePermission: (Boolean) - >Unit) {
getPermission.observe(this) {
preventRepeated {
havePermission.invoke(it)
}
}
// Single permission requestresultPermissionLauncher!! .launch(permission) }//-------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------//
/ * * *@authorPriscilla * a set of permissions */
private var resultPermissionsLauncher: ActivityResultLauncher<Array<String>>? = null
/** * output: returns a read-write delegate, granted successfully if True, failed if False */
val getPermissions: MutableLiveData<MutableMap<String, Boolean>> by lazy {
MutableLiveData<MutableMap<String, Boolean> > ()}/**
* input:
* @paramPermission Array of permissions to request */
fun AppCompatActivity.startPermissions(
permission: Array<String>,
havePermission: (MutableMap<String.Boolean- > >)Unit.) {
// A set of permission requests
getPermissions.observe(this) {
preventRepeated {
havePermission.invoke(it)
}
}
// A set of permission requestsresultPermissionsLauncher!! .launch(permission) }fun Fragment.startPermissions(
permission: Array<String>,
havePermission: (MutableMap<String.Boolean- > >)Unit.) {
// A set of permission requests
getPermissions.observe(this) {
preventRepeated {
havePermission.invoke(it)
}
}
// A set of permission requestsresultPermissionsLauncher!! .launch(permission) }//-------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------//
/ * * *@authorPriscilla * call file */
private var resultDocumentLauncher: ActivityResultLauncher<Array<String>>? = null
/** * output: returns a Uri path */
val documentUrl: MutableLiveData<Uri> by lazy {
MutableLiveData<Uri>()
}
/**
* input:
* @paramType Filter type */
fun AppCompatActivity.startDocument(type: Array<String>, permissionError:(()->Unit)? = null,
result: (Uri) - >Unit.) {
startPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE){
if (it){
documentUrl.observe(this){ preventRepeated { result.invoke(it) } } resultDocumentLauncher!! .launch(type) }elsepermissionError? .invoke() } }fun Fragment.startDocument(type: Array<String>, permissionError:(()->Unit)? = null,
result: (Uri) - >Unit.) {
startPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE){
if (it){
documentUrl.observe(this){ preventRepeated { result.invoke(it) } } resultDocumentLauncher!! .launch(type) }elsepermissionError? .invoke() } }//-------------------------------------------------------------------------------------------------//
//-------------------------------------------------------------------------------------------------//
/ * * *@authorPriscilla * calls filegroup */
private var resultDocumentsLauncher: ActivityResultLauncher<Array<String>>? = null
/** * output: returns a group Uri path */
val documentsUrl: MutableLiveData<MutableList<Uri>> by lazy {
MutableLiveData<MutableList<Uri>>()
}
/**
* input:
* @paramType Filter type */
fun AppCompatActivity.startDocuments(type: Array<String>, permissionError:(()->Unit)? = null,
result: (MutableList<Uri- > >)Unit.) {
startPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE){
if (it){
documentsUrl.observe(this){ preventRepeated { result.invoke(it) } } resultDocumentsLauncher!! .launch(type) }elsepermissionError? .invoke() } }fun Fragment.startDocuments(type: Array<String>, permissionError:(()->Unit)? = null,
result: (MutableList<Uri- > >)Unit.) {
startPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE){
if (it){
documentsUrl.observe(this){ preventRepeated { result.invoke(it) } } resultDocumentsLauncher!! .launch(type) }elsepermissionError? .invoke() } }//-------------------------------------------------------------------------------------------------//
private var lastClickTime: Long = 0
fun preventRepeated(click: () -> Unit) {
val time = System.currentTimeMillis()
val longTime = time - lastClickTime
if (0 < longTime && longTime > 500) {
click.invoke()
}
lastClickTime = time
}
}
Copy the code