From e7f907b49331590369c33d20ace3ca5663c32461 Mon Sep 17 00:00:00 2001 From: Richard Schilling Date: Mon, 15 May 2023 17:15:31 -0700 Subject: [PATCH 1/8] replace startActivityForResult (deprecated) with ActivityResultContract --- .../example/android/ktfiles/MainActivity.kt | 49 +++++++++++++------ 1 file changed, 33 insertions(+), 16 deletions(-) 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..4a28ca10 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,10 +17,12 @@ 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 @@ -28,6 +30,11 @@ import kotlinx.android.synthetic.main.activity_main.toolbar 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) @@ -58,19 +65,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() @@ -82,8 +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{result -> + val directoryUri = result.data + + this@MainActivity.contentResolver.takePersistableUriPermission( + directoryUri, + + Intent.FLAG_GRANT_READ_URI_PERMISSION + ) + showDirectoryContents(directoryUri) + + } + + return result?.data + } + } +} From f60bb2c5319c931e2c356185377fee4f031241b9 Mon Sep 17 00:00:00 2001 From: Richard Schilling Date: Mon, 15 May 2023 17:17:56 -0700 Subject: [PATCH 2/8] mavenCentral not jcenter --- ActionOpenDocumentTree/build.gradle | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ActionOpenDocumentTree/build.gradle b/ActionOpenDocumentTree/build.gradle index 4ff9bed3..2cd13335 100644 --- a/ActionOpenDocumentTree/build.gradle +++ b/ActionOpenDocumentTree/build.gradle @@ -20,7 +20,7 @@ buildscript { ext.kotlin_version = '1.5.30' repositories { google() - jcenter() + mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:7.0.1' @@ -34,7 +34,7 @@ buildscript { allprojects { repositories { google() - jcenter() + mavenCentral() } } From a84443085e039a351d32665caa87c3448d7a61d4 Mon Sep 17 00:00:00 2001 From: Richard Schilling Date: Mon, 15 May 2023 17:23:43 -0700 Subject: [PATCH 3/8] Set Android target to 33. --- ActionOpenDocumentTree/app/build.gradle | 4 ++-- ActionOpenDocumentTree/build.gradle | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ActionOpenDocumentTree/app/build.gradle b/ActionOpenDocumentTree/app/build.gradle index 71023528..7e77aea8 100644 --- a/ActionOpenDocumentTree/app/build.gradle +++ b/ActionOpenDocumentTree/app/build.gradle @@ -21,11 +21,11 @@ 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" diff --git a/ActionOpenDocumentTree/build.gradle b/ActionOpenDocumentTree/build.gradle index 2cd13335..7b2a69e0 100644 --- a/ActionOpenDocumentTree/build.gradle +++ b/ActionOpenDocumentTree/build.gradle @@ -23,7 +23,7 @@ buildscript { mavenCentral() } dependencies { - classpath 'com.android.tools.build:gradle:7.0.1' + classpath 'com.android.tools.build:gradle:7.0.4' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong From d74e0d4d672f8c2a1de74a3140002ee55e33cd85 Mon Sep 17 00:00:00 2001 From: Richard Schilling Date: Mon, 15 May 2023 17:35:39 -0700 Subject: [PATCH 4/8] build: set source/target compatability. export activity. MainActivity.kt: cast activity result data to Uri. Use gradle distribution 7.5 --- ActionOpenDocumentTree/app/build.gradle | 10 ++++++++++ .../app/src/main/AndroidManifest.xml | 1 + .../example/android/ktfiles/MainActivity.kt | 2 +- .../gradle/wrapper/gradle-wrapper.properties | 19 +------------------ 4 files changed, 13 insertions(+), 19 deletions(-) diff --git a/ActionOpenDocumentTree/app/build.gradle b/ActionOpenDocumentTree/app/build.gradle index 7e77aea8..698594a2 100644 --- a/ActionOpenDocumentTree/app/build.gradle +++ b/ActionOpenDocumentTree/app/build.gradle @@ -36,6 +36,16 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + /* + compileOptions { + sourceCompatibility JavaVersion.VERSION_1_8 + targetCompatibility JavaVersion.VERSION_1_8 + } + kotlinOptions { + jvmTarget = JavaVersion.VERSION_1_8.toString() + } + */ + } dependencies { diff --git a/ActionOpenDocumentTree/app/src/main/AndroidManifest.xml b/ActionOpenDocumentTree/app/src/main/AndroidManifest.xml index 523c7677..2af4fbba 100644 --- a/ActionOpenDocumentTree/app/src/main/AndroidManifest.xml +++ b/ActionOpenDocumentTree/app/src/main/AndroidManifest.xml @@ -30,6 +30,7 @@ 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 4a28ca10..14a0e21a 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 @@ -89,7 +89,7 @@ class MainActivity : AppCompatActivity() { } result?.let{result -> - val directoryUri = result.data + val directoryUri = result.data as Uri this@MainActivity.contentResolver.takePersistableUriPermission( directoryUri, diff --git a/ActionOpenDocumentTree/gradle/wrapper/gradle-wrapper.properties b/ActionOpenDocumentTree/gradle/wrapper/gradle-wrapper.properties index 14886b2d..8049c684 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-7.5-bin.zip zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-7.0.2-all.zip From 68762a3d7a3f7a94f1def6260cf848c70590e37e Mon Sep 17 00:00:00 2001 From: Richard Schilling Date: Mon, 15 May 2023 17:38:20 -0700 Subject: [PATCH 5/8] Upgrade Gradle to 8.0.1 using migration assistant. --- ActionOpenDocumentTree/app/build.gradle | 1 + ActionOpenDocumentTree/app/src/main/AndroidManifest.xml | 3 +-- ActionOpenDocumentTree/build.gradle | 4 ++-- ActionOpenDocumentTree/gradle.properties | 3 +++ .../gradle/wrapper/gradle-wrapper.properties | 2 +- 5 files changed, 8 insertions(+), 5 deletions(-) diff --git a/ActionOpenDocumentTree/app/build.gradle b/ActionOpenDocumentTree/app/build.gradle index 698594a2..be47afd1 100644 --- a/ActionOpenDocumentTree/app/build.gradle +++ b/ActionOpenDocumentTree/app/build.gradle @@ -36,6 +36,7 @@ android { proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } + namespace 'com.example.android.ktfiles' /* compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 diff --git a/ActionOpenDocumentTree/app/src/main/AndroidManifest.xml b/ActionOpenDocumentTree/app/src/main/AndroidManifest.xml index 2af4fbba..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"> Date: Mon, 15 May 2023 17:45:07 -0700 Subject: [PATCH 6/8] Replace deprecated ViewModelProviders.of with instance of ViewModelProvider. --- .../java/com/example/android/ktfiles/DirectoryFragment.kt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) 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..eb85edfb 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,6 +31,7 @@ import android.widget.Toast import androidx.core.net.toUri import androidx.fragment.app.Fragment import androidx.lifecycle.Observer +import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -54,8 +55,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 +92,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) } From 22bf8fe6513a1b6c85c5ae1afbd2cd6e314cae6b Mon Sep 17 00:00:00 2001 From: Richard Schilling Date: Mon, 15 May 2023 18:13:27 -0700 Subject: [PATCH 7/8] Set JDK version 18. Enable view binding. --- ActionOpenDocumentTree/app/build.gradle | 10 ++++++---- .../example/android/ktfiles/DirectoryFragment.kt | 1 - .../com/example/android/ktfiles/MainActivity.kt | 14 +++++++------- ActionOpenDocumentTree/build.gradle | 2 +- 4 files changed, 14 insertions(+), 13 deletions(-) diff --git a/ActionOpenDocumentTree/app/build.gradle b/ActionOpenDocumentTree/app/build.gradle index be47afd1..62266ce6 100644 --- a/ActionOpenDocumentTree/app/build.gradle +++ b/ActionOpenDocumentTree/app/build.gradle @@ -18,8 +18,6 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' -apply plugin: 'kotlin-android-extensions' - android { compileSdkVersion 33 defaultConfig { @@ -37,7 +35,7 @@ android { } } namespace 'com.example.android.ktfiles' - /* + compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 @@ -45,7 +43,11 @@ android { kotlinOptions { jvmTarget = JavaVersion.VERSION_1_8.toString() } - */ + + buildFeatures{ + viewBinding = true + } + } 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 eb85edfb..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 @@ -32,7 +32,6 @@ import androidx.core.net.toUri import androidx.fragment.app.Fragment import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider -import androidx.lifecycle.ViewModelProviders import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView 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 14a0e21a..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 @@ -25,8 +25,7 @@ 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() { @@ -37,10 +36,12 @@ class MainActivity : AppCompatActivity() { 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() } @@ -75,7 +76,6 @@ class MainActivity : AppCompatActivity() { } private fun openDirectory() { - val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE) directoryPicker.launch(0) } @@ -88,7 +88,7 @@ class MainActivity : AppCompatActivity() { return null } - result?.let{result -> + result?.let{ val directoryUri = result.data as Uri this@MainActivity.contentResolver.takePersistableUriPermission( diff --git a/ActionOpenDocumentTree/build.gradle b/ActionOpenDocumentTree/build.gradle index 65a29a61..94b8e291 100644 --- a/ActionOpenDocumentTree/build.gradle +++ b/ActionOpenDocumentTree/build.gradle @@ -17,7 +17,7 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.6.21' + ext.kotlin_version = '1.8.21' repositories { google() mavenCentral() From de46dd44986e4aa29a9731724d9f1e9ed883a3ad Mon Sep 17 00:00:00 2001 From: Richard Schilling Date: Mon, 15 May 2023 18:27:28 -0700 Subject: [PATCH 8/8] Update dependencies. --- ActionOpenDocumentTree/app/build.gradle | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/ActionOpenDocumentTree/app/build.gradle b/ActionOpenDocumentTree/app/build.gradle index 62266ce6..880417d8 100644 --- a/ActionOpenDocumentTree/app/build.gradle +++ b/ActionOpenDocumentTree/app/build.gradle @@ -54,24 +54,24 @@ android { 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' }