diff --git a/ActionOpenDocumentTree/app/build.gradle b/ActionOpenDocumentTree/app/build.gradle index 71023528..880417d8 100644 --- a/ActionOpenDocumentTree/app/build.gradle +++ b/ActionOpenDocumentTree/app/build.gradle @@ -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" @@ -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' } diff --git a/ActionOpenDocumentTree/app/src/main/AndroidManifest.xml b/ActionOpenDocumentTree/app/src/main/AndroidManifest.xml index 523c7677..ceb992f2 100644 --- a/ActionOpenDocumentTree/app/src/main/AndroidManifest.xml +++ b/ActionOpenDocumentTree/app/src/main/AndroidManifest.xml @@ -16,8 +16,7 @@ --> + xmlns:tools="http://schemas.android.com/tools"> diff --git a/ActionOpenDocumentTree/app/src/main/java/com/example/android/ktfiles/DirectoryFragment.kt b/ActionOpenDocumentTree/app/src/main/java/com/example/android/ktfiles/DirectoryFragment.kt index b6c73898..d555b61e 100644 --- a/ActionOpenDocumentTree/app/src/main/java/com/example/android/ktfiles/DirectoryFragment.kt +++ b/ActionOpenDocumentTree/app/src/main/java/com/example/android/ktfiles/DirectoryFragment.kt @@ -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 @@ -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) @@ -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) } diff --git a/ActionOpenDocumentTree/app/src/main/java/com/example/android/ktfiles/MainActivity.kt b/ActionOpenDocumentTree/app/src/main/java/com/example/android/ktfiles/MainActivity.kt index 803e3145..1cd3a9ab 100644 --- a/ActionOpenDocumentTree/app/src/main/java/com/example/android/ktfiles/MainActivity.kt +++ b/ActionOpenDocumentTree/app/src/main/java/com/example/android/ktfiles/MainActivity.kt @@ -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(R.id.fab_open_directory) + var binding: ActivityMainBinding = ActivityMainBinding.inflate(layoutInflater) + setContentView(binding.root) + setSupportActionBar(binding.toolbar) + + val openDirectoryButton = binding.fabOpenDirectory openDirectoryButton.setOnClickListener { openDirectory() } @@ -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() @@ -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() { + 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 + + this@MainActivity.contentResolver.takePersistableUriPermission( + directoryUri, + + Intent.FLAG_GRANT_READ_URI_PERMISSION + ) + showDirectoryContents(directoryUri) + + } + + return result?.data + } + } +} diff --git a/ActionOpenDocumentTree/build.gradle b/ActionOpenDocumentTree/build.gradle index 4ff9bed3..94b8e291 100644 --- a/ActionOpenDocumentTree/build.gradle +++ b/ActionOpenDocumentTree/build.gradle @@ -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 @@ -34,7 +34,7 @@ buildscript { allprojects { repositories { google() - jcenter() + mavenCentral() } } diff --git a/ActionOpenDocumentTree/gradle.properties b/ActionOpenDocumentTree/gradle.properties index f3420388..fdf5171b 100644 --- a/ActionOpenDocumentTree/gradle.properties +++ b/ActionOpenDocumentTree/gradle.properties @@ -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 diff --git a/ActionOpenDocumentTree/gradle/wrapper/gradle-wrapper.properties b/ActionOpenDocumentTree/gradle/wrapper/gradle-wrapper.properties index 14886b2d..da1db5f0 100644 --- a/ActionOpenDocumentTree/gradle/wrapper/gradle-wrapper.properties +++ b/ActionOpenDocumentTree/gradle/wrapper/gradle-wrapper.properties @@ -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