From 8509a517cc494c312f07f67662f678f88b795fd7 Mon Sep 17 00:00:00 2001 From: Lukas Roedl Date: Thu, 31 Oct 2019 13:08:35 +0100 Subject: [PATCH 1/4] Switch to alternative method to get the ExternalFilesDirectory and upgrade to API 29 for Travis and Robolectric --- .travis.yml | 4 ++-- logback-android/build.gradle | 6 +++--- .../ch/qos/logback/core/android/AndroidContextUtil.java | 8 +++++--- .../qos/logback/core/android/AndroidContextUtilTest.java | 6 +++--- 4 files changed, 13 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index f700d0a3a9..e2dd256376 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,8 +1,8 @@ language: android android: components: - - build-tools-28.0.3 - - android-28 + - build-tools-29.0.2 + - android-29 sudo: false diff --git a/logback-android/build.gradle b/logback-android/build.gradle index 043f65d53b..f29f0e93e3 100644 --- a/logback-android/build.gradle +++ b/logback-android/build.gradle @@ -7,7 +7,7 @@ apply from: "${rootProject.projectDir}/gradle/docs.gradle" apply from: "${rootProject.projectDir}/gradle/deploy.gradle" android { - compileSdkVersion 28 + compileSdkVersion 29 compileOptions { sourceCompatibility JavaVersion.VERSION_1_5 @@ -16,7 +16,7 @@ android { defaultConfig { minSdkVersion 9 - targetSdkVersion 28 + targetSdkVersion 29 versionCode VERSION_CODE.toInteger() versionName VERSION_NAME } @@ -52,7 +52,7 @@ dependencies { } testImplementation 'org.hamcrest:hamcrest-junit:2.0.0.0' - testImplementation 'org.robolectric:robolectric:4.0.2' + testImplementation 'org.robolectric:robolectric:4.3.1' testImplementation 'org.mockito:mockito-core:2.23.4' testImplementation 'joda-time:joda-time:2.10.1' diff --git a/logback-android/src/main/java/ch/qos/logback/core/android/AndroidContextUtil.java b/logback-android/src/main/java/ch/qos/logback/core/android/AndroidContextUtil.java index f05db6b5b2..68f7021e7e 100644 --- a/logback-android/src/main/java/ch/qos/logback/core/android/AndroidContextUtil.java +++ b/logback-android/src/main/java/ch/qos/logback/core/android/AndroidContextUtil.java @@ -67,7 +67,7 @@ public void setupProperties(LoggerContext context) { context.putProperties(props); } - private static ContextWrapper getContext() { + protected static ContextWrapper getContext() { try { Class c = Class.forName("android.app.AppGlobals"); Method method = c.getDeclaredMethod("getInitialApplication"); @@ -96,7 +96,7 @@ public String getMountedExternalStorageDirectoryPath() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { - path = absPath(Environment.getExternalStorageDirectory()); + path = this.context != null ? this.context.getExternalFilesDir(null).getAbsolutePath() : null; } return path; } @@ -107,7 +107,9 @@ public String getMountedExternalStorageDirectoryPath() { * @return the absolute path to the external storage directory */ public String getExternalStorageDirectoryPath() { - return Environment.getExternalStorageDirectory().getAbsolutePath(); + return this.context != null + ? this.context.getExternalFilesDir(null).getAbsolutePath() + : null; } public String getExternalFilesDirectoryPath() { diff --git a/logback-android/src/test/java/ch/qos/logback/core/android/AndroidContextUtilTest.java b/logback-android/src/test/java/ch/qos/logback/core/android/AndroidContextUtilTest.java index 1d98a2c08d..f4a141333b 100644 --- a/logback-android/src/test/java/ch/qos/logback/core/android/AndroidContextUtilTest.java +++ b/logback-android/src/test/java/ch/qos/logback/core/android/AndroidContextUtilTest.java @@ -50,14 +50,14 @@ public void before() { public void getMountedExternalStorageDirectoryPathReturnsPathWhenMounted() { ShadowEnvironment.setExternalStorageState(Environment.MEDIA_MOUNTED); assertThat(contextUtil.getMountedExternalStorageDirectoryPath(), - is(Environment.getExternalStorageDirectory().getAbsolutePath())); + is(contextUtil.getContext().getExternalFilesDir(null).getAbsolutePath())); } @Test public void getMountedExternalStorageDirectoryPathReturnsPathWhenMountedReadOnly() { ShadowEnvironment.setExternalStorageState(Environment.MEDIA_MOUNTED_READ_ONLY); assertThat(contextUtil.getMountedExternalStorageDirectoryPath(), - is(Environment.getExternalStorageDirectory().getAbsolutePath())); + is(contextUtil.getContext().getExternalFilesDir(null).getAbsolutePath())); } @Test @@ -111,7 +111,7 @@ public void getMountedExternalStorageDirectoryPathReturnsNullWhenShared() { @Test public void getExternalStorageDirectoryPathIsNotEmpty() { assertThat(contextUtil.getExternalStorageDirectoryPath(), - is(Environment.getExternalStorageDirectory().getAbsolutePath())); + is(contextUtil.getContext().getExternalFilesDir(null).getAbsolutePath())); } @Test From a1d8ddf6acd2528459acdaae16ed56ce952e1903 Mon Sep 17 00:00:00 2001 From: Lukas Roedl Date: Tue, 5 Nov 2019 17:35:29 +0100 Subject: [PATCH 2/4] Try to fix Travis issues --- .travis.yml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.travis.yml b/.travis.yml index e2dd256376..48b742dad0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,14 @@ language: android android: components: + # Use the latest revision of Android SDK Tools + - tools + - platform-tools + + # The BuildTools version used by the project - build-tools-29.0.2 + + # The SDK version used to compile the project - android-29 sudo: false From c135dc81d1512a0ddb43a2a131c8de3849cc9e29 Mon Sep 17 00:00:00 2001 From: Lukas Roedl Date: Tue, 5 Nov 2019 17:45:46 +0100 Subject: [PATCH 3/4] Try to fix Travis issues --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 48b742dad0..bdc1cb6208 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,7 @@ android: - platform-tools # The BuildTools version used by the project + - build-tools-28.0.3 - build-tools-29.0.2 # The SDK version used to compile the project From a6240744a7b9a73c59ee1a5875a9443b58dca74e Mon Sep 17 00:00:00 2001 From: Lukas Roedl Date: Wed, 6 Nov 2019 11:30:16 +0100 Subject: [PATCH 4/4] Implement proposed changes regarding API 29 changes --- .../core/android/AndroidContextUtil.java | 27 ++++++++++++++++--- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/logback-android/src/main/java/ch/qos/logback/core/android/AndroidContextUtil.java b/logback-android/src/main/java/ch/qos/logback/core/android/AndroidContextUtil.java index 68f7021e7e..72951a030c 100644 --- a/logback-android/src/main/java/ch/qos/logback/core/android/AndroidContextUtil.java +++ b/logback-android/src/main/java/ch/qos/logback/core/android/AndroidContextUtil.java @@ -96,7 +96,7 @@ public String getMountedExternalStorageDirectoryPath() { String state = Environment.getExternalStorageState(); if (state.equals(Environment.MEDIA_MOUNTED) || state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) { - path = this.context != null ? this.context.getExternalFilesDir(null).getAbsolutePath() : null; + path = getExternalStorageDirectoryPath(); } return path; } @@ -104,14 +104,33 @@ public String getMountedExternalStorageDirectoryPath() { /** * Gets the path to the external storage directory * + * This API is available on SDK 8+. On API versions 29 onwards, + * this function uses the implementation in + * {@link android.content.Context#getExternalFilesDir(java.lang.String)} + * which is the proposed replacement for + * {@link android.os.Environment#getExternalStorageDirectory()}. + * * @return the absolute path to the external storage directory */ + @TargetApi(8) + @SuppressWarnings("deprecation") public String getExternalStorageDirectoryPath() { - return this.context != null - ? this.context.getExternalFilesDir(null).getAbsolutePath() - : null; + if (Build.VERSION.SDK_INT >= 29) { + return getExternalFilesDirectoryPath(); + } else { + return Environment.getExternalStorageDirectory().getAbsolutePath(); + } } + /** + * Gets the path to the external storage directory + * + * This API is available on SDK 8+. This function uses the implementation in + * {@link android.content.Context#getExternalFilesDir(java.lang.String)}. + * + * @return the absolute path to the external storage directory + */ + @TargetApi(8) public String getExternalFilesDirectoryPath() { return this.context != null ? absPath(this.context.getExternalFilesDir(null))