Skip to content
This repository was archived by the owner on Aug 29, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 28 additions & 15 deletions ActionOpenDocumentTree/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,12 @@ apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
compileSdkVersion 28
compileSdkVersion 33
defaultConfig {
applicationId "com.example.android.ktfiles"
minSdkVersion 21
targetSdkVersion 28
targetSdkVersion 33
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand All @@ -36,29 +34,44 @@ android {
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
namespace 'com.example.android.ktfiles'

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = JavaVersion.VERSION_1_8.toString()
}

buildFeatures{
viewBinding = true
}


}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.1"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4"

implementation 'androidx.fragment:fragment:1.3.6'
implementation 'androidx.fragment:fragment-ktx:1.5.7'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
implementation 'androidx.recyclerview:recyclerview:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
implementation 'androidx.recyclerview:recyclerview:1.3.0'

implementation 'com.google.android.material:material:1.4.0'
implementation 'com.google.android.material:material:1.9.0'

implementation 'androidx.documentfile:documentfile:1.0.1'

implementation 'androidx.core:core-ktx:1.6.0'
implementation 'androidx.fragment:fragment-ktx:1.3.6'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
implementation 'androidx.core:core-ktx:1.10.1'
implementation 'androidx.fragment:fragment-ktx:1.5.7'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1'

testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test:runner:1.4.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:runner:1.5.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
}
4 changes: 2 additions & 2 deletions ActionOpenDocumentTree/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
-->

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.android.ktfiles">
xmlns:tools="http://schemas.android.com/tools">

<application
android:allowBackup="true"
Expand All @@ -30,6 +29,7 @@
<activity
android:name="com.example.android.ktfiles.MainActivity"
android:label="@string/app_name"
android:exported="true"
android:theme="@style/Theme.DirSelect.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import android.widget.Toast
import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

Expand All @@ -54,8 +54,7 @@ class DirectoryFragment : Fragment() {
directoryUri = arguments?.getString(ARG_DIRECTORY_URI)?.toUri()
?: throw IllegalArgumentException("Must pass URI of directory to open")

viewModel = ViewModelProviders.of(this)
.get(DirectoryFragmentViewModel::class.java)
viewModel = ViewModelProvider(this).get(DirectoryFragmentViewModel::class.java)

val view = inflater.inflate(R.layout.fragment_directory, container, false)
recyclerView = view.findViewById(R.id.list)
Expand Down Expand Up @@ -92,8 +91,8 @@ class DirectoryFragment : Fragment() {
return view
}

override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
override fun onViewStateRestored(savedInstanceState: Bundle?) {
super.onViewStateRestored(savedInstanceState)

viewModel.loadDirectory(directoryUri)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,31 @@
package com.example.android.ktfiles

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.View
import androidx.activity.result.contract.ActivityResultContract
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.commit
import com.google.android.material.floatingactionbutton.FloatingActionButton
import kotlinx.android.synthetic.main.activity_main.toolbar
import com.example.android.ktfiles.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

/**
* Replaces the old startActivityForResult approach.
*/
private val directoryPicker = registerForActivityResult(DirectoryBrowser()){}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)

val openDirectoryButton = findViewById<FloatingActionButton>(R.id.fab_open_directory)
var binding: ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.toolbar)

val openDirectoryButton = binding.fabOpenDirectory
openDirectoryButton.setOnClickListener {
openDirectory()
}
Expand All @@ -58,19 +66,6 @@ class MainActivity : AppCompatActivity() {
return false
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == OPEN_DIRECTORY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
val directoryUri = data?.data ?: return

contentResolver.takePersistableUriPermission(
directoryUri,
Intent.FLAG_GRANT_READ_URI_PERMISSION
)
showDirectoryContents(directoryUri)
}
}

fun showDirectoryContents(directoryUri: Uri) {
supportFragmentManager.commit {
val directoryTag = directoryUri.toString()
Expand All @@ -81,9 +76,31 @@ class MainActivity : AppCompatActivity() {
}

private fun openDirectory() {
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
startActivityForResult(intent, OPEN_DIRECTORY_REQUEST_CODE)
directoryPicker.launch(0)
}
}

private const val OPEN_DIRECTORY_REQUEST_CODE = 0xf11e
inner class DirectoryBrowser : ActivityResultContract<Int, Uri?>() {
override fun createIntent(context: Context, requestCode: Int) =
Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)

override fun parseResult(resultCode: Int, result: Intent?) : Uri? {
if (resultCode != Activity.RESULT_OK) {
return null
}

result?.let{
val directoryUri = result.data as Uri

[email protected](
directoryUri,

Intent.FLAG_GRANT_READ_URI_PERMISSION
)
showDirectoryContents(directoryUri)

}

return result?.data
}
}
}
8 changes: 4 additions & 4 deletions ActionOpenDocumentTree/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.5.30'
ext.kotlin_version = '1.8.21'
repositories {
google()
jcenter()
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:7.0.1'
classpath 'com.android.tools.build:gradle:8.0.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand All @@ -34,7 +34,7 @@ buildscript {
allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

Expand Down
3 changes: 3 additions & 0 deletions ActionOpenDocumentTree/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ org.gradle.jvmargs=-Xmx1536m
kotlin.code.style=official
android.useAndroidX=true
android.enableJetifier=true
android.defaults.buildfeatures.buildconfig=true
android.nonTransitiveRClass=false
android.nonFinalResIds=false
19 changes: 1 addition & 18 deletions ActionOpenDocumentTree/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,22 +1,5 @@
#
# Copyright 2019 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#Tue Oct 29 08:53:45 PDT 2019
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip