11package com.lazygeniouz.filecompat.example
22
33import android.annotation.SuppressLint
4- import android.app.Activity
5- import android.content.Context
64import android.content.Intent
75import android.os.Build.VERSION.SDK_INT
86import android.os.Bundle
97import android.os.storage.StorageManager
108import android.widget.Button
119import android.widget.ProgressBar
1210import android.widget.TextView
11+ import androidx.activity.addCallback
1312import androidx.activity.result.contract.ActivityResultContracts
1413import androidx.appcompat.app.AppCompatActivity
1514import androidx.core.view.isVisible
@@ -29,7 +28,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
2928
3029 private val folderResultLauncher =
3130 registerForActivityResult(ActivityResultContracts .StartActivityForResult ()) { result ->
32- if (result.resultCode == Activity . RESULT_OK ) {
31+ if (result.resultCode == RESULT_OK ) {
3332 val documentUri = result.data?.data
3433 if (documentUri != null ) {
3534 textView.text = " "
@@ -53,11 +52,10 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
5352 }
5453 }
5554
56-
5755 @SuppressLint(" SetTextI18n" )
5856 private val fileResultLauncher =
5957 registerForActivityResult(ActivityResultContracts .StartActivityForResult ()) { result ->
60- if (result.resultCode == Activity . RESULT_OK ) {
58+ if (result.resultCode == RESULT_OK ) {
6159 val documentUri = result.data?.data
6260 if (documentUri != null ) {
6361 textView.text = " "
@@ -83,6 +81,8 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
8381 textView = findViewById(R .id.fileNames)
8482 progress = findViewById(R .id.progress)
8583
84+ onBackPressedDispatcher.addCallback(this ) { finishAffinity() }
85+
8686 buttonDir.setOnClickListener {
8787 folderResultLauncher.launch(getStorageIntent())
8888 }
@@ -95,13 +95,9 @@ class MainActivity : AppCompatActivity(R.layout.activity_main) {
9595 private fun getStorageIntent (single : Boolean = false): Intent {
9696 return if (single) Intent (Intent .ACTION_GET_CONTENT ).setType(" */*" ) else {
9797 if (SDK_INT >= 30 ) {
98- val storageManager = getSystemService(Context . STORAGE_SERVICE ) as StorageManager
98+ val storageManager = getSystemService(STORAGE_SERVICE ) as StorageManager
9999 storageManager.primaryStorageVolume.createOpenDocumentTreeIntent()
100100 } else Intent (Intent .ACTION_OPEN_DOCUMENT_TREE )
101101 }
102102 }
103-
104- @Suppress(" override_deprecation" )
105- // this is just a sample app, ignore this.
106- override fun onBackPressed () = finishAffinity()
107103}
0 commit comments